having trouble displaying the program

i have a text file with a list of numbers and dates and i want to find the smallest (the numbers are not in order) and display the date

heres what i have so far

1
2
3
4
5
6
7
double smallest;
smallest = 0;
					
	for (int i = 1; i < 30; i++)
         if (smallest > report[i].sunshine)
	smallest = report[i].sunshine;
	cout << "Smallest is " << smallest << endl;


I think the line smallest is = 0 is wrong because it seems to just be outputing that line but it says i need to initialize smallest so whats wrong with the code?

thanks
Last edited on
Set smallest to a large number (higher than anything will ever be in report[x].sunshine), then try again.
Last edited on
closed account (zb0S216C)
It's simple enough:

1
2
3
4
5
double Smallest( report[0].sunshine );

for( int I( 0 ); I < 30; ++I )
    if( report[I].sunshine < Smallest )
        Smallest = report[I].sunshine;


Wazzak
Last edited on
ok thanks its displaying the smallest number but its not displaying the date next to it

1
2
3
4
5
6
7
8
9
10
11
12
13
double Smallest( report[0].sunshine );
					
	for( int i = 0 ; i < 30; ++i)
	{
		if( report[i].sunshine < Smallest )
		{
		         Smallest = report[i].sunshine;
		}
		infile >> date[i].day >> date[i].month >> date[i].year;
		cout << date[i].day << "/" << date[i].month << "/" << date[i].year 
		<< " with smallest hours of  " << Smallest << endl;
						
	}


it works fine til i add the code where i want to display the date next to it then
it displays all 30 dates instead of the 1 date with the smallest number
Last edited on
Ahhh. So what you should be keeping track of is "i". Something like this:

Note: this is off the top of my head, so may contain typos.

1
2
3
4
5
6
7
8
9
10
int index = 0;

for(int i = 0; i < 30; i++) {
    if (report[i].sunshine < report[index].sunshine) {
        index = i;
    }
}
infile >> date[index].day >> date[index].month >> date[index].year;
cout << date[index].day << "/" << date[index].month << "/" << date[index].year
    << " with smallest hours of " << report[index].sunshine;


So now you're not keeping track of just the "sunshine" number, but which "report" object contains the "sunshine" number. I hope this helps.
Last edited on
it works thanks
Topic archived. No new replies allowed.