You are here: Home » Content » Arrays

1. OBJECTIVE

The objective of Lab session 4 is to get familiar with arrays, one-dimensional and two-dimensional arrays.

2. EXPERIMENT

2.1 Test the following program.
#include <iostream.h>
const int NUM = 8;
void main()
{
int nums[NUM];
int total = 0; // Holds total of user’s eight numbers.
int ctr;
for (ctr=0; ctr<NUM; ctr++)
{
cout << “Please enter the next number...”;
cin >> nums[ctr];
total += nums[ctr];
}
cout << “The total of the numbers is “ << total << “\n”;
return;
}
2.2 If the array weights is declared as in the following statement, then what the value of weights[5] is ?
int weights[10] = {5, 2, 4};
2.3 Given the statement:
char teams[] = {‘E’,’a’,’g’,’l’,’e’,’s’,’\0', ’R’, ‘a’,’m’,’s’,’\0'};
which of the following statement is valid?
a. cout << teams;
b. cout << teams+7;
c. cout << (teams+3);
d. cout << teams[0];
e. cout << (teams+0)[0];
f. cout << (teams+5);
2.4 Given the array declaration:
int grades[3][5] = {80,90,96,73,65,67,90,68,92,84,70, 55,95,78,100};
Determine the value of the following subscripted variables:
a. grades[2][3]
b. grades[2][4]
c. grades[0][1]
2.5 Write a C++ program that inputs an array consisting of n single-precision floating point numbers and finds the smallest element in the array. (n is an integer that is entered by the user).
2.6 Write a C++ program that inputs an integer array and finds the last element in the array.
2.7 Write a C++ program that inputs an integer array and then inserts the integer value X in the first position in the array.
2.8 Write a C++ program that inputs an integer array and checks if all the elements in the array are unique (i.e. we can not find any pair of elements that are equal to each other).
2.9 Write a C++ program that inputs a matrix and then transposes it and displays the transposed matrix. Transposing a square matrix is to swap:
a(i,j) <==> a(j,i) for all i, j
For example, given the matrix
Figure 1
After transposing it, it becomes as follows:
Figure 2
2.10 Write a C++ program that inputs an integer n and two square matrices with order of n. Then the program calculates the multiplication of the two matrices and displays the resultant matrix.

Comments, questions, feedback, criticisms?

Send feedback