marathon program

Write your question here.
im new to programming. my question is my milesavg output is not getting any number what so ever i put cout statment just to see if i was getting any values at all but i didn't get none at all and i keep getting a blank page when it comes to output the final data.
Jason 10 15 20 25 18 20 26
Samantha 15 18 29 16 26 20 23
Ravi 20 26 18 29 10 12 20
Sheila 17 20 15 26 18 25 12
Ankit 16 8 28 20 11 25 21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//function prototype and variables
const int numRows=5;
const int numColumns = 7;
void openFile(ifstream& , ofstream& );
void initialize(ifstream&, ofstream&, double[][numColumns], double[], string[]);
void milesAvg(ifstream& , ofstream&, double , double, string[]);
void print(ifstream& inFile, ofstream& outFile , double results[][numColumns],double average[], string names[]);
int main()
{
    //declaring variables
    ifstream inFile;
    ofstream outFile;
    string names[numRows];
    double results[numRows][numColumns];
    double average[numRows];
    //call function
    openFile(inFile,outFile);

    initialize(inFile,outFile,results,average,names);
    return 0;
}
//open file
void openFile(ifstream& inFile , ofstream& outFile)
{
    inFile.open("run.txt");
    outFile.open("newrun.txt");

}
void  initialize(ifstream& inFile , ofstream& outFile, double results[][numColumns], double average[numRows], string names[numRows])
{

    int a;
    int i;


    for(i=0; i<numRows; i++)
    {
        inFile >> names[i];
        for(a=0; a< numColumns; a++)

        {
            inFile >> results[i][a];
            cout << endl;
        }
    }

}
void milesAvg(ifstream& inFile , ofstream& outFile, double results[][numColumns],double average[numRows],string names[numRows])
{
    //declaring variables
    int i;
    int j;
    int sum;
    //find the averages
    for(i=0; i<numRows; i++)
    {
        sum=0;
        for(j=0; j<numColumns; j++)
            average[i] = average[i]+(results[i][j]/7);
          cout << i << endl;

    }
       print(inFile, outFile,results,average,names);
}
void print(ifstream& inFile, ofstream& outFile, double results[numRows][numColumns], double average[numRows], string names[numRows])
{
    //declaring variables
    int k;
    int l;
    //print name of runners
    for(k=0; l < numRows; l++ )
    {
        //write the names to outfile
        outFile << names[k] << endl;
    }
    //print results
    for(k=0; k < numRows; k++)
    {
        //write the results to outfile
        for(l=0; l<numColumns; k++)
            outFile << results[k][l];
             outFile << endl;
    }
    //print average
    for(k=0; k < numRows; k++)
        //write the average to outfile
        outFile << average[k];



    inFile.close();
    outFile.close();




}
Function milesavg() isn't ever called from anywhere.
is that why i'm getting a blank page when it come to output my data?
should i call my function in main?? im sorry im kinda new
when i put it in main my data is
"10152017162.85185e-3062.85733e-3063.10546e-3147.23134e-3087.23137e-3086.89023e-31303.02172e-3114."
is that why i'm getting a blank page when it come to output my data?

Not exactly. It simply means you never do output any data, since function print() was called from within milesavg().

when i put it in main my data is
"10152017162.85185e-3062.85733e-3063.10546e-3147.23134e-3087.23137e-3086.89023e-31303.02172e-3114."

At a glance those look like uninitialised floating-point numbers. Somewhere you are outputting data which has never had anything sensible stored in it.


I've had a glance through your code, I think you are passing too many parameters, function milesAvg() definitely does not need the inFile because you already read the input in function initialize(). I don't think it needs outFile either.

I'd say the structure should go something like this:
1. read_input
    uses inFile names and results

2. calculate_averages
    uses results and average

3. Output_results
    uses outFile, names, results and average

