Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102890081
ch-01-ex-06-solution.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Feb 25, 06:05
Size
1 KB
Mime Type
text/x-c
Expires
Thu, Feb 27, 06:05 (1 d, 20 h)
Engine
blob
Format
Raw Data
Handle
24449886
Attached To
R1106 Programming Concept Rouaze
ch-01-ex-06-solution.cpp
View Options
/*
* exercise-06.cpp
*
* Record the number of cars passing past your house each day for five
* consecutive days and calculate the average.
*
* Created on: Jul 27, 2012
* Author: Radu Popescu <radu.popescu@epfl.ch>
*/
#include <iostream>
int main(int argc, char* argv[])
{
// Store the five values in an integer array
int number_of_cars[5];
// Read in the five values
std::cout << "Enter the number of cars on day one: ";
std::cin >> number_of_cars[0];
std::cout << "Enter the number of cars on day two: ";
std::cin >> number_of_cars[1];
std::cout << "Enter the number of cars on day three: ";
std::cin >> number_of_cars[2];
std::cout << "Enter the number of cars on day four: ";
std::cin >> number_of_cars[3];
std::cout << "Enter the number of cars on day five: ";
std::cin >> number_of_cars[4];
// Total number of cars that passed
int total_cars = number_of_cars[0] + number_of_cars[1] + number_of_cars[2]
+ number_of_cars[3] + number_of_cars[4];
/* To compute the average we need to convert the total number of cars to a
* double
*/
double average = ((double) total_cars) / 5;
/*
* NOTE: it is enough that one term of the division is a double. The
* following statement is equivalent to the previous:
* double average = total_cars / 5.0;
*/
std::cout << "The average number of cars is: " << average << std::endl;
return 0;
}
Event Timeline
Log In to Comment