BACS1013 & BACS1014 Problem Solving and Programming Tutorial 2
Tutorial 2 - Input Output
1. Write the output for each of the following segments of code.
(a) int x = 7;
int y = 3;
cout << x/y << " and " << x%y;
(b) double b = 5;
cout << b/2 << “ ” <<(int) b%2;
(c) int x = 2;
double y = 3.0;
cout << setprecision(2);
cout << x * y << " and " << x / y;
(d) double a = 5.5;
cout << (int) a / 2 << "\n";
cout << setprecision(2) << a;
2. Briefly explain and correct the error(s) in each of the code segments below.
(a) int a = 5;
int double = 2*a;
int triple = 3*a;
cout << "Doubling 5 gives " << double << " and tripling gives "
<< triple;
(b) int x, y;
cin << x << y;
cout << "x = " << x << "\ny = " << y;
3. Suppose x, y, and z are int variables and x = 2, y = 5, and z = 6. What is the output
of each of the following statements?
(a) cout<<"x = "<<x<<", y = "<<y<<", z = "<<z<<endl;
(b) cout<<"x + y = "<<x + y<<endl;
(c) cout<<"Sum of "<<x<<" and "<<z<<" is "<<x+z<<endl;
(d) cout<<"z / y = "<<z / y<<endl;
(e) cout<<"2 times "<<x<<" = "<<2*x<<endl;
4. Write a single cout statement that prints the text below.
Krodo says:
"I YOU!"
(Hint: The heart is given by the escape character \3.)
Page 1 of 4
BACS1013 & BACS1014 Problem Solving and Programming Tutorial 2
5. The program below is supposed to compute the average of two numbers. The program compiles
without error, but does not correctly compute the average. Describe the logic error(s) and write
the corrected lines. You do not need to rewrite the entire program. The line numbers are written
along the left column to help you write your answer; they are not part of the program code. Make
as few changes as possible.
1 #include <iostream>
2 using namespace std;
3 int main ( )
4 {
5 int total;
6 cout << "Enter a number: ";
7 int a;
8 cin >> a;
9 total = total + a;
10 cout << "Enter another number: ";
11 int b;
12 cin >> b;
13 total = total + a;
14 double average = total / 2;
15 cout << "The average is " << average << ".\n";
16 return 0;
17 }
6. Write the output for the following program statements (which are not related to one another)
(Note: Show all blank spaces with the symbol ):
(a) cout<<setw(10)<<”ABC”;
(b) cout<<setfill(‘#’)<<setw(10)<<”ABC”;
(c) cout<< left << setfill(‘@’) << setw(6) << “ABC”;
(d) cout<<fixed<<setprecision(2)<<123.456;
(e) cout<<setprecision(7)<<showpoint<<123.45;
(f) cout<<setprecision(5)<<123.5748<<endl<<setprecision(6)<<
123.5748;
7. Draw a flowchart and write a C++ program that will read 4 integers through a single cin function.
Then, calculate and display the sum (add up all the integers) and product (multiply all the integers)
of these values. Set the field width of the output values to be 10. The following is the example
output:
Output:
Enter 4 integers > 10 20 30 40
Sum of all : 100
Product of all : 240000
Page 2 of 4
BACS1013 & BACS1014 Problem Solving and Programming Tutorial 2
8. Nicholas wrote a program that computes the average of two integers entered by the user. The
program compiles, but doesn't seem to produce the correct results. The part of Nicholas’ code
that computes the average is shown below.
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "The average is " << (num1+num2)/2;
Explain what is wrong with Nicholas’ code and suggest how to fix it. Demonstrate your solution.
9. What are the outputs of this simple program? Test with a simple one-word name and one-word
city name. Then, test with your own full name and full city name.
string name;
string city;
cout << “Please enter your name : ”;
cin >> name;
cout << “Enter the city you live in : ”;
cin >> city;
cout << “Hello, “ << name << endl;
cout << “You live in “ << city << endl;
As you will observe, the output is incorrect when we type in our full name. How would you rectify
this with the proper suitable C++ command?
10. For the code snippet below, fill in the blank lines numbered 1 to 3 with the suitable C++ commands.
[Hint: Use commands that will capture the Enter key when pressed]
char ch;
cout << “This program has paused. Press Enter to continue.”;
__1___ ;
cout << “It has paused a second time. Please press Enter again.”;
__2___ ;
cout << “It has paused a third time. Please press Enter again.”;
__3___ ;
cout << “Thank you!”;
Page 3 of 4
BACS1013 & BACS1014 Problem Solving and Programming Tutorial 2
11. What is wrong with the following code snippet below?
Line 1 char ch
2 int number
3 cout << “Enter a number : ”
4 cin >> number
5 cout << “Enter a character : ”
6 ch = cin.get( )
7 cout << “Thank you! \n ”
Suggest how you would remedy the error. [Hint: It’s time to ignore an input]
Page 4 of 4