array in C

We need to declare the array by saying type of array.
Each element of the array is of a particular data type. We can’t mix different data types in an array.

Declaring array

int num[5];
int num[5]={19,34,21,55,41};
float percentage[4]={12.4,13.2,15.6,8.3};

Displaying elements of array

int i;
for(i=0;i<5;i++){
 printf(" %d Number = %d \n",i,num[i]);
}
for(i=0;i<4;i++){
 printf(" %d Number = %f \n",i,percentage[i]);
}

Asking users to input elements of an array

#include <stdio.h>
int main(void){
int num[5];
int i;
for(i=0;i<=4;i++){
printf("enter one number \n");
scanf("%d",&num[i]);
}
return 0;
}
Search for element after entering the data by user
#include <stdio.h>
int main(void){
int num[5];
int i;
int x;
int flag=0;
for(i=0;i<=4;i++){
printf("enter one number \n");
scanf("%d",&num[i]);
}

printf("Entry is over, now enter one number to search \n");
scanf("%d",&x);

for(i=0;i<=4;i++){
if(num[i]==x){
        flag=1;
 printf("The number is found = %d \n",num[i]);
}
}
if(flag !=1){
 printf("Number is not found");
}

return 0;
}

Sum and average values of elements of an array

#include <stdio.h>
int main(void){
float sum,avg,num[5];
int i;
//float avg;
for(i=0;i<=4;i++){
printf("enter one number \n");
scanf("%f",&num[i]);
sum=sum+num[i];
}
printf("Sum = %f \n",sum);
avg=sum/5;
printf("Average  = %f \n",avg);

return 0;
}
Output is here
enter one number
3
enter one number
6
enter one number
2
enter one number
8
enter one number
10
Sum = 29.000000
Average  = 5.800000

Highest and lowest number in an array

Two dimensional array

Declaring array and displaying the elements
#include <stdio.h>
int main(void){
int i,j;
int num[3][3]={
                {1,2,3},
                {4,5,6},
                {7,8,9}
                };
for(i=0;i<3;i++){
printf("Top row number %d \n\n", i);
for(j=0;j<3;j++){
printf("Column number %d , value=%d \n\n", j,num[i][j]);
}
}
return 0;
}
Output is here
Top row number 0

Column number 0 , value=1

Column number 1 , value=2

Column number 2 , value=3

Top row number 1

Column number 0 , value=4

Column number 1 , value=5

Column number 2 , value=6

Top row number 2

Column number 0 , value=7

Column number 1 , value=8

Column number 2 , value=9

Reading and displaying elements of two dimensional arrasy

#include <stdio.h>
int main(void){
int i,j;
int num[3][3];

num[2][2]=100;
printf("%d",num[2][2]);

for(i=0;i<3;i++){
printf("Top row number %d \n\n", i);
for(j=0;j<3;j++){
printf("Column number %d , Enter value=", j);
scanf("%d",&num[i][j]);
}
}
printf("\n Data entry is over , now displaying \n");
for(i=0;i<3;i++){
printf("\n value : %d", i);
for(j=0;j<3;j++){
printf("\n value : %d", num[i][j]);
}
}
return 0;
}
Output is here
Top row number 0

Column number 0 , Enter value=1
Column number 1 , Enter value=2
Column number 2 , Enter value=3
Top row number 1

Column number 0 , Enter value=4
Column number 1 , Enter value=5
Column number 2 , Enter value=6
Top row number 2

Column number 0 , Enter value=7
Column number 1 , Enter value=8
Column number 2 , Enter value=9

 Data entry is over , now displaying

 value : 0
 value : 1
 value : 2
 value : 3
 value : 1
 value : 4
 value : 5
 value : 6
 value : 2
 value : 7
 value : 8
 value : 9
Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer