Please Help! Sorting by date and depth [C++]

Hello world! I am having trouble with my following program c++ to sort these two functions…
sort_by_base_depth(SnowData _array[], int size)
sort_by_date(SnowData _array[], int size)
These are in my class_tester_snowdata.cpp file (in comment blocks)

(SnowData.h) (COMPLETED)

#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);
};

(SnowData.cpp) (COMPLETED)

#include "SnowData.h"
#include <iomanip>
/*
		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 functions 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;
}

Class_Tester.cpp (NOT FINISHED)

#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; //Initialize Accumulator

		for (int i = 0; i < 7; i++)
		{
			total_depth += i++;
		}
		return total_depth / 7;
	/*
	write code to iterate the array and add up base depth
	from each individual array element
	RANGE-BASED FOR LOOP CANNOT BE USED!
	*/
}
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!
	*/
	for (int index = 0; index < size; index++)
		cout << _array << index+1 << "   " ;
	cout << endl;
}
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
      */
}

Cool problem! Fingers crossed we get some snow here in CA soon!

Do you have a choice which sorting method you can use? For simplicity you might want to do a bubble sort or selection sort. These carry O(n^2) complexity and are not as efficient as merge or quick-sort but easier to implement.

Below’s a selection sort implementation for the 1st part sort_by_base_depth. The 1 gotcha is that you have to use the class method getBase_depth() inside the function. You can adapt this method for the 2nd one.

Spoiler Solution
void sort_by_base_depth(SnowData _array[], int size) {
    SnowData temp;

    for (int i = 0; i < size; i++) {
    
        int minPos = i;
        int startDepth = _array[i].getBase_depth();
    
        for (int j = 0; j < size; j++) {
        
            int currDepth = _array[j].getBase_depth();
        
            if (currDepth < startDepth) {
                minPos = j;
            }
        }
    
        if (minPos != i) {
            temp = _array[i];
            _array[i] = _array[minPos];
            _array[minPos] = temp;
        }
    }
}

On your other methods, make sure you are using the size argument you pass into your function for range checking instead of the integer 7. Although we know the value is 7 from the initialization, what if we add data to our dataset? The function in it’s current implementation wouldn’t account for the new values.

One suggestion if you were writing this from scratch is to use the vector data structure since that would give you a flexible container with useful methods for your snow data (similar to a Javascript array). You wouldn’t have to pass in the array size and you can use range-based for loops for example. #include <vector> vector<SnowData> data;

Hope this helps!

2 Likes