Professional Documents
Culture Documents
Arrays
Arrays
Arrays
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
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];
}
// Print out the Result
printf("\nLargest Element : %d\n", largest);
}
Two-Dimensional Array
data-type array-name[size-1][size-2];
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};
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");
} }
}