Professional Documents
Culture Documents
College of Engineering
Dept. of Computer Science & Engineering
Computer Programming
GENG106
Lab Handbook
Fall 2012 Semester
Lab 9: Arrays
Objectives
At the end of this lab, you should be able to
Use arrays in C++.
Pass arrays to functions.
Use arrays with files.
Use 2-dimensional arrays.
Quick Review
An array is a group of consecutive memory locations that are related by the fact that they all have
the same name and same type, e.g. double stress[3]; declares a 1-dimensional array of size
3, i.e. the number of elements is 3.
A subscript (sometimes called index) is used to refer to a particular location within the array, e.g.
stress[0], stress[1], etc.
To pass an array to a function, the name of the array is passed, e.g. x=max(stress);
By default arrays are passed to functions using pass-by-reference, e.g. sort(stress);
A 2-dimensional array represents a table of values consisting of information arranged in rows
and columns, e.g. double nodeStress[100][3];
To access a particular element, two subscripts are specified. The first subscript references the
row and the second references the column, e.g. nodeStress[53][1].
a)
b)
c)
d)
e)
f)
3) Write a C++ program that reads 10 real numbers into an array called distance, and then
calculates and prints the sum of positive numbers sumPositive, and the sum of negative numbers
sumNegative.
Index number
4.2
-6.3
9.4
-12
-4.1
-2
9.2
Sample output
and b
Sample output
b) The norm of a vector x, written as x, is a measure of its size (a scalar quantity). One of the
simple vector norms is the Euclidean norm, defined as x =
.
Add to your program of Question (4a) a function called norm2() that takes as parameters an
integer array, x, and its size, n. Let the function return the double value representing the
Euclidean norm of x.
Add to your program statements to print the norms of the vectors
Sample output
and
given above.
5) a) Write a C++ program that initializes an array called diameters with the following numbers,
and then stores the numbers into a file named result.dat:
16.25, 18.96, 22.34, 18.94, 17.42, 22.63.
b) Write another C++ program to read the numbers in the result.dat file created in Question-5a
and display the data. Additionally, your program should compute and display the sum and average
of the data.
NOTE: Review the use of files in Lab5 Exercises 4 through 8.
6) The following program initializes the 2-dimensional array marks[][]that represents a table of
marks of five students in three subjects: math, computer and English. Compile and run the program.
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
double marks[][3]={
{89,88,90},
{80,85,79},
{96,92,95},
{68,73,55},
{45,60,58}
};
cout<<"Stud# Math Computer English\n";
for(int i=0;i<5;i++)
{
cout<<setw(3)<<i+1;
for(int j=0;j<3;j++)
cout<<setw(8)<<fixed<<setprecision(1)<<marks[i][j];
cout<<endl;
}
return 0;
}
The following function returns the average of the elements of a double array x of size n:
double average(double x[], int n)
{
double sum=0;
for(int i=0;i<n;i++)
sum+=x[i];
return sum/n;
}
Add the above function to your program and then use it to output the average mark for each
student as shown in the typical output below. (Hint: the ith row of our array is marks[i]; a 1dimensional array of size 3).
3
Typical Output:
The following function returns the letter grade corresponding to a given mark:
char grade(double mark)
{
if(mark>=90)
return 'A';
else if (mark>=80)
return 'B';
else if (mark>=70)
return 'C';
else if (mark>=60)
return 'D';
else
return 'F';
}
Add the above function to your program and then use it to produce a table of the student
numbers and the corresponding letter grade for each of the three subjects, as well as for the
average grade.
Typical Output:
7) The norm of a
return sqrt(sum);
}
True or false: when we pass a 2-dimensional array to a function, the number of columns
should be constant.
Use the function above, to output the Frobenius norm of the marks array of Question (5).
Ans=303.814
8) Transposing a matrix is swapping rows and columns, for example the matrix
is transposed into
Write C++ program that reads a 2-dimensional array (3 rows, 3 columns), and then transposes this
array. Then display the array after transposing.
I/O Sample (user input is marked in red):
Enter row-1: 1 2 4
Enter row-2: 3 6 7
Enter row-3: 9 5 8
The transpose of the above matrix is:
1 3 9
2 6 5
4 7 8
Press any key to continue . . .
9) Specify whether each of the following C++ array declarations is correct () or wrong ():
____ int a[];
____
int a[3];
____
int a[5]={3,4};
____
int a[3]={0};