Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
25 views3 pages

CSC211 Test1

This document outlines an examination for the course 'Computer Programming I' at Covenant University, detailing the instructions, questions, and marking scheme. It includes questions on C++ programming concepts such as comments, expressions evaluation, age categorization, summation of squares using loops, and a power function implementation. The document also provides example solutions and explanations for each question.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

CSC211 Test1

This document outlines an examination for the course 'Computer Programming I' at Covenant University, detailing the instructions, questions, and marking scheme. It includes questions on C++ programming concepts such as comments, expressions evaluation, age categorization, summation of squares using loops, and a power function implementation. The document also provides example solutions and explanations for each question.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

COVENANT UNIVERSITY

CANAANLAND, KM 10, IDIROKO ROAD


P.M.B 1023, OTA, OGUN STATE, NIGERIA.
TITLE OF EXAMINATION: TEST 1
COLLEGE: College of Science and Technology
DEPARTMENT: Department of Computer and Information Sciences
SESSION: 2018/2019 SEMESTER: ALPHA
COURSE CODE: CSC 211 CREDIT UNIT: 3
COURSE TITLE: Computer Programming I
INSTRUCTION: Answer question ALL questions TIME: 1 HOUR

1. (a) Describe the two ways to include comments in a C++ program. (1 mark)
(b) Evaluate each of the following expressions, assuming in each case that m has the value 25
and n has the value 7:
i. m%n
ii. m%n++
iii. m%++n
iv. ++m - n-- (2 marks)
(c) What is wrong with this program:
#include <iostream>;
using namespace std;
int main
{ // prints "n = 22":
n = 22;
cout << "n = << n << endl;
} (2 marks)
2. Write a C++ program that reads the user’s age and then prints “You are a child.” if the
age < 18, “You are an adult.” if 18 ≤ age < 65, and “You are a senior citizen.” if age ≥ 65.
(3 marks)
3. Write a C++ program that uses a do ... while loop to compute and prints the sum of a given
number of squares. For example, if n = 5, then the program will print 55, which equals 12 + 22 +
32 + 42 + 52. NB: n should be received from the user. (3 marks)
4. Write and test the following power() function that returns x raised to the power n, where n
can be any integer:
double power(double x, int p); (4 marks)

1
Marking Scheme
1. (a) Describe the two ways to include comments in a C++ program. (1 mark – 0.5 mark each)

One way is to use the standard C style comment /* like this */


The other way is to use the standard C++ style comment // like this
The first begins with a slash-star and ends with a star-slash. The second begins with double
slash and ends at the end of the line.
(b) Evaluate each of the following expressions, assuming in each case that m has the value 25
and n has the value 7:
i. m%n
ii. m%n++
iii. m%++n
iv. ++m - n-- (2 marks – 0.5 mark each)

i. m%n evaluates to 25%7 = 4


ii. m%n++ evaluates to 25%(7++) = 25%7 = 4
iii. m%++n evaluates to 25%(++7) = 25%8 = 1
iv. ++m - n-- evaluates to (++25) - (7--) = 26 - 7 = 19
(c) What is wrong with this program:
#include <iostream>;
using namespace std;
int main
{ // prints "n = 22":
n = 22;
cout << "n = << n << endl;
} (2 marks – 0.5 mark each)
There are four errors: the pre-compiler directive on the first line should not end with a
semicolon, the parentheses are missing from main(), n is not declared, and the quotation
mark on the last line has no closing quotation mark.

2. Write a C++ program that reads the user’s age and then prints “You are a child.” if the
age < 18, “You are an adult.” if 18 ≤ age < 65, and “You are a senior citizen.” if age ≥ 65.
(3 marks)
#include <iostream>;
using namespace std;
int main()
{
int age;
cout << "Enter your age: ";
cin >> age;
if (age < 18)
cout << "You are a child.\n";
else if (age < 65)
cout << "You are an adult.\n";
else
cout << "you are a senior citizen.\n";
}

2
3. Write a C++ program that uses a do ... while loop to compute and prints the sum of a given
number of squares. For example, if n = 5, then the program will print 55, which equals 12 + 22 +
32 + 42 + 52. NB: n should be received from the user. (3 marks)

This program uses a do..while loop to compute the sum of the first n squares, where n is input:
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
int sum=0, i=1;
do
{
sum += i*i;
}
while (i++ < n);
cout << "The sum of the first " << n << " squares is "<< sum << endl;
}

4. Write and test the following power() function that returns x raised to the power n, where n
can be any integer:
double power(double x, int p); (4 marks)
This tests the power function:
double power(double, int);
int main()
{
cout << "Enter a positive float x and an integer n: ";
double x;
int n;
cin >> x >> n;
cout << "power(" << x << "," << n << ") = " << power(x, n) << endl;
}

double pow(double x, int n)


{
if (x == 0)
return 0;
if (n == 0)
return 1;
double y=1;
for (int i=0; i < n; i++)
y *= x;
for (int i=0; i > n; i--)
y /= x;
return y;
}

You might also like