0% found this document useful (0 votes)
1 views14 pages

array

Array in c programming

Uploaded by

lakshyagpta8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
1 views14 pages

array

Array in c programming

Uploaded by

lakshyagpta8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

Need of Array Variable

 Suppose we need to store rollno of the student in the integer variable.


Declaration
int rollno;

 Now we need to store rollno of 100 students.


Declaration
int rollno101, rollno102, rollno103, rollno104...;

 This is not appropriate to declare these many integer variables.


e.g. 100 integer variables for rollno.
 Solution to declare and store multiple variables of similar type is an array.
 An array is a variable that can store multiple values.
Definition: Array
 An array is a fixed size sequential collection of elements of same data type grouped
under single variable name.

[0] [1] [2] … [99]


int rollno[100];

Fixed Size Sequential Same Data type Single Name


Here, the size of an It is indexed to 0 to 99 All the elements (0-99) All the elements (0-99)
array is 100 (fixed) to in sequence will be integer will be referred as a
store rollno variables common name rollno
Declaring an array
Syntax  By default array index starts
data-type variable-name[size]; with 0.
 If we declare an array of size
5 then its index ranges from
Integer Array [0] [1] [2] [3] [4] 0 to 4.
int mark[5];
 First element will be store at
mark[0] and last element
integer will be stored at mark[4] not
mark[5].
Float Array [0] [1] [2] [3] [4]  Like integer and float array
float avg[5];
we can declare array of type
char.

float
Initialing and Accessing an Array
Declaring, initializing and accessing single integer variable
int mark=90; //variable mark is initialized with value 90
printf("%d",mark); //mark value printed

Declaring, initializing and accessing integer array variable


int mark[5]={85,75,76,55,45}; //mark is initialized with 5 values
printf("%d",mark[0]); //prints 85
printf("%d",mark[1]); //prints 75
printf("%d",mark[2]); //prints 65
printf("%d",mark[3]); //prints 55
printf("%d",mark[4]); //prints 45

[0] [1] [2] [3] [4]


mark[5] 85 75 65 55 45
Read(Scan) Array Elements
Reading array without loop Reading array using loop
1 void main() 1 void main()
2 { 2 {
3 int mark[5]; 3 int mark[5],i;
printf("Enter array element="); for(i=0;i<5;i++)
4 4
scanf("%d",&mark[0]); {
5 printf("Enter array element="); 5 printf("Enter array element=");
6 scanf("%d",&mark[1]); 6 scanf("%d",&mark[i]);
7 printf("Enter array element="); 7 }
8 scanf("%d",&mark[2]); 8 for(i=0;i<5;i++)
9 printf("Enter array element="); 9 {
10 scanf("%d",&mark[3]); 10 printf("%d",mark[i]);
11 printf("Enter array element="); 11 }
scanf("%d",&mark[4]);
12 12 }
13 printf("%d",mark[0]);
14 printf("%d",mark[1]);
15 printf("%d",mark[2]);
16 printf("%d",mark[3]); [0] [1] [2] [3] [4]
17 printf("%d",mark[4]); mark[5] 85 75 65 55 45
18 }
Develop a program to count number of positive or negative
number from an array of 10 numbers.
Program
1 void main(){
2 int num[10],i,pos,neg; Output
3 pos = 0; Enter array element=1
4 neg = 0; Enter array element=2
5 for(i=0;i<10;i++) Enter array element=3
6 { Enter array element=4
7 printf("Enter array element="); Enter array element=5
8 scanf("%d",&num[i]); Enter array element=-1
9 } Enter array element=-2
10 for(i=0;i<10;i++) Enter array element=3
11 { Enter array element=4
12 if(num[i]>0) Enter array element=5
13 pos=pos+1; Positive=8,Negative=2
14 else
15 neg=neg+1;
16 }
17 printf("Positive=%d,Negative=%d",pos,neg);
18 }
Develop a program to read n numbers in an array and print
them in reverse order.
Program
1 void main()
2 { Output
3 int num[100],n,i; Enter number of array
4 printf("Enter number of array elements="); elements=5
5 scanf("%d",&n); Enter array element=1
6 //loop will scan n elements only Enter array element=2
7 for(i=0;i<n;i++) Enter array element=3
8 { Enter array element=4
9 printf("Enter array element="); Enter array element=5
10 scanf("%d",&num[i]); 5
11 } 4
12 //negative loop to print array in reverse order 3
13 for(i=n-1;i>=0;i--) 2
14 { 1
15 printf("%d\n",num[i]);
16 }
17 }
Practice Programs
1) WAP a program to calculate sum of n array elements in C.
2) WAP a program to calculate average of n array elements in C.
3) WAP a program to find largest array element in C.
4) WAP a program to print sum of second and second last element of an array.
5) WAP a program to copy array elements to another array.
6) WAP a program to count odd and even elements of an array.
Multi Dimensional Array
Declaring 2 Dimensional Array
Syntax  A two dimensional array can
data-type variable-name[x][y]; be seen as a table with ‘x’
rows and ‘y’ columns.
Declaration  The row number ranges from
int data[3][3]; //This array can hold 9 elements 0 to (x-1) and column
number ranges from 0 to
(y-1).
int data[3][3];
Column-0 Column-1 Column-2

