[go: up one dir, main page]

0% found this document useful (0 votes)
33 views13 pages

PF Lab 09

The document discusses programming fundamentals related to arrays in C++. It covers array of characters (strings), 2-dimensional arrays, multidimensional arrays, and provides examples of declaring, initializing, and accessing elements of 1D, 2D and 3D arrays. It also lists 2 programming tasks - the first involves storing temperature data of 2 cities for a week using a 2D array and displaying it, and the second requires inputting 2 2D arrays from the user, adding them together, and displaying the result.

Uploaded by

Muhammad Haziq
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)
33 views13 pages

PF Lab 09

The document discusses programming fundamentals related to arrays in C++. It covers array of characters (strings), 2-dimensional arrays, multidimensional arrays, and provides examples of declaring, initializing, and accessing elements of 1D, 2D and 3D arrays. It also lists 2 programming tasks - the first involves storing temperature data of 2 cities for a week using a 2D array and displaying it, and the second requires inputting 2 2D arrays from the user, adding them together, and displaying the result.

Uploaded by

Muhammad Haziq
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/ 13

PROGRAMMING

FUNDAMENTALS
Lab-09

SYED AHMAD MEHMOOD

INFT231101096

BSIT

1B,2023

___________________________________________________________________________100
Programming Fundamentals - Lab [COSC-1201]
9.1. OBJECTIVES
1. Array of Characters
2. 2-Dimensional Array
3. Multidimensional Array

9.2. Array of characters – String:

“A collection of characters written in double quotations is called string”. A String is stored as an


array of characters.
To store a string, we declare a character array:
Char arrayname [length];
A character array can be initialized as:
Char bookname[50] = “Beginning C++”;

Note: Characters can be inserted in to an array using cin object. But the limitation of cin object is
that it cannot input a string with blank spaces.
To insert a string using cin object, consisting of characters and blank spaces, we use “getline”
function with cin object.
The syntax is as:

Char book[40];
Cout<<”Enter a book name”;
Cin.getline(book,40);
Cout<<book; //will output the string stored in book array

9.3. 2-Dimensional Array

Two-dimensional array can be considered as a table that consists of rows and column. Each
element in a 2-D Array is referred with the help of two indexes. We can think this array as table
with 3 rows and each row has 4 columns as shown below.

To declare a 2-d array:


int arrayname[rows][columns];
___________________________________________________________________________101
Programming Fundamentals - Lab [COSC-1201]
9.3.1. Initializing 2-D Arrays

To initialize a 2 D array with 2 rows and 3 columns, we write as:


int matrix[2][3] = {{12,11,10},
{11,12,13}};
Another method to initialize a 2-dimensional array is:
Int matrix[2][3] = {12,11,10,11,12,13};

9.3.2. Accessing 2-D Array Elements

An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and
column index of the array. For example:
int val = a[2][3];

Example
#include <iostream>
using namespace std;
int main () {
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
// output each array element's value
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ )
{
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}

When the above code is compiled and executed, it produces the following result:
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4

___________________________________________________________________________102
Programming Fundamentals - Lab [COSC-1201]
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8

As explained above, we can have arrays with any number of dimensions, although it is likely that
most of the arrays you create will be of one or two dimensions.

9.4. Three Dimensional Arrays

A three-dimensional (3D) array is an array of arrays of arrays. It's an array or collection of 2D


arrays, and a 2D array is an array of 1D array. A 3-d array can be think of as collection of tables
where each table has rows and columns. The syntax of declaring 3-d array is:
Data-type arrayName [Tables][rows][cols];
Where data-type can be any valid C++ data type and array name will be a valid C++ identifier,
Tables is actually the number of collection of tables that the user wants and rows and columns
refers to the numbers of rows and columns in each table respectively.
The diagram below may help you understand:

The array in figure above has three dimensions and can be declared as:
int arr[3][3][3];

9.4.1. Initializing 3-d array

