Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   array issue (http://www.programmingforums.org/showthread.php?t=15727)

jimJohnson1 Apr 28th, 2008 10:48 PM

array issue
 
2 questions first did the main daniweb site get hacked or something...2nd can someone take a look at my code and see what I am doing wrong...can't seem to pinpoint it

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

:

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6. using namespace std;
  7.  
  8. const int MAX_SIZE = 100;
  9.  
  10. void Fill_Array(int Size[], int& count, int& numbers_used);
  11. void Print_Array(int Size[], int count);
  12. double Calc_Average(int Size[], double average);
  13. void Sort(int Size[], int numbers_used);
  14. void Swap(int& v1, int& v2);
  15. int index_of_smallest(const int Size[], int start_index, int number_used);
  16. int Calc_Median(int median, int Size[]);
  17. void Print_Array_and_Calculations(int median, double average);
  18.  
  19. int main()
  20. {
  21.         ifstream in_size;
  22.         int count;
  23.         int Size [MAX_SIZE],
  24.             numbers_used = 0,
  25.             median = 0;
  26.         double average = 0.0;
  27.  
  28.         Fill_Array(Size, count, numbers_used);
  29.         Print_Array(Size, count);
  30.         average = Calc_Average(Size, average);
  31.         Sort(Size, numbers_used);
  32.         Calc_Median(median, Size);
  33.         Print_Array_and_Calculations(median, average);
  34.  
  35.         in_size.close();
  36.         return 0;
  37. }
  38.  
  39. void Fill_Array(int Size[], int& count, int& numbers_used)
  40. {
  41.         int size;
  42.         ifstream in_size;
  43.         string text_file;
  44.  
  45.  
  46.         cout << "Enter the file to read in: ";
  47.         getline(cin, text_file);
  48.  
  49.         cout << endl << "The numbers in the array are:" << endl << endl;
  50.         in_size.open (text_file.c_str ());
  51.         if(in_size.fail())
  52.         {
  53.                 cerr  << "Error opening file" << endl;
  54.                 exit(1);
  55.         }
  56.         count = 0;
  57.         in_size >> size;
  58.         while((!in_size.eof()) && (count <= MAX_SIZE))
  59.         {
  60.                 Size[count] = size;
  61.                 count++;
  62.                 in_size >> size;
  63.         }
  64.         in_size.close();
  65. }
  66.  
  67. void Print_Array(int Size[], int count)
  68. {
  69.         int number_used = 0;
  70.         for(int index = 0; index < number_used; index++)
  71.                 cout << Size[index] << " ";
  72. }
  73.  
  74. double Calc_Average(int Size[], double average)
  75. {
  76.         int total = 0;
  77.         for (int i = 0; i < MAX_SIZE; i++)
  78.         {
  79.                 total = total + Size[i];
  80.         }
  81.         average = double(total) / MAX_SIZE;
  82.  
  83.         return average;
  84. }
  85.  
  86. void Sort(int Size[], int number_used)
  87. {
  88.         int index_of_next_smallest;
  89.  
  90.         for (int index = 0; index < number_used - 1; index++)
  91.         {
  92.                 index_of_next_smallest = index_of_smallest(Size, index, number_used);
  93.                 Swap(Size[index], Size[index_of_next_smallest]);
  94.         }
  95. }
  96.  
  97. void Swap(int& v1, int& v2)
  98. {
  99.         int temp;
  100.         temp = v1;
  101.         v1 = v2;
  102.         v2 = temp;
  103. }
  104.  
  105. int index_of_smallest(const int Size[], int start_index, int number_used)
  106. {
  107.         int min = Size[start_index],
  108.                 index_of_min = start_index;
  109.         for (int index = start_index + 1; index < number_used; index++)
  110.                 if(Size[index] < min)
  111.                 {
  112.                         min = Size[index];
  113.                         index_of_min = index;
  114.                 }
  115.                 return index_of_min;
  116. }
  117.  
  118. int Calc_Median(int median, int Size[])
  119. {
  120.         median = Size [ MAX_SIZE / 2 ];
  121.         return median;
  122. }
  123.  
  124. void Print_Array_and_Calculations(int median, double average)
  125. {
  126.         cout << endl << "The average of the numbers is " << average;
  127.         cout << endl << "The median of the numbers is " << median;
  128.         cout << endl << endl;
  129. }


derzok Apr 29th, 2008 9:33 PM

Re: array issue
 
Here's something that does pretty much the same thing. I wrote it earlier this semester for my principle of programming languages class:
:

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <exception>
using namespace std;

// Parses the input file into the array's size and contents.
// Dynamically allocates memory for the array, fills it, and returns.
void parse_file(char * filename, double *&a, int &size) {
        ifstream myfile;
        myfile.exceptions( fstream::failbit );

        // Get the file name, and try to open the said file
        printf("Input the filename: "); 
        scanf("%s",filename);
        // Try opening the file, if it doesn't exist, throw an exception
        try {
                myfile.open(filename);
        } catch(ifstream::failure e) {
                printf("File does not exist or is unreadable\n");
        }

        // Read the first thing from the file as an int and store it as 'num'
        myfile>>size;

        // allocate an array to the exact that we need
        a = (double *) malloc(size*sizeof(double));
        // Fill the array
        for(int i=0; i<size; i++) {
                myfile>>a[i];
        }
        myfile.close();
}

// Standard way of calculating average
// Sums the elements of a, puts them into total, divides by size and returns.
double find_average(double *&a, int size) {
        double total = 0;
        for(int i=0; i<size; i++) {
                total = total + a[i];
        }
        return (total/(double)size);
}

// Standard way of calculating standard deviation
// Sums the square of each element minus the average
// Divides the total of squares by the size and returns the positive square root of the sum.
double find_standard_dev(double *&a, int size, double average) {
        double dev = 0;
        for(int i=0; i<size; i++) {
                dev = dev + pow(a[i]-average,2);
        }
        dev = dev/size;
        return (sqrt(fabs(dev)));
}

int main() {

        // Set up some variables
        char filename[50]; // 50 characters for the filename should be good enough unless the path is included.
        double average = 0, standard_dev = 0; // Zero out our variables before we start using them
        int num; // Array size
        double * array; // Make a pointer to our array

        parse_file(filename, array, num);

        average = find_average(array,num);

        standard_dev = find_standard_dev(array, num, average);

        printf("Average: %lf\n",average);
        printf("Standard Deviation: %lf\n",standard_dev);
        free(array);
        return 0;
}


The Dark Apr 30th, 2008 12:24 AM

Re: array issue
 
In your Fill_Array function, you don't set the number_used variable to the number of items found.

In your print_array function:
:

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

You should use the count that is passed in, rather than the number_used variable that is set to 0

The Calc_Average and Calc_Median functions need to be passed the number of items in the array, rather than using MAX_SIZE

Also, your array is of type int, and the input has floating point numbers, you should look into using type double.

Dannyo329 Jun 4th, 2008 4:02 AM

Re: array issue
 
Guess no one has answered his first question, the ALMIGHTY Dannyo329 has answered:
I Do Not Think So.( I was just on it , not sure at the time when you posted your questions though.)


All times are GMT -5. The time now is 12:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC