Exercise 1.3: Write a program to print "Hello, World" on the standard output.
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
} Exercise 1.4: Our program used the built-in addition operator, +, to generate the sum of two numbers. Write a program that uses the multiplication operator, *, to
generate the product of two numbers.
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
std::cout << "The product of " << v1 << " and " << v2
<< " 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, v2;
std::cin >> v1 >> v2;
std::cout << "The sum of ";
std::cout << v1 ;
std::cout << " and ";
std::cout << v2;
std::cout << " is ";
std::cout << v1 + v2;
std::cout << std::endl;
return 0;
} Exercise 1.6: Explain what the following program fragment does:
1std::cout << "The sum of " << v1;
2 << " and " << v2;
3 << " is " << v1 + v2
4 << std::endl; Is this code legal? If so, why? If not, why not?
This code is illegal, because semicolon mark the end of the statement. That means that the statement from line 2 to line 4 each belongs to a separate statements and the operator << in the lines (2 to 4) miss the left-operand which should be an object of type ostream.
Exercise 1.9: What does the following for loop do? What is the final value of sum?
int sum = 0;
for(int i = -100; i <= 100; ++i)
sum += i; The for loop first initialize i with -100, then evaluate i <= 100, then execute the body statement. The body statement add i to sum and assign the result to sum, then i is increment 1 and again evaluate the condition in the for loop header, until i equal to 101. The final value is 0, because the above the statement calculates the sum the numbers from -100 to 100.
Exercise 1.10: Write a program that uses a while loop to print the numbers from 50 to 100. Now rewrite the program using a while.
for:
int main()
{
int sum = 0;
for(int i = 50; i <= 100; ++i)
sum += i;
return 0;
} while:
int main()
{
int sum = 0;
int i = 50; // init-statement
while(i <= 100) // condition
{
sum += i;
++i; // expression
}
return 0;
} Exercise 1.11: Write a program using a while loop to print the numbers from 10 down to 0. Now rewrite the program using a for.
while:
#include <iostream>
int main()
{
int i = 10;
while(i >= 0)
{
std::cout << i << std::endl;
--i;
}
return 0;
} for:
#include <iostream>
int main()
{
for(int i = 10; i >= 0; --i)
std::cout << i << std::endl;
return 0;
} Exercise 1.14: What happens in the program presented in this section if the input values are equal?
The body statement would execute only once, which means that the value of sum is equal to the input value.
Exercise 1.16: Write a program to print the larger of two inputs supplied by the user.
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
int smaller, larger;
if(v1 <= v2)
{
smaller = v1;
larger = v2;
}
else
{
smaller = v2;
larger = v1;
}
std::cout << "The larger of " << v1
<< " and " << v2
<< " is " << larger
<< std::endl;
return 0;
} Exercise 1.17: Write a program to ask the user to enter a series of numbers. Print a message saying how many of the numbers are negative numbers.
#include <iostream>
int main()
{
int i = 0, value;
while(std::cin >> value)
{
if(value < 0)
++i;
}
std::cout << "The numbers of negative numbers is " << i << std::endl;
return 0;
} Exercise 1.18: Write a program that prompts the user for two numbers and writes each number in the range specified by the two numbers to the standard output.
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
int lower, upper;
if(v1 <= v2)
{
lower = v1;
upper = v2;
}
else
{
lower = v2;
upper = v1;
}
for(int i = lower; i <= upper; ++i)
std::cout << i << " ";
return 0;
} Exercise 1.19: What happens if you give the numbers 1000 and 2000 to the program written for the previous exercise? Revise the program so that it never prints more than 10 numbers per line.
The output list the number per line from 1000 to 2000 which needs to scroll through more than a page.
Revise version:
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
int lower, upper;
if(v1 <= v2)
{
lower = v1;
upper = v2;
}
else
{
lower = v2;
upper = v1;
}
for(int i = lower; i <= upper; ++i)
{
std::cout << i << " ";
/**//**//**//* Here I use operator %, which take the mod of i and 10,
* then the result was being complement
* (using operator !, non-zero to 0 and 0 to 1).
*/
if(!(i % 10))
std::cout << std::endl;
}
return 0;
} Exercise 1.21: The Web site (
http://www.awprofessional.com/cpp_primer) contains a copy of Sales_item.h in the Chapter 1 code directory. Copy that file to your working directory. Write a program that loops through a set of book sales transactions, reading each transaction and writing that transaction to the standard output.
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item book;
while(std::cin >> book)
std::cout << " Output book: " << book << std::endl;
return 0;
} Exercise 1.22: Write a program that reads two Sales_item objects that have the same ISBN and produces their sum.
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2;
if(item1.same_isbn(item2))
{
std::cout << item1 + item2 << std::endl;
}
else
{
std::cout << "Data must refer to same ISBN" << std::endl;
}
return 0;
} Exercise 1.23: Write a program that reads several transactionss for the same ISBN. Write the sum of all the transactions that were read.
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1, item2, sum;
std::cin >> item1;
while(std::cin >> item2)
{
if(item1.same_isbn(item2))
item1 += item2;
else
{
std::cout << "All data must refer to same ISBN" << std::endl;
return -1;
}
}
std::cout << "The sum of all the transactions is " << item1 << std::endl;
return 0;
} Exercise 1.24: Write a program that reads several transactions. For each new transaction that you read, determine if it is the same ISBN as the previous transaction, keeping a count of how many transactions there are for each ISBN. Test the program by giving multiple transactions. These transactions should represent multiple ISBNs but the records for each ISBN should be grouped together.
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total, trans;
int i = 0;
if(std::cin >> total)
{
while(std::cin >> trans)
if(total.same_isbn(trans))
{
total = total + trans;
++i;
}
else
{
std::cout << i << std::endl;
total = trans;
i = 0;
}
std::cout << i << std::endl;
}
else
{
std::cout << "No data?!" << std::endl;
return -1;
}
return 0;
} Exercise 1.26: In the bookstore we used the addition operator and not the compound assignment operator to add trans to total. Why didn't use the compound assignment operator?
In the former section, it describes that we only know the operations in Page 21 to 22 that the class provides and there are no operator += listed, so we can't use operator += to perform the operation.