for loop statement


I am working on a lab for statement problem and i can't figure out how to start it. Make a program that will input the 10 numbers and determine their average and display all numbers that are smaller than the average.

sample possible output:
Enter number : 1
2
3
4
5
6
7
8
9
15
average is: 6
numbers smaller than the average: 1
2
3
4
5

 
  
Last edited on
Here:

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
#include <iostream>
void getInput(int (&numbers)[], int& quantityOfNum)
{
    std::cout << "Enter numbers:\n";
    int temp;
    for (int count{0}; ; count++, quantityOfNum++)
    {
        std::cin >> temp;
        if (std::cin.fail() || count < 10)
        {
            break;
        }
        else
        {
            numbers[count] = temp;
        }
    }
}
int getAverage(const int (&numbers)[], const int& quantityOfNum)
{
    int sum{0};
    for (int count{0}; count < quantityOfNum; count++)
    {
        sum += numbers[count];
    }
    return sum / quantityOfNum;
}
void lessThanAverage(const int (&numbers)[], const int& average, const int& quantityOfNum)
{
    for (int count{0}; count < quantityOfNum; count++)
    {
        if (numbers[count] < average)
        {
            std::cout << numbers[count];
        }
    }
}
int main()
{
    int numbers[10];
    int average, quantityOfNum{0};
    getInput(numbers, quantityOfNum);
    average = getAverage(numbers, quantityOfNum);
    std::cout << "\nAverage: " << average << '\n' << '\n';
    std::cout << "Numbers less than average:\n";
    lessThanAverage(numbers, average, quantityOfNum);
    return 0;
}


Tell me any problems with it I'll fix it for you.

P.S. Please take a look at the code and understand it so you learn something.
Last edited on
That is called "homework"; something that a person does in order to learn/become better in some subject.

This site has: http://www.cplusplus.com/doc/tutorial/
Read the Basics, Control Structures, and Arrays.

Proceed in steps. The first step is to read 10 numbers from input.
wondering if these codes will run in Turbo C++ 4.0 Windows 7 Windows 8 64Bit Version? i see unfamiliar code like std::
I see unfamiliar code like std::
std:: is actually being used millions of times worldwide everyday.

@spike32

cout and cin are in the std namespace.

That's why putting using namespace std; before using cout allows you to just use cout.

Otherwise you can specify the namespace it's in like std::cout.

I do that to avoid name collisions (good practice too) and many others do too.

1
2
3
4
5
6
7
8
9
#include <iostream>
// Global
using namespace std;
int main()
{
    // using namespace std; can be put main function (local)
    cout << "Hello World!";
    return 0;
}

1
2
3
4
5
6
7
#include <iostream>
int main()
{
    // Specifying which namespace cout is in
    std::cout << "Hello World";
    return 0;
}


Both of these are correct.
Last edited on
Turbo C++ is ancient: https://en.wikipedia.org/wiki/Turbo_C%2B%2B

Its version of C++ Standard Library is probabny too old to support namespaces

More current (and free) compilers:
MSYS2 + mingw-w64
TDM-GCC
MS Visual Studio
@keskiverto
Do you prefer MinGW or TDM - GCC?
I haven't got MinGW to work since whenever I invoke g++ via command line it only works if I'm in MinGW folder where the compiler is.
@ keskiverto
yes ! i know Turbo C++ is old but our school still using since they are only teaching us the basics and fundamentals of c++ programming. I'm only a second year computer engineering student
Last edited on
Topic archived. No new replies allowed.