Featured
- Get link
- X
- Other Apps
Arrays In C
An array is a variable that can store multiple values. Like you can store your name as a string and you can also store integer values, float values etc. For example you can store many numbers of integer in a single array. You don't have to make 10 or 100 or many scanfs in your code.
Here is the simple example of an Array:-)
//Example of 1D Array
#include<stdio.h>
int main()
{
int arr;
char str[10];
printf("Enter your Name: ");
scanf("%s",&str);
printf("Enter your roll no: ");
scanf("%d",&arr);
printf("Hello %s and %d is your roll no.",str,arr);
int n,i,physum=0,matsum=0,chemsum=0;
printf("\nEnter the number of students in your class: ");
scanf("%d",&n);
int maths[n],chem[n],phy[n];
printf("Enter marks of Maths for\n");
for(i=1;i<=n;i++)
{
printf("\nStudent %d: ",i);
scanf("%d",&maths[i]);
}
printf("Enter marks of Physics for\n");
for(i=1;i<=n;i++)
{
printf("\nStudent %d: ",i);
scanf("%d",&phy[i]);
}
printf("Enter marks of Chemistry for\n");
for(i=1;i<=n;i++)
{
printf("\nStudent %d: ",i);
scanf("%d",&chem[i]);
}
for(i=1;i<=n;i++)
{
physum+=phy[i];
matsum+=maths[i];
chemsum+=chem[i];
}
printf("\nSum of all the marks obtained by %d students in\nMaths:%d\nPhysics:%d\nChemistry:%d",n,matsum,physum,chemsum);
printf("\n");
for(i=1;i<=n;i++)
{
printf("\nAverage of all marks for student %d is:%d",i,(phy[i]+maths[i]+chem[i])/3);
}
printf("\nAverage marks in Maths: %d",matsum/n);
printf("\nAverage marks in Physics: %d",physum/n);
printf("\nAverage marks in Chemistry: %d",chemsum/n);
return 0;
}
//Example of 2D Array
#include<stdio.h>
int main()
{
char name_of_students[6][8]={"Govind","Yesha","Mark","Tina","Porter"};
for(int i=0;i<6;i++)
{
for(int j=0;j<8;j++)
{
printf("%c",name_of_students[i][j]);
}printf("\n");
}
return 0;
}