Write a program that can be used by a ski resort to keep track of local snow conditions for one week. It should have a seven-element array of class objects, where each object holds a date and the number of inches of snow in the base on that date. Class UML diagram is as follows:
I am having a hard time understanding why the following loops are not working.
/*
Write down the for loop to print out elements from array
RANGE-BASED FOR LOOP CANNOT BE USED!
*/
-------------------------------------------------------------------
/*
write code to iterate the array and add up base depth
from each individual array element
RANGE-BASED FOR LOOP CANNOT BE USED!
*/
--------------------------------------------------------------------
/*
Write down sort code to sort by base depth of each element in the
array. Use the getBase_depth() function of each array element
*/
----------------------------------------------------------------------
/*
Write down sort code to sort by date of each element in the
array. Use the getSnow_date() function of each array element
*/
-----------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
SnowData.h - No changes needed to this file
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*************
SnowData specification file
**************/
class SnowData
{
private:
string snow_date;
double base_depth;
public:
SnowData();
SnowData(string _date, double _inches);
void print();
string getSnow_date();
double getBase_depth();
void setBase_depth(double);
void setSnowDate(string);
};
| |
----------------------------------------------------------------
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
|
SnowData.cpp - (No changes needed)
#include "SnowData.h"
/*******************
Class default constructor
Sets default values for class private variables
********************/
SnowData::SnowData()
{
snow_date = "";
base_depth = 0;
}
/*
Overloaded constructor
Parameters used to populate class private variables via set functions
*/
SnowData::SnowData(string _date, double _inches)
{
setSnowDate(_date);
base_depth = 0;
setBase_depth(_inches);
}
/*
print functions
prints out class private variables
*/
void SnowData::print()
{
cout << setw(15) << left << snow_date
<< setw(5) << fixed << showpoint << setprecision(2) << right
<< base_depth << endl;
}
/*
accessor function for snow_date
*/
string SnowData::getSnow_date()
{
return snow_date;
}
/*
accessor function for base_depth
*/
double SnowData::getBase_depth()
{
return base_depth;
}
/*
mutator function for base_depth.
ensures that base_depth is not set to a negative value
*/
void SnowData::setBase_depth(double _inches)
{
if (_inches >= 0)
base_depth = _inches;
}
/*
mutator function for snow_date
*/
void SnowData::setSnowDate(string _date)
{
snow_date = _date;
}
| |
---------------------------------------------------------------------
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
|
Class_tester_INCOMPLETE_tester.cpp
#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] = { "Jan 15", "Jan 16", "Jan 17", "Jan 18", "Jan 19", "Jan 20", "Jan 21" };
double base_depth[7] = { 34.5, 23.6, 25.5, 31.5, 40.6, 30.9, 38.4 };
SnowData jan_snow[7];
int i = 0;
for (auto &one_snow_day : jan_snow)
{
one_snow_day.setSnowDate(dates[i]);
one_snow_day.setBase_depth(base_depth[i]);
i++;
}
cout << setprecision(2) << fixed << showpoint;
cout << " --- array after set functions invoked to populate array --\n";
print_array_elements(jan_snow, 7);
cout << "Average base depth for the period "
<< jan_snow[0].getSnow_date() << " - "
<< jan_snow[6].getSnow_date() << " : "
<< get_average_base_depth(jan_snow, 7) << endl;
sort_by_base_depth(jan_snow, 7);
cout << " --- array after sort by base_depth --\n";
print_array_elements(jan_snow, 7);
sort_by_date(jan_snow, 7);
cout << " --- array after sort by date --\n";
print_array_elements(jan_snow, 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
*/
}
| |
Not sure if the following loops are correct, which is why if someone could show me the correct way to code. I am having a hard time understanding arrays for this certain program.
---------------------------------------
This is what i wrote for the get_Average_base)depth
/*
write code to iterate the array and add up base depth
from each individual array element
RANGE-BASED FOR LOOP CANNOT BE USED!
*/
1 2 3 4 5 6 7 8 9 10
|
double get_average_base_depth(SnowData _array[], int size)
{
double total_depth = 0; //Initialize Accumulator
for (int i = 0; i < 7; i++)
{
total_depth += i++;
}
return total_depth / 7;
}
| |
---------------------------------
This is what i wrote to print_array_elements.
/*
Write down the for Loop to print out elements from array
RANGE-BASED FOR LOOP CANNOT BE USED!
*/
1 2 3 4 5 6 7
|
void print_array_elements(SnowData _array[], int size)
{
for (int i = 0; i < size; i++)
cout << &_array[i] << " ";
cout << endl;
}
| |
------------------------------------
I'm having issues with
void sort_by_base_depth(SnowData _array[], int size)
and
void sort_by_date(SnowData _array[], int size)
---------------------------------------------------------------------------------------
//This is what i have for sorting based on base depth.
1 2 3 4 5 6
|
void sort_by_base_depth(SnowData _array[], int size)
{
for (int passN = 1; passN < (size-1) ; passN++)
for (int subs = 0; subs < (size - 2); subs++)
if ( base_depth[passN] > base_depth[passN + 1])
swap( _array[subs], _array[subs + 1]);
| |
/*
Write down sort code to sort by base depth of each element in the array.
Use the getBase_depth() function of each array element
*/
}