Battling Average

Pages: 12
Im trying to fix my code and get the result as give but it wont give the same result. and the output should be two as shown below in output section. i fixed what look wrong to be but couldnt find or fix the problem
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	int atBat[10], hitS[10], baseS[10];
	int data, k = 0;
    int averageB, averageS;

	ifstream Data("Data.txt");   // this define the "Data" file object and opens Data.txt input file
	do{
		Data >> data; // get input from Data file object
		if (data != -1)
			k = data;
		atBat[k] = atBat[k] + 1;
		Data >> data;
		if (Data == 0)
			hitS[k] = hitS[k] + 1;
		baseS[k] = baseS[k] + data;
		Data >> data;  
	}while(data == -1);
	for (int n = 1; n <= 9; ++n){
		averageB = hitS[k] / atBat[k];
		averageS = baseS[k] / atBat[k];
		do{
			cout <<"The batting average for each position was: " << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout << endl;
			cout <<"The slugging percentage for each position was; "<< endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
		}while (n == 9);
	}
	system("PAUSE");
	return 0;
}


here is the Data.txt file
1
2
3
4
5
6
7
8
9
10
1 4 7 
2 1 2 
4 7 2 
8 4 4 
6 4 6 
8 1 2 
9 10 11 
4 10 7
5 8 3
9 5 4


and here is the output file that the result should look like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
The batting average for each position was: 
Position1 1 batting average is 0.215451415
Position1 2 batting average is 0.531431543
Position1 3 batting average is 0.235616616
Position1 4 batting average is 0.151757577
Position1 5 batting average is 0.617746714
Position1 6 batting average is 0.076786808
Position1 7 batting average is 0.086868678
Position1 8 batting average is 0.826828266
Position1 9 batting average is 0.056505680

The slugging percentage for each position was
Position1 1 batting average is 0.168989898
Position1 2 batting average is 0.215462868
Position1 3 batting average is 0.250606868
Position1 4 batting average is 0.686086868
Position1 5 batting average is 0.897909229
Position1 6 batting average is 0.056562868
Position1 7 batting average is 0.052727872
Position1 8 batting average is 0.728298928
Position1 9 batting average is 0.038628878
Last edited on
Your code I honestly have no idea what you were trying to do. Especially here:
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
	for (int n = 1; n <= 9; ++n){
		averageB = hitS[k] / atBat[k];
		averageS = baseS[k] / atBat[k];
		do{
			cout <<"The batting average for each position was: " << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout <<"Position " << k << " batting average is " << averageB << endl;
			cout << endl;
			cout <<"The slugging percentage for each position was; "<< endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
			cout <<"Position " << k << " batting average is " << averageS << endl;
		}while (n == 9);
	}


If you could explain how you got your batting averages from the data file it would be easier to help you. I can't seem to figure out where you got those numbers from I figure they are hits, misses, and bases but I don't know which order would make sense.


Anyways here would be the easiest way
1
2
3
4
5
6
7
8
std::ifstream in("data.txt");
int hits, misses, bases;
int pos = 0;
std::cout << "The batting average for each position was:" << std::endl;
while(in >> hits >> misses >> bases && ++pos)
{
    std::cout << "Position " << pos << " batting average is " << bases / static_cast<double>(hits + misses + bases) << std::endl;
}



*Though if you are putting these on an output file you will have to replace all couts with your output file stream.
Last edited on
Line 27: What is the purpose of the do/while loop? It's only going to execute once (when n = 9). An if statement would have done the same thing.

Lines 25-26: These are calculated each time through the for loop, but since the do/while loop executes only when n=9, you're only going to get output the last time through the for loop.
AbstractionAnon wrote:
the do/while loop executes only when n=9

