Week 10 lecture
Week 10 lecture
Fundamentals
Lecture # 19
Thursday, November 02, 2023
FALL 2023
FAST – NUCES, Faisalabad Campus
Rizwan Ul Haq
2
Arrays
cout << "The average of the given numbers = " << average ;
cout << "\nand the numbers are n1 = " << n1 << " n2 = " << n2
<< " n3 = " << n3 << " n4 = " << n4
<< " n5 = " << n5 << endl ;
return 0;
}
CS1002 - Fall 2023
5 Example
Five variables must be declared because the numbers are to be
printed later
All variables are of type int, that is, of the same data type
The way in which these variables are declared indicates that the
variables to store these numbers all have the same name, except the
last character, which is a number
list[5] = 75;
[0] [1] [2] [3] [4] [5] [6] [7]
list 75
list[6]=100;
First declare a named constant and then use it to declare an array of this specific size.
When an array is declared its size must be known. You cannot do this:
int arr_size;
cout << "Enter size of array ";
cin >> arr_size;
int arr[arr_size];
Example:
for (i = 0; i < 100; i++) //Line 1
cin >> list[i]; //Line 2
CS1002 - Fall 2023
16 Processing One-Dimensional Arrays (cont'd.)
double scores[10];
int index;
double largest, sum, average;
Initializing an array
for (index = 0 ; index < 10 ; ++index)
scores[index] = 0.0 ;
100 103 104 107 108 111 112 115 116 119 120 123 124 127 128 131 132 135 136 139
Note: Compiler just knows the address of the first element of the array known as the base
address of the array. All other indexes are calculated relative to base address.
CS1002 - Fall 2023
26 How array element is calculated
Can we have –ve indexes in the array
Like array[-2]
Solution:
for(int i = 0 ; i < 5 ; ++i)
yourList[i] = myList[i] ;
CS1002 - Fall 2023
33 Some Restrictions on Array Processing (cont'd.)
The following is illegal too:
cin >> yourList ; //illegal
Solution:
for(int i=0 ; i<5 ; ++i)
cin >> yourList[i] ;
The following statements are legal, but do not give the desired results:
cout << yourList ;
if(myList <= yourList)
.
.
.
26
27 cout << "Face" << setw( 13 ) << "Frequency" << endl;
28
29 // output frequency elements 1-6 in tabular format
30 for ( int face = 1; face < ARRAY_SIZE; face++ )
31 cout << setw( 4 ) << face
32 << setw( 13 ) << frequency[ face ] << endl;
33
34 return 0; // indicates successful termination
35
36 } // end main
Program Output …
Face Frequency
1 1003
2 1004
3 999
4 980
5 1013
CS1002 - Fall 2023
6 1001
40 Arrays by an Example –VII …
1 // C++ Program
2 // Student poll program.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <iomanip>
9
10 using std::setw;
11
12 int main()
13 {
14 // define array sizes
15 const int RESPONSE_SIZE = 40; // size of array responses
16 const int FREQUENCY_SIZE = 11; // size of array frequency
17
18 // place survey responses in array responses
19 int responses[RESPONSE_SIZE ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8,
20 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7,
21 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
22
23 // initialize frequency counters to 0
24 int frequency[FREQUENCY_SIZE ] = { 0 };
25
CS1002 - Fall 2023
41