0% found this document useful (0 votes)
7 views10 pages

9th Lecture

This document is a lecture on arrays in C++ prepared by Dr. Ali Al-Sabaawi. It covers one-dimensional arrays, including operations like adding two arrays and finding prime numbers, as well as two-dimensional arrays, their declaration, initialization, and operations such as summing rows and columns. The lecture concludes with practical lab work assignments related to these concepts.

Uploaded by

wezlo913
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

9th Lecture

This document is a lecture on arrays in C++ prepared by Dr. Ali Al-Sabaawi. It covers one-dimensional arrays, including operations like adding two arrays and finding prime numbers, as well as two-dimensional arrays, their declaration, initialization, and operations such as summing rows and columns. The lecture concludes with practical lab work assignments related to these concepts.

Uploaded by

wezlo913
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Programming In C++

Course 2: Lecture 2, Arrays

Prepared By Dr. Ali Al-Sabaawi


Nineveh University- Faculty of IT- department of software

1
1-D Arrays
• A program to add two arrays.

2
1-D array
Lab work:
1- read an array and check how many prime numbers in this array?

2- read an array and find the max value of this array.

3
2-D arrays
An array is useful for storing and working with a set of data. Sometimes, though, it’s necessary to
work with multiple sets of data.

For example, in a grade-averaging program a teacher might record all of one student’s test scores
in an array of doubles.

If the teacher has 30 students, that means she’ll need 30 arrays of doubles to record the scores for
the entire class. Instead of defining 30 individual arrays, however, it would be better to define a
two-dimensional array.

4
Declaring 2-D array
To define a two-dimensional array, two size declarators are required. The first one is
for the number of rows, and the second one is for the number of columns.

double scores[3][4];

The element of the first row (row 0) The elements of row 1 are The elements of row 2 are

To assign the value 92.25 to the element at row 2, column 1 of


the scores array

Scores [2][1]=92.25; 5
Initialization 2-D array
One way of initializing a two-dimensional array is

int hours[3][2] = {{8, 5}, {7, 9}, {6, 3}};

Second way of assigning elements to array


hours[0][0] = 8
hours[0][1] = 5
hours[1][0] = 7
hours[1][1] = 9
hours[2][0] = 6
hours[2][1] = 3

Third way is using loops


for (int i = 0; i < 3; i++)
for (int j = 0; j < 2; j++)
cin >> hours[i][j] ;
6
2-D array
Summing the Rows of a Two-Dimensional Array.
Suppose a two-dimensional array is used to hold a set of test scores for a set of
students. Each row in the array is a set of test scores for one student. To get the sum
of a student’s test scores (perhaps so an average may be calculated), you use a loop
to add all the elements in one row.

7
2-D array

8
2-D array

Create 2-D array 3*4 and then find the


summation of the each column, assign
the summation in 1-D array.

9
The End

10

You might also like