Beginner. Need help.

I am trying to write a program that will read a 100 element array and output the mean, median, min value, max value, and std deviation of the 100 elements. I can figure out how make each equation so it prints on the screen.

What I'm having trouble with is building the array. I have read multiple forum archives and also have watched Buckys C++ tutorial videos and arrays are just not making sense to me. It's making me feel stupid.

The following code is reading an input file of around 1k numbers and it needs to output a 100 element array. Any help would be greatly appreciated!!

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

// Declare global constants
const char* IN_FILE_NAME = "stats.txt";
const char* OUT_FILE_NAME = "results.txt";
const int ELEMENTS = 100;

// Function Prototypes
int mean;
int median;
int minimum;
int maximum;
int stand;
int printTrialResults;

// This program performs basic statistics on a large set of data points
int main()
{
    // Declare variables
    ifstream inFile;
    ofstream outFile;
    int trialNumber = 0;
    double elementArray[ELEMENTS];

    // Open input and output files
    inFile.open(IN_FILE_NAME);
    outFile.open(OUT_FILE_NAME);

    // Loop through all of the data sets in the input file
    while(getTrialElements(inFile, elementArray, ELEMENTS))
    {
        // Keep track of the number of data sets processed
        trialNumber++;
        // Output the results to the output file
        printTrialResults(outFile, trialNumber, elementArray, ELEMENTS);
    }

    // Close input and output files
    outFile.close();
    inFile.close();

    return 0;
}

// Function definitions

Last edited on
Hello mrwhit,

Welcome to the forum.

I would move your global variables inside main where they will be better used. Declaring a global variable and then passing it to a function from defeats the purpose of a global variable. Not being able to compile the program right now I am not sure if IN_FILE_NAME and OUT_FILE_NAME will even work the way you have them written. If your compiler is capable of C++11 standards a "std::string" will work as well as a "char *".

Your proto types are written incorrectly. They should match the function definition like:
void printTrialResults(std::ofstream outFile, typeNeeded trialNumber, typeNrrded elementArray, ELEMENTS);
Not seeing the functions I am not completely sure how to fix them.

Do not need the complete file, maybe a sample of 10 for testing would be useful and at least the functions you are calling from main right now to see if everything fits together the way it should.

Hope that helps,

Andy
Last edited on
Andy,

Thank you very much for your response. I was told that I could not change the Int main() at all, and to deal with everything outside of it. I need an integer so I changed them from void since I want to return the integers to the output.

The infile is a file of around 1,000 numbers. The program is supposed to grab 100 of those random numbers and output the mean, median, minimum, maximum, and standard deviation on one line. I am just extremely confused on how to do it.
Hello mrwhit.

Changing the return type of the proto type does not fix your problem. You may find some of this useful:
http://www.cplusplus.com/forum/beginner/22038/
http://www.cplusplus.com/doc/tutorial/functions/

or a Google search on C++ function prototype
https://www.google.com/#safe=strict&q=c%2B%2B+function+prototype&*

A lot of the time I will write the function then copy the first line and paste it above "main" and put a semi-colon at the end for my function prototype. The function prototype needs to know the return value if any the function name and the type of parameters i.e., int, double, string or some other kind ending in a semi-colon so as not to be confused as a function definition.

You say you have changed the prototypes to return an "int", but some of those functions may need to return a double and may also need to take doubles as parameters for the calculations as using "int"s could give you the wrong answer from integer division.

Picking 100 random numbers from file of 1000 will be a challenge in its self, so I would concentrate on getting other parts of the program working first.

Hope that helps,

Andy
Topic archived. No new replies allowed.