lundi 19 février 2018

Program isn't giving me the percentage that each int takes up of my sum

My program is doing everything correctly except for giving me the percentage that each integer takes up of my sum. I'm totally stuck on why it's not (honestly probably super obvious, but I can't tell, sorry).

This is the output of the program:

Sum Equals:  10000
       110      0%
       220      0%
       330      0%
       440      0%
       550      0%
         6      0%
         4      0%
      2966      0%
      4000      0%
      1374      0%



#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    //Initializations
    const int size=25;
    int ar[size], pos=0, num=0, sum=0;

    //opens file
    ifstream file;
    file.open("exam.dat");

    //puts the ints from the file into an array and calculates sum and number of ints
    file>>num;
    while(file)
    {
        ar[pos]=num;
        sum+=num;
        pos++;
        file>>num;
    }

    //prints the sum
    cout<<setw(10)<<"Sum Equals:"<<setw(7)<<sum<<endl;

    //prints the list of ints along with their percentages
    for (int i=0;i<pos;i++)
    {   
        cout<<setw(10)<<ar[i]<<setw(7)<<((ar[i]/sum)*100)<<"%"<<endl;
    }

    file.close();
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire