I need help with arrays in c++

I need to create a program that will find the total of all the elements in array. I need to use for loop and the array size is 10.

The output should be like this: i used user input

Input array 0 : 2
Input array 1 : 4
Input array 2 : 6
Input array 3 : 7
Input array 4 : 8
Input array 5 : 9
Input array 6 : 10
Input array 7 : 12
Input array 8 : 16
Input array 9 : 20

Sum of 10 numbers in an array is = 94
Press any key to continue...

I have a code but the output is not like that. Here is the code, pls. Help

// Program to store 10 integers array

#include <iostream>
#include <stdio.h>

using namespace std;

int getArray(int x[]);


int main ()
{
int a[10];

int e = getArray(a);


return 0;
}

int getArray(int x[])
{
int userInput;


for (int count = 0; count < 10; count ++)
{
cout << "Input Array : ";
cin >> userInput;

userInput = x[10];

}


system("pause");
Return 0;
}


The input of this is:

Input array
Press any key to continue...

PLS. GUYS HELP ME WITH THIS.

change:
userInput = x[10];
to
x[count] = userInput;
Last edited on
there's aerror. the return 0

it says "return" undeclared(first use this function)
Return is not return. Case matters.
i got. it. but the output is still the same with what i have given
1
2
3
4
5
6
7
8
9
10
11
12
void getArray(int x[])
{
   for ( int count = 0; count < 10; count ++ )
   {
      cout << "Input Array : ";

      int userInput;
      cin >> userInput;

      x[count] = userInput;
   }
}
Last edited on
Please, show your current code and output verbatim, i.e. diligent copy and with code tags.
i got. it. but the output is still the same with what i have given


because you didnt write the code... we only fixed what you did wrong..

what you need now is to add elements of a[10] array. I bet you can do that.
How to add elements? Any help? Pls....
In case of ambiguity: "add elements" means same as "calculate the sum of the values of elements of the array".
Sorry but i really can't understand.



correct me if i'm wrong guys but in order to add elements in an array you can use a code like this
1
2
3
4
5
6
7
8
9
10
11
12
13
int total=0;
int array[10];

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

for(int x=0; x<10; x++)
{total=total+array[x];}

for(int x=0; x<10; x++)
{cout<<"input array "<<x<<" : "<<array[x]<<endl;}
cout<<"the total is: "<<total<<endl;
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
#include <cstdlib>
#include <iostream>

using namespace std;

const unsigned ARRAY_SIZE = 10;
int my_array[ARRAY_SIZE] = {0};

void getInput();
void showResult();

int main()
{
	getInput();
	showResult();
  
	system("pause");
	return 0;
}

void getInput()
{
	for (unsigned n = 0; n < ARRAY_SIZE; ++n)
	{
		cout << "Input array " << n << " : ";
		int num;
		while (!(cin >> num))
		{
			cout << "\n\tplease enter a number\n";
			cin.clear();
			cin.ignore(100, '\n');
		}
		my_array[n] = num;
	}
}

void showResult()
{
	int total(0);
	for (unsigned n = 0; n < ARRAY_SIZE; ++n)
		total += my_array[n];
	cout << "\nSum of " << ARRAY_SIZE << " numbers in array is = " << total << endl;
}
i really doubt that this person has gotten to functions since he is still asking about arrays, we should try and keep it simple in order not to confuse people.
i really doubt that this person has gotten to functions since he is still asking about arrays, we should try and keep it simple in order not to confuse people.


If you bothered to read the code in the OP, you could allay your doubt.
#include <iostream>
#include <stdio.h>
using namespace std;
int getArray(int x[]);
int main ()
{
int a[10];
getArray(a);
return 0;
}

int getArray(int x[])
{
int sum=0;
for (int count = 0; count < 10; count ++)
{
cout << "Input Array "<<count<<":";
cin >> x[count];//assigning the user input to the elements in the array
sum=sum+x[count]; //adding the elements of the array x
}
cout<<"Sum of 10 numbers in an array is ="<<sum<<"\n";
system("pause");
return 0;
}
thank you guys. your help is appreciated.
Topic archived. No new replies allowed.