Connecting Array with Date

My program starts with user entering rainfall amount.
Then sorting the array in descending order a using selection sort algorithm.
Then displaying the result
Then use a binary search algorithm to find a rainfall amount entered by a user

My question is how do you connect the user's input rainfall with the month associated? And how do I find the rainfall amount entered by the user?

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
#include <iostream>
#include <string>
using namespace std;

struct Data
{
	int month;
	double rain;
};


void inputData(Data sky[]);
void sortRainfall(Data water[]);
void displayData(Data liquid[]);
void searchRainfall(Data notsolid[]);

int main()
{
	const int rainfall = 12;
	Data bar[rainfall];
	inputData(bar);
	return 0;
}

void inputData(Data sky[]) 
{
	cout << "Enter the rainfall (in inches) for January: ";
	cin >> sky[0].rain;
	cout << "Enter the rainfall (in inches) for February: ";
	cin >> sky[1].rain;
	cout << "Enter the rainfall (in inches) for March: ";
	cin >> sky[2].rain;
	cout << "Enter the rainfall (in inches) for April: ";
	cin >> sky[3].rain;
	cout << "Enter the rainfall (in inches) for May: ";
	cin >> sky[4].rain;
	cout << "Enter the rainfall (in inches) for June: ";
	cin >> sky[5].rain;
	cout << "Enter the rainfall (in inches) for July: ";
	cin >> sky[6].rain;
	cout << "Enter the rainfall (in inches) for August: ";
	cin >> sky[7].rain;
	cout << "Enter the rainfall (in inches) for September: ";
	cin >> sky[8].rain;
	cout << "Enter the rainfall (in inches) for October: ";
	cin >> sky[9].rain;
	cout << "Enter the rainfall (in inches) for November: ";
	cin >> sky[10].rain;
	cout << "Enter the rainfall (in inches) for December: ";
	cin >> sky[11].rain;

	sortRainfall(sky); 
	searchRainfall(sky);
}

void sortRainfall(Data water[])
{
	int i, j, minIndex, tmp;
	for (i = 0; i < 12 - 1; i++) 
	{
		minIndex = i;
		for (j = i + 1; j < 12; j++)
			if (water[j].rain < water[minIndex].rain)
				minIndex = j;
		if (minIndex != i) 
		{
			tmp = water[i].rain;
			water[i] = water[minIndex];
			water[minIndex].rain = tmp;
		}
	}
	displayData(water); 
}

void displayData(Data liquid[]) 
{
	cout << "Monthly Rainfall Totals" << endl;
      
         
}

void searchRainfall(Data notsolid[]) 
{
	int find;
	cout << "Enter search for rainfall: ";
	cin >> find;

        
        cout << "Found record of " << find << " rainfall during " << month << endl;
}
Last edited on
1. What is the purpose of line 7?

2. Why do you swap only rain rather than entire Data objects on lines 67-69?

3. Find the index of the Data object, whose member rain equals the user given value.
* Why rain is in double, but user gives an int?
* Assume that user can give a value that does not exist in the array, i.e. search can return "invalid index".
* There is no easy "equals" for doubles.
if (minIndex != i)
{
tmp = water[i].rain;
water[i] = water[minIndex];
water[minIndex].rain = tmp;
}


This is called "cooking the books", you're adjusting the monthly rainfall instead of sorting it.

1
2
3
4
5
6
if (minIndex != i) 
		{
			Data tmpData = water[i];
			water[i] = water[minIndex];
			water[minIndex] = tmpData;
		}
Topic archived. No new replies allowed.