Dunno what my professor means...

I have a completed RainFall Statistics Mod program that uses a linked list (but in this case I called it RainFall). When my professor saw it, he said its wrong. He mentioned its just suppose to hold data, and something about main going to get the data, and no calculations. What exactly is he talking about?

Here's my complete code.

Header file.
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
#ifndef RAINFALL_H
#define RAINFALL_H

#include <iostream>
using namespace std;

class RainFall
{
   private:
	struct ListNode
	{
	   int value;
	   ListNode* next;
	};

	ListNode* head;

   public:
	RainFall()
	{ 
	   head = NULL; 
	}

	void appendNode(int num);
	void displayList() const;
	int getHighestRainfall() const;
	int getLowestRainfall() const;
	int getTotal() const;
};
#endif 

Functions cpp file.
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
#include "RainFall.h"

int RainFall::getLowestRainfall() const
{
	ListNode* nodePtr;
	nodePtr = head;
	int lowest = nodePtr -> value;

	while(nodePtr)
	{
		if(nodePtr -> value < lowest)
			lowest = nodePtr -> value;
		nodePtr = nodePtr -> next;
	}

	return lowest;
}

int RainFall::getHighestRainfall() const
{
	ListNode* nodePtr;
	nodePtr = head;
	int highest = nodePtr -> value;

	while(nodePtr)
	{
		if(nodePtr -> value > highest)
			highest = nodePtr -> value;
		nodePtr = nodePtr -> next;
	}

	return highest;
}

int RainFall::getTotal() const
{
	ListNode* nodePtr;
	nodePtr = head;
	int total = 0;

	while(nodePtr)
	{
		total += nodePtr -> value;
		nodePtr = nodePtr -> next;
	}

	return total;
}

void RainFall::appendNode(int num)
{
	ListNode* newNode;
	ListNode* nodePtr;

	newNode = new ListNode;
	newNode -> value = num;
	newNode -> next = NULL;

	if(!head)
		head = newNode;
	else
	{
		nodePtr = head;

		while(nodePtr -> next)
			nodePtr = nodePtr -> next;
		nodePtr -> next = newNode;
	}
}

void RainFall::displayList() const
{
	ListNode* nodePtr;
	nodePtr = head;

	while(nodePtr)
	{
	   cout << "Element " << nodePtr -> value;
	   nodePtr = nodePtr -> next;
	}
}


main file.
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
#include "RainFall.h"
#include <string>
#include <iomanip>

int main(void)
{
	const int NUM_MONTHS = 12;
	int rain;
	int numMonths;
	RainFall Rain;

	string months[NUM_MONTHS] = { "January", "February", "March",
				      "April", "May", "June", "July",
				      "August", "September", "October",
				      "November", "December" };

	cout << "\nEnter Number of Months: ";
	cin >> numMonths;

	for(int month = 1; month <= numMonths; month++)
	{
		do
		{
		  cout << "\nEnter Total Rainfall For: " << months[month - 1];
		  cin >> rain;

		}while(rain < 0);

		Rain.appendNode(rain);
	}

	cout << setw(35) << "\nTotal Rainfall Is: " << setw(5) << Rain.getTotal();
	cout << setw(35) << "\nThe Average Rainfall Is: " << setw(5) << Rain.getTotal() / (double)numMonths;
	cout << setw(35) << "\nHighest Rainfall Is: " << setw(5) << Rain.getHighestRainfall();
	cout << setw(35) << "\nLowest Rainfall Is: " << setw(5) << Rain.getLowestRainfall();
}
Maybe you should go back to him and let him explain clearly what he meant because this code looks like it does what it was meant to do; and by that I mean what you specified it does.
Where do u input data?
closed account (Dy7SLyTq)
@terry: line 18 and 25

and i cannot even begin to write a linked list, so i have no idea if i am correct in saying this. so plz correct me if im wrong. so from what i understand, aren't linked lists just supposed to the next value? not hold input data
Your professor means that your linked list class should not be concerned with how it is used. It should not have any functions to calculate rainfall, etc, because it should literally just be a linked list.

Would it make sense if your locker did your homework for you? It would be awesome, but it doesn't make sense. What if it didn't do it the way you would? Your locker is for holding things, not for doing your work.
Last edited on
@Smac: He explained a number of times but for some reason I just don't understand the way he wants it. I recall now him saying he should be able to take my code and use it for grades, and the way I have my code set up he can't do that. He sure has confused the hell outta me.

@L B: What locker are you talking about?
@BinaryGeek the metaphorical locker that I used in my analogy in the hopes you would see what it was analogous to.
@L B: I see..I dunno I figure on just putting comments in and comment blocks and just submit it, the hell with it.
Consider the following using std::list.

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
#include <list>
#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>

int sum(const std::list<int>& list)
{
    int sum = 0 ;
    for ( auto element : list )
        sum += element ;
    return sum ;
}

int average(const std::list<int>& list)
{
    return sum(list) / list.size() ;
}

int max( const std::list<int>& list)
{
    int highest = list.front() ;

    for ( auto element : list )
        if ( element > highest )
            highest = element ;

    return highest ;
}

int min( const std::list<int>& list)
{
    int lowest = list.front() ;

    for ( auto element : list )
        if ( element < lowest )
            lowest = element ;

    return lowest ;
}

