c++ beginner - read 10 integers into an array

hi guys. i got the description of the prac im working but i cant seem to figure the solution. i also have attached the pseudocode and my code below so i hope it's easier for you to understand what im wanting to ask. im so lost. i got the whole address code but not the numbers.

Read ten integers into an array (use a for loop) in main().
Pass the array to a function and calculate and return the average of the ten numbers.
(Remember to add the integers to a total that is of type int).
Pass the array to a second function and print all numbers greater than the average, then the average on the next line ( 2 decimal places), then all numbers less than or equal to average on the third line.
Test data: For user input of 9 7 3 4 8 6 1 2 5 10 the output should look exactly like this:
9 7 8 6 10
5.50
3 4 1 2 5

Declare array
Loop 10 times
Prompt and read number
End loop
Invoke foo() to calculate average
Invoke foo2() to print numbers

float foo()
loop through the array
Sum the numbers
End loop
Return average
void foo2()
Loop 10 times
If current number > average
Print current numb
End if
End loop
Print average
Loop 10 times
If current number ≤ average
Print current number
End if
End loop

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
  #include<iostream>
#include<stdio.h>
#include<conio.h>
#include<iomanip>
#include<array>
using namespace std;

float foo( int inputArray );
void foo2( int inputArray );

int main()
{
	int inputArray[ 10 ];

	cout <<"Please enter ten number: \n";

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

	float average = foo( inputArray );
	foo2( inputArray );
	getch();
	return 0;
}
//----------------------------------------------
float foo(int inputArray[])
{
  int total = 0;
  for (int i = 0; i < 10; i++)
  {
	total += inputArray[i];
  }
  float average = (float)total / 10;
  return average;
}
//------------------------------------------------------------------------------
void foo2( int inputArray[] )
{
	float average;
	int total;

	for ( int i = 0; i < 10; i++ )
	{
		if ( inputArray[ i ] > average )
		{
			cout << inputArray;
		}
	}
	average = total / 10;
	cout << average;

	for ( int i = 0; i < 10; i++ )
	{
		if ( inputArray[ i ] <= average )
		{
			cout << inputArray;
		}
	}
	getch();
}


thank youuu
Last edited on
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
#include <iostream>
#include <iomanip>

constexpr size_t arrSze {10};

double foo(const int []);
void foo2(const int [], double);

int main()
{
	int inputArray[arrSze] {};

	std::cout << "Please enter " << arrSze << " numbers: ";

	for (size_t i = 0; i < arrSze; ++i)
		std::cin >> inputArray[i];

	foo2(inputArray, foo(inputArray));
}

//----------------------------------------------
double foo(const int inputArray[])
{
	int total {};

	for (size_t i = 0; i < arrSze; ++i)
		total += inputArray[i];

	return (total + 0.0) / arrSze;
}

//------------------------------------------------------------------------------
void foo2(const int inputArray[], double average)
{
	for (size_t i = 0; i < arrSze; ++i)
		if (inputArray[i] > average)
			std::cout << inputArray[i] << ' ';

	std::cout << '\n' << std::fixed << std::setprecision(2) << average << '\n';

	for (size_t i = 0; i < arrSze; ++i)
		if (inputArray[i] <= average)
			std::cout << inputArray[i] << ' ';

	std::cout << '\n';
}



Please enter 10 numbers: 9 7 3 4 8 6 1 2 5 10
9 7 8 6 10
5.50
3 4 1 2 5

Last edited on
thank youuuuuuuuuuuuuuuuuu
Topic archived. No new replies allowed.