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?
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.
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.
@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.
#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 ;
constint 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.)
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.