Introducing Arrays in C
PURPOSE: Storing multiple data items under the same name Example: Salaries of 10 employees Percentage of marks of my dear 63 students Note: The data items in an array must be of the same data type ( int,float,char )
Array -definition Array is a sequenced collection of related data items that share a common name
Declaring an array variable int marks[63] int salary[10] float temperature[100] Note: static keyword can be used to intialise the values to 0 ie static float temperature [ ]
Representation of Arrays in memmory salary[10]
Accessing Elements of an array We make use of the index value to access the elements of an array ie salary[0]- Represents 1 st element of an array salary[1]- Represents 2 nd element of an array salary[6]- salary[9]-
Array initialisation int marks[5]={30,23,44,45,32} float temp[ ]={22.5, 54.6, 71.5, 8.7}
Entering data into an array for(i=0;i<63;i++) { printf(“Enter marks”); scanf(“%d”,&marks[i]); }
Reading data from an Array for(i=0;i<63;i++) { printf(“%d”,marks[i]); }
PROBLEM 1: Write a program to enter the marks obtained by 5 students in a test into an array and then display it
PROBLEM 2: Write a program to enter the marks obtained by 63 students in a test into an array and then find the highest mark in the list
PROBLEM 3: Write a program to find the average marks obtained by class of 63 students in a test