I need help with arrays in c++

Aug 28, 2013 at 9:26am
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.

Aug 28, 2013 at 9:57am
change:
userInput = x[10];
to
x[count] = userInput;
Last edited on Aug 28, 2013 at 9:58am
Aug 29, 2013 at 7:27am
there's aerror. the return 0

it says "return" undeclared(first use this function)
Aug 29, 2013 at 7:35am
Return is not return. Case matters.
Aug 29, 2013 at 7:37am
i got. it. but the output is still the same with what i have given
Aug 29, 2013 at 7:41am
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 Aug 29, 2013 at 7:41am
Aug 29, 2013 at 7:43am
Please, show your current code and output verbatim, i.e. diligent copy and with code tags.
Aug 29, 2013 at 8:26am
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.
Aug 29, 2013 at 11:52am
How to add elements? Any help? Pls....
Aug 29, 2013 at 12:04pm
In case of ambiguity: "add elements" means same as "calculate the sum of the values of elements of the array".
Aug 29, 2013 at 12:07pm
Sorry but i really can't understand.



Aug 29, 2013 at 12:45pm
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 Aug 29, 2013 at 12:52pm
Aug 29, 2013 at 1:24pm
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;
}
Aug 29, 2013 at 1:26pm
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.
Aug 29, 2013 at 3:36pm
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.
Aug 29, 2013 at 4:24pm
#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;
}
Aug 30, 2013 at 3:03am
thank you guys. your help is appreciated.
Topic archived. No new replies allowed.