Hw1

King Saud University
College of Engineering GE211 (programming in C++)
HW 1
===============================================================================
Q. 1
For the given matrix
h=
2
-4
-1
3
2
row 1
3
2
0
-2
6
row 2
4
6
0
-1
6
row 3
Write a C++ program with the following requirements (all matrix elements are integers)
1.
2.
3.
4.
5.
6.
Enter all values column by column from the keyboard.
Find the summation of the first row (by calling function named sum1).
Find the minimum number in the matrix (by calling function named min).
Find the summation of elements h[i][j] (for all j > = i) (by calling function named sumd).
Find the matrix transpose (ht).
Find the number of elements in the matrix that is greater than the summation of the first
row.
Q. 2
Given a one-dimensional array of integer numbers, write and set a function that prints the array elements
in reverse order.
Q. 3
Write a C program to evaluate a polynomial f(x) of degree n for a given value x = b.
Step 1: Declare and initialize an array a of n + 1 entries corresponding to the n + 1 coefficients of f(x);
a[0] = a0, a[1] = a1, .., a[n] = an.
Step 2: Accept a value b from the keyboard.
Note: The size of array, which is one plus the degree of the polynomial (a polynomial of degree n has n + 1 coefficients)
Q. 4
Write the output of the following segments of code.
1.
2.
int d = 28;
int c = 1;
while (d%2==0 || d > 1) {
while (c < 10) {
d = d / 2;
if (c%3 != 0)
cout<< d << "\n";
cout<< c << "x";
}
c++;
}
3.
4.
int c[4];
string
c[0] = 1;
w.erase (1,4);
for (inti=1; i<4; i++)
cout<< w;
w = " football";
c[i] = 2*c[i-1];
for (inti=0; i<4; i++)
cout<< c[i] << “ “;
5.
6.
int x = 19;
inti, a[4];
a[4] = {1, 2, 3, 4};
while (x > 2)
{
for (i = 0; i< 3; ++i)
x /= 2;
{
cout<< x;
a[i+1] += a[i];
if (x%2 == 0)
cout<< a[i] <<endl;
cout<< "\n";
}
}
cout<< a[3];
Q. 5
a) Reads quiz scores for each student into the two-dimensional array grade. Computes the
average score for each student and the average score for each quiz. Displays the quiz scores and
the averages.
The screenshot of the input and output data should look like the figure below.
b) Modify part (a) so that input data is read from an input file and it should include student’s first and
last names (i.e. student 1 name is Hamad Saleh, student 2 name is Ali Fahad , student 3 name is Ibrahim
Yousef and student4 name is Faisal Khalid)
Q6.
Write a function named "concatenate" that copies the cells of one array into a larger array, and then
copies the cells of another array into the larger array just beyond the contents of the first array. The
contents of the cells will be integers. The arguments will be as follows:
(1) the first array that will be copied;
(2) the number of cells that will be copied from the first array;
(3) the second array that will be copied;
(4) the number of cells that will be copied from the second array;
(5) the large array into which all copying will be performed;
(6) the number of cells available in the large array.
If the function discovers that the number of cells in the large array is not large enough to hold all the
numbers to be copied into it, then the function should return 0 to indicate failure. Otherwise, it should
return 1. The function should not alter the contents of the first two arrays. To take an example, if the
first two arrays passed to the function look like this:
58 | 26 | 91 | 34 | 70
34 | 88 | 29 | 41 | 10 | 66
Then, provided the size of the large array is at least 11, the large array should look like this when the
function returns:
58 | 26 | 91 | 34 | 70 | 34 | 88 | 29 | 41 | 10 | 66
Q7.
a. The following table shows the monthly sale of three different companies:
Company1
Company2
Company3
Jan
50000
64000
40000
Feb
63000
70000
66000
Mar
67000
68000
65000
Apr
52000
57000
73000
Write a program to do the following:
1. Store company sales in three 1-D arrays named sale1, sale2 and sale3. Data should
be entered from the keyboard.
2. Find the company that has the highest sale of any single month.
3. Find the company that has highest total sale.
4. Find the average sale of each month.
Q8.
Write a function named "enough" that takes one integer argument, call it "goal" and returns as its
value the smallest positive integer n for which 1+2+3+. . . +n is at least equal to goal . Thus, for
example,
cout<< enough(9) <<endl; // will print 4 because 1+2+3+4 9 but 1+2+3<9
cout<< enough(21) <<endl;// will print 6 'cause 1+2+ . . .+6 21 but 1+2+ . . . 5<21
cout<< enough(-7) <<endl;// will print 1 because 1 -7 and 1 is the smallest
// positive integer
cout<< enough(1) <<endl; // will print 1 because 1 1 and 1 is the smallest
// positive integer