Three dimensional arrays may be initialized by specifying bracketed values for each row where
each table is further nested in brackets. Following is an array of 2 tables, having 3 rows and 4
columns:
int a[2][3][4] = {
{ /*Opening nested bracket for table indexed by 0 */
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
}, /*Closing nested bracket for table indexed by 0 */

{ /*Opening nested bracket for table indexed by 1 */


___________________________________________________________________________103
Programming Fundamentals - Lab [COSC-1201]
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
} /*Closing nested bracket for table indexed by 1 */
};
Three-dimensional (3D) array contains three for loops in programming. So, to initialize and print
three-dimensional array, you have to use three for loops. Third for loop (the innermost loop)
forms columns, Second for loop forms rows and the third for loop (the outermost loop) forms
tables, as shown here in the following program.

Example
Following is a simple C++ program to initialize three-dimensional (3D) array of dimensions
3*4*2, then it will access some elements present in the array and display the element on the
screen:
/* C++ Program - Three Dimensional Array Program */

#include<iostream>
using namespace std;
int main()
{
int arr[3][4][2] = {
{
{2, 4},
{7, 8},
{3, 4},
{5, 6}
},
{
{7, 6},
{3, 4},
{5, 3},
{2, 3}
},
{
{8, 9},
{7, 2},
{3, 4},
{5, 1}
}
};
cout<<"arr[0][0][0] = "<<arr[0][0][0]<<"\n";
cout<<"arr[0][2][1] = "<<arr[0][2][1]<<"\n";
cout<<"arr[2][3][1] = "<<arr[2][3][1]<<"\n";
}
When the above C++ program is compile and executed, it will produce the following result:

___________________________________________________________________________104
Programming Fundamentals - Lab [COSC-1201]
Example
Here is an example using the for loop command:
#include<iostream>
using namespace std;

int main()
{
int i, j, k;
int val=0;
int arr[3][3][3];

cout<<":::3D Array Elements:::\n\n";

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
arr[i][j][k] = val;
cout<<arr[i][j][k]<<“\t”;
val++;
}
cout<<"\n";
}
cout<<"\n";
}
}

9.5. LAB TASKS


9.5.1. Program # 1

C++ Program to store temperature of two different cities for a week using 2-D array and display
it. (Hint: Declare a 2-d array with 2 rows and 7 columns).
Output of the Program should be like this:

Enter all temperature for a week of first city and then second city.
City 1, Day 1 : 32
City 1, Day 2 : 33
City 1, Day 3 : 32
City 1, Day 4 : 34
___________________________________________________________________________105
Programming Fundamentals - Lab [COSC-1201]
City 1, Day 5 : 35
City 1, Day 6 : 36
City 1, Day 7 : 38
City 2, Day 1 : 23
City 2, Day 2 : 24
City 2, Day 3 : 26
City 2, Day 4 : 22
City 2, Day 5 : 29
City 2, Day 6 : 27
City 2, Day 7 : 23

Displaying Values:
City 1, Day 1 = 32
City 1, Day 2 = 33
City 1, Day 3 = 32
City 1, Day 4 = 34
City 1, Day 5 = 35
City 1, Day 6 = 36
City 1, Day 7 = 38
City 2, Day 1 = 23
City 2, Day 2 = 24
City 2, Day 3 = 26
City 2, Day 4 = 22
City 2, Day 5 = 29
City 2, Day 6 = 27
City 2, Day 7 = 23

#include <iostream>
using namespace std;

int main() {
const int CITY = 2;
const int WEEK = 7;
int temperature[CITY][WEEK];

cout << "Enter all temperature for a week of first city and then second city." << endl;

___________________________________________________________________________106
Programming Fundamentals - Lab [COSC-1201]
for (int i = 0; i < CITY; i++) {
for (int j = 0; j < WEEK; j++) {
cout << "City " << i + 1 << ", Day " << j + 1 << " : ";
cin >> temperature[i][j];
}
}

cout << "Displaying Values:" << endl;

for (int i = 0; i < CITY; i++) {


for (int j = 0; j < WEEK; j++) {
cout << "City " << i + 1 << ", Day " << j + 1 << " = " << temperature[i][j] << endl;
}
}

return 0;
}

9.5.2. Program No 2

Write a program that inputs two, 2-D arrays from user. Add the arrays, and display their result.
(Addition in arrays(Matrices) is only possible if number of rows and columns are equal for both
arrays.)
#include <iostream>
using namespace std;

int main() {
int r, c, i, j;
cout << "Enter the number of rows and columns of the matrices: ";
cin >> r >> c;

int a[r][c], b[r][c], sum[r][c];

cout << "Enter the elements of the first matrix: " << endl;
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
cin >> a[i][j];
}
}

