recursion issue.

hi guys, i have some problems regarding to recursion and pass by reference, can someone explain how to apply these things on my current code?(it can apply at least 8 functions other than main(), i suppose? ) i am a real beginner, i always read online but i still cant understand how it really works. hope someone can help me though, thanks.

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
#include <iostream>
using namespace std;

int main()
{
	double temperature[31] = {22.9, 19.7, 18.0, 23.3, 27.1, 18.7, 22.0, 28.1, 25.2, 21.3, 28.8, 27.5, 31.3, 19.2, 21.5, 30.5, 28.6, 19.5, 20.6, 31.3, 21.5, 29.7, 21.0, 20.2, 21.5, 31.9, 30.6, 22.0, 22.2, 20.0, 31.4};
	
	cout << "***************************************************" << endl;
	cout << "Welcome to the Monthly Temperature Analysis Program" << endl;
	cout << "***************************************************" << endl;

	int choice; 
	do
	{
		
		do
		{
			cout << "\nMenu: " << endl;
			cout << "1. Print Daily Temperature for the whole month" << endl;
			cout << "2. Print Daily Temperature of a choosen day" << endl;	
			cout << "3. Change Temperature of a day" << endl;
			cout << "4. Calculate Average Temperature" << endl;
			cout << "5. Find The Hottest and Coldest Day of the Month" << endl;
			cout << "6. Exit Program" << endl;
			cout << "What do you want to do (Choose 1 to 6)? ";
	
			
			cin >> choice; 
			cin.ignore();
		}while(choice < 1 || choice > 6); 

		switch(choice)
		{
			case 1: 
				{
					cout << "Daily Temperature This Month: " << endl;
					for(int i = 0; i < 31; i++)
					{
						cout << "Day " << i+1 << ": " << temperature[i] << " Celsius" << endl;
					}

					break;
				}
			case 2: 
				{	
					int day;
					do
					{
						cout << "Choose the day (1 to 31): ";
						
						cin >> day;
						cin.ignore();
					}while(day < 1 || day > 31);

					cout << "Day " << day << ": " << temperature[day-1] << " Celsius" << endl;
				
					break;
				}
			case 3: 
				{
					int day;
					do
					{
						cout << "Choose the day (1 to 31): ";
						
						cin >> day;
						cin.ignore();
					}while(day < 1 || day > 31);

					cout << "The existing temperature is " << temperature[day-1] << " Celsius. Enter the new temperature: ";
					cin >> temperature[day-1];
					cin.ignore();
				
					break;
				}
			case 4: 
				{
					double total = 0.0;
					for(int i = 0; i < 31; i++)
					{
						total += temperature[i];
					}
					cout << "The average temperature this month is " << total/31.0 << " Celsius" << endl;
					break;
				}
			case 5: 
				{
					int highestDay = 0, lowestDay = 0;
					for(int i = 1; i < 31; i++)
					{
						if(temperature[i] > temperature[highestDay])
						{
							highestDay = i;
						}

						if(temperature[i] < temperature[lowestDay])
						{
							lowestDay = i;
						}
					}
				
					cout << "The hottest day (" << temperature[highestDay] << " Celsius) is on day " << highestDay+1 << " of the month" << endl;
					cout << "The coldest day (" << temperature[lowestDay] << " Celsius) is on day " << lowestDay+1 << " of the month" << endl;
					break;
				}
			default:
				cout << "\nThank you for using the Monthly Temperature Analysis Program. Have a nice day!" << endl;
		}

	}while(choice != 6);

	
	system("pause");
	return 0;
}
Nah, that programm is perfectly fine without recursion but I think you didn't quite understand what recursion means
Mmm... How about applying 8 functions other than main()? And also pass them by reference?
yeah you could do that, that'd make it look easier to read.
yes. i know i can do that, but i dont know how to do that, as im still confusing after reading through all the chapters. so can u explain how other functions and pass by reference been applied on the existing code above?
okey so you know the word recursion but you don't know how to use functions?
That's interesting...

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

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
#include <iostream>
using namespace std;

