Summation and Averaging Problem - Separate Files

I have been wracking my brain over this for hours and I know it has to be something incredibly simple that I'm missing. My assignment is to write a program that sums and averages the contents of an array. I'm required to use separate files.

I think I've narrowed the issue down to my average.cpp file. When summation.cpp is called in main, it sums the numbers just fine. However, in average.cpp the sum does not come out correctly when the array elements are greater than 4 which messes up the average. (Edit: It actually looks like it's adding 10 to what would be the final sum at element 4 then continuing with the correct summation). I'm at a total loss.

Here's an example of the messed up output (I've used cout just to try to figure out where the problem was):

Please assign the size of the array:
7

Please enter the array elements:
1 2 3 4 5 6 7 

The sum of the array is: 28.00 (sum function called here)

sum: 1.00
sum: 3.00
sum: 6.00
sum: 10.00
sum: 38.00
sum: 44.00
sum: 51.00

The average of the array is: 7.29


I've posted all of my code below just in case the problem isn't actually in average.cpp.

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

/**************
* summation.h *
***************/

#ifndef SUMMATION_H
#define SUMMATION_H

double sum(const double*, const int);

#endif // SUMMATION_H


/****************
* summation.cpp *
*****************/

#include<iostream>

double sum(const double* arr, const int size)
{
    double sum = 0.0;

    for(int i = 0; i < size; i++)
    {
        sum += *arr;
        arr++;
    }

    return sum;
}


/************
* average.h *
*************/

#ifndef AVERAGE_H
#define AVERAGE_H

double avg(const double*, const int);

#endif // AVERAGE_H


/**************
* average.cpp *
***************/

#include <iostream>

double avg(const double* arr, const int size)
{
    double average;
    double sum = 0.0;

    for(int i = 0; i < size; i++)
    {
        sum += *arr;
        arr++;

        std::cout << "sum: " << sum;
        std::cout << std::endl;

        /*std::cout << "average: " << average;
        std::cout << std::endl;*/
    }

    average = sum / size;

    return average;
}


/***************
*   main.cpp   *
****************/

#include "summation.h"
#include "average.h"
#include <iostream>
#include <iomanip>

int main()
{
    int size = 0;
    double arr[size];

    std::cout << "Please assign the size of the array: \n";
    std::cin >> size;
    std::cout << std::endl;

    std::cout << "Please enter the array elements: \n";
    std::cout << std::setprecision(2) << std::fixed << std::showpoint;

	for (int i = 0; i < size; i++)
    {
        double num;

        std::cin >> num;

		arr[i] = num;
    }

    std::cout << std::endl;
    std::cout << "The sum of the array is: " << sum(arr, size) << std::endl;
    std::cout << std::endl;

    std::cout << "The average of the array is: " << avg(arr, size) << std::endl;

    return 0;
}
Last edited on
Line 87: You are creating an array of size 0, then assigning 'size' a different variable. The size of the array will still be 0 as you cannot change the size of an array once its created.

My best guess is that what you are experiencing right now is undefined behavior.

To fix the problem, simply move line 87 to below line 91 i.e., AFTER user has inputted the size, you can them make an array of that size.
Last edited on
Regarding line 59 ad 60:

Incrementing arr is unnecessary when the loop itself is incrementing. Just use the loop counter to advance arr like so:
sum += *(arr+i);

Even better if you replace it with array syntax, like so:
sum += arr[i];

Line 60 can then be deleted.



But remember in C++ array sizes must be compile time constants. If you want a variable sized array I suggest you consider using a std::vector. Otherwise you'll need to dynamically allocate memory with new[] and delete[] the memory when you're finished.

closed account (48T7M4Gy)
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
#include <iostream>
#include <iomanip>

double sum(const double* arr, const int size)
{
    double total = 0.0;

    for(int i = 0; i < size; i++)
    {
        total += arr[i];
    }

    return total;
}

double avg(const double* arr, const int size)
{
    double average = sum(arr, size) / size;

    return average;
}

int main()
{
    int size = 0;    
    double num = 0;

    std::cout << "Please assign the size of the array: \n";
    std::cin >> size;
    
    double* arr = new double[size];
    
    std::cout << "Please enter the array elements: \n";
    
    for (int i = 0; i < size; i++)
    {
        std::cin >> num;
		arr[i] = num;
    }

    std::cout << std::endl;
    std::cout << "The sum of the array is: " << sum(arr, size) << std::endl;
    std::cout << std::endl;

    std::cout << "The average of the array is: " << avg( arr, size ) << std::endl;

    delete [] arr;
    
    return 0;
}
Please assign the size of the array: 
5
Please enter the array elements: 
6
7
2
3
1

The sum of the array is: 19

The average of the array is: 3.8
 
Exit code: 0 (normal program termination)
Last edited on
Thank you, everyone for your help! Arslan7041, I made the changes you suggested and now it works perfectly. I knew it had to be something simple :) jlb, thank you for the tip as well! I really appreciate it.
Topic archived. No new replies allowed.