Can anybody tell me wants wrong with this program it doesnt work the way it should be..
[b]
#include<iostream>
using namespace std;
class Computation
{
public:
int getSum(int MyArray[],int n)
{
int sum;
for(n=0;n<10;n++)
{
sum+=MyArray[n];
}
return sum;
}
int getProduct(int MyArray[],int z)
{
int prod=1;
for(int z=0;z<10;z++)
{
prod=prod*MyArray[z];
}
return prod;
}
int getFactorial(int s[0], int d[9])
{
int a=1;
for(int y=1;y<=s[0];y++)
{
a=(a) *(s[0]);
s[0]--;
}
cout<<"The factorial of the first array is:"<<a<<endl;
int x=1;
for(int y=1;y<=d[0];y++)
{
x=x*d[9];
d[9]--;
}
cout<<"The factorial of the last array is:"<<x<<endl;
}
};
int main()
{
Computation math;
int MyArray[10];
int y, a;
for(y=0;y<10;y++)
{
a =MyArray[y];
cout<<"Enter a number:";
cin>>a;
#include<iostream>
usingnamespace std;
class Computation{
public:
int getSum(int MyArray[],int n){//Why you get n and you put it on zero?
int sum=0;
for(n=0;n<10;n++){
sum = sum + MyArray[n];
}
return sum;
}
int getProduct(int MyArray[],int z){//Why you get z and put it on zero??
int prod=1;
for(int z=0;z<10;z++){
prod=prod*MyArray[z];
}
return prod;
}
void getFactorial(int s[], int d[]){
int a=1;
for(int y=1;y<=s[0];y++){
a=(a) * (s[0]);
s[0]--;
}
cout<<"The factorial of the first array is:"<<a<<endl;
int x=1;
for(int y=1;y<=d[0];y++){
x=x*d[9];
d[9]--;
}
cout<<"The factorial of the last array is:"<<x<<endl;
}
};
int main(){
Computation math;
int MyArray[10];
int y, a;
for(y=0;y<10;y++){
cout<<"Enter a number:";
cin>>a;
MyArray[y] = a;
}
cout<<"The sum is:"<<math.getSum(MyArray,10)<<endl;
cout<<"The product is:"<<math.getProduct(MyArray,10)<<endl;
math.getFactorial(MyArray, MyArray);
return 0;
}
Ok, first of all you had:
1 2 3
a =MyArray[y];
cout<<"Enter a number:";
cin>>a;
Which assign the information from MyArray[y] (garbage) to 'a' and the a reads a value... If you want to read to MyArray[y] you should write:
1 2 3
cout<<"Enter a number:";
cin>>a;
MyArray[y] = a;
Then when you call getFactorial you want to send the whole array and not just the address of MyArray[0] and 9. So you sould send it as math.getFactorial(MyArray, MyArray); and the get factorial function should be void getFactorial(int s[], int d[])
You should have it void because you don't need it to return something and also you want it to accept two arrays (actually as i see is the same array so you dnt need two of them. Or if you just use some values send them as integers, not arrays of integers. like: math.getFactorial(MyArray[0], MyArray[9]); and read it with int getFactorial(int s, int d)
Also the getSum and getProduct take arguments that you change their values before you use them, so you don;t need to send them, just initialize new ones in the functions.
now i already run the program and it gives a wrong output
i inputted 10 of 5 numbers
i've got the sum of 205338... instead of 50.
i've got the product of -17... instead of 50..
the factorial did not exist...
but i think i got the formula right...
#include<iostream>
usingnamespace std;
class Computation{
public:
int getSum(int MyArray[]){//remove the unessesery variables
int sum=0;
for(int n=0;n<10;n++){
sum = sum + MyArray[n];
}
return sum;
}
int getProduct(int MyArray[]){
int prod=1;
for(int z=0; z<10; z++){
prod = prod * MyArray[z];
}
return prod;
}
void getFactorial(int s, int d){//recieve just two ints
int a=1;
if( (s!=0) && (s!=1) ){ //If s==0 or s==1 the factorial is 1
//for(int y=1; y<=s; y++){//because you always change s it will not be done as many times
for(;s>0;s--){//it just enter the loops and check on s
a = a * s;
}
}
cout<<"The factorial of the first array is:"<<a<<endl;
int x=1;
if( (d!=0) && (d!=1)){
for(; d>0; d--){
x = x * d;
}
}
cout<<"The factorial of the last array is:"<<x<<endl;
}
};
int main(){
Computation math;
int MyArray[10];
int y, a;
for(y=0;y<10;y++){
cout<<"Enter a number:";
cin>>a;
MyArray[y] = a;
}
cout<<"The sum is:"<<math.getSum(MyArray)<<endl;
cout<<"The product is:"<<math.getProduct(MyArray)<<endl;
math.getFactorial(MyArray[0], MyArray[9]);//sending just the values you need in the function
return 0;
}
This works fine with me, i just made some changes because the factorial was wrong as logic.
What do you mean it terminates after 10 inputs? If you just want the program to wait bfore ending use the system("pause"); before return 0;