Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.

Similar presentations


Presentation on theme: "Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration."— Presentation transcript:

1 Arrays

2 Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops 2

3 Introduction to Arrays Arrays acts to store related data under a single variable name with an index. It is easiest to think of an array as simply a list or ordered grouping of variables. Index is zero Based. 3

4 Arrays Example Using normal variables we can only store one value per variable at a given time. There are situations when we need to store multiple values. Example : In a program you need to store marks of 20 students and then find out the maximum, minimum and the average marks. If we use normal variables you need 20 variables to store the values. 4

5 Arrays Example contd. The code will look like: 5 int marks1, marks2, marks3,…,marks20; Cout<<"Enter marks for student 1”; Cin>>marks1; Cout<<“Enter marks for student 2”; Cin>>marks2; ……

6 Array Declaration Syntax NAME[size]; Ex: int numbers[6]; If we wish to initialize as we declare, we write int vector[6]={0,0,1,0,0,0}; 6

7 Declaring Arrays Just as with variables, arrays must also be declared before they are used. The declaration of an array involves declaring the: ◦ Data type of the array elements such as int, float, or char ◦ The maximum number of elements that will be stored in the array. The C++ compiler needs this information to determine the amount of memory that need to be reserved for an array 7

8 Example : Arrays Note that array index numbers starts at 0 If the array has n elements, the last array index number is n-1 You can use an integer variable to refer to the index number. Example: 8 int min; int r; r=0; min = marks[r]; r=1; if (min >marks[r]) min = marks[r];

9 Declaring Arrays As an example, the declaration of an array to store marks of 20 students will look like: int marks[20]; Valid index numbers for this array will be from ??? 0 through 19. 9

10 Arrays Think about evaluating the maximum and minimum marks, you will have to write a series of if statements. Soon you will realize the program is unnecessarily complex. We can overcome this unnecessary complexity by using a structure that can hold multiple values. Array is a data structure that holds multiple values of the same type. 10

11 Example : Arrays You can define a variable called marks, which represents not a single value of a marks, but an entire set of grades. Each element of the set can then be referenced by means of a number called an index If you need the marks of the 1 st student it is at: marks[0] If you need the marks of the 4 th student it is at: marks[3] 11 14 56 44 36 89 43 012345012345 marks Index Number

12 Assigning Values to Array Elements marks[0] = 14 marks[1] = 56 marks[2] = 44 marks[3] = 36 marks[4] = 89 marks[5] = 43 The values may come as an input from user or from a file etc.. 12 14 56 44 36 89 43 012345012345 marks Index Number

13 Assigning Values to Array Elements You can also assign the values at the time you are declaring the array. int marks[6] = {14,56,44,36,89,43} Determined the number of elements automatically based on the number of initialization elements int marks[] = {14,56,44,36,89,43} 13 14 56 44 36 89 43 012345012345 marks Index Number

14 Array Exercise Write a program that has an integer array to store marks of 10 students. The marks for each student will be input by the user. 14

15 Array Exercise Improve your program for exercise Q.61 such that when the marks reading is over you will print all the values to the screen. 15

16 Array Exercise Write a program that has an integer array to store marks of 10 students. The marks for each student will be input by the user. when the marks reading is over display the following details: ◦ Average marks ◦ Maximum marks ◦ Minimum marks 16

17 Break a Continue Statements You must have realized that it is very easy to manipulate an array using loops Most of the time we use the for loop with an array break and continue statements are also useful in array manipulation. 17

18 Array Exercise Declare an integer array that has 10 numbers. Store the values 45,56,67,78,89,90,98,0,0,0 in the array Declare another integer array that has 10 elements Initially all the elements in this array are empty Create a copy of 1 st array to 2 nd array until a zero(0) is encountered. Display the content of the 2 nd array. 18

19 Arrays of Other Data Types Even though we looked at only integer arrays so far you can store any element of a valid data type in C to an array Example : A character array char word[]={'H','e','l','l','o','!'}; Example : A float array float data[] = {1.0f, 100.0f, 200.0f}; 19


Download ppt "Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration."

Similar presentations


Ads by Google