0% found this document useful (0 votes)
85 views5 pages

PART A - Short Answers (5 Marks) : Identifier Missing (Exchange of Int and Const Is Allowed!)

The document contains questions about C++ programming concepts like loops, functions, arrays, and more. It asks the student to find errors, rewrite code using different structures, implement functions, and analyze output.

Uploaded by

Lily Rose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views5 pages

PART A - Short Answers (5 Marks) : Identifier Missing (Exchange of Int and Const Is Allowed!)

The document contains questions about C++ programming concepts like loops, functions, arrays, and more. It asks the student to find errors, rewrite code using different structures, implement functions, and analyze output.

Uploaded by

Lily Rose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PART A – Short answers

Question 1 [5 marks]

Find at least 5 errors in the following main() and correct them. You can add explanation on the right
int main () {
int const = 30; identifier missing (exchange of int and const is allowed!)
int x = 2.5; double assigned to an int (not a syntax error though)
while x < 10 { brackets missing around the condition
sum = sum + x ; sum not declared
if (sum = 0) ; 1) double equal (==) 2) no semicolon
cout << "zero sum";
else {
x = x+1;
Z = Z+2; Z not declared nor initialized
cout << x , Z; replace , by <<
}
} } missing

Question 2 [5 marks]

a) Rewrite the following for loop as a while loop.

for (int i=n; i>=1; i=i/2)


cout << i;

int i = n;
while (i >= 1){
cout << i;
i = i/2;
}

b) What is the output produced if n contains the value 15? Output: 15 7 3 1 (no spaces)
Question 3 [4 marks]

The following piece of code aims at depositing or withdrawing a maximum of $1000 into/from a
bank account. Rewrite it using a combination of switch and if statements.

if (transaction == 'D' && amount <= 1000)


balance = balance + amount;
if (transaction == 'D' && amount > 1000){
balance = balance + 1000;
cout << "cannot deposit more than $1000";
}
if (transaction == 'W' && amount <= 1000)
balance = balance - amount;
if (transaction == 'W' && amount > 1000){
balance = balance – 1000;
cout << "cannot withdraw more than $1000";
}

switch (transaction){ if (amount <= 1000)


case 'D': if (amount <= 1000) switch (transaction){
balance = balance + amount; case 'D' : balance = balance + amount;
else { break;
balance = balance + 1000; case 'W' : balance = balance - amount;
cout << "can’t dep > $1000"; break;
} }
break; else
case 'W': if (amount <= 1000) switch (transaction){
balance = balance - amount; case 'D' : balance = balance + 1000;
else { cout << "can’t dep > $1000";
balance = balance - 1000; break;
cout << "can’t withd >$1000"; case 'W' : balance = balance - 1000;
} cout << "can’t with > $1000";
break; break;
} }

Question 4 [3 marks]

In the code below, complete the statement in bold by using the substr() function to remove the
character at position i in the string word (Hint: concatenate what’s before position i and what’s after it )

string word = "programming";


int i; // i is a position between 0 and word.length()-1
cout << ”Enter a position between 0 and ” << word.length()-1;
cin >> i;
// your code to remove the character at position i
word = word.substr(0,i) + word.substr (i+1);
cout << word; // e.g. if i=3, the program will print “programming”
.
Question 5 [5 marks]

a) What is the output from the program?

int do_something (int x, int & y){ .


int sum = x+y;
x++;
y++;
return sum;
}

int main(){
int num1 = 5;
int num2 = 10;
int result = do_something (num1, num2);
cout << num1 << num2 << result; output: 5 11 15 (no spaces)
}

Question 7 [3 marks]

What is the output from the following program?

int main(){
int A[6] = {10, 20, 30, 40, 50, 60};
int B[6];

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


B[i] = A[i];

for (int i=0; i<6; i++)


cout << B[i] << “ “; output: 10 20 30 garbage garb garb
}
PART B – Programming

Question 8 [10 marks]

Write a main() that:

- Asks the user to enter 3 distinct numbers, and reads them.


- If the numbers are not distinct, the program should output an error message and stop*.
- If they are distinct, the program should:
 Find their sum and outputs it.
 Find the smallest of the 3 numbers and outputs it.
 Output the sum of the largest two.

* 2 bonus marks will be given if you use a do-while loop to ensure the numbers are distinct.

int main(){
int a, b, c;
do {
cout << "Please enter 3 distinct numbers ";
cin >> a >> b >> c;
} while (a==b || b==c | a==c);

int sum = a + b + c;
cout << "their sum is: " << sum << endl;

int smallest = a;
if (b < smallest)
smallest = b;
if (c < smallest)
smallest = c;

cout << "The smallest number is: " << smallest << endl;

cout << "the sum of the larger two is: " << sum - smallest;
}
Question 9 [12 marks]

a) Write a complete C++ function that takes a number n as a parameter. The function reads n
integers entered by the user, and calculates and returns the alternating sum of these integers.
For example, if n = 5, and the user enters 3,7,8,2,6, your function should compute + 3 – 7 +
8 – 2 + 6, and returns the sum 8.

b) Show how the function can be called from the main().

int alt_sum (int n){


int num;
int sum = 0;
int sign = 1;
for (int i=0; i<n; i++){
cin >> num;
sum = sum + sign*num;
sign = -sign;
}
return sum;
}

int main () {
cout << "The alternating sum of the numbers is: " << alt_sum(5);
}

or

for (int i=1; i<=n; i++){


cin >> num;
if (i%2 == 0)
sum = sum + num;
else sum = sum - num;
}

or

for (int i=1; i<=n; i++){


cin >> num;
if (i%2 != 0)
num = - num;
sum = sum + num;
}

Or

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


cin >> num;
sum = sum + num * pow(-1,i);
}

You might also like