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..

Recommended Answers

All 5 Replies

You have already done the hard part. What don't you understand about calculating the median? Just sort the array and select the value in the center of the array. There are many sorting algorithms, the easiest one (but also the slowest one) to code is the bubble sort.

Thanks for the tips...i'm glad that someone point me to the correct way
=)

is this the correct way to bubblesort a file..if it yes..how can i see the sorted number?

#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);

     void bubbleSort(int list[], int last);


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("C:\\Documents and Settings\\Acer\\My Documents\\Visual Studio 2008\\Projects\\ajin lagi 2\\TextFile6.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

/*===================================BubbleSort=====================================*/
void bubbleSort(int list[], int last)
{
//local declaration
int temp;

//statements
   //outer loop
   for (int current = 0; current < last; current++)
    {
     //inner loop
     for (int walker = last;
          walker = current;
          walker--)
     if(list[walker] < list[walker - 1])
      {
                     temp           = list[walker];
                     list[walker]   = list[walker - 1];
                     list[walker -1]= temp;
                     }//if
            }//for current
 return;
 }//bubble sort
//============================Program Ended======================================

i just add:

void bubbleSort(int list[], int last)
{
//local declaration
int temp;

//statements
   //outer loop
   for (int current = 0; current < last; current++)
    {
     //inner loop
     for (int walker = last;
          walker = current;
          walker--)
     if(list[walker] < list[walker - 1])
      {
                     temp           = list[walker];
                     list[walker]   = list[walker - 1];
                     list[walker -1]= temp;
                     }//if
            }//for current
 return;

>>..how can i see the sorted number?

Print out the array after sorting and you will see for yourself whether your sort function worked correctly or not.

If you dont want to implement your own sorting function then check out this link.

PS I know this link is from a C++ reference, but I think it will work in c code as well. Others please correct me if I am wrong

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.