Computer Science, City University of
Semester B 2021-22
CS2310 Computer Programming
Copyright By PowCoder代写 加微信 powcoder
LT6: Array (1D and 2D)
int mark1, mark2, mark3, mark4, mark5,
mark6, mark7, mark8, mark9, mark10, average;
cin >> mark1 >> mark2 >> mark3 >> mark4 >> \\
mark5 >> mark6 >> mark7 >> mark8 >> mark9 >> mark10;
cout << “The mark of the students are: “ << mark1 \\
<< mark2 << mark3 << mark4 << mark5 << mark6 \\
<< mark7 << mark8 << mark9 << mark10 << endl;
average=(mark1+mark2+mark3+mark4+mark5
+mark6+mark7+mark8+mark9+mark10)/10;
cout << "Average mark"<< average << endl;
100 30 44 66 50 60 80 75 80 100
The mark of the students are: 100 30 44 66 50 60 80 75 80 100
Average mark=68
What is an Array?
Sequence of data items of the same type
Stored contiguously
Can be accessed by index, or subscript
data_type array_name[size];
Array definition
Array initialization
Updating array elements
Printing the content of arrays
Array definition
There are ten elements in this array
mark[0], mark[1], ……, mark[9]
The ith array element is mark[i-1].
The range of the subscript i ranges from 0 to array_size-1
The location mark[10] is invalid. Array out of bound!
int mark[10];
Data type of the array element
Size of the array
Name of the array
Array definition
Array size
int mark[50*50];
int mark[n]
int const n=10;
int mark[n]
Storing values to array elements
Suppose the mark for the first student is 30. We can use the notation
mark[0] = 30;
Input the marks of the second student
cin >> mark[1];
Input the marks for all 10 students
for (i=0;i<10;i++)
cin >> mark[i];
for (i=1;i<=10;i++) cin >> mark[i-1];
int mark[10]
Retrieving the values of an array element
Print the mark of the second student
cout << mark[1]; Print and Sum the marks of all the students for (i=0;i<10;i++) { cout << mark[i]; sum += mark[i]; Example 1 (using an integer array with 10 elements) /*define variables for storing10 students' mark*/ int marks[10], sum=0, average; /*input marks of student*/ for (i=0;i<10;i++) cin >> mark[i];
/*print the marks*/
cout << "The mark of the students are:";
for (i=0;i<10;i++) {
cout << mark[i];
sum=sum + mark[i];
/*compute and print the average*/
average=sum/10;
cout << "Average mark=“ << average << endl;
C++ Macro: #define
#define is a C++ predefined macro keyword. It is very useful. It globally replaces all occurrences of A to B in the ENTIRE source code listing (all .cpp and all .h files).
#define A B
#define N 100
#define SIZE 10
Using #define
#define N 10
int main() {
int marks[N], sum=0, average;
for (i=0;i
cout << "The mark of the students are:";
for (i=0;i
using namespace std;
void main(){
int count[10]; //number of occurrence of digits
int digit; //input digit
int i; //loop index
//read the digits
cin >> digit;
if (digit>=0 && digit<=9) //necessary to avoid out-of-bound
count[digit]++;
} while(digit != -1); //stop if the input number is -1
//print the occurances
for (i=0; i<10;i++){
cout << "Frequency of " << i << " is " << count[i] << endl;
The actual output (incorrect!)
3 4 1 3 1 3 -1
Frequency of 0 is 2089878893
Frequency of 1 is 2088886165
Frequency of 2 is 1376256
Frequency of 3 is 3
Frequency of 4 is 1394145
Frequency of 5 is 1245072
Frequency of 6 is 4203110
Frequency of 7 is 1394144
Frequency of 8 is 0
Frequency of 9 is 1310720
It's a good practice to initialize arrays
Otherwise, the values of the elements in the array is unpredictable
A common way to initialize an array is to set all the elements to zero
for (i=0; i<10; i++)
count[i]=0;
Array initializer
int mark[2]={100, 90};
int mark[10]={100, 90};
Define an array of 10 elements, set the 1st element to 100 and the 2nd element to 90.
We list fewer values than the array size (10). The remaining elements are set to 0 by default
To initialize all elements to 0,
int mark[10]={0};
Correct program for example 2
#include
using namespace std;
void main(){
int count[10]={0}; //number of occurrence of digits, and initialization
int digit; //input digit
int i; //loop index
//read the digits
cin >> digit;
if (digit>=0 && digit<=9)
count[digit]++;
} while(digit != -1); //stop if the input number is -1
//print the frequency
for (i=0; i<10;i++){
cout << "Frequency of " << i << " is " << count[i] << endl;
Array Initializer
char array
char univ[5]={‘C’, ‘i’, ‘t’, ‘y’, ‘U’};
char univ[] = “CityU"; or
char * univ = “CityU";
Array Initialization Summary
Only fixed-length arrays can be initialized when they are defined.
Variable length arrays must be initialized by inputting or assigning the values.
Summary of Array Declaration and Access
Type Variable Array Variable Access Array Access
int int x; int x[20]; x=1; x[0]=1
float float x; float x[10]; x=3.4; x[0]=3.4;
double double x; double x[20]; x=0.7; x[0]=0.7;
char char x; char x[5]; x='a'; x[0]=‘c’;
Example 3: Comparing 2 arrays
We have two integers arrays, each with 5 elements
int array1[5]={10, 5, 3, 5, 1};
int array2[5];
The user inputs the values of array2
Compare whether all of the elements in array1 and array2 are the same
Array equality
Note that you have to compare array element 1 by 1.
The following code generates incorrect results
if (array1 == array2)
cout << "The arrays are equal ";
cout << "The arrays are not equal ";
The Program
void main(){
int array1[5]={10, 5, 3, 5, 1};
int array2[5];
bool arrayEqual=true;
cout << "Input 5 numbers\n";
for (i=0;i<5;i++)
cin >> array2[i];
for (i=0; i<5 && arrayEqual; i++){
if (array1[i]!=array2[i]){
arrayEqual=false;
if (arrayEqual)
cout << "The arrays are equal";
cout << "The arrays are not equal";
Input 5 numbers
10 5 3 5 1
The arrays are equal
Input 5 numbers
10 4 3 5 2
The arrays are not equal
Example 4: Searching
Read 10 numbers from the user and store them in an array
User input another number x
The program checks if x is an element of the array
If yes, output the index of the element
If no, output -1
Searching for the rabbit (Case I)
Search sequentially
If found, skip the rest
Searching for the rabbit (Case II)
Searching for x=15 (Case 1)
Output i=2
break out of the loop
Searching for x=8 (Case 2)
The program
void main()
const int N = 6;
int a[N], i, x, position = -1;
for (i=0; i
cout << "Input your target: ";
for (i=0; i
for(j=0; j
if (a[k]> page [i][j]; //[row][column]
The above statement will input an integer into row i and column j of the array
BMI Program
void main()
const int N=10;
double data[N][2];// N records, each holds weight and height
int i, position;
for (i=0; i
cin >> data[i][1];
for (i=0; i