Answer Exam 2019 Comp104
Answer Exam 2019 Comp104
int option;
cin >> option ;
if (option == 0){
cout << "Add process" << endl;
cout << "Mult process" << endl;
}
else if ( option == 2){
cout << "Add process" << endl;
cout << "Mult process" << endl;
}
else if ( option == 3){
cout << "Mult process" << endl;
}
else if ( option == 4 ){
cout << "Div Process" << endl;
cout << "Invalid option";
}
else
cout << "invalid option";
#include <iostream>
using namespace std;
int main() {
int Hours,Minutes,Seconds,X;
cout << "Enter Large number of Seconds : ";
cin >> X;
Hours = X / 3600;
Minutes = ( X % 3600 ) / 60;
Seconds = ( X % 3600 ) % 60;
cout << "(" << Hours << ":" << Minutes << ":" <<
Seconds << ")";
return 0;
}
Question two [ 22 Marks]:
a) [11 Marks] Trace the following C++ code and conclude the output:
First loop: K = 3, Fact = 3, Sum = 3
Second loop: K = 5, Fact = 15, Sum = 3
Third loop: K = 7, Fact = 105, Sum = 3
Fourth loop: K = 9, Fact = 945, Sum = 948
Fifth loop: K = 11, Fact = 10395, Sum = 948
OUTPUT:
K = 3 Fact = 3 Sum = 3
K = 9 Fact = 945 Sum = 948
b) [12 Marks] Write C++ program to get the sum of the following
series (using for-loop).
𝒔𝒖𝒎=√𝟏−√𝟐+√𝟑−√𝟒…+√(𝟐𝒏−𝟏)
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int N;
float Sum;
cout << "Enter the N-th term : ";
cin >> N;
for (int i=1; i <= (2*N) - 1;i++){
Sum += sqrt(i);
}
cout << "Sum of the series = " << Sum;
return 0;
}
Question Three [22 Marks]
Write a C++ program that reads an array of some integers (of length
n ≥ 10). Then
a) [10 Marks] Print only the sum of even integers and their mean.
b) [12 Marks] Find out the maximum and minimum of all integers in
the array and the difference between them.
#include <iostream>
using namespace std;
int main(){
int Max,Min,N,A[1000],Sum=0;
float average,Counter=0;
do {
cout << "Enter the Size of the Array : ";
cin >> N;
} while (N < 10);
for (int i=0; i < N; i++){
cout << "Enter the A[" << i << "] : ";
cin >> A[i];}
for (int i=0; i < N; i++){
if ( A[i] % 2 == 0 ){
Sum += A[i];
Counter++;}
}
average = Sum / Counter;
cout << "Sum of even integers = " << Sum << endl;
cout << "Mean (Average) of even integers = " <<
average << endl;
Max = A[0];
Min = A[0];
for (int i=0; i < N; i++){
if (A[i] > Max){
Max = A[i];
}
if (A[i] < Min){
Min = A[i];}
}
cout << "Max = " << Max << endl;
cout << "Min = " << Min << endl;
cout << "Diff between Max and Min = " << Max - Min;
return 0;
}
Question Four [23 Marks]
a) [11 Marks] Trace the following C++ code and conclude the output:
At i = 5, A[i] = 6
Sum = 0 + ( 6 + 2 * 5 ) = 16
At i = 4, A[i] = 3
Sum = 16 + ( 3 + 2 * 4 ) = 27
At i = 1, A[i] = 21
Sum = 27 + ( 21 + 2 * 1 ) = 50
At i = 0, A[i] = 9
Sum = 50 + ( 9 + 2 * 0 ) = 59
Output :
Sum = 59
b) [12 Marks] Write a C++ program that reads a two dimensional
array M of size (10 × 10). Then print out the sum of third column.
#include <iostream>
using namespace std;
int main(){
int A[10][10],Sum=0;
for (int i=0; i < 10; i++){
for (int j=0; j < 10; j++){
cout << "Enter A[" << i << "]" << "[" << j <<
"] : ";
cin >> A[i][j];
}
}
for (int i=0; i < 10; i++){
Sum += A[i][3];
}
cout << "Sum of the third column = " << Sum;
return 0;
}