2.6 ARRAYS IN C

An array is a collection of variables of the same type. In a collection variable of the same type, an individual variable is known as an array element of the array. Total number of variables in the collection is known as size of the array. Each element in the array is identified by an integer index. In C the index begins at zero and is always written inside square brackets. For example if we have an array of type integer for storing marks of all the papers in a semester of students in a class. In C this can be declared as:

int Marks[size];

here size is the total number of subject in that particular semester.

Here Marks is a single dimensional array.If you declare Marks like this

int Marks[10];

Then you can store marks of 10 subjects. You can identify marks of each subject separately as Marks[0], Marks[1], Marks[2], Marks[3], …………,Marks[9].

Arrays can have more dimensions, in which case they might be declared as

int results_2d[20][5];

int results_3d[20][5][3]; C Programming Language

Here each of the index has its own set of square brackets.

Now let us see an example to find average percentage marks of a student in a semester.

Here we assume that student is having 6 subjects is a semester and each subject is having maximum marks 100.

/* Finding average marks using array variable*/

#include <stdio.h>

int main(void)

{

int Marks[6], sum =0;

for ( int i=0; i <6; i++)

{

printf("Give the Marks of Subject %d:",i+1);

scanf("%d",&Marks[i]);

sum = sum+Marks[i];

}

printf("The average percentage Marks of the Semester is:%d",sum/6);

return(0);

}

output:

Give the Marks of Subject 1: 50

Give the Marks of Subject 2:70

Give the Marks of Subject 3:75

Give the Marks of Subject 4:80

Give the Marks of Subject 5:40

Give the Marks of Subject 6:85

The average percentage Marks of the Semester is:66

Now in the last section of this unit let us see some areas of applications of C programming

language.

Comments

Popular posts from this blog

3.8 SECURE NETWORK DEVICES

3.5 SECURITY ISSUES FOR SMALL AND MEDIUM SIZED BUSINESSES

3.6 TOOLS FOR NETWORK SECURITY