introducing arrays in c. purpose: storing multiple data items under the same name example: salaries...

12
Introducing Arrays Introducing Arrays in C in C

Upload: amice-hunt

Post on 18-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

Introducing Arrays Introducing Arrays in Cin C

Page 2: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

PURPOSE:PURPOSE:

Storing multiple data items under the same nameExample: Salaries of 10 employeesPercentage of marks of my dear 63 students

Note: The data items in an array must be of the same data type (int,float,char)

Page 3: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

Array -definitionArray -definition

Array is a sequenced collection of related data items that share a common name

Page 4: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

Declaring an array Declaring an array variablevariable

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 [ ]

Page 5: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

Representation of Arrays in Representation of Arrays in memmorymemmory

salary[10]

Page 6: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

Accessing Elements of an Accessing Elements of an arrayarray

We make use of the index value to access the elements of an array

ie salary[0]-Represents 1st element of an arraysalary[1]-Represents 2nd element of an arraysalary[6]-

salary[9]-

Page 7: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

Array initialisationArray initialisation

int marks[5]={30,23,44,45,32}float temp[ ]={22.5, 54.6, 71.5, 8.7}

Page 8: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

Entering data into an Entering data into an arrayarray

for(i=0;i<63;i++){

printf(“Enter marks”);scanf(“%d”,&marks[i]);

}

Page 9: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

Reading data from an Reading data from an ArrayArray

for(i=0;i<63;i++){printf(“%d”,marks[i]);}

Page 10: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

PROBLEM 1: PROBLEM 1: Write a program to enter the Write a program to enter the marks obtained by 5 marks obtained by 5 students in a test into an students in a test into an array and then display itarray and then display it

Page 11: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

PROBLEM 2: PROBLEM 2: Write a program to enter the Write a program to enter the marks obtained by 63 marks obtained by 63 students in a test into an students in a test into an array and then find the array and then find the highest mark in the listhighest mark in the list

Page 12: Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear

PROBLEM 3: PROBLEM 3: Write a program to find Write a program to find the average marks the average marks obtained by class of 63 obtained by class of 63 students in a teststudents in a test