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("temp.data", "r")== NULL)
{
printf("The file \"temp.data\" either does not exist or could not be
found.\n"
return EXIT_FAILURE;
}
else
{
number_of_temps = read_data( temps, infile );
printf("There are %d tempatures in all.\n\n", 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, "%s%d", 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[] = "Temperature Analysis";
banner_length = strlen(banner);
printf("%s\n", banner);
for( j = 0; j < banner_length; j++)
putchar('-');
printf("\n"
}
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
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("temp.data", "r")== NULL)
{
printf("The file \"temp.data\" either does not exist or could not be
found.\n"
return EXIT_FAILURE;
}
else
{
number_of_temps = read_data( temps, infile );
printf("There are %d tempatures in all.\n\n", 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, "%s%d", 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[] = "Temperature Analysis";
banner_length = strlen(banner);
printf("%s\n", banner);
for( j = 0; j < banner_length; j++)
putchar('-');
printf("\n"
}
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