Need to use Arrays & Functions

I'm supposed to be able to create two arrays and a text file, then store the 20 numbers(grades) into one array and transfer that to the second array of which will distribute and display the distribution, all through functions. Along with that I also must use the data to calculate the largest, smallest, and average in a separate function, but my output is just hella crazy. Just dove into programming this semester and its tough. Any suggestions will help. Please and thank you!

NOTE: The output is showing negative and exaggerated numbers i.e. (-8990377) and it is looping 20 times in both double calcResults and void displayResults functions.
The content of the txt file was: 88 85 89 64 78 85 92 60 91 96 63 59 83 89 74 93 92 92 63 100

cpp.sh/5adm6

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

int getDataFromFile(int data []); //data type int to capture the integer values into the function
double calcResults(double smallest, double largest, double average); //double data type needed for calculating sums and the average
void displayResults(int); //int data type to display all results

int main()
{
const int dataFromFile = 19; //array for storing the data from the file
int data[dataFromFile];
const int gradeCount= 19; //array for distributing grades
int grades[gradeCount];

double smallest = 0,
largest = 0,
average = 0.0;
int i;

getDataFromFile(data);
for(i = 0; i < dataFromFile; i++)
{
grades[i] = data[i];
}

calcResults(smallest, largest, average);

for(i = 0; i < gradeCount; i++)
{
displayResults(grades[i]);
}

system("pause");
return 0;
}
int getDataFromFile(int data[])
{
int NUM_GRADES = 19;
data[NUM_GRADES];

ifstream file;
file.open("input.txt");
if (!file)
{ cout << "Error opening file\n"; }
for(int i = 0; i < NUM_GRADES; i++)
{
file >> data[i];
file.close();
}
return NUM_GRADES;
}

double calcResults(double smallest, double largest, double average)
{
const int NUMS = 19;
int calculate[NUMS];
double totalNum = 0.0;
average = 0.0,
largest,
smallest;

for (int x = 0; x < NUMS; x++) //finding the sums
{
totalNum += calculate[x];

average = totalNum / NUMS;

largest = smallest = calculate[0];

for (int x = 1; x < NUMS; x++)
{
if (calculate[x] > largest)
largest = calculate[x];
else if (calculate[x] < smallest)
smallest = calculate[x];
}

cout << fixed << showpoint << setprecision(2) << endl;
cout << "Smallest: " << setw(9) << smallest << endl;
cout << "Largest: " << setw(9) << largest << endl;
cout << "Average: " << setw(9) << average << endl;
}
return smallest, largest, average;
}
void displayResults(int result)
{
cout << "The final grade results are " << result << endl;
}
Last edited on
 
int calculate[NUMS]; 


This array contains random values - no wonder that you get strange results.

 
return smallest, largest, average;


A function can only return one value. I just don't understand why this compiles.

 
const int dataFromFile = 19;


Your files contains 20 values.

I would rewrite the calcResults function like this :

1
2
3
4
void calcResults(int data[], double& smallest, double& largest, double& average)
{
  // your code here
}


So many things wrong. I've got a complete program here with comments against the corrections, and it'll be here for posterity.
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
I'm supposed to be able to create two arrays and a text file, then store the
20 numbers(grades) into one array and transfer that to the second array of
which will distribute and display the distribution, all through functions.

Along with that I also must use the data to calculate the largest, smallest,
and average in a separate function, but my output is just hella crazy. Just
dove into programming this semester and its tough. Any suggestions will help.

Please and thank you!

NOTE: The output is showing negative and exaggerated numbers i.e. (-8990377)
and it is looping 20 times in both double calcResults and void displayResults
functions.

The content of the txt file was:
88 85 89 64 78 85 92 60 91 96 63 59 83 89 74 93 92 92 63 100

cpp.sh/5adm6
 */

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

//data type int to capture the integer values into the function
int getDataFromFile(int data[], int size);

//double data type needed for calculating sums and the average
double calcResults(int data[],int size, double& smallest, double& largest, double& average);

//int data type to display all results
void displayResults(int data[],int size);