... all called from main()
i fixed it but i still getting those outrages numbers
there was something wrong with my print function when i fixed it the data becames this
Jason
Samantha
Ravi
Sheila
Ankit
10
15
20
17
16
5.56737e-307
5.57423e-307
3.10546e-314
7.23134e-308
7.23137e-308
6.89023e-313
0
3.02172e-311
4.09086e-321
5.00791e-312
8.48798e-314
5.09279e-312
2.122e-314
and goes on for
Well, I don't know if your code has changed. I actually made my own version of the entire program first, which generated this output:
Name        Day 1  Day 2  Day 3  Day 4  Day 5  Day 6  Day 7   Averages
Jason       10.00  15.00  20.00  25.00  18.00  20.00  26.00    19.14
Samantha    15.00  18.00  29.00  16.00  26.00  20.00  23.00    21.00
Ravi        20.00  26.00  18.00  29.00  10.00  12.00  20.00    19.29
Sheila      17.00  20.00  15.00  26.00  18.00  25.00  12.00    19.00
Ankit       16.00   8.00  28.00  20.00  11.00  25.00  21.00    18.43


Then I swapped out my output_results(), dropped in your print() function (which was disconcerting as it required the input file as a parameter - mine didn't). But still, the output it gave was very wrong. No names, just a lot of numbers with no spacing, all run together. It also crashed without ever completing properly.

However, if you'd like to post the latest version of your code I'll take another look.
Last edited on
@msf3456
Errors in your print function??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//print name of runners
    for(k=0; l < numRows; l++ )//Shouldn't this look like -> for(k=0; k < numRows; k++ )
    {
        //write the names to outfile
        outFile << names[k] << endl;
    }
    //print results
    for(k=0; k < numRows; k++)
    {
        //write the results to outfile
        for(l=0; l<numColumns; k++) // And this, look like ->  for(l=0; l<numColumns; l++)
            outFile << results[k][l];
             outFile << endl;
    }
@whitenite1 yes i fix that part now it look like this
Jason
Samantha
Ravi
Sheila
Ankit
10
15
20
25
18
20
26
15
18
29
16
26
20
23
20
26
18
29
10
12
20
17
20
15
26
18
25
12
16
8
28
20
11
25
21
19.1429
21
5.2208e+265
19
18.4286
here the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//function prototype and variables
const int numRows=5;
const int numColumns = 7;
void openFile(ifstream& , ofstream& );
void initialize(ifstream&,string[], double[][numColumns]);
void runnerAvg(double[][numColumns] , double[]);
void print(ofstream& outFile , string names[], double results[][numColumns],double average[]);
int main()
{
    //declaring variables
    ifstream inFile;
    ofstream outFile;
    string names[numRows];
    double results[numRows][numColumns];
    double average[numRows];
    //call function
    openFile(inFile,outFile);

    initialize(inFile,names, results);
    runnerAvg(results, average);
    print(outFile,names, results, average);


    return 0;
}
//open file
void openFile(ifstream& inFile , ofstream& outFile)
{
    inFile.open("run.txt");
    outFile.open("newrun.txt");

}
void  initialize(ifstream& inFile , string names[numRows], double results[][numColumns])
{

    int a;
    int i;


    for(i=0; i<numRows; i++)
    {
        inFile >> names[i];
        for(a=0; a< numColumns; a++)

        {
            inFile >> results[i][a];
            cout << endl;
        }
    }

}
void runnerAvg(double results[][numColumns],double average[numRows])
{
    //declaring variables
    int i;
    int j;
    int sum;
    //find the averages
    for(i=0; i<numRows; i++)
    {
        sum=0;
        for(j=0; j<numColumns; j++)
            average[i] = average[i]+(results[i][j]/7);


    }

}
void print( ofstream& outFile, string names[numRows], double results[][numColumns], double average[numRows])
{
    //declaring variables
    int k;
    int i;
    //print name of runners
    for(k=0; k < numRows; k++ )
    {
        //write the names to outfile
        outFile << names[k] << endl;

    }
    //print results
    for(k=0; k < numRows; k++)
    {
        //write the results to outfile
        for(i=0; i<numColumns; i++)
          outFile << results[k][i] << endl;



    }
    //print average
    for(k=0; k < numRows; k++)
        //write the average to outfile
        outFile << average[k] << endl;




    outFile.close();




}
In function print() you need to re-arrange the structure of the loops.

Think of it in terms of printing the data for one person at a time. You would need an outer loop which steps through each person in turn. Inside that, print the name, then a loop to print the 7-days figures, and last the average.
outterloop???
outterloop???

Have you come across the concept of nested loops? An example
1
2
3
4
5
6
7
8
for (int x=0; x<10; x++)
{
    for (int y=0; y<10; y++)
    {
        cout << x << ' ' << y << ' ';
    }
    cout << '\n';
}

Here the first loop using variable x is the outer one. The second, using variable y is the inner.

Last edited on
yes
Jason
10
15
20
25
18
20
26
19.1429 averages
Samantha
15
18
29
16
26
20
23
21 averages
Ravi
20
26
18
29
10
12
20
5.2208e+265 averages
Sheila
17
20
15
26
18
25
12
19 averages
Ankit
16
8
28
20
11
25
21
18.4286 averages
why is my ravi averages mess up and thank you so much for the help
here the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//function prototype and variables
const int numRows=5;
const int numColumns = 7;
void openFile(ifstream& , ofstream& );
void initialize(ifstream&,string[], double[][numColumns]);
void runnerAvg(double[][numColumns] , double[]);
void print(ofstream& outFile , string names[], double results[][numColumns],double average[]);
int main()
{
    //declaring variables
    ifstream inFile;
    ofstream outFile;
    string names[numRows];
    double results[numRows][numColumns];
    double average[numRows];
    //call function
    openFile(inFile,outFile);

    initialize(inFile,names, results);
    runnerAvg(results, average);
    print(outFile,names, results, average);


    return 0;
}
//open file
void openFile(ifstream& inFile , ofstream& outFile)
{
    inFile.open("run.txt");
    outFile.open("newrun.txt");

}
void  initialize(ifstream& inFile , string names[numRows], double results[][numColumns])
{

    int a;
    int i;


    for(i=0; i<numRows; i++)
    {
        inFile >> names[i];
        for(a=0; a< numColumns; a++)

        {
            inFile >> results[i][a];
            cout << endl;
        }
    }

}
void runnerAvg(double results[][numColumns],double average[numRows])
{
    //declaring variables
    int i;
    int j;
    int sum;
    //find the averages
    for(i=0; i<numRows; i++)
    {
        sum=0;
        for(j=0; j<numColumns; j++)
            average[i] = average[i]+(results[i][j]/7);


    }

}
void print( ofstream& outFile, string names[numRows], double results[][numColumns], double average[numRows])
{
    //declaring variables
    int k;
    int i;
    //print name of runners
    for(k=0; k < numRows; k++ )
    {
        //write the names to outfile
         outFile << names[k] << endl;
         for(i=0; i<numColumns; i++)
        {
            //write the results to outfile
            outFile << results[k][i] << "  " <<  endl;
        }

        //write the average to outfile
        outFile << average[k] <<" averages " << "   " <<endl;

    }









    outFile.close();




}




The way you calculate the averages seems a little non-intuitive to me.

First, the immediate cause of the error is that array defined at line 20
double average[numRows]; is uninitialized. It contains garbage, whatever happens to already reside in the block of memory where it is stored.
You can fix that by making sure all the values are set to zero at the start.
double average[numRows] = {}; // initialise the array to all zero

But still, the way I did it was to first calculate the total:
1
2
3
    double total = 0;
    for (int col=0; col<numColumns; col++)
        total += results[row][col];

and then assign total / numColumns to the average array.
Doing it that way, it doesn't matter at all what was already in the array.
By the way, it's not a good idea to hard code numbers such as a 7 in the program.
results[i][j]/7
Instead you should use the defined constant numColumns.

That way, if the value should change, say there were only 4 columns, you only have to change it in one place, rather than having to hunt through the code for every place where the number might be used.
thank you so much for the help
Topic archived. No new replies allowed.