CSC 101 Lecture Notes Week 4
Details of Program Input/Output;
Introduction to Looping
|
Data Type
of Variable | Value Input |
| char | A single character except a blank, tab, or newline |
| int | An integer literal constant, optionally signed |
| float | A floating point literal constant, in decimal or floating point notation, optionally signed |
has exactly the same effect ascout << "0
cout << endl;
Figure 1: Details of cin behavior.
1 ////
2 //
3 // This program computes simple statistics for five real numbers input from a
4 // data file named "stats_in.dat". The statistics computed are the sum of the
5 // numbers, the arithmetic mean, and the standard deviation. The results of
6 // the computation are output to a data file named "stats_out.dat".
7 //
8 // Author: Gene Fisher (gfisher@calpoly.edu)
9 // Created: 18apr99
10 // Last Modified: 18apr99
11 //
12 ////
13
14 #include <fstream.h> // File input library
15 #include <math.h>
16
17 const int NUM_DATA_POINTS = 5; // Fixed number of data points
18
19 int main () {
20
21 float i1, i2, i3, i4, i5; // Input variables
22 float sum; // Computed sum
23 float mean; // Computed mean
24 float std_dev; // Computed standard deviation
25 ifstream in_file; // File input stream
26 ofstream out_file; // File output stream
27
28 //
29 // Open the input and output files, named "stats_in.dat" and
30 // "stats_out.dat", respectively.
31 //
32 in_file.open("stats_in.dat");
33 out_file.open("stats_out.dat");
34
35 //
36 // Read in the five numbers from the input file.
37 //
38 in_file >> i1 >> i2 >> i3 >> i4 >> i5;
39
40 //
41 // Compute the sum.
42 //
43 sum = i1 + i2 + i3 + i4 + i5;
44
45 //
46 // Compute the mean.
47 //
48 mean = sum / NUM_DATA_POINTS;
49
50 //
51 // Compute the standard deviation.
52 //
53 std_dev = sqrt((pow(i1 - mean, 2) +
54 pow(i2 - mean, 2) +
55 pow(i3 - mean, 2) +
56 pow(i4 - mean, 2) +
57 pow(i5 - mean, 2)) / (NUM_DATA_POINTS - 1));
58
59 //
60 // Output the results to the output file.
61 //
62 out_file << "Sum = " << sum << ", "
63 << "Mean = " << mean << ", "
64 << "Standard deviation = " << std_dev
65 << endl;
66 }
#include <iostream.h>
main() {
int i, j, k; // Three input variables
i = 10; j = 20; k = 30;
cin >> i >> j >> k;
cout << "i: " << i << " j: " << j << " k: " << k << endl;
}
1234.56 7 89the program outputs
i: 1234 j: 20 k: 30
End of Topics Related to Program Input from Chapter 4;
Now on to Topics of Program Looping from Chapter 6.
////
//
// This program makes change, given an amount of purchase and an amount
// tendered. The output is a total amount of change, followed by change in
// five denominations of money: dollars, quarters, dimes, nickels, and pennies.
// All inputs and outputs are in integer cents.
//
// The program performs multiple change-making transactions by continuing to
// accept input until the user enters zero for the amount of purchase.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 21mar99
// Modified: 2apr99
//
////
#include <iostream.h>
int main() {
//
// Declare program variables to hold the purchase amount, amount tendered,
// total change amount, and the amounts of each denomination of change,
// from dollars to pennies.
//
int purchase; // Amount of purchase
int tendered; // Amount tendered
int change; // Total change due
int dollars; // Number of dollars in change
int quarters; // " " quarters " "
int dimes; // " " dimes " "
int nickels; // " " nickels " "
int pennies; // " " pennies " "
//
// Prompt for and input the amount of purchase, for the first transaction.
//
cout << "Input the amount of the purchase, in cents (enter 0 to stop): ";
cin >> purchase;
//
// Continue processing transactions until a purchase amount of 0 is
// entered.
//
while (purchase != 0) {
//
// Prompt for and input the amount tendered.
//
cout << "Input the amount tendered, in cents: ";
cin >> tendered;
cout << endl;
//
// Compute the total amount of change due, in cents. Assume that this
// value is non-negative.
//
change = tendered - purchase;
//
// Output the total change amount, followed by a blank line.
//
cout << "Total change due = " << change << endl
<< endl;
//
// Compute the number of dollars in change by dividing the total change
// by 100.
//
dollars = change / 100;
//
// Compute the remaining amount of change by subtracting the amount of
// change in dollars from the total change.
//
change = change - (dollars * 100);
//
// Proceed in the same way as for dollars with quarters, dimes,
// nickels, and pennies.
//
quarters = change / 25;
change = change - (quarters * 25);
dimes = change / 10;
change = change - (dimes * 10);
nickels = change / 5;
pennies = change - (nickels * 5);
//
// Output the results of the pieces of change computations.
//
cout << "Change in dollars through pennies is:" << endl
<< " " << dollars << " dollars" << endl
<< " " << quarters << " quarters" << endl
<< " " << dimes << " dimes" << endl
<< " " << nickels << " nickels" << endl
<< " " << pennies << " pennies" << endl << endl;
//
// Prompt for and input the amount of purchase for the next
// transaction.
//
cout << "Input the amount of the purchase, in cents (enter 0 to stop): ";
cin >> purchase;
}
return 0;
}
loop_counter = 1; //Initialization
while (loop_counter <= 10) { //Test
.
. //Repeated body
.
loop_counter = loop_counter + 1; //Test
}
can be used in place of (and means exactly the same thing as) the incrementing assignment statementloop_counter++
loop_counter = loop_counter + 1
////
//
// This program computes simple statistics for five real numbers entered from
// the terminal. The statistics computed are the sum of the numbers, the
// arithmetic mean, and the standard deviation.
//
// At the end of one computation, the program inputs a char sentinal value. If
// the sentinel is 'y' (for yes) the program continues execution, otherwise it
// stops.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 23apr99
// Last Modified: 23apr99
//
////
#include <iostream.h>
#include <math.h>
const int NUM_DATA_POINTS = 5; // Fixed number of data points
int main () {
float i1, i2, i3, i4, i5; // Input variables
float sum; // Computed sum
float mean; // Computed mean
float std_dev; // Computed standard deviation
char continue_loop = 'y'; // Loop sentinel
//
// Excute the loop while the sentinel says yes.
//
while (continue_loop == 'y') {
//
// Prompt and input the five numbers.
//
cout << "Enter five real numbers, separated by spaces: ";
cin >> i1 >> i2 >> i3 >> i4 >> i5;
//
// Compute the sum.
//
sum = i1 + i2 + i3 + i4 + i5;
//
// Compute the mean.
//
mean = sum / NUM_DATA_POINTS;
//
// Compute the standard deviation.
//
std_dev = sqrt((pow(i1 - mean, 2) +
pow(i2 - mean, 2) +
pow(i3 - mean, 2) +
pow(i4 - mean, 2) +
pow(i5 - mean, 2)) / (NUM_DATA_POINTS - 1));
//
// Output the results.
//
cout << endl
<< "Sum = " << sum << ", "
<< "Mean = " << mean << ", "
<< "Standard deviation = " << std_dev << ", "
<< endl << endl;
//
// Prompt for and input the sentinel value.
//
cout << "Continue? (enter y for yes, n for no): ";
cin >> continue_loop;
cout << endl;
}
}
#include <fstream.h>
int main() {
ifstream in_file; // Input file
int i; // Integer input from the file
//
// Open the file.
//
in_file.open("infile.dat");
//
// Read the first item, if any.
//
in_file >> i;
//
// Continue reading until end-of-file is reached.
//
while (in_file) {
//
// Echo the most recent input.
//
cout << i << endl;
//
// Input the next value.
//
in_file >> i;
}
}
////
//
// This program computes simple statistics for real numbers input from a data
// file named "stats_in.dat". The statistics computed are the number of data
// points, the sum of the numbers, and the arithmetic mean. The results of the
// computation are output to the terminal.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 23apr99
// Last Modified: 23apr99
//
////
#include <fstream.h> // File input library
#include <math.h>
int main () {
float i; // Input from file
int num_data_points; // Number of inputs read
float sum; // Computed sum
float mean; // Computed mean
float std_dev; // Computed standard deviation
ifstream in_file; // File input stream
ofstream out_file; // File output stream
//
// Open the input file.
//
in_file.open("stats_in.dat");
//
// Initialize the sum and number of data points to zero.
//
sum = 0;
num_data_points = 0;
//
// Read the first item, if any.
//
in_file >> i;
//
// Count and sum file data until end-of-file is reached.
//
while (in_file) {
//
// Increment the number of data points.
//
num_data_points = num_data_points + 1;
//
// Add in the input value to the running sum.
//
sum = sum + i;
//
// Input the next value.
//
in_file >> i;
}
//
// Compute the mean.
//
mean = sum / num_data_points;
//
// If any inputs were read, output the results.
//
if (num_data_points != 0) {
cout << "Number of data points = " << num_data_points << ", "
<< "Sum = " << sum << ", "
<< "Mean = " << mean << ", "
<< endl;
}
else {
cout << "The input file contained no data or was not found." << endl;
}
}