Please I need help very badly!!!

I need help with my programming homework I been having problems
with for loops with arrays can someone give me examples the details
are located in the class tester. I been trying to figure it out but I cant
do it I just need to be guide in the right direction so i can learned how to do
this next time by myself.



snow data.h



#pragma once
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*
SnowData specification file
*/
class SnowData
{
private:
string date;
double depth;
public:
SnowData(string _date = "2000/01/01", double _inches = 0);
void print();
string getDate();
double getDepth();
void setDepth(double);
void setDate(string);
};

SnowData.cpp


#include "SnowData.h"

/*
Overloaded constructor
Parameters used to populate class private variables via set functions
*/
SnowData::SnowData(string _date, double _inches)
{
setDate(_date);
depth = 0;
setDepth(_inches);
}
/*
print functions
prints out class private variables
*/
void SnowData::print()
{
cout << setw(15) << left << date
<< setw(5) << fixed << showpoint << setprecision(2) << right
<< depth << endl;
}
/*
accessor function for snow_date
*/
string SnowData::getDate()
{
return date;
}
/*
accessor function for base_depth

*/
double SnowData::getDepth()
{
return depth;
}
/*
mutator function for base_depth.
ensures that base_depth is not set to a negative value
*/
void SnowData::setDepth(double _inches)
{
if (_inches >= 0)
depth = _inches;
}
/*
mutator function for snow_date

*/
void SnowData::setDate(string _date)
{
date = _date;
}



->_class tester_<-

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "SnowData.h"

void print_array_elements(SnowData[], int);
void sort_by_base_depth(SnowData[], int);
void sort_by_date(SnowData[], int);
double get_average_base_depth(SnowData[], int);
int main()
{
string dates[7] = { "2016/01/15","2016/01/16","2016/01/17","2016/01/18",
"2016/01/19" , "2016/01/20" , "2016/01/21" };
double base_depth[7] = { 34.5, 23.6, 25.5, 31.5, 40.6, 30.9, 38.4 };

SnowData snow_days[7];
int i = 0;
for (auto &one_snow_day : snow_days)
{
one_snow_day.setDate(dates[i]);
one_snow_day.setDepth(base_depth[i]);
i++;
}
cout << setprecision(2) << fixed << showpoint;

cout << " --- array after set functions invoked to populate array --\n";
print_array_elements(snow_days, 7);

cout << "Average base depth for the period "
<< snow_days[0].getDate() << " - "
<< snow_days[6].getDate() << " : "
<< get_average_base_depth(snow_days, 7) << endl;

sort_by_base_depth(snow_days, 7);
cout << " --- array after sort by base_depth --\n";
print_array_elements(snow_days, 7);

sort_by_date(snow_days, 7);
cout << " --- array after sort by date --\n";
print_array_elements(snow_days, 7);

return 0;
}
double get_average_base_depth(SnowData _array[], int size)
{
double total_depth = 0;
/*
write code to iterate the array and add up base depth
from each individual array element
RANGE-BASED FOR LOOP CANNOT BE USED!
*/
return total_depth / 7;
}
void print_array_elements(SnowData _array[], int size)
{
/*
Write down the for loop to print out elements from array
RANGE-BASED FOR LOOP CANNOT BE USED!
*/
}

void sort_by_base_depth(SnowData _array[], int size)
{
/*
Write down sort code to sort by base depth of each element in the
array. Use the getBase_depth() function of each array element
*/
}
void sort_by_date(SnowData _array[], int size)
{
/*
Write down sort code to sort by date of each element in the
array. Use the getSnow_date() function of each array element
*/

}
Hi, JulianV304.

Have you already read here?
http://www.cplusplus.com/forum/beginner/1/

You are at least expected to:
Use meaningful, specific subject headers
The subject header is your golden opportunity to attract qualified experts' attention. Don't waste it on babble like 'Please help me' Don't try to impress us with the depth of your anguish; use the space for a super-concise problem description instead.
[...]
•Describe the symptoms of your problem carefully and clearly.
•Describe the environment in which it occurs (machine, OS, application, whatever).
•Describe the research you did to try and understand the problem before you asked the question.
•Describe the diagnostic steps you took to try and pin down the problem yourself before you asked the question.


Also: there’s a menu on the right that allow you to include your code into "code" tags, which make it human-readable. What about using it?

So, what about editing your post to make it more ‘appealing’?

About your code, at first glance there are a couple of things that looks weird – to soon to say if they are the source of your (not described) troubles.
Here, in main():
SnowData snow_days[7];
Are you aware that you are *not* creating any instance of SnowData, are you? You are just reserving space for future instances.
I’m asking it because immediately later you access that array trying to modify elements which are not yet there:
1
2
one_snow_day.setDate(dates[i]);
one_snow_day.setDepth(base_depth[i]);


Another thing is, later you state that RANGE-BASED FOR LOOP CANNOT BE USED!, but previously you write: for (auto &one_snow_day : snow_days).
Isn’t that a range-based for?

Granted that you are clearer with your questions, someone could come and give a real help.
Good luck!
Another thing is, later you state that RANGE-BASED FOR LOOP CANNOT BE USED!, but previously you write: for (auto &one_snow_day : snow_days).
Isn’t that a range-based for?

Perhaps it would be clearer if that message read "RANGE-BASED FOR LOOP WILL NOT WORK HERE!" since ranged based loops only work with arrays when the definition is in scope. You loose the ability to use ranged based for() loops when you pass the array to a function. IMO, the solution would be to use std::vector or std::array instead of the "raw" array.

Hello JulianV304,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Make some attempt to do something. Most people here will not do the work for you, but will explain what is wrong like:

1
2
3
4
5
6
for (auto &one_snow_day : snow_days)
{
one_snow_day.setDate(dates[i]);
one_snow_day.setDepth(base_depth[i]);
i++;
}


Starts as a range for loop, but the guts are trying to work as a regular for loop. Although one_snow_day seems to be a variable it is only one variable. Every time you iterate through the loop the information in one_snow_day is overwritten because it is not an array of any type. Also if your intention was to fill snow_days array you missed.

Take a look at http://www.cplusplus.com/doc/tutorial/control/#for to help you out.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.