Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array problem.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hey guys, here's a newbie question.

I have this program that I'm working on where I take a file and then use the scan function to read the file then I fill it into an array called temps (since the integar values happen to be temperature values). All of this are working perfectly.

My problem is trying to keep track of how many temperature are in each division (meaning, in the 90s, 80s, 70s, etc.) and printing how many of that are in each divisions.

If it'll make things easier, here's my program (sorry I don't have comments in it).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DATE_SIZE 9
#define TEMP_SIZE 62

int read_data( int *temps, FILE *infile );
void selection_sort( int *temps, int number );
void analyze_data( int *temps, int number_of_temps );

int main()
{
int number_of_temps;
int temps[TEMP_SIZE];
FILE *infile;

if((infile = fopen(&quot;temp.data&quot;, &quot;r&quot;))== NULL)
{
printf(&quot;The file \&quot;temp.data\&quot; either does not exist or could not be
found.\n&quot;);
return EXIT_FAILURE;
}

else
{
number_of_temps = read_data( temps, infile );
printf(&quot;There are %d tempatures in all.\n\n&quot;, number_of_temps);

selection_sort( temps, number_of_temps );

analyze_data( temps, number_of_temps );

return EXIT_SUCCESS;
}
}

int read_data( int *temps, FILE *infile )
{
int count = 0;


int new_temps;
char date[DATE_SIZE];

while( fscanf( infile, &quot;%s%d&quot;, date, &new_temps ) == 2 )
{
temps[count] = new_temps;
count++;
}

void selection_sort( int *temps, int number )
{
int selected_index;
int maximum_index;
int j;
int exchange;
for( selected_index = 0; selected_index < number - 1; selected_index++ )
{
maximum_index = selected_index;

for( j = selected_index + 1; j < number; j++ )

if( temps[j] > temps[maximum_index] )
maximum_index = j;

if( maximum_index != selected_index )
{
exchange = temps[selected_index];
temps[selected_index] = temps[maximum_index];
temps[maximum_index] = exchange;
}

}
}
void analyze_data(int *temps, int number_of_temps)
{
int banner_length;
char banner[] = &quot;Temperature Analysis&quot;;

banner_length = strlen(banner);
printf(&quot;%s\n&quot;, banner);

for( j = 0; j < banner_length; j++)
putchar('-');
printf(&quot;\n&quot;);
}

That's what I have so far. I'm hoping someone can give me clues or anything that'll help me. All I'm asking is how can I get something to keep track of how many temperatures there are in each division. The data file that I have has 61 temperature values.


Oh I forgot. The program that I'm working on is supposed to have an output like this:


Temperature Analysis
--------------------

100's+: 0 (temperature of 100 or above and how many of that are in that division)
90's: 5
80's: 2
70's: 4
60's: 5


No 50's or below. Hope that helps.

Newbieonarray
 
Hi there,

after storing temperaturs try to sort the array using any sorting algorithm like bubble sort etc and then use binary search method to find the no of temperatures in desire range.

I hope you understand.

Nilux #include<NoOneLivesForever>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top