Should compute the following menu with call by reference. Thx for ur help.(ps : I'm not that gd in function)

#include <iostream>
using namespace std;
void readValues (int & ,int &); // 2 values A and B
int factorial (int &); // A
int divCount (int); // A
void evSum (int &, int &,long); // A and B and I should have the long sum

int main ()
{
int A,B; int choice;

do {
cout<<"Enter a positive integer A :"<<endl; // how do I use call by reference to ask the user for A and B?
cin>>A;
cout<<"Enter a positive integer B :"<<endl;
cin>>B;
}while (A<=0 || B<=0 || A>B);

do {
cout<<"Choose a computation from the following menu :"
<<"1- Compute sum of the Factorials of "<<A<<"."
<<"2- Count all divisors of "<<A<<"."
<<"3- Compute the sum of all even integers between "<<A<<" and "<<B<<"."
<<"4- Exit the program.";
cin>>choice;

switch (choice) {

case 1 : {
cout<<"The following program computes the sum of factorials of "<<A<<"."<<endl;
long int sumFactorial = 0; //I want to compute 1! + 2! + 3! .....A! should I put this in main or not?
for (int K=1; K<=A;K++);
sumFactorial += factorial (A); // error saying that overloaded
cout<<"The sum of factorials of "<<A<<" is equal to :"<<sumFactorial<<endl;
break; }

case 2 : {
cout<<"The following program counts all the divisors of "<<A<<"."<<endl;
int divcount;
divcount = divCount (A); // It should count all divisors of A just count them and output the number
cout<<"The number of divisors of "<<A<<" is equal to : "<<divcount<<endl;
break; }

case 3 : {
cout<<"The following program computes sum of even integers between "<<A<<" and "<<B<<"."<<endl;
long int evsum = 0; // it should count the even integers between A and B.
evSum (A,B,evsum);
cout<<"The sum of even integers between "<<A<<" and "<<B<<" is equal to :"<<endl;
break; }

case 4 : {
cout<<"Exit the program"<<endl;
break; }
default :
cout<<"Wrong imput , Please choose a number from the menu."<<endl;
}
return 0;
}while (choice != 4);
}

long int factorial (int a) { // I need to use call by reference in these function to call the values

if(a == 0)
return 1; // I have some error that I can't understand . it's not working
else
return (a) * factorial(a - 1);

int divCount (int a) { // error on this brace

for (int i=1;i<=a;i++)
if (a % i == 0)
divCount++; // same in here I should call the values . I have error on "divCount" saying local function definitions are illegal.
return divCount;

void evSum (int a,int b) {

for ( int i>=a,i<=b,i++)
if (i % 2 == 0)
int evSum = 0; // I don't know if I should use "evsum" from main or "evSum"
evSum += i;
return evSum;
}
Hey. First of all, please edit your post and use code tags so it's readable - http://www.cplusplus.com/articles/jEywvCM9/

Second, all you did was give us your code. You didnt ask a question. What specifically do you need help with? Be specific.
Sorry but I'm not familiar with tagging that y I couldn't do it. I have some prob with my functions . I still didn't understand how should I call them from main to compute and return them back. I should also use call by reference which I recently learnt I'm practicing on it but still struggling a bit plz read the messg (//....) I wrote if it helps. thx for ur help.
Last edited on
I'm sorry I never used a forum before and I will try to help u as much as I can .
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
#include <iostream>

// invariant: n! is within the range of unsigned int
unsigned int factorial( unsigned int n )
{ return n < 2 ? 1 : n * factorial(n-1) ; }

void factorial_byref( unsigned int n, unsigned int& result ) { result = factorial(n) ; }

// invariant: n != 0
unsigned int div_count( unsigned int n )
{
    if( n == 1 ) return 1 ;

    unsigned int cnt = 2 ; // 1 and n are divisors
    for( unsigned int div = 2 ; div <= (n/2) ; ++div ) if( n%div == 0 ) ++cnt ;
    return cnt ;
}

void div_count_byref( unsigned int n, unsigned int& result ) { result = div_count(n) ; }

// invariant: a <= b , sum is within the range of unsigned int
unsigned int ev_sum( unsigned int a, unsigned int b )
{
    if( a%2 != 0 ) return ev_sum( a+1, b ) ;

    // invariant: here, a is an even number
    unsigned int sum = 0 ;
    for( ; a <= b ; a += 2 ) sum += a ;
    return sum ;
}

void ev_sum_byref( unsigned int a, unsigned int b, unsigned int& result ) { result = ev_sum(a,b) ; }

int main()
{
    unsigned int n = 8 ;
    std::cout << "factorial(" << n << ") == " << factorial(n) << '\n' ;

    unsigned int fact ;
    factorial_byref( n, fact ) ;
    std::cout << "fact == " << fact << '\n' ;

    ///////////////////////////////////////////////////

    n = 100 ; // divisors are 1, 2, 4, 5, 10, 20, 25, 50, 100
    std::cout << "div_count(" << n << ") == " << div_count(n) << '\n' ;

    unsigned int dcnt ;
    div_count_byref( n, dcnt ) ;
    std::cout << "dcnt == " << dcnt << '\n' ;

    ///////////////////////////////////////////////////


    unsigned int a = 23 ;
    unsigned int b = 23 ;
    std::cout << "ev_sum( " << a << ", " << b << " ) == " << ev_sum(a,b) << '\n' ;

    b = 24 ;
    std::cout << "ev_sum( " << a << ", " << b << " ) == " << ev_sum(a,b) << '\n' ;

    a = 1 ;
    b = 201 ;
    std::cout << "ev_sum( " << a << ", " << b << " ) == " << ev_sum(a,b) << '\n' ;
    // 2 + 4 + ... + 200 == 2 * ( 1 + 2 + ... + 100 ) == 2 * 100 * 101 / 2 == 100 * 101 == 10100

    unsigned int sum ;
    ev_sum_byref( a, b, sum ) ;
    std::cout << "sum == " << sum << '\n' ;
}

http://coliru.stacked-crooked.com/a/53f327ca9aecf0ff
first you can use online Code formatter to beautify code.
http://www.tutorialspoint.com/online_c_formatter.htm

and for using code tag simpley select < > icon in right panel in Foramt: section and simply
put your code in enclosing tag

finaly your beautified code is here:


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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include <iostream>
using namespace std;
void readValues (int & ,int &); // 2 values A and B
int factorial (int &); // A
int divCount (int); // A
void evSum (int &, int &,long); // A and B and I should have the long sum

int main ()
{
    int A,B;
    int choice;

    do {
        cout<<"Enter a positive integer A :"<<endl; 
        // how do I use call by reference to ask the user for A and B?
        cin>>A;
        cout<<"Enter a positive integer B :"<<endl;
        cin>>B;
    } while (A<=0 || B<=0 || A>B);

    do {
        cout<<"Choose a computation from the following menu :"
            <<"1- Compute sum of the Factorials of "<<A<<"."
            <<"2- Count all divisors of "<<A<<"."
            <<"3- Compute the sum of all even integers between "<<A<<" and "
            <<B<<"."
            <<"4- Exit the program.";
        cin>>choice;

        switch (choice) {

        case 1 : {
            cout<<"The following program computes the sum of factorials of "
            <<A<<"."<<endl;
            long int sumFactorial = 0; //I want to compute 1! + 2! + 3! .....
            //A! should I put this in main or not?
            for (int K=1; K<=A; K++);
            sumFactorial += factorial (A); // error saying that overloaded
            cout<<"The sum of factorials of "<<A<<" is equal to :"<<sumFactorial
            <<endl;
            break;
        }

        case 2 : {
            cout<<"The following program counts all the divisors of "<<A<<"."
            <<endl;
            int divcount;
            divcount = divCount (A); // It should count all divisors of A just 
            //count them and output the number
            cout<<"The number of divisors of "<<A<<" is equal to : "
            <<divcount<<endl;
            break;
        }

        case 3 : {
            cout<<"The following program computes sum of even integers between "
            <<A<<" and "<<B<<"."<<endl;
            long int evsum = 0; // it should count the even integers between A 
            //and B.
            evSum (A,B,evsum);
            cout<<"The sum of even integers between "<<A<<" and "<<B<<" is equal
            // to :"<<endl;
            break;
        }

        case 4 : {
            cout<<"Exit the program"<<endl;
            break;
        }
        default :
            cout<<"Wrong imput , Please choose a number from the menu."<<endl;
        }
        return 0;
    } while (choice != 4);
}

long int factorial (int a) { // I need to use call by reference in these 
	//function to call the values

    if(a == 0)
        return 1; // I have some error that I can't understand . it's not working
    else
        return (a) * factorial(a - 1);

    int divCount (int a) { // error on this brace

        for (int i=1; i<=a; i++)
            if (a % i == 0)
                divCount++; // same in here I should call the values . 
     //I have error on "divCount" saying local function definitions are illegal.
        return divCount;

        void evSum (int a,int b) {

            for ( int i>=a,i<=b,i++)
                if (i % 2 == 0)
                    int evSum = 0; // I don't know if I should use "evsum" from
                    // main or "evSum"
            evSum += i;
            return evSum;
        }





reading http://www.cplusplus.com/articles/jEywvCM9/
must clearify all unclear aspect of using forum.
Last edited on
hi cooka, seems you are a student and this is a college program.
as i see many simple programming errors in your code i will suggest to you these steps:

1. try to find a talent classmate to study computer programs together not alone.
2. listen to your professor advices.
3. read all tutorial in http://www.cplusplus.com/doc/tutorial/
3. Work Work Work Hard to find and implement code by own

finally your corrected code is here

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
using namespace std;



/// i highly recomment to not use this function declaration as begineer
//~ void readValues (int & ,int &); // 2 values A and B
//~ long int factorial (int ); // A
//~ int divCount (int); // A
//~ void evSum (int &, int &,long int ); // A and B and I should have the 
//long sum


void
evSum (int &a,int &b, long int &c) /// give 3 argument by ref, 
{
	///as c is by ref, calculated evSum assign to c 
	/// c hold it back, not return but hold it as i use call by reference
	
	long int  evSum=0;
	for ( int i=a+1;i<b;i++){
		if (i % 2 == 0){
			evSum += i;
			///cout<<evSum<<" "<<i<<endl; 
		}
	}

/// this last assign lead to c hold true value of evSum by ref
c = evSum;


///cout <<c;
}


///must use call by value as you have factorial(a-1) in orginal code but 
///this method correct it

/// get a by ref and compute factorial (a) and return it.
long int 
factorial (int &a)//
{ 
	int t=a;
	int s = t-1;
	
    if(t == 0)
        return 1; // I have some error that I can't understand . it's not 
        //working
    else
        return (t) * factorial(s);
}


int 
divCount (int &a) ///input a call by ref, return number of divisers 
{
	int divCount=0;
	for (int i=1; i<=a; i++){
		if (a % i == 0){
			divCount++;
		}
	}         
	
	return divCount;
}


/// he want to use call by reference to save memory 
int
main (int argc, char **argv)
{
    int A,B;
    int choice;

    do {
        cout<<"Enter a positive integer A :"; 
        // how do I use call by reference to ask the user for A and B?
        cin>>A;
        cout<<"Enter a positive integer B :";
        cin>>B;
    } while (A<=0 || B<=0 || A>B);//why A>B


    do {
        cout<<"Choose a computation from the following menu :"<<endl
            <<"1- Compute sum of the Factorials of "<<A<<"."<<endl
            <<"2- Count all divisors of "<<A<<endl
            <<"3- Compute the sum of all even integers between "<<A<<" and "
            <<B<<endl
            <<"4- Exit the program."<<endl;
        cin>>choice;

        switch (choice) {

        case 1 : {
            cout<<"The following program computes the sum of factorials of "
            <<A<<endl;
            long int sumFactorial = 0;
            //I want to compute 1! + 2! + 3! .....
            //A! should I put this in main or not?
            
            for (int K=1; K<=A; K++){
				sumFactorial += factorial (K); 
				// error saying that overloaded
            }
            
            cout<<"The sum of factorials of "<<A<<" is equal to :"
            <<sumFactorial
            <<endl;
            break;
        }

        case 2 : {
            cout<<"The following program counts all the divisors of "<<A
            <<endl;
            int divcount;
            divcount = divCount (A); // It should count all divisors of A just 
            //count them and output the number
            cout<<"The number of divisors of "<<A<<" is equal to : "
            <<divcount<<endl;
            break;
        }

        case 3 : {
            cout<<"The following program computes sum of even integers between"
            <<A<<" and "<<B<<"."<<endl;
            long int evsum = 0; // it should count the even integers between A 
            //and B.
            
            evSum (A,B,evsum);
            cout<<"The sum of even integers between "<<A<<" and "<<B<<
            " is equal to :"<<evsum<<endl;
            break;
        }

        case 4 : {
            cout<<"Exit the program"<<endl;
            break;
        }
        default :
            cout<<"Wrong imput , Please choose a number from the menu."<<endl;
        }
        return 0;
    } while (choice != 4);
}





to find how to use call by value/ reference read this link:

http://www.cplusplus.com/doc/tutorial/functions/

feel free to any other questions.


Last edited on
Topic archived. No new replies allowed.