Aug 28, 2013 at 9:26am Aug 28, 2013 at 9:26am UTC
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 Aug 28, 2013 at 9:57am UTC
change:
userInput = x[10];
to
x[count] = userInput;
Last edited on Aug 28, 2013 at 9:58am Aug 28, 2013 at 9:58am UTC
Aug 29, 2013 at 7:27am Aug 29, 2013 at 7:27am UTC
there's aerror. the return 0
it says "return" undeclared(first use this function)
Aug 29, 2013 at 7:35am Aug 29, 2013 at 7:35am UTC
R eturn is not r eturn. Case matters.
Aug 29, 2013 at 7:37am Aug 29, 2013 at 7:37am UTC
i got. it. but the output is still the same with what i have given
Aug 29, 2013 at 7:41am Aug 29, 2013 at 7:41am UTC
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:41am UTC
Aug 29, 2013 at 7:43am Aug 29, 2013 at 7:43am UTC
Please, show your current code and output verbatim, i.e. diligent copy and with code tags.
Aug 29, 2013 at 11:52am Aug 29, 2013 at 11:52am UTC
How to add elements? Any help? Pls....
Aug 29, 2013 at 12:04pm Aug 29, 2013 at 12:04pm UTC
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 Aug 29, 2013 at 12:07pm UTC
Sorry but i really can't understand.
Aug 29, 2013 at 12:45pm Aug 29, 2013 at 12:45pm UTC
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 12:52pm UTC
Aug 29, 2013 at 1:26pm Aug 29, 2013 at 1:26pm UTC
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 4:24pm Aug 29, 2013 at 4:24pm UTC
#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 Aug 30, 2013 at 3:03am UTC
thank you guys. your help is appreciated.