/* arrays in c */
Before going to discuss about arrays lets go through some sample program
main()
{
int a,b;
printf("\n enter any two values");
scanf("%d%d",&a,&b);
printf("the given two values are a=%d\tb=%d",a,b);
}
output:
enter any two values
10 20
the given two values are a=10 b=20
well this program good for reading two values
so what if we want to read 50 variables from keyboard ?
one solution is intialising 50 variables like a,b,c,d,,,,,,,, and reading varibales of
keyoard from scanf function.
But declaring 50 variables is looking complex
Arrays:
main()
{
int a[10];
a[0]=101;
a[1]=102;
a[2]=103;
a[3]=104;
a[4]=105;
a[5]=106;
a[6]=107;
a[7]=108;
a[8]=109;
a[9]=110;
printf("\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
}
output:101 102 103 104 105 106 107 108 109
main()
{
int a[10];/* a is an array which occupies (10*2)bytes of memory */
int i,n;
printf("enter your range");
scanf("%d",&n);/* no of elements */
printf("enter %d elements",n);
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nentered elements are");
for(i=0;i<10;i++)
{
printf("\t%d",a[i]);
}
}
output:
enter your range
10
enter 10 elements
1 2 3 4 5 6 7 8 9 10
entered elements are 1 2 3 4 5 6 7 8 9 10
/* program to calculate the total marks and average marks for give six subjects*/
main()
{
int a[6],i,total=0;
float avg;
printf("\n enter your six subject marks");
for(i=0;i<6;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<6;i++)
{
total=total+a[i];
}
avg=total/6.0;
printf("\n total marks is %d",total);
printf("\average marks is %f",avg);
}
output:
enter your six subject marks
90 90 90 80 100 90
total marks is 540
average marks is 90
arrays in c, derived data type array, array declaration, array initialization , sample program of arrays linear search using arrays binary search using arrays, calculating using total and average marks of a student,matrix multiplication
Before going to discuss about arrays lets go through some sample program
main()
{
int a,b;
printf("\n enter any two values");
scanf("%d%d",&a,&b);
printf("the given two values are a=%d\tb=%d",a,b);
}
output:
enter any two values
10 20
the given two values are a=10 b=20
well this program good for reading two values
so what if we want to read 50 variables from keyboard ?
one solution is intialising 50 variables like a,b,c,d,,,,,,,, and reading varibales of
keyoard from scanf function.
But declaring 50 variables is looking complex
Arrays:
main()
{
int a[10];
a[0]=101;
a[1]=102;
a[2]=103;
a[3]=104;
a[4]=105;
a[5]=106;
a[6]=107;
a[7]=108;
a[8]=109;
a[9]=110;
printf("\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
}
output:101 102 103 104 105 106 107 108 109
main()
{
int a[10];/* a is an array which occupies (10*2)bytes of memory */
int i,n;
printf("enter your range");
scanf("%d",&n);/* no of elements */
printf("enter %d elements",n);
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nentered elements are");
for(i=0;i<10;i++)
{
printf("\t%d",a[i]);
}
}
output:
enter your range
10
enter 10 elements
1 2 3 4 5 6 7 8 9 10
entered elements are 1 2 3 4 5 6 7 8 9 10
/* program to calculate the total marks and average marks for give six subjects*/
main()
{
int a[6],i,total=0;
float avg;
printf("\n enter your six subject marks");
for(i=0;i<6;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<6;i++)
{
total=total+a[i];
}
avg=total/6.0;
printf("\n total marks is %d",total);
printf("\average marks is %f",avg);
}
output:
enter your six subject marks
90 90 90 80 100 90
total marks is 540
average marks is 90
arrays in c, derived data type array, array declaration, array initialization , sample program of arrays linear search using arrays binary search using arrays, calculating using total and average marks of a student,matrix multiplication
No comments:
Post a Comment