Exercises Section 1
Exercise 1.1: Review the documentation for your compiler and determine what file naming convention it uses. Compile and run the main program from page2.
Exercise 1.2: Change the program to return -1. A return value of -1 is often treated as an indicator that the program failed. Recompile and rerun your program to see how your system treats a failure indicator from main.
Exercise1.3 Write a program to print Hello, World on the standard output.
#include <iostream>
int main()
{
std::cout << "Hello, World" << std::endl;
return 0;
}
Exercise1.4 Our program used the addition operator, +, to add two numbers. Write a program that uses the multiplication operator, *, to print the product instead.
#include <iostream>
int main()
{
std::cout << "Enter two numbers: " << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The product is " << v1 * v2 << std::endl;
return 0;
}
Exercise 1.5 We wrote the output in one large statement. Rewrite the program to use a separate statement to print each operand.
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The product of ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << "is " << v1 * v2 << std::endl;
return 0;
}
Exercise 1.6 Explain whether the following program fragment is legal
std::cout << "The sum of " << v1;
<< " and ";
<< " is " << v1 + v2 << std::endl;
If the program is legal, what does it do? If the program is not legal, why not? How would you fix it?
- Error: expected primay-expression befor '<<' token
- Fixed if: remove the spare semicolons;
std::cout << "The sum of "
<< v1 << " and " << v2 << " is "
<< v1 + v2 << std::endl;
Exercise 1.7 Compile a program that has incorretly nested comments.
/*
* comment pairs /* */ cannot nest.
* ''cannot nest'' is considered source code,
* as is the rest of the program
*/
int main()
{
return 0;
}
Exercise 1.8 Indicate which, if any, of the following output statements are legal:
std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */;
std::cout << /* "*/" /* "/*" */;
After you've predicated what will happen, test your answers by compiling a program with each of these statements. Correct any errors you encounter.
- ...
Exercise 1.9 Write a program that uses a while to sum the numbers from 50 to 100.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
auto sum(int lo, int hi)
{
int sum = 0;
while(lo < hi)
sum += lo++;
return sum;
}
int main()
{
cout << "sum is: "
<< sum(50, 100+1) << endl;
return 0;
}
Exercise 1.10 In addition to the ++ operator that adds 1 to its operand, there is a decrement operator(--) that subtracts 1. Use the decrement operator to write a while that prints the numbers from ten down to zero.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int i = 10;
while(i >= 0)
std::out << i-- << " ";
return 0;
}
Exercise 1.11 Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void print_range(int lo, int hi)
{
if (lo > hi)
{
print_range(hi, lo);
}
for (int i = lo; i != hi; ++i)
cout << i << " ";
}
int main()
{
int low = 0, high = 0;
cout << "Please input two integers:\n";
cin >> low >> high;
print_range(low, high);
return 0;
}