#include<iostream>
#include<stdlib.h>
void arrayfunction(int size, int a[]); //make it a void function
usingnamespace std;
int main()
{
int arr[20] = {};
int sz, i = 0;
double sum = 0;
cout << "How many numbers you want to be added? \n";
while (!(cin >> sz))
{
cout << "Invalid input! Please enter number only " << endl;
system("PAUSE");
system("CLS");
cin.clear();
cin.ignore(10000, '\n');
cout << "How many numbers you want to be added? \n";
}
cout << "Enter " << sz << " numbers: \n" << endl;
while (i<sz)
{
while (!(cin >> arr[i]))
{
cout << "Invalid input! Please enter number only " << endl;
cin.clear();
cin.ignore(10000, '\n');
}
i++;
}
system("CLS");
//now that you have your array, pass it into your function.
arrayfunction(sz, arr);
cin.ignore();
cin.get();
return 0;
}
//put all your fun code into your function
void arrayfunction(int size, int a[])
{
cout << "The inputed numbers are: " << endl;
for (int i = 0; i < size; i++) {
cout << a[i] << " ";
}
cout << endl << endl;
cout << "The sum of the numbers is: ";
int sum = 0;
for (int i = 0; i < size; i++) {
sum += a[i];
}
cout << sum << endl << endl;
cout << "The average is: ";
double avg = (double)sum / (double)size;
cout << avg << endl << endl;
cout << "The reverse order is: ";
for (int i = size-1; i > -1; i--) {
cout << a[i] << " ";
}
cout << endl << endl;
}
ok to do what you want, you will need to put a switch statement into your function and include a third parameter that will tell the function which output to use. Then you will have to make repeating calls to your function, each with a different switch statement value.
#include<iostream>
#include<stdlib.h>
usingnamespace std;
int addition(int);
double average(int);
int reverse(int);
int main()
{
int val, count=0;
cout << "Enter a number that has minimum of 4 digits and maximum of 9: \n";
while(!(cin >> val) || val<=1000 || val>1000000000)
{
cout << "The value you inputed is invalid! Please try again."<<endl;
system("PAUSE");
system("CLS");
cin.clear();
cin.ignore(10000, '\n');
cout << "Enter a number : \n";
}
int sum= addition(val);
double avg= average(val);
cout <<endl<< "Sum= " << sum<<endl;
cout <<endl<< "Average= "<<avg<<endl;
cout <<endl<< "The reverse of the inputed numbers are: "<<endl;
cout <<endl<<reverse(val)<<endl;
}
int addition(int v)
{
int num=v, sum=0, count=0;
while (num != 0)
{
sum = sum + num % 10;
num = num / 10;
count++;
}
return sum;
}
double average(int v)
{
int num=v, sum=0, count=0;
while (num > 0)
{
sum = sum + num % 10;
num = num / 10;
count++;
}
double avg= (double)sum/(double)count;
return avg;
}
int reverse(int v)
{
int num=v, reversedNumber = 0, remainder, count=0;;
while(num > 0)
{
remainder = num%10;
reversedNumber = reversedNumber*10 + remainder;
num = num / 10;
count++;
}
return reversedNumber;
}
I don't think recursion could work for this. You see, once a function returns a value, the program jumps out of the function. So any attempt to recursive call the function after return, would be skipped.
No recursion. No switch. Do all three operations and return all results in one go:
1 2 3 4 5 6 7
//put compute code into your function
void arrayfunction( size_t size, int arr[], int & sum, double & average )
{
// calculate sum
// calculate average
// rearrange elements of arr
}
Yes, but what if you wanted to pass a different array into the function each time. Then you would have to have a different sum variable and a different average variable for each array.
int total {};
double avg {};
constexpr size_t foos {7};
int foo[ foos ];
// fill foo
// ...
arrayfunction( foos, foo, total, avg );
// show
std::cout << total << ' ' << avg;
for ( auto x : foo ) std::cout << ' ' << x;
std::cout << '\n';
constexpr size_t bars {42};
int bar[ bars ];
// fill bar
// ...
arrayfunction( bars, bar, total, avg );
// show
std::cout << total << ' ' << avg;
for ( auto x : bar ) std::cout << ' ' << x;
std::cout << '\n';
// just first half of bar
arrayfunction( bars/2, bar, total, avg );
// show
std::cout << total << ' ' << avg;
// ...