-3

so I am writing this program that reads user input into an array and then finds the mean and median. I have three different files that I am going to compile together in unix:
1) stats.h, which contains the prototypes that I was given for my mean, median, and sort functions
2) stats.cpp, where I am supposed to implement these three functions, and
3) main.cpp, with my main function.

So far, my main() is functioning correctly. There are two main issues that I am having:

1) I want to make sure that I am passing the array[] and its size correctly to each of the three functions (I'm a little rusty on passing arrays).
2) I was given the prototypes for my three functions (including the void Sort function) and I was told to call the Sort function in the Median function.

I don't really understand how to update the old array in the Median function if I can't return the sorted array in the Sort function (if that makes sense).

So here are my three files: 1) stats.h

//not sure if I need to include anything at top
float Mean (const int* array, size_t size);
float Median (int* array, size_t size);
void Sort (int* array, size_t size);

2) stats.cpp

#include <stats.h>  //only include header file with prototypes?
//function to find the mean that should be called by
main()                                         
float Mean (int NewArray[], size_t size)
{                                                                    
   float sum, mean = 0.0;          

   for (int i = 0; i < size; i++)
   {                           
      sum += NewArray[i];                                
   }                                         
   mean = ((float)sum)/size;

   return mean;                                   
}

//function to find median that should be called by main()
float Median (int NewArray[], size_t size);
{//not sure how to call sort (void) function and return new sorted array to median function

    if(size % 2 == 0)                                            
    {                      
      median = (NewArray[size/2] + NewArray[size/2-1])/2.0f;
    }                                               
    else            
    {                                                  
      median = NewArray[size/2];                        
    }                                                                      
 return median;  //return median to main()                 
} 

// void function to sort array in ascending order and called by median()
Sort(int NewArray[], size_t size)                    
{                                               
   for (i = 0; i < size; i++)
   {                                                    
      for(int j = 0; j < size-1; j++)                          
      {                           
         if(NewArray[j] > NewArray[j+1])             
         {
             int temp = NewArray[j+1];                  
             NewArray[j+1] = NewArray[j];            
             NewArray[j] = temp;               
          }
       }
    }
}

3) And main()

#include <iostream>                                
#include <stats.h>                           
#include <stats.cpp>                  

int main()                                      
{                                              
    int input, sum, NumberOfEntries = 0;     
    const int SIZE = 100                      

    int array[SIZE];                               

    std::cout << "Please enter integers ('x' to quit): ";

    for (int i = 0; i < SIZE; i++)    
    {                            
       if (std::cin >> array[i])               
       {                                             
       }
       else                                        
       {                                      
          NumberOfEntries = i;                  
          break;                                    
       }                                                    
    }                                             

    std::cout << "Data as entered: ";                
    for (int i = 0; i < NumberOfEntries; i++)     
       std::cout << array[i] << " ";    

    std::cout << "Mean: " << float Mean (array[SIZE], NumberOfEntries);

    std::cout << "Median: " << float Median (array[SIZE], NumberOfEntries);

    std::cout << "Data after sort: "; //would print out sorted array 

    return 0;                                    
}
Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144
mch5904
  • 11
  • 3
  • 1
    This is a QA site. What's your question? – R Sahu Feb 01 '15 at 21:23
  • Save yourself and everybody effort by using `std::vector` instead of arrays. The vectors are easier to pass to functions and they can be accessed like arrays. – Thomas Matthews Feb 01 '15 at 22:26
  • You have two `main` functions, one in `main` and one in `stats`. You are only allow one. Pick one and rename the other (or get rid of it). – Thomas Matthews Feb 01 '15 at 22:27
  • Also, your function, variable and class names should differ in more than just case. For example, "Mean" and "mean" should be different. The function name could be "Calculate_Mean". – Thomas Matthews Feb 01 '15 at 22:30
  • Your function declarations are not requiring arrays, but pointers to **single** integer. The data type, pointer to an integer, means that the pointer could point to a single integer anywhere and there doesn't have to be any integers following it. For tips on how to declare a function requiring an array, search the web for "c++ typedef array". – Thomas Matthews Feb 01 '15 at 22:32

1 Answers1

0

You should create a synonym for an array (which makes passing easier):

typedef int DataArray[/* Some size constant*/];

(See StackOverflow question about array typedef)

You can declare your functions as:

double Calculate_Mean(const Data_Array array, unsigned int size);
double Calculate_Median(const Data_Array array, unsigned int size);

By the way, unless you need to conserve space, use double, not float. Most of the libraries in C++ that use floating point use the double type. It's more accurate.

Also, use unsigned int for quantities. I have yet to see an array that is -5 in length. The unsigned keyword will help the compiler check for negative values assigned to indices and quantities (Hmm, can you purchase -5 apples?).

Community
  • 1
  • 1
Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144