Algorithm Deviation

Hey guys. I am working on this project and having a huge amount of trouble. The assignment calls for me to create a program that reads several numbers from a file, stores them in an array, finds the average value of the numbers and then prints out each number individually and says how much that number deviates from the average.

I apologize because my code for this is a total mess. It might be so off that it isn't even possible to correct. But I would appreciate any feedback you might have. I know I'm not supposed to ask homework questions so I'm not asking for the answer, just a point in the right direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
float sum(float val1, float val2);  //Declare sum function
void average(float sum, float total);  //Declare average function
void deviation(float average, float newtotal);

int main()
{
    int flag = 0;
    float total = 0;  //Initialize total
    float count = 0;  //Initialize count
    float newtotal;
    float twoDarray[total][count];  //Initialize array
    
    ifstream input("Arraynumbers.txt");
    string numbers;
    for (int i=0; i<6; i++)
    {
        input >> numbers;
        }
    input.close();
    
    while (count=0; count<6; count++)
    {
          sum(total, total);
          }
    
    average();
    
    for (count=0; count<6; count++)
    do
    {
        cout << "Element " << count << "is " << total << "and it deviates " <<
             << deviation << " from the average" << endl;
             }
    
    system("pause");
    return 0; //Terminate with success
}

float sum(float val1, float val2)
{
      return (val1+val2);
      }
      
void average(float sum, float total)  //Print average
{
     cout << sum/total << endl;
     }

void deviation()  //Print deviation
{
     cout << average-newtotal << endl;
     }


Yes its a mess. A few things

twoDarray[0][0] (total=0,count=0). How much are you going to fit in that? Also total and count are floats. You should have integer literals in there, e.g. twoDarray[6][2] or something. Why do you need 2D array? (just asking - maybe you do?)

In the first loop you repeatedly, read from the file into the string numbers. These values are not used for anything else (thrown away).

In the second loop you repeatedly call sum(0,0). (total still = 0 at this point)
Do you want to add total to total or maybe the next number onto the total?

This is just the first observations - maybe start again?






Last edited on
If you've learned about vectors yet, consider using a vector of integers to store the input numbers. That will allow you to design the program such that you don't have to know beforehand how many numbers you'll receive.

One tricky problem you'll have to solve is to convert the number you get out of the string into an int type, as I don't believe you can simply cast a string as an int and get the desired result. You can construct a loop that builds up the integer's value by traversing the digits of the string.

If you're expected to return a floating-point (non-integer) average value, remember to cast your calculations correctly so that your fractional digits don't get truncated in your calculations.

Unless your assignment requires you to write separate functions, I wouldn't bother. If you're still learning how to do these sorts of things, the last thing you need to worry about is passing arrays of variables back and forth between functions. :)
Topic archived. No new replies allowed.