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
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
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.
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.
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?
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.
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.
#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 = newdouble[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.