Upload
gregory-king
View
214
Download
0
Embed Size (px)
DESCRIPTION
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
Citation preview
Arrays
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
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
Arrays ExampleUsing 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
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;……
Array DeclarationSyntax<type> 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
Declaring ArraysJust 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
Example : ArraysNote that array index numbers starts
at 0If the array has n elements, the last
array index number is n-1You 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];
Declaring ArraysAs 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
ArraysThink 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
Example : ArraysYou 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 1st student it is at: marks[0]
If you need the marks of the 4th student it is at: marks[3]
11
145644368943
012345
marks
Index Number
Assigning Values to Array Elementsmarks[0] = 14marks[1] = 56marks[2] = 44marks[3] = 36marks[4] = 89marks[5] = 43
The values may come as an input from user or from a file etc..
12
145644368943
012345
marks
Index Number
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
145644368943
012345
marks
Index Number
Array ExerciseWrite 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
Array ExerciseImprove your program for
exercise Q.61 such that when the marks reading is over you will print all the values to the screen.
15
Array ExerciseWrite 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
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
Array ExerciseDeclare an integer array that has 10
numbers.Store the values
45,56,67,78,89,90,98,0,0,0 in the arrayDeclare another integer array that has
10 elementsInitially all the elements in this array
are emptyCreate a copy of 1st array to 2nd array
until a zero(0) is encountered.Display the content of the 2nd array. 18
Arrays of Other Data TypesEven 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 arraychar word[]={'H','e','l','l','o','!'};
Example : A float arrayfloat data[] = {1.0f, 100.0f, 200.0f};
19