int main()
{
    const int dataFromFile = 19;    //array for storing the data from the file
    int data[dataFromFile];

    // kbw: We don't need a duplicate, do we?
//    const int gradeCount = 19;    //array for distributing grades
//    int grades[gradeCount];

    double smallest = 0,
    largest = 0,
    average = 0.0;
//    int i;

    // kbw: pass in array and its size, get number of items used
    int n = getDataFromFile(data, dataFromFile);

    // kbw: we don't need a seperate grades
//    for(i = 0; i < n; i++)
//    {
//        grades[i] = data[i];
//    }

    // kbw: pass in data we read from file, and its size,
    // kbw: pass results by reference
    calcResults(data, n, smallest, largest, average);

    // kbw: print the grades we read in
    displayResults(data, n);
//    for(i = 0; i < gradeCount; i++)
//    {
//        displayResults(grades[i]);
//    }

    system("pause");
    return 0;
}

int getDataFromFile(int data[],int size)
{
//    int NUM_GRADES = 19;         // kbw: we pass in NUM_GRADES as a parameter
//    data[NUM_GRADES];            // kbw: we use data[] as a parameter, we don't need another one

    ifstream file("input.txt");    // kbw: initialise the stream with the filename
//    file.open("input.txt");      // kbw: don't call open/close
    if (!file)
    {
        cout << "Error opening file\n";
    }

    // kbw: loop while there's more to read and we have space for it
    int i;                        // kbw: i isn't local to the loop
    for(i = 0; i < size && file; i++)
    {
        file >> data[i];
//        file.close();            // kbw: we don't close() until we're done
    }
    return i;                      // kbw: return number items in the array
}

double calcResults(int data[],int size, double& smallest, double& largest, double& average)
{
//    const int NUMS = 19;          // kbw: use the size passed in
//    int calculate[NUMS];          // kbw: use the data passed in

    // kbw: assign reasonable start values
    average = 0.0;
    largest = 0.0;
    smallest = 100.0;

    //finding the sums
    double totalNum = 0.0;
    for (int x = 0; x < size; x++)      // kbw: use size passed in
    {
        totalNum += data[x];            // kbw: use data passed in

//        average = totalNum / NUMS;    // kbw: calculate the average out the loop

        // kbw: we don't do min/max like this
//        largest = smallest = calculate[0];
//        for (int x = 1; x < NUMS; x++)
//        {
//            if (calculate[x] > largest)
//                largest = calculate[x];
//            else if (calculate[x] < smallest)
//                smallest = calculate[x];
//        }

        // kbw: we do min/max like this
        if (data[x] > largest)
            largest = data[x];
        if (data[x] < smallest)
            smallest = data[x];
    }

    average = totalNum / size;      // kbw: all this stuff moved from inside the loop
    cout << fixed << showpoint << setprecision(2) << endl;
    cout << "Smallest: " << setw(9) << smallest << endl;
    cout << "Largest: "  << setw(9) << largest  << endl;
    cout << "Average: "  << setw(9) << average  << endl;
    return totalNum;
}

void displayResults(int data[],int size)
{
    cout << "The final grade results are:" << endl;

    // kbw: write all the grades in a loop
    for (int i = 0; i < size; ++i)
        cout << " " << data[i] << endl;
}
Last edited on
Oh.. huh so a lot of it was the misuse of parameters and unnecessary syntax. Damn. Definitely need to start practicing more. Thanks a ton for the complete response kbw, along with everyone else. You all are lifesavers. kbw would you have any recommendations for me to look at to practice/learn efficiently i.e. books, websites, etc.? Obviously you know what you're doing and I would like to be able to do that too.
Last edited on
Are you talking about your calcResult function? Since this is a double, this has to return a double back, and in this case it will return the total

However, since you are passing the values by reference, smallest, largest, and average will also act as a "return" and their value will be updated in the main
Presuming that I edited my program the way you advised, what would you assume the return value would be?
A function just returns one thing. Typically, if more than one output is required, they're passed by reference as parameters.

There is a trick however. A function can return just one thing, but there's no restriction on what that thing can be. So it can return an object that wraps a whole bunch of values. Check this out.
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
#include <tuple>
#include <vector>
#include <iostream>

std::tuple<double, double, double> process(const std::vector<double> &values)
{
        double sum = {};
        double product = 1;
        for (double v : values)
        {
                sum += v;
                product *= v;
        }

        return std::make_tuple(sum, sum / values.size(), product);
}

int main()
{
        std::vector<double> data{ 0, 1, 2, 3, 4, 5 };

        double sum, avg;
        std::tie(sum, avg, std::ignore) = process(data);

        std::cout << "sum=" << sum << " avg=" << avg << "\n";
}
Last edited on
Topic archived. No new replies allowed.