Help with c++ max min sum

Project 1: Max, Min and Sum
In this task, use
"data.txt" as input file
"output.txt" as output file
Store a number integers (any number between 0 and 100) in the input file, write
to the
ouput file to show number of integers, the Max, the Min and their sum.
Sample input: ("data.txt")
3 5 2 6 4 1
Sample output: ("output.txt")
Number of integers: 6
Max: 6
Min: 1
Sum: 21

Solution of Project 1:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int x, n, sum, max, min;
ifstream infile("data.txt");
ofstream outfile(“output.txt”);
n = 0;
sum = 0;
max = 0;
min = 100;
while (infile>>x) {
n++;
sum = sum + x;
if ( x > max ) max = x;
if (x < min ) min = x;
}
outfile << " The maximum is : " << max << endl;
outfile << " The minimum is :" << min << endl;
outfile << "The number ofintegers in the file is: " << n << endl;
outfile << "The sum of integers in the file is: " << sum << endl;
system("pause");
return 0;
}

Hey guys im new to c++ and im very inexperienced.My questions are , what does >> or << mean ? what does (infile>>x) mean ? What does the x stands for in the program ? Also , for the "outfile << " The maximum is : " << max << endl;
outfile << " The minimum is :" << min << endl;
outfile << "The number ofintegers in the file is: " << n << endl;
outfile << "The sum of integers in the file is: " << sum << endl"
How would it look like when i compile and run it ? Additionally , why must we set the minimum to 100 ?
Thanks.
Last edited on
outfile << means out to the file just like cout << means out to the terminal. infile >> mean into the file like cin >> means into the variable.'x' looks like the incoming variable from the file in this case to me. The rest I don't know enough about yet to explain very well. Though you could always run the program to see how it looks in the end.
you wont SEE anything but the pause at the end which will be waiting for you to press enter so that the program will end. WHY- Because there is no output to the screen like what cout does. Here everything is written to the output file instead. You could always rewrite it with cout's instead of outfile's to see how your output file would look on the screen.

as far as max and min

your looking for the largest of a set of numbers - you dont know the largest number to start and you start by retrieving only one number. So you cant compare two numbers to find the largest if you only have one number at first - so you have to have a constant starting point - so if you make the constant starting point say 0 - then the first number you retrieve will then become your new highest or max

max's go up and min's go down

note - if the min started at 0 what number would you retrieve that would be lower - none

hope i helped
Topic archived. No new replies allowed.