In DisArray over this Array

I'm new to this site and have only been learning C++ for a couple months
I'm having a problem with the sum function after the for loop
1)In main, create an array of 10 integer elements. Write a while loop that repeatedly asks the user for an integer to populate the array with values.
2) The sumArray function will take an integer array and an integer (representing how many items are in the array) as input parameters and will sum up all of the values contained in the array. It will return that sum.

I'd appreciate any help
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

#include<iostream>
using namespace std;
int sumArray();

int main()
{
	const int ARRAY_SIZE = 10;
	int numbers[ARRAY_SIZE];
	int count = 0;
	int num;

	cout << "enter a number or -1 to quit \n";
	cin >> num;
	while (num != -1 && count < ARRAY_SIZE)
	{
		count++;
		numbers[count - 1] = num;
		cout << "enter a number or -1 to quit \n";
		cin >> num;
	}
	sumArray();

	system("pause");
	return 0;
}

int sumArray(10);
int total = 0;

for (count = 0; count < sumArray; count++)
{
total += count;
	return << total;
}


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

int sumArray(int [], int);

int main()
{
   const int ARRAY_SIZE = 10;
   int numbers[ARRAY_SIZE];
   int count = 0;
   int num;

   std::cout << "Enter 10 numbers or -1 to quit\n";

   for (int i = 0; i < ARRAY_SIZE; i++)
   {
      std::cin >> num;

      if (num == -1)
      {
         break;
      }

      numbers[i] = num;
      count++;
   }

   int sum = sumArray(numbers, count);

   std::cout << "\nThe sum is: " << sum << '\n';
}

int sumArray(int arr[], int num_entries)
{
   int sum = 0;

   for (int i = 0; i < num_entries; i++)
   {
      sum += arr[i];
   }

   return sum;
}
Enter 10 numbers or -1 to quit
1
2
3
4
5
6
7
8
9
10

The sum is: 55
Enter 10 numbers or -1 to quit
5
10
15
20
-1

The sum is: 50
Thanks Furry Guy this helps a lot
You had most of the logic already, just needed a bit of a tweak, and a function rewrite to let you pass your array.
подскажите литературу для начинающей?

Нет русского, только английский, пожалуйста.
Topic archived. No new replies allowed.