Exam Mid Programming
Exam Mid Programming
a) b)
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
for (int i = 10; i > 6; i = i - 2) int n=15;
cout << i; for( ; ; )
for (int i = -5; i > -7; i--) cout<<n;
cout << i + 1; return 0;
return 0; }
}
c) d)
#include <iostream> #include <iostream>
using namespace std;
using namespace std;
int main()
int main() {
{ int array1[] = { 100, 200, 300, 400, 100 };
int i; int array2[] = { 10, 20, 30, 40, 20 };
for(i=0 ;i<=6 ;i+=2 ) int temp, result = 0;
if(i%2==0) for (temp = 0; temp < 5; temp++) {
result += array1[temp];
cout<<i++<<"*"<<endl;
}
else
for (temp = 0; temp > 5; temp++)
cout<<++i<<"#"<<endl; {
cout<<i<<"@"<<endl; result += array2[temp];
return 0; }
} cout << result;
return 0;
}
0*
4#
6*
9@
P a g e | 1 of 4 Model 1
Q2:Each of the following definitions and program segments has errors.
Locate as many as you can. (4 Marks)
a) b)
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
for(int i=0,i<10,i++); int num1 = 0, num2 = 10, result;
cout <<i; num1++;
return 0; result = (num1 + num2)++;
} cout << num1 << " " << num2 << " " << result;
}
c) d)
#include <iostream> using namespace std;
using namespace std; int main ()
int main() {
{ double number1, number2, sum;
int num1, num2; Cout << "Enter a number: ";
char again; Cin << number1;
while (again == y | again = 'Y') Cout << "Enter another number: ";
cout << "Enter a number: "; Cin << number2;
cin >> num1; number1 + number2 = sum;
cout << "Enter another number: "; Cout 1 "The sum of the two numbers is " <<
cin >> num; sum
cout << "Their sum is 1 << (num1 + num2) << return 0;
endl; }
cout << "Do you want to do this again? ";
Answer: missing #include <iostream>
cin >> again;
And << }
return 0;
}
P a g e | 2 of 4 Model 1
Q3: Write a Complete C++ Program and Draw Flowchart for one of the following problems (10 Marks)
Note one mark for flowchart
a) The arrays Array1 and Array2 have 500 elements. Write a code to fill Array1 with data from user input
then copies the values in Array1 to Array2 then find and display the Maximum value stored in Array2.
(4 marks)
#include <iostream>
using namespace std;
int main()
{
int numberArray1[500],numberArray2[500], maximum;
for(int i=0; i <500; i++)
{
cout<<"Enter element #"<< i <<" : ";
cin>>numberArray1[i];
numberArray2[i]=numberArray1[i];
if(i == 0)
{
maximum = numberArray1[i];
}
else if(numberArray1[i] > maximum)
{
maximum = numberArray1[i];
}
}
cout<<"The max : "<< maximum;
return 0;
}
P a g e | 3 of 4 Model 1
Write a program that asks the user for a positive integer value. The program should use
a loop to get the sum of all the integers from 1 up to the number entered. For example,
if the user enters 100, the loop will find the sum of 1, 2, 3, 4, … 100.
Input Validation: Do not accept a negative starting number.(3 marks)
b)
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a positive integer value: ";
cin>>n;
if(n<=0){
cout<<"Not valid input";
}else{
int sum = 0;
for(int i=1; i<=n; i++){
sum+=i;
}
cout<<"The sum is "<<sum;
}
return 0;
}
c) Write a C++ program that calculates the volume of a sphere. User enters the data Volume of
sphere= 4/3 π r³(2 marks)
#include <iostream>
using namespace std;
int main()
{
int r;
cout<<"Enter a radius: ";
cin>>r;
P a g e | 4 of 4 Model 1
Q4:Answer the Following Questions ( 2 Marks ):
a) The following statement should assign -1 to X if z is less than 100, otherwise it should
assign 50 to X. What is wrong with it?
X = (z > 100) : 0 ? 50;
Answer: X = (z < 100) ? -1 : 50;
b) The following statement should determine if x is not greater than 20. What is wrong
with it?
if (!x > 20)
Answer:
if (!(x > 20)) or if (x <= 20)
P a g e | 5 of 4 Model 1