Array calling from main function to member function of a class

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;

}
cout<<"The sum is:"<<math.getSum(MyArray,y)<<endl;
cout<<"The product is:"<<math.getProduct(MyArray,y)<<endl;
math.getFactorial(&MyArray[0], &MyArray[9]);
return 0;

}[/b]
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
44
45
46
47
48
49
50
51
52
#include<iostream>
using namespace 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.
ohh i see now i know thank you!... i'm really confuse when it comes to array now i understand thank you for the reply...

Godbless!
but why is it that when i run the program after i input 10 numbers, it terminates
Last edited on
what do you think should be the value of n and z.. i used them for the looping..

u think it wud be better not to declare them?
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...
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
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
using namespace 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;
i already try that but that doesn't work
i still got the wrong answer..

do you have any other options??
can you post the file (or a sample file) here so i can see it?
Topic archived. No new replies allowed.