int main()
{
    using std::string ;
    using std::cout ;
    using std::cin ;
    using std::setw ;

    const int NUM_MONTHS = 12;
    int rain;
    int numMonths;


    string months[NUM_MONTHS] = { "January", "February", "March",
        "April", "May", "June", "July",
        "August", "September", "October",
        "November", "December" };

    cout << "\nEnter Number of Months: ";
    cin >> numMonths;

    std::list<int> rainfallAmounts ;

    for(int month = 1; month <= numMonths; month++)
    {
        int rainfallAmount ;
        do
        {
            cout << "\nEnter Total Rainfall For: " << months[month - 1];
            cin >> rainfallAmount ;

        }while(rainfallAmount < 0);

        rainfallAmounts.push_back(rainfallAmount);
    }

    cout << setw(35) << "\nTotal Rainfall Is: " << setw(5) << sum(rainfallAmounts);
    cout << setw(35) << "\nThe Average Rainfall Is: " << setw(5) << average(rainfallAmounts) ;
    cout << setw(35) << "\nHighest Rainfall Is: " << setw(5) << max(rainfallAmounts);
    cout << setw(35) << "\nLowest Rainfall Is: " << setw(5) << min(rainfallAmounts);
}


See how the algorithms for the calculations are not a part of the list? See how they're also not connected to Rainfall? You instructor wants you to incorporate this separation of responsibility in your implementation of the linked list.

All you really need to do is stuff the functionality for the 4 calculations into separate functions and rename your RainFall class to something more appropriate (like List, perhaps.)
Last edited on
So, this program collects data and displays it in total, average, highest and lowest? Here is what I think:

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

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    int rainfall[16];
    int data;
    char* month_name[16];
    int total_rain = 0;
    int average_rain = 0;
    int highest_rain = 0;
    int lowest_rain = 0;

    month_name[1] = "January";
    month_name[2] = "February";
    month_name[3] = "March";
    month_name[4] = "April";
    month_name[5] = "May";
    month_name[6] = "June";
    month_name[7] = "July";
    month_name[8] = "August";
    month_name[9] = "September";
    month_name[10] = "October";
    month_name[11] = "November";
    month_name[12] = "December";

    cout << "This program collects rainfall data of 1 year and shows" << endl;
    cout << "the total, avarage, highest, and lowest rainfall." << endl;
    cout << "Enter rainfall in inches." << endl << endl;

    for(int month = 1;month < 13;month++)
    {
        cout << "Enter amount of rainfall for " << month_name[month] << ": ";
        cin >> data;
        total_rain += data;
        rainfall[month] = data;
    }

    average_rain = total_rain / 12;

    cout << endl << "Here is your data set: " << endl << endl;

    for(int relay = 1; relay < 13; relay++)
    {
        cout << "The amount of rainfall for month " << month_name[relay] << " is: " << rainfall[relay] << " inches" << endl;
    }

    int max = 0;
    int min = 9999;
    int max_counter = 0;
    int min_counter = 0;
    int key = 0;

    range:
    for(int relay2 = 1;relay2 < 13;relay2++)
    {
        if(key == 1 && rainfall[relay2] == max)
        {
            max = rainfall[relay2];
            max_counter++;
        }

        if(rainfall[relay2] > max)
        {
            max = rainfall[relay2];
        }
    }

    for(int relay3 = 1;relay3 < 13;relay3++)
    {
        if(key == 1 && rainfall[relay3] == min)
        {
            min = rainfall[relay3];
            min_counter++;
        }

        if(rainfall[relay3] < min)
        {
            min = rainfall[relay3];
        }
    }

    if(key == 0)
    {
        key = 1;
        goto range;
    }


    highest_rain = max;
    lowest_rain = min;

    cout << endl;
    cout << "The total amount of rainfall for the year is: " << total_rain << " inches." << endl;
    cout << "The avarage amount of rainfall for the year is: " << average_rain << " inches." << endl;
    if(max_counter == 1)
    {
        cout << "The highest amount of rainfall was in the month of " << month_name[max] << " with " << highest_rain << " inches." << endl;
    }
    else
    {
        cout << "The highest amount of rainfall was in many months with a record of: " << highest_rain << " inches." << endl;
    }
    if(min_counter == 1)
    {
        cout << "The lowest amount of rainfall was in the month of " << month_name[min] << " with " << lowest_rain << " inches." << endl;
    }
    else
    {
        cout << "The lowest amount of rainfall was in many months with a record of: " << lowest_rain << " inches." << endl;
    }

    system("PAUSE");
    return 0;
}


Please correct me if i'm wrong.
Did you just completely ditch the linked list all together? I thought that was crucial to your assignment?
I'm going to try your method Cire, if it works out I greatly appreciate it.

Update: Couldn't get it to work, I failed to mention I'm unfamiliar with #include <algorithm> and #include <list>
Last edited on
If your assignment requires you to create your own linked list, then cire's code is not for you.
BinaryGeek wrote:
Update: Couldn't get it to work, I failed to mention I'm unfamiliar with #include <algorithm> and #include <list>

I think it's probably more a case of your compiler not being C++11 compliant.
I'm going to try your method Cire, if it works out I greatly appreciate it.


The code I presented was just intended to show you how the different elements fit together. Obviously, there's no need for you to use std::list as you're rolling your own. <algorithm> isn't needed; I'm not sure why I included it. The only C++11 code there is the range-based for loops, and those should be easy enough to replace with "normal" ones.
If you wanted to do link my program. Just break it down into functions, then libraries.
Topic archived. No new replies allowed.