HElp with for loop

Here is the question and answer-

Task: Soccer Games results ( Version 1 )
There are n number of soccer matches ( n < 30 ) being played,
the results of the matches were entered in a file "input1.txt"
which has the format as described below:
The 1st line is an integer for number of matches follow by
n pairs of results showing the goals of both teams.
Sample input:
10
2 1
1 1
1 3
2 2
4 2
0 0
2 1
8 1
0 4
2 1
The maximum goals difference in this case is in the match
no. 8, and the difference is 7. The number of drawn matches
is 3.
Write a program which will process the input file "input1.txt"
and write to the ouput file "output1.txt" the maximum goal diffrence
and the number of drawn matches as follow:
Sample output:
The maximum goals difference: 7
Number of drawn matches: 3
Solution of assignment 2:
#include <fstream>
using namespace std;
int main() {
int n, max, ndrawn, g1, g2, diff, i;
ifstream infile("input1.txt");
ofstream outfile("output1.txt");
infile >> n;
max = 0;
ndrawn = 0;for ( i=1; i<=n; i++) {
infile >> g1 >> g2;
diff = abs(g1 - g2);
if (diff>max) max = diff;
if (diff==0) ndrawn++;
}
outfile << "The maximum goals difference: " << max << endl;
outfile << "Number of drawn matches: " << ndrawn << endl;
infile.close();
outfile.close();
system("pause");
return 0;
}
Project 2:
Task: Numbers game
Two players A and B have

My questions- why is the answer infile >> n and NOT infile >> i?
Secondly,what does (i=1; i<=n; i++) means?
Thirdly , what does abs in "abs(g1-g2) mean?
Fourthly, why are there 2 equal signs in "(diff==0)?
Lastly, what does infile.close and outfile.close mean? Is it neccessary
Last edited on
Ask the question who wrote the program.
Have a good read of this:

http://www.cplusplus.com/doc/tutorial/


Also note the reference section at the top left of this page, and the Articles section.

Google & wiki are always very helpful too.

Hope all goes well.
1. It says the first line is showing the number of matches, that's where n comes from. After that, it is displaying the scores. It is just a variable name, so don't need to worry too much about why it is, just how it was designated (i is really common for iterations in for loops).

2. Those are the conditions of the for loop, it means set i=1 initially, and while i does not exceed n, add 1 to i for each time the loop is completed. Normally, you would use
 
for (i=0; i<n; i++)

but when it comes to human reading, it's more natural to read it as match 1,2,3.... rather than 0,1,2.....

3. abs means absolute, if the answer inside the brackets was positive, it would do nothing, but it it was negative, it would turn it into a positive number.

4. == is a comparison operator in most languages (a single = being an assignment). It just means to make sure the values are the same.

5. It means save and close the file, it might not be strictly necessary (I believe it always saves anyway), but it's good practice to close files when you're done using them.
wait, so i stands for loop? is it standard or can it be any integer like a, b, c , etc ?
It can be any Integer, but i is the most commonly used.
you can also write loopInteger, loop1, or gibberish like asfha.
Really eveyrthing that is a integer does this job.

you can even declare variables in the loop for its purpose like:
for ( int forInteger = 0; i != n; i++)

And additionally to 4.
I don't know if you HAVE to close them at the end, but its better to do so.
Also you usually close a file when done editing because you might have 2 Functions trying to read the file and the program will give out errors since you would try to open a already open file.
Last edited on
Topic archived. No new replies allowed.