Simple program using functions

Hi!

I have written a fully functioning program that prompts user to type in values, stores them in an array, outputs array in ascending order and then outputs statistics on the values without using any functions. I am currently in the process of modifying the code to include the functions prototyped below. I am a little bit confused as to how to go about defining the functions and calling them. I declared them above the main function and now need to define and call them to make the program execute the exact same tasks as before, only using functions instead. In particular I'm unsure of how to use the ReadData function as the number of values to be read and stored in the array is unknown. I would highly appreciate any input on this as I am completely stuck on how to move forward. Thank you!!

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
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>


using namespace std;

//Prototyping functions

void ReadData(float list [], int& count);

float Variance(float list [], int count);

float StandardDev(float list [], int count);

void Sort(float list [], int count);

float SqRoot(float value);


void main()
{
    float value, sum = 0, largestValue, smallestValue, average, median,
        var = 0;
    float list[100];
    int count = 0;
    char answer = 'y';


    while (count <= 100)
    {
        cout << "Would you like to add a value? (Y or N)." << endl;
        cin >> answer;


        if (answer == 'y' || answer == 'Y')
        {
            cout << "Enter a value: ";
            cin >> list[count];


            sum += list[count];
            ++count;
        }


        if (answer == 'n' || answer == 'N')
            break;


        if (answer != 'y' && answer != 'n')


            cout << "Invalid input. Please enter Y or N." << endl;
    }


    for (int i = (count - 1); i > 0; i--)
        for (int j = 0; j < i; j++)
            if (list[j] > list[j + 1])
            {
                list[j] = list[j] + list[j + 1];
                list[j + 1] = list[j] - list[j + 1];
                list[j] = list[j] - list[j + 1];
            }


            for (int i = 0; i < count; i++)
                cout << list[i] << endl;
            cout << endl;


            average = sum / count;
            smallestValue = list[0];
            largestValue = list[count - 1];

            if (count % 2 == 0)
            {
                median = (list[count / 2 - 1] + list[count / 2]) / 2;
            }


            else
            {
                median = list[count / 2];
            }


            for (int i = 0; i < count; i++)
            {
                var += (list[i] - average)*(list[i] - average) / count;
            }


            cout << "You entered " << count << " values." << endl;
            cout << "The sum of the values is " << sum << endl;
            cout << "The largest value entered is " << largestValue << endl;
            cout << "The smallest value entered is " << smallestValue << endl;
            cout << "The average of the array is " << average << endl;
            cout << "The median of the array is " << median << endl;
            cout << "The variance of the values is " << var << endl;


            return 0;
}
Assuming that you mean "square root" from the function "SqRoot", you could make the function look something like this:

1
2
3
float SqRoot(float value) {
  return sqrt(value);
}


I don't know what to tell you to put in ReadData() though. That is something you will have to ask your professor so you can make sure you write the function properly.
Topic archived. No new replies allowed.