#include <iostream.h>
#include <conio.h>
#include <stdio.h>
usingnamespace std;
// function declaration:
void getAverage(int*);
void getSum(int*);
void getDifference(int*);
void getProduct(int*);
main ()
{
// an int array with 5 elements.
int balance[5];
for(int x=0; x<5; x++){
cin>>balance[x];
}
// pass pointer to the array as an argument.
// output the returned value
cout << "Product: " << getProduct(balance) <<endl;
cout << "Difference value is: " << getDifference(balance)<< endl;
cout << "Sum value is: " << getSum(balance) << endl;
cout << "Average value is: " << getAverage(balance)<< endl;
getch();
}
void getAverage(int* arr)
{
int i, sum = 0;
double avg;
for (i = 0; i < 5; ++i)
{
sum += arr[i];
}
avg = double(sum) / 5;
}
void getSum(int* arr)
{
int sum=0;
for (int i = 0; i < 5; ++i)
{
sum += arr[i];
}
}
void getDifference(int* arr)
{
int diff=0;
for (int i = 0; i < 5; ++i)
{
diff -=arr[i];
}
}
void getProduct(int* arr)
{
int prod=1;
for (int i = 0; i < 5; ++i)
{
prod *=arr[i];
}
}
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
usingnamespace std;
// function declaration:
double getAverage(int*);
int getSum(int*);
int getDifference(int*);
int getProduct(int*);
main ()
{
// an int array with 5 elements.
int balance[5];
for(int x=0; x<5; x++){
cin>>balance[x];
}
// pass pointer to the array as an argument.
// output the returned value
cout << "Product: " << getProduct(balance) <<endl;
cout << "Difference value is: " << getDifference(balance)<< endl;
cout << "Sum value is: " << getSum(balance) << endl;
cout << "Average value is: " << getAverage(balance)<< endl;
getch();
}
double getAverage(int* arr)
{
int i, sum = 0;
double avg;
for (i = 0; i < 5; ++i)
{
sum += arr[i];
}
avg = double(sum) / 5;
return avg;
}
int getSum(int* arr)
{
int sum=0;
for (int i = 0; i < 5; ++i)
{
sum += arr[i];
}
return sum;
}
int getDifference(int* arr)
{
int diff=0;
for (int i = 0; i < 5; ++i)
{
diff -=arr[i];
}
return diff;
}
int getProduct(int* arr)
{
int prod=1;
for (int i = 0; i < 5; ++i)
{
prod *=arr[i];
}
return prod;
}
I think this should work BUT I have another problem. The product and difference doesn't seem to print the correct numbers. I can't think of the logic.
Don't get frustrated. This is actually pretty good beginner code.
What are the inputs you're giving and the output for getProduct(). You might have a problem if the inputs are large because the product could overflow the size of an int. This is likely only if the product is over 2 billion.
By the way, note that getAverage() could be computed with the help of getSum():
1 2 3
int getAverage(int *arr) {
return (double)getSum(arr) / 5;
}
The difference doesn't seem to print the correct numbers though. Instead of subtracting the numbers, the program adds the numbers up and prints it with a negative (-) sign.
The difference doesn't seem to print the correct numbers though. Instead of subtracting the numbers, the program adds the numbers up and prints it with a negative (-) sign.
Like if I subtract 12,1,1,1,1 it prints -16.
What do you expect to happen, and what it supposed to be a difference of an array? Are you sure you're not supposed to calculate the difference of two arrays?
Is this a homework assignment? If so, then please re-read the assignment carefully to see what it says about the difference. This is an odd operation.
If the difference should be "first element minus all the other elements" then you need to change your getDifference() function. Initialize diff to the value of the first element. Then run the for loop from the 2nd to 5th elements.