int choose_item()
{
    int choice;
    do
    {
        cout << "\nMenu: " << endl;
        cout << "1. Print Daily Temperature for the whole month" << endl;
        cout << "2. Print Daily Temperature of a choosen day" << endl;
        cout << "3. Change Temperature of a day" << endl;
        cout << "4. Calculate Average Temperature" << endl;
        cout << "5. Find The Hottest and Coldest Day of the Month" << endl;
        cout << "6. Exit Program" << endl;
        cout << "What do you want to do (Choose 1 to 6)? ";

        cin >> choice;
        cin.ignore();
    }while(choice < 1 || choice > 6);

    return choice;
}

void apply_choice(int choice, double (&temperature)[31])
{
    switch(choice)
    {
    case 1:
    {
        cout << "Daily Temperature This Month: " << endl;
        for(int i = 0; i < 31; i++)
        {
            cout << "Day " << i+1 << ": " << temperature[i] << " Celsius" << endl;
        }
        break;
    }
    case 2:
    {
        int day;
        do
        {
            cout << "Choose the day (1 to 31): ";
            cin >> day;
            cin.ignore();
        } while(day < 1 || day > 31);

        cout << "Day " << day << ": " << temperature[day-1] << " Celsius" << endl;
        break;

    }
    case 3:
    {
        int day;
        do
        {
            cout << "Choose the day (1 to 31): ";
            cin >> day;
            cin.ignore();
        } while(day < 1 || day > 31);

        cout << "The existing temperature is " << temperature[day-1] << " Celsius. Enter the new temperature: ";
        cin >> temperature[day-1];
        cin.ignore();

        break;

    }
    case 4:
    {
        double total = 0.0;
        for(int i = 0; i < 31; i++)
        {
            total += temperature[i];
        }
        cout << "The average temperature this month is " << total/31.0 << " Celsius" << endl;
        break;
    }
    case 5:
    {
        int highestDay = 0, lowestDay = 0;
        for(int i = 1; i < 31; i++)
        {
            if(temperature[i] > temperature[highestDay])
            {
                highestDay = i;
            }
            if(temperature[i] < temperature[lowestDay])
            {
                lowestDay = i;
            }
        }
        cout << "The hottest day (" << temperature[highestDay] << " Celsius) is on day " << highestDay+1 << " of the month" << endl;
        cout << "The coldest day (" << temperature[lowestDay] << " Celsius) is on day " << lowestDay+1 << " of the month" << endl;
        break;
    }
    default:
        cout << "\nThank you for using the Monthly Temperature Analysis Program. Have a nice day!" << endl;
        break;
    }
}

int main()
{
    double temperature[31] = {22.9, 19.7, 18.0, 23.3, 27.1, 18.7, 22.0, 28.1, 25.2, 21.3, 28.8, 27.5, 31.3, 19.2, 21.5, 30.5, 28.6, 19.5, 20.6, 31.3, 21.5, 29.7, 21.0, 20.2, 21.5, 31.9, 30.6, 22.0, 22.2, 20.0, 31.4};

    cout << "***************************************************" << endl;
    cout << "Welcome to the Monthly Temperature Analysis Program" << endl;
    cout << "***************************************************" << endl;

    int choice;
    do
    {
        choice = choose_item();
        apply_choice(choice, temperature);
    } while(choice != 6);

    system("pause");
    return 0;
}
in what condition i need to do address(&)? also, why there is a [31] after &temperature?

1
2
3
4
void apply_choice(int choice, double (&temperature)[31])
{
 /* content */
}
the and-symbol in variable decleration states that this is a reference.
In this example temperature is a reference to an array with 31 doubles
noted. thank u so much, Gamer.
To understand recursion, you must first understand recursion:

1
2
3
4
5
6
//returns -1 every time... but not before doing fibbonacci!
inline int fibber(const unsigned long& in1, const unsigend long& in2)
{
    if((in1 + in2) < in2) return -1; //god forbid we go past std::numeric_limits<int>::max!!!
    return fibber(in2, (in1 + in2));
}
Last edited on
Topic archived. No new replies allowed.