Click here to Skip to main content
15,559,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
BACKGROUND: If you've followed lecture, you already know almost everything there is about arrays. Now you need some practice! The following problem appeared several years ago on an exam as a question. (Consequently it should be a short program!) Working completely alone (although you can ask the instructor questions, as always), see if you understand arrays well enough to do it.
TASK:   Write a complete C++ program to solve the following problem.

        PROGRAM DESCRIPTION:  An unknown number of integers, but no
        more than 25, are to be read from the file exam.dat.
        (Yes, this means you have to open that file.)  Calculate
        and output their sum.  Next output, one entry per line, each
        number and its percentage of the sum.  Line these up in columns.
        The first column should be 10 characters wide; the second column
        should be 7 characters wide, not including the percent sign.
        The percentages should be rounded to the nearest tenth.  See
        sample output below.

        PROGRAM INTERNALS:  Since the program is so short, you do not
        need to define any functions other than main().  You may
        assume the input data is error-free and need not be checked for
        correctness.  The input data file is to be read only once; you
        are NOT allowed to open the input file a second time NOR in some
        way rewind the input file. Output is to standard output (stdout).

        SAMPLE OUTPUT:
        Sum Equals:    200
       
                45   22.5%
                 5    2.5%
                30   15.0%
                20   10.0%
               100   50.0%
IMPLEMENTATION NOTES: Use as your source file name "percentage.cpp". Use as your executable file name "ola204". Do not forget to use the standard style conventions; that is, to comment, indent, and use mnemonic names appropriately. When you are ready to test run your program, symbolically link the file $PUB/percentage.dat to your account as exam.dat and use it.

        $ cd ~/OLA/ola204
        $ ln -s $PUB/percentage.dat exam.dat
REQUIRED: You are to electronically submit the source program. Once you have a working program, the following UNIX commands will do what is required:

        $ cd ~/OLA/ola204
        $ handin "ola204" percentage.cpp

What I have tried:

I have tried using this code 
<pre>#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

//Main function
int main()
{
    int cnt=0, i, sum=0;
    int arr[25];

    //Opening file in read mode
    fstream fin("exam.dat", ios::in);

    //Iterating over file data
    while(fin.good())
    {
        //Reading a number
        fin >> arr[cnt];

        //Accumulating sum
        sum = sum + arr[cnt];

        //Incrementing count
        cnt++;
    }

    //Closing file
    fin.close();

    //Printing sum
    cout << "\n Sum Equals: " << sum << " \n";

    //Set precision to 1
    cout << fixed << setprecision(1);

    //Iterating over array
    for(i=0; i<cnt; i++)
="" {
="" printing="" a="" line="" of="" result
="" cout="" <<="" "\n="" "="" right="" setw(10)="" arr[i]="" setw(7)="" ((arr[i]="" (double)sum)="" *="" 100.0)="" %";
="" }

="" "\n\n";
="" return="" 0;
}
but="" it="" still="" was="" not="" giving="" me="" the="" out="" put="" like="" sample="" output.
and="" this="" file="" in="" exam.dat="" data
110
220
330
440
550
6
4
2966
4000
1374
="=======================================
" and="" aslo="" used="" code="" too="" but="" stilll="" no
<pre="">#include <iostream>
#include <stdio.h>
#include<fstream>

using namespace std;

int main()
{
    
    int numbers[25];
    int i = 0;
   // read the file eam.dat
    ifstream myfile;
    myfile.open("eam.dat");
    if (myfile.is_open())
    {
        while (myfile >> numbers[i])
        {
            i++;
        }
    }
    else
    {
        cout << "Unable to open file";
    }

    // find the sum of the numbers
    int sum = 0;
    for(int j=0;j
Posted
Comments
CPallini 22-Feb-22 13:16pm    
What is the problem, then (excluding code-formatting, of course)?
Anuoluwa 22-Feb-22 13:38pm    
it not showing the right percentage... it just showed this ouptut to me

110 0.0 %
220 0.0 %
330 0.0 %
440 0.0 %
550 0.0 %
6 0.0 %
4 0.0 %
2966 0.0 %
4000 0.0 %
1374 0.0 %
870780922 100.0 %
k5054 22-Feb-22 13:59pm    
You have an erroneous value at the end of your list - eg 870780922 You'll have to take a look at your data file and see what's going on with it - otherwise your code works fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900