Arrays

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Arrays in C

memory
a=50
int a;
int a=50;

int a[10];
float x[50];
char q[20];
 When we work with a large number of data values we need many

number of different variables.

 As the number of variables increases, the complexity of the

program also increases and so the programmers get confused with

the variable names.

 There may be situations where we need to work with a large

number of similar data values.

 To make this work easier, C programming language provides a

concept called "Array".


An array is a special type of variable used to store
multiple values of same data type at a time.

An array is a collection of similar data items


stored in continuous memory locations with
single name.
Declaration of an Array
In C programming language, when we want to create an array we
must know the datatype of values to be stored in that array and also
the number of values to be stored in that array.

datatype arrayName [ size ] ;


Syntax for creating an array with size and initial
values

datatype arrayName [ size ] = {value1, value2, ...} ;

datatype arrayName [ ] = {value1, value2, ...} ;


int age[5][9][9];
int a[2][3]; int a[10];
age[0] age[1] age[2] age[3] age[4]

float age[5] = {22.3, 25.4, 30.6, 32.8 ,35.1};


Char a[]={‘a’,’b’};
int age[] = {22, 25, 30, 32 ,35};
100 104 108 112 116
#include<stdio.h>
main()
{
int i;
int arr[] = {2, 3, 4}; Arr[0]=2
for(i = 0 ; i < 3 ; i++) Arr[1]=3
{ Arr[2]=4
printf("%d\t",arr[i]);
}
}
#include<stdio.h>

void main()
{ Arr[0]=2
int arr[4]; Arr[1]=3
int i, j; Arr[2]=4
printf("Enter array element");
for(i = 0; i < 4; i++) Arr[3]=55
{
scanf("%d", &arr[i]);
}
for(j = 0; j < 4; j++)
{
printf("array elements:\n");
printf("%d\n", arr[j]);
}
}
#include <stdio.h>
main()
{
int arr[5];
arr[0] = 5;
arr[2] = -10;
arr[3/2] = 2; // this is same as arr[1] = 2
arr[3] = arr[0];

printf("%d %d %d %d %d", arr[0], arr[1],


arr[2], arr[3],arr[4]);
}
#include<stdio.h>
main()
{
int i, arr[50], num;
printf("Enter no of elements :");
scanf("%d", &num);

//Reading values into Array


printf("\nEnter the values :");
for (i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}

//Printing of all elements of array


for (i = 0; i < num; i++)
{
printf("\narr[%d] = %d", i, arr[i]);
}
}
Read n elements find sum and average
#include<stdio.h>
sum=0;
main()
for(i=0;i<n;i++)
{
int i,a[50],n; {

float sum,ave; sum=sum+a[i];


printf("enter the no.of elements"); }
scanf("%d",&n); ave=sum/n;
printf("\nEnter the numbers:");
printf("\nThe sum is: %f",sum);
for(i=0;i<n;i++)
printf("\nThe average is: %f",ave);
{
}
scanf("%d",&a[i]);
}
X[5+1]
X[6]
X[5+5] x[10]
X[2*5] x[10]
X[2*5-3]
#include<stdio.h>
LARGEST ELEMENT
int main() {
int a[30], i, num, largest;
printf("\nEnter no of elements :");
scanf("%d", &num);
//Read n elements in an array
for (i = 0; i < num; i++)
scanf("%d", &a[i]);
//Consider first element as largest
largest = a[0];
for (i = 1; i < num; i++)
{
if (a[i] > largest)
largest = a[i];

}
// Print out the Result
printf("\nLargest Element : %d\n", largest);

}
Two-Dimensional Array
data-type array-name[size-1][size-2];

2-D Array: int a[3][4];


Initialization
int stud[4][2] = { { 1234, 56 }, { 12, 33 }, { 14, 80 }, { 13, 78 } } ;

int stud[4][2] = { 1234, 56, 12, 33, 14, 80, 13, 78 } ;

It is important to remember that while initializing a 2-D array it is

necessary to mention the second (column) dimension, whereas the

first dimension (row) is optional.

int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ; // correct

int arr[2][ ] = { 12, 34, 23, 45, 56, 45 } ; //wrong

int arr[ ][ ] = { 12, 34, 23, 45, 56, 45 } ; // wrong

int stud[3][2] = {0}; // all elements are 0


#include<stdio.h>
main()
{
int a[3][3],i,j,m,n;
printf("enter the elements of the matrix \n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
I j
//PRINT MATRIX A[0][0] = 10
printf(" matrix is\n"); A[0][1] = 40
for(i=0;i<3;i++) A[0][2] = 50
{
for(j=0;j<3;j++)
A[1][0] = 90
{ A[1][1]=11
printf("%d",a[i][j]); A[1][2]=22
}
printf("\n"); A[2][0]=222
}
}
#include<stdio.h>
main() //PRINT MATRIX
{
int a[10][10],i,j,m,n; printf(" matrix is\n");
printf("enter the order of the matrix \n"); for(i=0;i<m;i++)
scanf("%d%d",&m,&n); {
printf("enter the elements of the matrix \n");for(j=0;j<n;j++)
for(i=0;i<m;i++) {
for(j=0;j<n;j++) printf("%d",a[i][j]);
scanf("%d",&a[i][j]); }
printf("\n");
}
}
#include<stdio.h>

main()
Addition of Two matrices
{
int a[5][5],b[5][5],c[5][5],i,j,r1,c1,r2,c2;
printf("Enter the size of the matrix A\n");
/* Logic for sum of two matrices */
scanf("%d%d",&r1,&c1);
for(i=0;i<r1;i++)
printf("Enter the size of the matrix B\n");
for(j=0;j<c1;j++)
scanf("%d%d",&r2,&c2);
c[i][j]=a[i][j]+b[i][j];
printf("The addition of two matrices is\n");
/* Condition for summation of two matrices*/
if(r1==r2 && c1==c2)
for(i=0;i<r1;i++)
{
{
printf("Enter the elements of matrix A\n");
for(j=0;j<c1;j++)
for(i=0;i<r1;i++)
printf("%d ",c[i][j]);
for(j=0;j<c1;j++)
printf("\n");
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix B\n");
else
for(i=0;i<r2;i++)
printf("Addition of matrices is not possible\n");
for(j=0;j<c2;j++)
}
scanf("%d",&b[i][j]);
Matrix
Multiplication
Passing arrays to function in C
#include <stdio.h>
void display(int age1, int age2)
{
printf("%d\n", age1);
printf("%d\n", age2);
}

int main()
{
int ageArray[] = {2, 8, 4, 12};

// pass second and third elements to display()


display(ageArray[1], ageArray[2]);
return 0;
}
#include<stdio.h>
float findAverage(int marks[]); Program to calculate the
int main()
sum of array elements by
{
float avg;
passing to a function
int marks[] = {99, 90, 96, 93, 95};
avg = findAverage(marks); // name of the array is passed as
argument.
printf("Average marks = %.1f", avg);
return 0;
}

float findAverage(int marks[])


{
int i, sum = 0;
float avg;
for (i = 0; i <= 4; i++) {
sum += marks[i];
}
avg = (sum / 5);
return avg;
}
#include<stdio.h>
int SumofNumbers(int a[][10], int m,int n);

int main()
{ int SumofNumbers(int a[][10],int m,int n)
int i, Size, a[10][10]; {
int Addition; int Addition = 0;
printf("Please Enter the Size of an Array: "); int i;
scanf("%d%d", &m,&n); for(i = 0; i < Size; i++)
printf("\nPlease Enter Array Elements\n"); {
for(i = 0; i < Size; i++) for(j= 0; j < Size; j++)
{ {
for(j= 0; j < Size; j++) }
}
{
scanf("%d", &a[i][j]);
}
}
Addition = SumofNumbers(a, m,n);
printf("Sum of All Elements in an Array = %d \n", Addition);
return 0;
}
#include <stdio.h> Pass Multidimensional Arrays to a Function
void displayNumbers(int num[2][2]);

int main()
{
int num[2][2], j,i;
printf("Enter 4 numbers:\n"); void displayNumbers(int num[][2])
for (i = 0; i < 2; ++i) {
{ int i,j;
for (j = 0; j < 2; ++j) printf("Displaying:\n");
{ for (i = 0; i < 2; ++i)
scanf("%d", &num[i][j]); {
} for (j = 0; j < 2; ++j)
} {
printf("%d\n", num[i][j]);
displayNumbers(num);
}
return 0; //printf("\n");
} }
}

You might also like