C++ Accelerated Questions Chapter 2.
2-0 Compile and run the program presented in this chapter.
Done.
2-1 Change the framing program so that it writes its greeting with no separation from the frame.
Code:
#include<iostream>
#include<string>
using std::cin; using std::endl;
using std::cout; using std::string;
int main()
{
// Ask the name of the person.
cout << "Please enter your name here: ";
// Read the name.
string name;
cin >> name;
// Build the greeting.
const string greeting = "*Hello, " + name + "!*";
// The amount of stars to be printed above and beneath the greeting.
const string::size_type padding = greeting.size();
// Print the first line of stars.
for(int unsigned r = 0; r < padding; r++)
{
cout << "*";
}
// Print the greeting.
cout << endl << greeting << endl;
// Print the second line of stars.
for(int unsigned r = 0; r < padding; r++)
{
cout << "*";
}
return 0;
}
2-2 Change the framing program so that it uses a different amount of space to separate the sides from the greeting than it uses to separate the top and bottom borders from the greeting.
Code:
#include<iostream>
#include<string>
using std::cout; using std::cin;
using std::endl; using std::string;
int main()
{
// Ask the name of the person.
cout << "Please enter your name: ";
// Read the name.
string name;
cin >> name;
// Build the greeting.
const string greeting = "Hello, " + name + "!";
// Set the ammount of spacing on the middle line between the stars and the greeting.
const int spacer = 10;
// Set the amount of space for the padding.
const int pad = 2;
// The amount of stars to be printed above and beneath the greeting (cols) and tbe amount of rows.
const string::size_type cols = greeting.size() + spacer * 2 + 2;
const int rows = pad * 2 + 3;
// Write a blank line to seperate input from output.
cout << endl;
// Write rows of output.
for(int r = 0; r != rows; ++r)
{
string::size_type c = 0;
while(c != cols)
{
// Is it time to write the greeting?
if(r == pad + 1 && c == spacer +1)
{
cout << greeting;
c += greeting.size();
}
else
{
// Are we on the border?
if(r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
}
return 0;
}
2-3 Rewrite the framing program to ask the user to supply the amount of spacing to leave between the frame and the greeting.
Code:
#include<iostream>
#include<string>
using std::cout; using std::cin;
using std::endl; using std::string;
int main()
{
// Ask for the persons name.
cout << "Please enter your name: ";
// Get and store the name.
string name;
cin >> name;
// Ask for the amount of spacing between the greeting and the frame.
cout << endl << "Now enter the amount of spacing you want: ";
// Get and store the spacing.
unsigned int spacing;
cin >> spacing;
// Build the greeting.
const string greeting = "Hello, " + name + "!";
// The number of rows and columns to write.
const unsigned int rows = spacing * 2 + 3;
const string::size_type cols = greeting.size() + spacing * 2 + 2;
// Write a blank line to seperate input from output
cout << endl;
// Start writing the output.
for(unsigned int r = 0; r != rows; r++)
{
string::size_type c = 0;
while(c != cols)
{
// Is it time to write the greeting.
if(r == spacing + 1 && c == spacing + 1)
{
cout << greeting;
c += greeting.size();
}
else
{
if(r == 0 || r == rows -1 || c == 0 || c == cols -1)
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
}
return 0;
}
2-4 The framing program writes the mostly blank lines that separate the borders from the greeting one character at a time. Change the program so that it writes all the spaces needed in a single output expression.
I need some help with this one. If someone could give me a hint on how to do this it would be great.
2-5 Write a set of "*" characters so that they form a square, a rectangle and a triangle.
Note: There must be a more easy way to draw the triangle at least. Could someone give me a hint on that one as well?
The square:
Code:
#include<iostream>
#include<string>
using std::cout; using std::endl;
using std::string;
int main()
{
// The square.
const string::size_type x = 10; // Amount of stars.
for(unsigned int r = 0; r != x; r++)
{
cout << endl;
string::size_type c = 0;
while(c != x)
{
cout << "*";
++c;
}
}
return 0;
}
The rectangle:
Code:
#include<iostream>
#include<string>
using std::cout; using std::endl;
using std::string;
int main()
{
const string::size_type x = 30; // Lenght.
const string::size_type p = 5; // Height.
for(unsigned int y = 0; y != p; y++)
{
cout << endl;
for(unsigned int c = 0; c != x; c++)
{
cout << "*";
}
}
return 0;
}
The triangle:
Code:
#include<iostream>
#include<string>
using std::cout; using std::endl;
using std::string;
int main()
{
string::size_type c = 5;
if(c == 5)
{
cout << " *";
cout << endl;
c--;
}
if(c == 4)
{
cout << " ***";
cout << endl;
c--;
}
if(c == 3)
{
cout << " *****";
cout << endl;
c--;
}
if(c == 2)
{
cout << " *******";
cout << endl;
c--;
}
if(c == 1)
{
cout << " *********";
cout << endl;
c--;
}
return 0;
}
2-6 What does the following code do?
Code:
int i=0;
while(i < 10){
i += 1;
std::cout << i << std::endl;
}
It creates the variable "i" the variable is set to be an integer and the value is "0". It then looks if "i" is smaller then "10" and if so it adds "1" to the variable "i". After that it prints the value of "i" and goes back to the beginning of the "while" loop untill the returned value is false.
2-7 Write a program to count down from 10 to -5.
Code:
#include<iostream>
using std::cout;
using std::endl;
int main()
{
signed int x = -6;
signed int i = 10;
while(i != x)
{
cout << i << endl;
i--;
}
return 0;
}
2-8 Write a program to generate the product of the numbers in the range [1,10].
Code:
#include<iostream>
using std::cout; using std::string;
using std::endl;
int main()
{
for(unsigned int r = 1; r != 11; r++)
{
for(unsigned int x = 1; x != 11; x++)
{
unsigned int y = r * x;
cout << r << "*" << x << "=" << y << endl;
}
}
}
2-9 Write a program that asks the user to enter two numbers and tells the user which number us larger than the other.
Code:
#include<iostream>
#include<string>
using std::cout; using std::cin;
using std::endl; using std::string;
int main()
{
// Ask for the first number.
cout << "Enter the first number here: ";
string::size_type number1;
cin >> number1;
// Ask for the second number.
cout << "Please enter the second number here: ";
string::size_type number2;
cin >> number2;
if(number1 > number2)
cout << "Number 1 is bigger then number 2.";
else if(number1 == number2)
cout << "Number 1 and number 2 are the same.";
else
cout << "Number 2 is bigger then number 1.";
return 0;
}
2-10 Explain each of the uses of std:: in the following program.
Code:
int main(){
int k = 0;
while (k != n){ //invariant: we have written k asterisks so far
using std::cout;
cout << "*";
++k;
}
std::cout << std::endl; // std::is required here
return 0;
}
At first std:: is used to tell the program that the call to cout will be part of the standard library. In the second use it tells that the call to endl is part of the standard library.