Actually the do while will execute at least once then if n is 9 it will execute until it crashes since he has no way to break out of it.
heres my recent updated code, and it wont calculation the average of each line in data.txt
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	int atBat[10], hitS[10], baseS[10];
	int data, k = 0;
    int averageB, averageS;

	ifstream Data("Data.txt");   // this define the "Data" file object and opens Data.txt input file
	do{
		Data >> data; // get input from Data file object
		if (data != -1){
			k = data;
		atBat[k] = atBat[k] + 1;
		Data >> data;
		hitS[k] = hitS[k] + 1;
		baseS[k] = baseS[k] + data; 
		}
	}while(data != -1 && Data);
	for (int n = 1; n <= 9; ++n){
		averageB = hitS[k] / atBat[k];
		averageS = baseS[k] / atBat[k];
	}
		cout <<"The batting average for each position was: " << endl;
		cout <<"Position " << k << " batting average is " << averageB << endl;
		cout <<"Position " << k << " batting average is " << averageB << endl;
		cout <<"Position " << k << " batting average is " << averageB << endl;
		cout <<"Position " << k << " batting average is " << averageB << endl;
		cout <<"Position " << k << " batting average is " << averageB << endl;
		cout <<"Position " << k << " batting average is " << averageB << endl;
		cout <<"Position " << k << " batting average is " << averageB << endl;
		cout <<"Position " << k << " batting average is " << averageB << endl;
		cout <<"Position " << k << " batting average is " << averageB << endl;
		cout << endl;
		cout <<"The slugging percentage for each position was; "<< endl;
		cout <<"Position " << k << " batting average is " << averageS << endl;
		cout <<"Position " << k << " batting average is " << averageS << endl;
		cout <<"Position " << k << " batting average is " << averageS << endl;
		cout <<"Position " << k << " batting average is " << averageS << endl;
		cout <<"Position " << k << " batting average is " << averageS << endl;
		cout <<"Position " << k << " batting average is " << averageS << endl;
		cout <<"Position " << k << " batting average is " << averageS << endl;
		cout <<"Position " << k << " batting average is " << averageS << endl;
		cout <<"Position " << k << " batting average is " << averageS << endl;
	system("PAUSE");
	return 0;
}
1
2
3
4
5
6
7
		Data >> data; // get input from Data file object
		if (data != -1){
			k = data;
		atBat[k] = atBat[k] + 1;
		Data >> data;
		hitS[k] = hitS[k] + 1;
		baseS[k] = baseS[k] + data;


Is the first number in your file supposed to be the position? That wouldn't really make sense with the results. You should iterate from 1-9 for the person hitting.
no but its just normal positions like position 1, position 2...etc i cant get position from number to 9 in output as above but it gives only number 4 in all of them
If you look at my earlier post you can see an easier way. Instead of doing all 10 at once why not do one at a time? I am still confused on what the data file is since you never answered my question on what they are supposed to be.
1 4 7 could you explain these? From your code it looks like times at bat, hits, and bases. Though this doesn't make sense. How can you be at the bat 1 time and hit 4 balls and 7 bases?
ohh sorry they are: the first row is hits of player, second is misses of a player and the third is how many times was the player at the base
So that means:

1
2
3
1 4 7 
2 1 2 
4 7 2


Player 1: Hits: 1 Misses: 2 Bases: 4
Player 2: Hits: 2 Misses: 1 Bases: 7
Player 3: Hits: 4 Misses: 2 Bases: 2

Still not sure how they got 0.215451415 from that for player 1. Also why are there 10 rows instead of 9?

Could you show an example for just one player how you get the results. It would mean we could help you better.

If I am correct batting average = hits / times at bat
that result is from my instructor sample program, he said to use our own numbers to test, sorry about that.
heres my recent updated code: i have fixed the the position problem by learning what you have provided above and now the averages for battling and slugging are giving the correctly, i guess they are not reading it.

heres my updated 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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	int atBat[10], hitS[10], baseS[10];
	int data, k = 0;
    int averageB, averageS;

	ifstream Data("Data.txt");   // this define the "Data" file object and opens Data.txt input file
	do{
		Data >> data; // get input from Data file object
		if (data != -1){
			k = data;
		atBat[k] = atBat[k] + 1;
		hitS[k] = hitS[k] + 1;
		baseS[k] = baseS[k] + data; 
		}
	}while(data != -1 && Data);
	
	averageB = hitS[k] / atBat[k];
	averageS = baseS[k] / atBat[k];
	// The counter variable can be declared in the init-expression.
	cout <<"The batting average for each position was: " << endl;
	for (int pos = 1; pos <= 9; pos++){
		cout <<"Position " << pos << " batting average is " << averageB << endl;
	}
	cout <<"\nThe slugging percentage for each position was; "<< endl;
	for (int pos = 1; pos <= 9; pos++){
		cout <<"Position " << pos << " batting average is " << averageS << endl;
	}
	
	system("PAUSE");
	return 0;
}
Okay, I was trying all different ways to try and get those numbers and couldn't figure it out. Also if your professor lets you. You should make them columns and not rows. So the file would look like:
1
2
3
4
5
6
7
8
            Hits Misses Bases
