I'd like to get some help with splitting my main function into different main functions and pass array data to functions.

So basically, the main is only supposed to have the file input output. but the lowest, highest, sum and average of all numbers all need to be in functions of their own. I read that it's easier to just write the whole code first then split it, but I'm somewhat stumped a bit after doing that. Thanks in adcance

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

using namespace std;

int main()
{
    const int ARRAY_SIZE = 12; // Array size.
    int numbers[ARRAY_SIZE]; // Array with 12 elements.
    int count = 0; // Loop counter variable.

    // Get the file name from the user.
    cout << "Enter the name of the file you wish to open : ";
    string filename; cin >> filename;

    // Open the file.
    ifstream inputFile(filename.c_str());

    // If the file successfully opened, process it.
    if (inputFile)
    {
        while (count < ARRAY_SIZE && inputFile >> numbers[count])
        {
            count++;
        }

        // Close the file.
        inputFile.close();
    }
    else
    {
        // Display an error message.
        cout << "Error opening the file.\n\n";
        return -1;
    }

    // Display the numbers read.

    int value = count;

    cout << "There are " << value << " numbers in the array.\n\n";

    cout << "The numbers are : ";

    for (int index = 0; index < count; index++)
    {
        cout << numbers[index] << " ";

    }
    cout << "\n";

    // Display the sum of the numbers.

    cout << "The sum of these numbers is : ";

    int sum = 0; // Initialize accumulator.

    for (int count_Sum = 0; count_Sum < ARRAY_SIZE; count_Sum++)
    {
        sum += numbers[count_Sum];
    }

    cout << sum << "\n\n";

    // Display the average of the numbers.

    cout << "The average of these numbers is : ";

    double total = 0; // Initialize accumulator.
    double average; // To hold the average.

    for (int count_Average = 0; count_Average < ARRAY_SIZE; count_Average++)
    {
        total += numbers[count_Average];
    }
    average = total / ARRAY_SIZE;

    cout << average << "\n\n";

    // Display the highest value of the numbers.

    cout << "The highest value of these numbers is : ";

    int count_Highest;
    int highest;

    highest = numbers[0];
    for (count_Highest = 1; count_Highest < ARRAY_SIZE; count_Highest++)
    {
        if (numbers[count_Highest] > highest)
        {
            highest = numbers[count_Highest];
        }
    }
    cout << highest << "\n\n";

    // Display the lowest value of the numbers.

    cout << "The lowest of these numbers is : ";

    int count_Lowest;
    int lowest;

    lowest = numbers[0];
    for (count_Lowest = 1; count_Lowest < ARRAY_SIZE; count_Lowest++)
    {
        if (numbers[count_Lowest] < lowest)
        {
            lowest = numbers[count_Lowest];
        }
    }
    cout << lowest << "\n\n";

    return 0;

}
What has you stumped?

Do you know how to write and use functions?

yea, so and so but passing array to function is probably the issue more thannaything else
What have you tried?

I recommend you start with a function to display the array, since that appears to be the first bit that you need to move to a function.

yea I have that already. I thought I could just go to where each function would be needed and just created the function there but passing the array there is the issue
yea I have that already.

Where? Do you know that you can't implement one function inside another function?

What is the issue, I'm not clairvoyant and I can't see your screen so you need to show what you've tried and ask specific questions about that code.

Topic archived. No new replies allowed.