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?

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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<stdlib.h>

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 "<<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");

    cout<< "The inputed numbers are: "<<endl;

    for(int i= 0; i<sz; i++)
    {
        cout<<arr[i]<< " ";
    }

    sum= addition(sz, arr);
    double avg= sum/sz;

    cout<<endl<< "\nSum= "<<sum<<endl;
    cout<<endl<< "Average= "<<avg<<endl;

    cout<< "\nThe reverse of the inputed numbers are: "<<endl;

    for(int j=sz-1; j>-1; j--)              //for reversing the entered variable
    {
        cout<<arr[j]<< " ";
    }
    return 0;
}
int addition(int size, int a[])
{
    double sum= 0;
    for(int j= 0; j<size; j++)
    {
        sum+=a[j];
    }
    return sum;
}


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
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<stdlib.h>

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<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;
	
}
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

https://wandbox.org/permlink/jVrLYzaLh0ds77ea
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using std::cout;

void printarray( const int arg[], size_t length ) {
  for (size_t n=0; n<length; ++n)
    cout << arg[n] << ' ';
  cout << '\n';
}

void iota( int arg[], size_t length, int first = 0 ) {
  for (size_t n=0; n<length; ++n) {
    arg[n] = first;
    ++first;
  }
}

int main ()
{
  constexpr size_t N {4};
  int firstarray[N] {};
  iota( firstarray, N, 42 );  // modifies firstarray
  printarray( firstarray, 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.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<stdlib.h>
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."<<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;
}
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:
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.
Bah humbug, I just want numbers to show:
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
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;
// ... 
Topic archived. No new replies allowed.