player 1 1       4          4
player 2 2       1          7
player 3 4       7          2
...
1 4 4
2 1 7
4 7 2


Also, to confirm so the batting average for player one it should be 1 / 4 = .25 (i assume by at bases you mean times at the home base batting?)

If this is the case doing it column instead of row way would look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <fstream>  //std::ifstream, std::ofstream

int main()
{
    std::ifstream in("input.txt");
    std::ofstream out("output.txt");
    int hits = 0;
    int misses = 0;
    int atBat = 0;
    int pos = 0;
    out << "The batting average for each position was:" << std::endl;
    
    while(in >> hits >> misses >> atBat && ++pos)
    {
        out << "Position: " << pos << " batting average is " << hits / static_cast<double>(atBat) << std::endl;
    }
}
--Input--
1 2 3
1 4 7
4 10 20
3 7 10
2 4 6

--output--
The batting average for each position was:
Position: 1 batting average is 0.333333
Position: 2 batting average is 0.142857
Position: 3 batting average is 0.2
Position: 4 batting average is 0.3
Position: 5 batting average is 0.333333




*EDIT just saw your post. The problem is probably this: k = data; Also averageB = hitS[k] / atBat[k]; that is integer division and you are assigning to an integer. You must make the average a double and cast one of the operands in the division to a double.
Last edited on
but all this has to be in arrays of 10. the data.txt numbers should be in array of each:

atBat[10];
hits[10];
bases[10];
Last edited on
here is an updated code, but it wont calculate the average correctly
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	float atBat[9];
	float hits[9] ;
	double bases[9];
	int data;
	int i = 0;
    float averageB, averageS;

	ifstream Data("Data.txt");   // this define the "Data" file object and opens Data.txt input file
	do{
		Data >> data; // get input from Data file object
		if (data != -1){
			//i = data;
			atBat[i] = atBat[i] + 1;
			hits[i] = hits[i] + 1;
			bases[i] = bases[i] + data; 
		}
	}while(data != -1 && Data);
	
	averageB = hits[i] / atBat[i];
	averageS = bases[i] / atBat[i];
	// The counter variable can be declared in the init-expression.
	cout <<"The batting average for each position was: " << endl;
	for (int pos = 1; pos <= 9; pos++){
		cout <<"Position " << pos << " batting average is " << averageB << endl;
	}
	cout <<"\nThe slugging percentage for each position was; "<< endl;
	for (int pos = 1; pos <= 9; pos++){
		cout <<"Position " << pos << " batting average is " << averageS << endl;
	}
	
	system("PAUSE");
	return 0;
}

here is the update txt file
1
2
3
4
5
6
7
8
9
10
          hits atBat baseS
player1   1      4     7 
player2   2      1     2 
player3   4      7     2 
player4   8      4     4 
player5   6      4     6 
player6   8      1     2 
player7   9      3     5 
player8   4      2     7
player9   5      8     3


