LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with arrays (https://www.linuxquestions.org/questions/programming-9/help-with-arrays-635268/)

rec3g 04-14-2008 10:20 AM

help with arrays
 
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.

SciYro 04-14-2008 10:52 AM

I would say the best idea is to do your own homework and step thru the program with a debugger.

Randux 04-14-2008 11:08 AM

Quote:

Originally Posted by SciYro (Post 3120887)
I would say the best idea is to do your own homework and step thru the program with a debugger.

How selfish! You forgot to tell him to post his finished project so others with the same assignment can use it ;)

rec3g 04-14-2008 11:27 AM

man you guys are mean. it's cool though I figured it out after sitting here for a few hours

taylor_venable 04-14-2008 12:28 PM

How does it go? Genius is 1% inspiration and 99% perspiration, I believe.

b0uncer 04-14-2008 12:54 PM

Anything to do with dividing by an integer that is bigger than any number in the array, without casting it to double or float? Especially before multiplying by 100? :) Just a thought..

Randux 04-15-2008 07:04 AM

Quote:

Originally Posted by rec3g (Post 3120929)
man you guys are mean. it's cool though I figured it out after sitting here for a few hours

Ahhhhh, now doesn't that feel a lot better now that you did it yourself?

Congratulations! Keep up the good work.


All times are GMT -5. The time now is 06:27 PM.