cout << "Enter the elements of the second matrix: " << endl;
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
cin >> b[i][j];
}
}

cout << "The sum of the two matrices is: " << endl;
for (i = 0; i < r; ++i) {

___________________________________________________________________________107
Programming Fundamentals - Lab [COSC-1201]
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
cout << sum[i][j] << " ";
}
cout << endl;
}
}

9.5.3. Program No 3

Write a program that inputs a 2-D array consisting of 3 rows and 4 columns. Finds the maximum
and minimum number from array and display the result.

#include <iostream>
using namespace std;

int main() {
int arr[3][4];
int i, j, max, min;

cout << "Enter the elements of the array: " << endl;
for (i = 0; i < 3; ++i) {
for (j = 0; j < 4; ++j) {
cin >> arr[i][j];
}
}

max = arr[0][0];
min = arr[0][0];

for (i = 0; i < 3; ++i) {


for (j = 0; j < 4; ++j) {
if (arr[i][j] > max) {
max = arr[i][j];
}
if (arr[i][j] < min) {
min = arr[i][j];
}
}
}

cout << "The maximum number in the array is: " << max << endl;
cout << "The minimum number in the array is: " << min << endl;

return 0;
}

___________________________________________________________________________108
Programming Fundamentals - Lab [COSC-1201]
9.5.4. Program No 4

Write a C++ program that initialize a 3D array of 2 tables 4 rows and 2 columns and then
displays the maximum and minimum number in the complete array.
#include <iostream>
using namespace std;

int main() {
int arr[2][4][2];
int i, j, k, max, min;

cout << "Enter the elements of the array: " << endl;
for (i = 0; i < 2; ++i) {
for (j = 0; j < 4; ++j) {
for (k = 0; k < 2; ++k) {
cin >> arr[i][j][k];
}
}
}

max = arr[0][0][0];
min = arr[0][0][0];

for (i = 0; i < 2; ++i) {


for (j = 0; j < 4; ++j) {
for (k = 0; k < 2; ++k) {
if (arr[i][j][k] > max) {
max = arr[i][j][k];
}
if (arr[i][j][k] < min) {
min = arr[i][j][k];
}
}
}
}

cout << "The maximum number in the array is: " << max << endl;
cout << "The minimum number in the array is: " << min << endl;

return 0;
}

___________________________________________________________________________109
Programming Fundamentals - Lab [COSC-1201]
9.5.5. Program No 5
Write a program that gets the name from user and then print back on the screen.

#include <iostream>
using namespace std;

int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Your name is: " << name << endl;
return 0;
}

___________________________________________________________________________110
Programming Fundamentals - Lab [COSC-1201]
9.5.6. Program No 6
Write a program that keeps on entering sentences and paragraphs until user presses!

#include <iostream>
#include <string>
using namespace std;

int main() {
string input;
cout << "Enter your sentences and paragraphs. Press
enter to submit each one. Type ! to exit." << endl;
while (getline(cin, input)) {
if (input == "!") {
break;
}
cout << "You entered: " << input << endl;
}
return 0;
}

___________________________________________________________________________111
Programming Fundamentals - Lab [COSC-1201]
EXERCISE QUESTIONS
1. How many dimensions the following array has?
int arr[1][2][3][1][2];
a. 2
b. 3
c. 4
d. 5
2. How many loops will be required to fill the elements in following array has?
int arr[1][2][3][1][2];
a. 2
b. 3
c. 4
d. 5
3. What total number of elements the following array can store?
int arr[1][2][3][1][2];
a. 14
b. 10
c. 12
d. 9
4. How many bytes are occupied if an array is declared as below considering integer is of 4 bytes each:
int arr[2][2][3][2];
a. 24
b. 96
c. 72
d. 48
5. What size is set by compiler for the first dimension from the following array initialization:
int arr[ ][3][4] = {
{
{2, 4, 6, 8},
{3, 5, 7, 9},
{5, 8, 11, 14}
},
{
{12, 14, 16, 18},
{13, 15, 17, 19},
{15, 18, 21, 24}
}
};
a. 1
b. 2
c. 3
d. 4

___________________________________________________________________________112
Programming Fundamentals - Lab [COSC-1201]

You might also like