Click here to Skip to main content
14,216,449 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have 12 numbers in a file. The numbers are for Janaruar, Febuary, March, and so on. I plan to use an array and I can scan the numbers in and find the average, sum, highest, lowest, and so on but I need to have the program display, for example, "highest number was Janaury with 123". not just display 123 as the highest number. Also my program is not display the data in order. But everything else is working.

What I have tried:

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

#define SIZE 12
#define FREQ_SIZE 101

void printData (const float values[], int num_values);
float findLargest (const float values[], int num_values);
float findSmallest (const float values[], int num_values);
void findTotAvg (const float values[], int num_values, float * total, float * average);
void makeFreq (const float values[], int num_values, int freq_ary[], int freq_size);
int readData (float values[], int max_size);
int findMode (const int freq_ary[], int freq_size);
void bubbleSort(float values[], int num_values);

int main ()
{
	float values[SIZE];
	int freq_ary[FREQ_SIZE]={0};
	float total, average, largest, smallest, median;
	int num_values, mode;
	int i;


	// read the data from a file
	num_values = readData (values, SIZE);

	// print the values
	//printData (values, num_values);

	// find the total and average of the values
	findTotAvg (values, num_values, &total, &average);

	// find the largest value
	findLargest (values, SIZE);
	largest = findLargest (values, SIZE);

	// find the smallest value
	findSmallest (values, SIZE);
	smallest = findSmallest (values, SIZE);

	// find the mode
	mode = findMode(freq_ary, FREQ_SIZE);

	bubbleSort(values,num_values);

	if(num_values % 2 == 0)
	{//Even number of scores
		median = (values[num_values/2] + values[num_values/2+1])/2;
	}
	else
	{//Odd number of scores
		median = values[num_values/2+1];
	}

	printf ("Total: %5.1f\n", total);
	printf ("Average: %5.1f\n", average);
	printf ("Largest: %5.1f\n", largest);
	printf ("Smallest: %5.1f\n", smallest);
	printf ("Median: %5.1f\n", median);
	printf ("\n\nSorted marks : \n\n");
	printf (values,num_values);
	return 0;
}

int readData (float values[], int max_size)
{
	FILE * inFile;
	int num_values = 0;
	int result;
	float temp;

	inFile = fopen ("C:\\Users\\Windows User\\Desktop\\dogs.txt", "r");
	if (!inFile)
	{
		printf ("Error opening data file!\n");
		return 100;
	}

	// read the data from the file
	num_values = 0;
	result = fscanf(inFile, "%f", &temp);
	while (result == 1 && num_values <= max_size && !feof(inFile))
	{
		if(temp >=0.0 && temp <= 100.0) values[num_values] = temp;
		num_values++;
		result = fscanf(inFile, "%f", &temp);
	}
	return num_values;
	fclose(inFile);
}


// print the values
void printData (const float values[], int num_values)
{
	int i;

	for (i=0; i < num_values; ++i)
	{
		printf ("%f\n", values[i]);
	}
	printf("\n\n");
}


float findLargest (const float values[], int num_values)
{
	float largest;
	int i;

	// find the largest value
	largest = values[0];
	for (i=1; i < num_values; ++i)
	{
		if (values[i] > largest)
		{
			largest = values[i];
		}
	}

	return largest;
}

// find the smallest value
float findSmallest (const float values[], int num_values)
{
	float smallest;
	int i;

	smallest = values[0];
	for (i=1; i < num_values; ++i)
	{
		if (values[i] < smallest)
		{
			smallest = values[i];
		}
	}

	return smallest;
}

// find the total and average of the values
void findTotAvg (const float values[], int num_values, float * total, float * average)
{
	int i;

	*total = 0;
	for (i=0; i < num_values; ++i)
	{
		*total = *total + values[i];
	}
	*average = *total / num_values;
}

void makeFreq (const float values[], int num_values, int freq_ary[], int freq_size)
{
	int f, i;

	// zero out freq array
	for (f=0; f < freq_size; f++)
		freq_ary[f] =0;

	// build frequency
	for (i=0; i < num_values; i++)
	{
		freq_ary[(int)(values[i])]++;
	}
}

// the mode is the index of the largest value in the frequency array
int findMode (const int freq_ary[], int freq_size)
{
	int mode;
	int i;

	// find the largest value
	mode = 0;
	for (i=1; i < freq_size; ++i)
	{
		if (freq_ary[i] > freq_ary[mode])
		{
			mode = i;
		}
	}

	return mode;
}

void bubbleSort(float values[], int num_values){
	int i,j;
	float temp;
	for(i=0;i<num_values;i++){ for(j="0;j<num_values-i-1;j++){" if(values[j]=""> values[j+1]){
				temp = values[j];
				values[j] = values[j+1];
				values[j+1] = temp;
			}
		}
	}
}
Posted
Updated 1-Dec-16 12:59pm
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

The easiest way I can think of is to have your functions that return the largest and smallest values return indexes instead of values. Given an index you can access the value and it tells you which month it came from so you have everything you need.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

hmm I could try that but I was thinking about putting in a loop? something along these lines. Idk I can see it in my head just having problems putting it into my code now.
-----------------------------------

#define MAX_MONTHS 12

FILE *file = .... // open the file

int adoptionCounts[MAX_MONTHS];

int n = 0;
int result;
do
{
result = fscanf(file, "%d", &adoptionCounts[i]);
if (result == 1)
n = n + 1;
} while (result == 1);

// adoptionCounts should be filled with n months

...

int min = find_min(adoptionCounts, n);
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100