0% found this document useful (0 votes)
8 views

7_Arrays_0

Uploaded by

ayusssssh100
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

7_Arrays_0

Uploaded by

ayusssssh100
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Arrays

What is an Array?
• is a collection of similar items stored at contiguous memory
locations and share same name space.
• elements can be accessed randomly using indices of an array
• can be used to store collection of primitive data types such
as int, float, double, char, etc of any particular type.
• Int a[9]
What is an Array?

Index of an array starts from 0 to size-1


i.e for arr[6] , first element of arr array will be stored at arr[0] address
and the last element will occupy arr[5].
Why do we need arrays?
We can use normal variables (v1, v2, v3, ..) when we
have a small number of objects,
but if we want to store a large number of instances, it
becomes difficult to manage them with normal
variables.
The idea of an array is to represent many instances in
one variable.
Array Declaration
Syntax:
datatype arrayname [size];
Example:
int a[5]; Compiler creates an array of size 5

int a[5] = {10, 20, 30, 40,50};


int a[] = { 10, 20, 30, 40,50 };
int n = 10;
int a[n];
Array Declaration
int arr[6] = { 10, 20, 30, 40 };
Compiler creates an array of size 6, initializes first 4 elements
as specified by user and rest two elements as 0.
So it is same as
int arr[] = {10, 20, 30, 40, 0, 0};

There is no index out of bounds checking in C/C++, for


example, the following program compiles fine but may
produce unexpected output when run.
Array Initialization
• Compile- time Initialization
• During compilation the array is initialized with elements.
• Example:
data-type array-name[size] = { list of values };
int marks[4]={ 67, 87, 56, 77 };
float area[5]={ 23.4, 6.8, 5.5 };
• Run-time Initialization
• The array elements are taken as user input i.e. during run-
time.
• We use scanf() and loops to take the values from the user.
Array Initialization #include<stdio.h>
void main(){
#include<stdio.h> int arr[4];
void main() int i, j;
{ printf("Enter array element");
int i;
for(i = 0; i < 4; i++) {
int arr[] = {2, 3, 4};
for(i = 0 ; i < 3 ; i++) scanf("%d", &arr[i]);
{ }
printf("%d\t",arr[i]); for(j = 0; j < 4; j++) {
} printf("%d\n", arr[j]);
} }
Compile time initialization } Run time initialization
Find the sum and average of numbers in 1D array.
#include <stdio.h>
int main(){
int i, num;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
int array[num];
printf("Enter the numbers:”); Output:
for (i = 0; i < num; i++) {
scanf("%d", &array[i]); Enter the value of N 5
} Enter the numbers:
printf("Input array elements \n"); 10 20 30 40 50
for (i = 0; i < num; i++) {
printf("%d\n", array[i]); Input array elements
} 10 20 30 40 50
for (i = 0; i < num; i++) { Sum of all numbers=150.00
total+=array[i];
} Average of all input numbers=30.00
average = total / num;
printf("\n Sum of all numbers = %.2f\n", total);
printf("\n Average of all input numbers = %.2f\n", average);
}
Program to reverse array elements using #include <stdio.h>
another array. int main(){
int n,i,j;
printf("\nEnter the number of elements:");
scanf("%d",&n);
int a[n],b[n];
printf("\n Enter the array elements:");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0,j=n-1;i<n;i++,j--)
b[i]=a[j];
printf("\nThe original array is:");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
printf("\nThe reversed array is:");
for(i=0;i<n;i++){
printf("%d\t",b[i]);
}
return 0;
Program Practices
• Sum & Avg of Elements of an Integer Array
Eg: Input Array: a[5]={2,7,20,1,6}
Output: Sum: 36, Avg: 7.2

• Print the Highest Valued element in an Array


Input Array: a[5]={2,7,20,1,6}, Output: 20

• Print the SECOND Highest Valued element in an Array


Input Array: a[5]={2,7,20,1,6}, Output: 7

You might also like