Tutorial 6 Answers
1) Write a program that prints the sum of all odd numbers between 1 and n, where n is entered by the
user.
#include <iostream>
using namespace std;
int main() {
int n; //input from user
int sum = 0; //integer to track sum of odd numbers
cout << "Enter a number n: ";
cin >> n;
for (int i = 1; i <= n; i += 2) { // loop through the odd numbers only from 1 to n
sum += i;
}
cout << "The sum of all odd numbers between 1 and " << n << " is: " << sum << endl;
return 0;
}
2) “[Link]” is a file containing the recorded temperatures for all employees for that day with a
temperature of 0 C terminating the data. Write code that reads these values and only outputs those that
are higher than 35.5 C.
#include <iostream>
#include <fstream> //for reading files
using namespace std;
int main() {
ifstream inFile("[Link]"); //open the text file
if (!inFile) { //checking to see if the file exists
cout << "Error: Could not open file [Link]" << endl;
return 1;
}
double temp; //
cout << "Temperatures higher than 35.5 C:" << endl;
while (inFile >> temp) { //loop while there are variables in the file
if (temp == 0) {
break; // stop reading the file when 0 is found
}
if (temp > 35.5) { //output once file is more than 35.5
cout << temp << " C" << endl;
}
}
[Link](); //close file
return 0;
}
3) Write code that outputs the ID and score of students in a course in table format. The data is stored in
a file called “[Link]” and terminated by an ID of 00000.
#include <iostream>
#include <fstream>
#include <iomanip> // for table formatting
using namespace std;
int main() {
ifstream inFile("[Link]"); //open file for reading
if (!inFile) { //checking to see if file exists
cout << "Error: Could not open file [Link]" << endl;
return 1;
}
string id;
int score;
// Table header display
cout << left << setw(10) << "ID" << setw(10) << "Score" << endl; //left aligned and sets width of
columns to 10
cout << "----------------------" << endl;
// Read data from file and display in table
while (inFile >> id >> score) {
if (id == "00000") {
break; // stop if termination id (00000) is found
}
cout << left << setw(10) << id << setw(10) << score << endl;
}
[Link](); // close file
return 0;
}
EXAMPLE OF OUTPUT
ID Score
----------------------
12345 78
54321 90
57990 70
11223 81
4) A file called “[Link]” contain a list of integers with -1000 terminating the file. Write code to
output all the numbers in the file and the sum of all numbers in the file.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile("[Link]"); // Open the file to read
if (!inFile) { //check to make sure that file exists
cout << "Error: Could not open file [Link]" << endl;
return 1;
}
int num;
int sum = 0;
cout << "Numbers in the file:\n";
while (inFile >> num) { // Read each number in the file
if (num == -1000) { //checking for value to exit
break;
}
cout << num << " "; // output each number
sum += num; // add to sum
}
[Link](); //close file
cout << "\n\nSum of all numbers = " << sum << endl;
return 0;
}
5) A file “[Link]” contains the cost of an item and the shipping cost on each line. Data is
terminated in the file by -1. Write code to calculate the total cost of each item in the file where total
cost= cost of item + shipping cost. Find the grand total of all costs as well. This is the sum of all total
costs
#include <iostream>
#include <fstream> //to read file
#include <iomanip> // for table formatting
using namespace std;
int main() {
ifstream inFile("[Link]"); //open text file
if (!inFile) { //checking to make sure file can be opened
cout << "Error: Could not open [Link]" << endl;
return 1;
}
double itemCost, shippingCost;
double totalCost, grandTotal = 0;
//Table Header Formatting
//aligned left and setting column width to 15 spaces
cout << left << setw(15) << "Item Cost" << setw(15) << "Shipping" << setw(15) << "Total Cost"
<< endl;
cout << "-----------------------------------------" << endl;
while (inFile >> itemCost >> shippingCost) { // read file until -1 is found
if (itemCost == -1) {
break;
}
totalCost = itemCost + shippingCost;
grandTotal += totalCost;
cout << left << setw(15) << itemCost<< setw(15) << shippingCost << setw(15) << totalCost <<
endl; //output in table format
}
[Link](); //closing file
cout << "\nGrand Total of all items: $" << grandTotal << endl;
return 0;
}
EXAMPLE OF OUTPUT
Item Cost Shipping Total Cost
-----------------------------------------
30 10 40
80 15 95
Grand Total of all items: $135.00