@ giblit and all the other members that helped me THANK YOU. i found the problem by looking at the giblit last post thank you all.
I want to display the last while loop also but it doesnt show up
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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	float atBat = 0, hits = 0, bases = 0;
	int data;
	int i = 0, pos = 0, pos1 = 0;
 
	ifstream input("Data.txt");   // this define the "Data" file object and opens Data.txt input file
	//do{
	input >> data; // get input from Data file object
	if (data == -1){
		atBat = atBat + 1;
		hits = hits + 1;
		bases = bases + data; 
	}
	//}while(data != -1 && input);
	
	// The counter variable can be declared in the init-expression.
	cout <<"The batting average for each position was: " << endl;
	while(input >> hits >> atBat >> bases && ++pos1)
	{
		cout << "Position " << pos1 << " batting average is " << hits / static_cast<float>(atBat) << endl;
	}
	cout <<"\nThe slugging percentage for each position was: " << endl;
	
	while(input >> bases >> atBat >> hits && ++pos)
	{
		cout << "Position " << pos << " batting average is " << bases / static_cast<double>(atBat) << endl;
	}

	system("PAUSE");
	return 0;
}
You already read everything from the file that is why the second one doesn't work. You would have to read the file again to do it that method.

but all this has to be in arrays of 10.
Well if you need arrays you could try something like:

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
#include <fstream>  //std::ifstream, std::ofstream

int main()
{
    std::ifstream in("input.txt");
    std::ofstream out("output.txt");
    unsigned people = 0u;
    in >> people;


    double *sluggingAverage = new double[people];
    //I would suggest using:
    //std::vector<double> battingAverage, sluggingAverage;
    //Then you wouldn't need to have the number of people in the file.
    //and its neater
    //Also, if you wish to save the batting averages
    //you can addd them to an array aswell

    int hits  = 0;
    int atBat = 0;
    int bases = 0;

    unsigned pos = 0;

    out << "The batting average of each position was:" << std::endl;
    while(in >> hits >> atBat >> bases)
    {
        out << "Position " << pos + 1 << " has a batting average of: " << hits  / static_cast<double>(atBat) << std::endl;
        sluggingAverage[pos++] = bases / static_cast<double>(atBat);
    }

    out << std::endl << "The slugging average of each position was:" << std::endl;
    for(unsigned i = 0; i < people; ++i)
    {
        out << "Position " << i + 1 << " has a slugging average of: " << sluggingAverage[i] << std::endl;
    }

    delete[] sluggingAverage; //delete the heap memory when done with it

    return 0;
}
--INPUT--
9
1 4 7 
2 1 2 
4 7 2 
8 4 4 
6 4 6 
8 1 2 
9 3 5 
4 2 7
5 8 3

--OUTPUT--
The batting average of each position was:
Position 1 has a batting average of: 0.25
Position 2 has a batting average of: 2
Position 3 has a batting average of: 0.571429
Position 4 has a batting average of: 2
Position 5 has a batting average of: 1.5
Position 6 has a batting average of: 8
Position 7 has a batting average of: 3
Position 8 has a batting average of: 2
Position 9 has a batting average of: 0.625

The slugging average of each position was:
Position 1 has a slugging average of: 1.75
Position 2 has a slugging average of: 2
Position 3 has a slugging average of: 0.285714
Position 4 has a slugging average of: 1
Position 5 has a slugging average of: 1.5
Position 6 has a slugging average of: 2
Position 7 has a slugging average of: 1.66667
Position 8 has a slugging average of: 3.5
Position 9 has a slugging average of: 0.375
can you show the the last part of while without array. because i have learned how its done with arrays but i was trying to read data and still it wont work
The easiest way would be to use some sort of container to store the relevant information. Otherwise you would have to read in the information twice which you probably don't want to do. There are probably other ways but I wouldn't really say they are worth the time of doing.
Pages: 12