Row-0 data[0][0] data[0][1] data[0][2]

Row-1 data[1][0] data[1][1] data[1][2]

Row-2 data[2][0] data[2][1] data[2][2]


Initialing and Accessing a 2D Array: Example-1
Program
1 int data[3][3] = {
2 {1,2,3}, //row 0 with 3 elements
3 {4,5,6}, //row 1 with 3 elements
4 {7,8,9} //row 2 with 3 elements
5 }; Column-0 Column-1 Column-2
6 printf("%d",data[0][0]); //1
7 printf("%d",data[0][1]); //2 Row-0 1 2 3
8 printf("%d\n",data[0][2]); //3
9 Row-1 4 5 6
10 printf("%d",data[1][0]); //4
11 printf("%d",data[1][1]); //5 Row-2 7 8 9
12 printf("%d\n",data[1][2]); //6
13
14 printf("%d",data[2][0]);//7
15 printf("%d",data[2][1]); //8
16 printf("%d",data[2][2]); //9
1 // data[3][3] can be initialized like this also
2 int data[3][3]={{1,2,3},{4,5,6},{7,8,9}};
Initialing and Accessing a 2D Array: Example-2
Program
1 int data[2][4] = {
2 {1,2,3,4}, //row 0 with 4 elements
3 {5,6,7,8}, //row 1 with 4 elements
4 };
5 printf("%d",data[0][0]); //1
6 printf("%d",data[0][1]); //2 Col-0 Col-1 Col-2 Col-3
7 printf("%d",data[0][2]); //3
8 printf("%d\n",data[0][3]); //4 Row-0 1 2 3 4
9
10 printf("%d",data[1][0]); //5 Row-1 5 6 7 8
11 printf("%d",data[1][1]); //6
12 printf("%d",data[1][2]); //7
13 printf("%d",data[1][3]); //8

1 // data[2][4] can be initialized like this also


2 int data[2][4]={{1,2,3,4},{5,6,7,8}};
Read(Scan) 2D Array Elements
Program
1 void main(){
2 int data[3][3],i,j;
3 for(i=0;i<3;i++)
4 {
5 for(j=0;j<3;j++)
6 { Output
7 printf("Enter array element="); Enter array element=1
8 scanf("%d",&data[i][j]); Enter array element=2
9 } Enter array element=3
10 } Enter array element=4
11 for(i=0;i<3;i++) Enter array element=5
12 { Enter array element=6
13 for(j=0;j<3;j++) Enter array element=7
14 { Enter array element=8
15 printf("%d",data[i][j]); Enter array element=9
16 } 123
17 printf("\n"); 456
18 } 789
19 }
Develop a program to count number of positive, negative and
zero elements from 3 X 3 matrix
Program
1 void main(){
2 int data[3][3],i,j,pos=0,neg=0,zero=0; Output
3 for(i=0;i<3;i++) Enter array element=9
4 { Enter array element=5
5 for(j=0;j<3;j++) Enter array element=6
6 { Enter array element=-3
7 printf("Enter array element="); Enter array element=-7
8 scanf("%d",&data[i][j]); Enter array element=0
9 if(data[i][j]>0) Enter array element=11
10 pos=pos+1; Enter array element=13
11 else if(data[i][j]<0) Enter array element=8
12 neg=neg+1; positive=6,negative=2,zero=1
13 else
14 zero=zero+1;
15 }
16 }
17 printf("positive=%d,negative=%d,zero=%d",pos,neg,zero);
18 }

You might also like