Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   array issue (http://www.go4expert.com/forums/array-issue-t10283/)

jimJohnson 29Apr2008 08:29

array issue
 
Not sure what I am doing wrong but have an issue with a program...i am to input a file name that i have in my project and it is not doing it right...

This is what my program should look like for each textfile...

Sample Output 1
Enter the file to read in: data1.txt
The numbers in the array are:

45.3 57.4 23.6 34.2
234.6 34.9 54.8 934.9
8432.5 9809.3 539.7 43.9
12.0


The numbers in the array are:

12.0 23.6 34.2 34.9
43.9 45.3 54.8 57.4
234.6 539.7 934.9 8432.5
9809.3


The average of the numbers is 1558.2
The median of the numbers is 54.8

Press any key to continue


What I am getting is...

Enter the file to read in: data1.txt

The numbers in the array are:

The average of the numbers is 45
The median of the numbers is 0

Press any key to continue...

Not sure what I am doing wrong any help would mean alot

Code:


#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

const int MAX_SIZE = 100;

void Fill_Array(int Size[], int& count, int& numbers_used);
void Print_Array(int Size[], int count);
double Calc_Average(int Size[], double average);
void Sort(int Size[], int numbers_used);
void Swap(int& v1, int& v2);
int index_of_smallest(const int Size[], int start_index, int number_used);
int Calc_Median(int median, int Size[]);
void Print_Array_and_Calculations(int median, double average);

int main()
{
        ifstream in_size;
        int count;
        int Size [MAX_SIZE],
            numbers_used = 0,
            median = 0;
        double average = 0.0;

        Fill_Array(Size, count, numbers_used);
        Print_Array(Size, count);
        average = Calc_Average(Size, average);
        Sort(Size, numbers_used);
        Calc_Median(median, Size);
        Print_Array_and_Calculations(median, average);
       
        in_size.close();
        return 0;
}

void Fill_Array(int Size[], int& count, int& numbers_used)
{
        int size;
        ifstream in_size;
        string text_file;
       
       
        cout << "Enter the file to read in: ";
        getline(cin, text_file);
       
        cout << endl << "The numbers in the array are:" << endl << endl;
        in_size.open (text_file.c_str ());
        if(in_size.fail())
        {
                cerr  << "Error opening file" << endl;
                exit(1);
        }
        count = 0;
        in_size >> size;
        while((!in_size.eof()) && (count <= MAX_SIZE))
        {
                Size[count] = size;
                count++;
                in_size >> size;
        }
        in_size.close();
}

void Print_Array(int Size[], int count)
{
        int number_used = 0;
        for(int index = 0; index < number_used; index++)
                cout << Size[index] << " ";
}

double Calc_Average(int Size[], double average)
{
        int total = 0;
        for (int i = 0; i < MAX_SIZE; i++)
        {
                total = total + Size[i];
        }
        average = double(total) / MAX_SIZE;

        return average;
}

void Sort(int Size[], int number_used)
{
        int index_of_next_smallest;
       
        for (int index = 0; index < number_used - 1; index++)
        {
                index_of_next_smallest = index_of_smallest(Size, index, number_used);
                Swap(Size[index], Size[index_of_next_smallest]);
        }
}

void Swap(int& v1, int& v2)
{
        int temp;
        temp = v1;
        v1 = v2;
        v2 = temp;
}

int index_of_smallest(const int Size[], int start_index, int number_used)
{
        int min = Size[start_index],
                index_of_min = start_index;
        for (int index = start_index + 1; index < number_used; index++)
                if(Size[index] < min)
                {
                        min = Size[index];
                        index_of_min = index;
                }
                return index_of_min;
}

int Calc_Median(int median, int Size[])
{
        median = Size [ MAX_SIZE / 2 ];
        return median;
}

void Print_Array_and_Calculations(int median, double average)
{
        cout << endl << "The average of the numbers is " << average;
        cout << endl << "The median of the numbers is " << median;
        cout << endl << endl;
}



All times are GMT +5.5. The time now is 12:13.