### add, average and reverse in 1 function

the codes run it is i want. But the problem is how to make the addition, average and reverse will be in one function?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include #include int addition(int size, int a[]); using namespace 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 "<>arr[i])) { cout<< "Invalid input! Please enter number only "<-1; j--) //for reversing the entered variable { cout<

 ``` The inputed numbers are: 4 3 5 2 1 Sum= 15 Average= 3 The reverse of the inputed numbers are: 1 2 5 3 4 ```
Last edited on
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include #include void arrayfunction(int size, int a[]); //make it a void function using namespace 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> 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; }``````
thanks but i want the body of the function has no cout or cin.
Last edited on
You can just copy std::reverse and tweak it a little bit. Then can do everything in 1 loop.
http://en.cppreference.com/w/cpp/algorithm/reverse

Last edited on
In other words you want a single function to compute and "return" three things:
1. Sum
2. Average
3. Change the order of values in the array

A function can return only one value. However,

Read section Arguments passed by value and by reference from http://www.cplusplus.com/doc/tutorial/functions/
and section Arrays as parameters from http://www.cplusplus.com/doc/tutorial/arrays/

What is not clear in the latter that function can modify caller's array similarly to by reference parameters:
 ``123456789101112131415161718192021222324`` ``````#include using std::cout; void printarray( const int arg[], size_t length ) { for (size_t n=0; n
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.
can i use recursion to this? if so, how to do it? in order to have 1 function only? also all the cout or cin is in the main function only.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include using namespace 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."< 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; } ``````
Last edited on
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:
 ``1234567`` ``````//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.
Bah humbug, I just want numbers to show:
 ``1234567891011121314151617181920212223242526272829`` ``````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; // ... ``````