Arrays for input numbers that are +12 greater and -12 smaller than average

Hi guys. The following is the code I made. How do I make arrays that will print out the input numbers that are +12 greater and -12 smaller than the average?
#include <iostream>
using namespace std;

int main()
{

int n;
double sum=0, average;
cout<<"How many numbers do you want to add?";
cin>>n;
double arr[n];
cout<<"\n Enter numbers:";

for(int i=0;i<n;i++) {
cin>>arr[i];
}

for(int i=0;i<n;i++) {
sum+=arr[i];
}
int i=0;
average= sum/n;
double arr2[]={average};

for (int i=0; i<1; ++i)
{
cout <<"average is"<< arr2[i];
i++;
}
return 0;
}
Hello Enna Ko,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Lin 10 is not proper C++. C++ does not allow for VLA, (Variable Length Arrays). You could use this to create a dynamic array.

You should initialize all your variables when you define them.

In your first for loop, line 17, the "cin" would look nicer if you have a prompt to know what to enter. Also you could add to the "sum" here which would eliminate the next for loop.

Line 24 "i" is an unused variable.

Line 28 creates an array for just 1. Is there a point to this?

The following for loop prints just 1 element of the array. The value in the variable "average" can replace this. The "i++" in the for loop is adding 1 to the loop iterator not the "i" defined on line 24. At this point "i" is a local variable to the for loop and does not see the "i" defined before the for loop.

For the most part the program does work, but could be cleaned up a bit.

Andy
Hello Enna Ko,

I have worked on your code and offer these suggestions. Be sure to read the comments in the code.
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
#include <iostream>
#include <iomanip>  // <--- Added.

using namespace std;

int main()
{
    constexpr int MAXSIZE{ 20 };  // <--- Added.

    int numsEntered{4}; // <--- Used for testing. Remove number when finished.
    double sum{}, average{};
    double arr[MAXSIZE]{ 10.25, 20.5, 30.75, 40.875 };  // <--- Could be replaced with a vector. Used for testing. Remove numbers when finished.

    std::cout << std::fixed << std::setprecision(4);  // <--- Added. Only needs done once.

    // <--- Next lines commented out for testing. Uncomment when finished.
    //cout << "How many numbers do you want to add? ";  
    //cin >> numsEntered;

    // <--- Should check that "numsEntered" is less than MAXSIZE. You do not want to go past the end of the array.

    cout << "\n Enter numbers:\n";

    for (int idx = 0; idx < numsEntered; idx++)
    {
        // <--- Next lines commented out for testing. Uncomment when finished.
        //std::cout << "Number " << idx + 1 << " of " << numsEntered << ": ";
        //cin >> arr[idx];
        
        sum += arr[idx];
    }

    average = sum / numsEntered;

    std::cout << "\nAverage is " << average << '\n';


    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

This is enough to get you started. After line 35 you will need to step through the array and check if that number is 12 off from average.

That code can either fill a new array, not really needed, or just use "cout" to display the numbers that fit.

Notice that I changed the variable name "n" to "numsEntered". A good variable name makes the code easier to follow and understand. "numsEntered" is just a suggestion. Feel free to use something else if you like. And do not be afraid of long names. A good IDE can help you finish typing a long variable name with just a few characters.

I have set up this code to bypass the input to save time when testing the code. The other advantage is that you can change the numbers in the array definition to try different combinations.

You could create multiple definitions of line 12 with different numbers and then change the comment on any 2 lines to use what you need.

Andy
As Andy said, variable length arrays aren't standard C++. Use a vector instead.

Once you have computed the average, go back over the numbers and print the ones that are less than average-12, or greater than average+12. Here is your code with the changes.
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
#include <iostream>
#include <vector>
using namespace std;

int
main()
{

    int n;
    double sum = 0, average;

    // Get the numbers from the user
    cout << "How many numbers do you want to add?";
    cin >> n;
    vector<double> arr;
    double d;
    cout << "\n Enter numbers:";

    for (int i = 0; i < n; i++) {
	cin >> d;
	arr.push_back(d);
    }

    // Compute the average
    for (int i = 0; i < n; i++) {
	sum += arr[i];
    }
    average = sum / n;


    // Print outliers: numbers that are +12 greater or -12 smaller
    // than average
    cout << "Average = " << average << '\n'; // print the average for testing
    cout << "Outliers:";
    for (int i = 0; i < n; ++i) {
	if (arr[i] < average - 12 || arr[i] > average + 12) {
	    cout << ' ' << arr[i];
	}
    }
    cout << '\n';
    return 0;
}

Only 1 iteration over the array is needed. Consider:

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
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	const int range {12};
	int n {};
	double sum {};

	cout << "How many numbers do you want to add? ";
	cin >> n;

	vector<double> arr(n);

	cout << "Enter numbers: ";

	for (auto& no : arr) {
		cin >> no;
		sum += no;
	}

	const double average {sum / n};
	cout << "Average = " << average << '\n';
	cout << "Outliers:";

	for (const auto& no : arr)
		if (no < average - range || no > average + range)
			cout << ' ' << no;

	cout << '\n';
}

Topic archived. No new replies allowed.