Is there a way to complete my averaging program, as I've started?

For average, I have it read number entries into the array, but how do I sum each value in the array to divide by num?

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
    Designed by: B.M.
    Date: 12/23/09
    Purpose: Satisfy boredom
*/
#include <iostream>
#include <string>
//#include <>
using namespace std;

int ave ();
int sum ();
int freq ();
int fate ();

int main ()
{
   char choice;
   bool terminate = false; 
    
   while (!terminate)
   {
         cout << "Welcome to the Datadyne menu\n\n" 
              << "What would you like to do?\n" 
              << "     A)Find the average value of a data set\n"
              << "     B)Find the sum of a data set\n"
              << "     C)Find the most common number in a data set\n"
              << "     D)Test your fate\n"
              << "     E)Quit application\n\n";
         
         cin >> choice;
         
         switch (choice)
         {
                case a:
                case A:
                     ave();
                     break;
                case b:
                case B:
                     sum();
                     break;
                case c:
                case C:
                     freq();
                     break;
                case d:
                case D:
                     fate();
                     break;
                case e:
                case E:
                     terminate = true; //put in every function!!!
                     break;
                default:
                        cout << "The option you entered is invalid. Please try"
                        << " again\n\n";
                        break;
         };
   };
   
   cout << "Thank you for your time, please come back again";
    
   return 0;
};

int ave()
{
    int i, set [], num=0, average=0;
    
    cout << "How many numbers are you using? (less than 100)\n";
    cin >> num;
    
    if (num > 100 || num < 0)
    {
       cout << "Error: Invalid entry\n\n";        
       terminate = true;   
    };
    else
    {
        set[num];
        
        for(i=0; num > i; i++)
        {
                 cout << "Enter a data entry:\n\n";
                 cin >> set[i];
        };
    
    //(sum each number in data set)/num
    };
    terminate = true;    
};    
Line 69: The declaration of set is invalid. A size must be provided.
Line 78: The semicolon closes the if statement completely. The else on the line below will be invalid because it's not attached to any if. Statement block don't need to be followed by a semicolon.
1
2
3
for (/*...*/){
   //...
}

An lone semicolon on global scope, as on lines 65 and 92, is outright illegal.
Line 81: This doesn't declare set as an array of num ints. In C++, arrays defined as T array[n]; need n to be a compile-time constant.
To learn how to allocate a dynamic array (that is, an array whose size can be defined at run time), see http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
Topic archived. No new replies allowed.