ok so I have this program that calculates the total of all the numbers in an array and then prints out all the numbers in that array. My only problem is that I'm supposed to calculate the number's percentage of the sum. Here's what I have so far...
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
int examScores[10];
float percentage;
ifstream myin;
int sum=0;
//open file
myin.open("exam.dat");
//read in scores
for(int i=0; i <10; i++)
myin >> examScores[i];
//calculate sum
for(int count=0; count<10; count++)
sum += examScores[count];
cout << "Sum Equals: " << sum << endl;
for(int count2=0; count2<10; count2++)
{
percentage = examScores[count2]/sum * 100;
cout << examScores[count2] << '\t' << percentage <<
endl;
}
myin.close();
}
Since the percentage part isn't working (it prints all zeros), I'm pretty sure my problem is with my percentage formula or format. originally I had the last output statement as follows...
cout << examScores[count2]<< '\t' <<examScores[count2]/sum * 100 <<endl;
but neither this or assigning the formula into the variable percentage is working. Any ideas??
oh and I also have to round the percentages to the nearest tenth.