I have so much trouble in making this thing work.
This is what i supposed to do:
1.build program to make histogram
2.The program should also calculate average and median.
3.The array should come from the text file.
The problem is..i can build the histogram..but i have problem to get the file from the text file and use it to count an average and median.
could someone help me on this??
this is the code that i already make:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ELMNTS 51
#define ANLYS_RNG 101
//function Declaration
int getData (int numbers[], int size, int range);
void printData (int numbers[], int size, int lineSize);
void makeFrequency (int numbers[], int size, int frequency[], int range);
void makeHistogram (int frequency[], int range);
int main (void)
{
//local declaration
int size;
int nums [MAX_ELMNTS];
int frequency [ANLYS_RNG];
//statements
size = getData (nums, MAX_ELMNTS, ANLYS_RNG);
printData (nums, size, 10);
makeFrequency(nums, size, frequency, ANLYS_RNG);
makeHistogram(frequency, ANLYS_RNG);
system("Pause");
return 0;
}//main pograme
/*===============================GetDATA FROM FILE==========================*/
int getData (int data [], int size, int range)
{
//local declaration
int dataIn;
int loader = 0;
FILE* fpData;
//statements
if (!(fpData = fopen("text.txt", "r")))
printf("Error opening file\a\a\n") ,exit(100);
while (loader < size &&fscanf(fpData, "%d", &dataIn) !=EOF)
if (dataIn >= 0 && dataIn < range)
data[loader++] = dataIn;
else
printf("\nData point &d invalid. Ignored. \n", dataIn);
//while testing
if (loader == size)
printf("\nToo much data. Process what read.\n");
return loader;
}
/*====================================PrintData=============================*/
void printData(int data[], int size, int lineSize)
{
//local declaration
int numPrinted = 0;
//statements
printf("\n\n");
for (int i = 0; i < size; i++)
{
numPrinted++;
printf("%2d", data[i]);
if (numPrinted >= lineSize)
{
printf("\n");
numPrinted = 0;
}//if
}//for
printf("\n\n");
return ;
}//PrintData
/*================================MakeFequency=================================*/
void makeFrequency (int nums[], int last, int frequency[], int range)
{
//statements
//initialize the frequency array
for (int f = 0; f < range;f++)
frequency[f]= 0;
//scan numbers and builed frequency
for(int i = 0; i< last; i++)
frequency [nums [i]]++;
return ;
}//make frequency
/*=================================MakeHistogram===============================*/
void makeHistogram (int freq[], int range)
{
int i;
//statements
for (i = 0; i < range; i++)
{
printf("%2d %2d", i, freq[i]);
for (int j = 1; j <= freq[i]; j++)
printf("*");
printf("\n");
}//for i
return ;
}//make Histogram
//============================Program Ended======================================
the item inside the text file is:
1 3 4 7 90 78 65 15 87 46
53 9 67 12 9 7 82 39 56 48
16 27 45 12 94 37 1 98 65 90
73 82 96 9 74 58 29 74 83 100
100 87 45 27 48 93 28 100 89 62
so how should make a code to read and count it??
Please help..