Thread: arrays

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    72

    arrays

    Hello, I've got problems with the following code.


    Code:
    // Headers and Other Technical Items
    
    #include <iostream>
    #include <fstream>         // For file I/O 
    #include <iomanip>         // For setw()
    using namespace std;
    
    // Function Prototypes
    
    int  count_file_values(char input_filename[]);
    void load_array_from_file(char input_filename[], int things[], int array_size);
    void display_array(int things[], int array_size);
    void bubble_sort_array(int things[], int array_size);
    void pause(void);
    
    // Variables
    
    // Place the Demo_Farm_Acres_Input.txt file in a folder on your
    // drive.  We suggest that you create a folder named: 
    // Data_Files on the root level of your C: (hard disk) drive
    // or flash drive as appropriate. 
    
                               // File Specifications for the input file
                               // A file specificatoin includes a 
                               // drive, path, filename and file extention 
    char     data_filename[]  = "C:\Cpp_Source_Code_Files\Chapter_21\Lab_21_Input.txt";
    
    //******************************************************
    // main
    //******************************************************
    
    int main(void)
      {
      int record_count = count_file_values(data_filename);
      int acres[record_count];
      load_array_from_file(data_filename, acres, record_count);
      cout << "The individual grades are: \n";
      display_array(acres, record_count);
      bubble_sort_array(acres, record_count);
      cout << "\n\nThe sorted grades are: \n";
      display_array(acres, record_count);
      pause();
    
      return 0;
      }
    
    //******************************************************
    // count_file_values
    //******************************************************
    
    int count_file_values(char input_filename[])
      {
      fstream  inData;           // device token for the input stream
      double   next_value;
      int      number_of_values = 0;
      
      inData.open(input_filename, ios::in);        //Open input file
      if (!inData)
        {
        cout << "\n\nError opening input data file: " << input_filename << "\n\n";
        pause();
        exit(EXIT_FAILURE);
        }
      // Get data values and count them
    
      while (inData >> next_value)
        {
        number_of_values++;
        }
    
      // Close the files
      inData.close();                              //Close input file
    
      return number_of_values;
      }
    
    //******************************************************
    // load_array_from_file
    //******************************************************
    
    void load_array_from_file(char input_filename[], int things[], int array_size)
      {
      fstream  inData;           // device token for the input stream
    
      inData.open(input_filename, ios::in);        //Open input file
      if (!inData)
        {
        cout << "\n\nError opening input data file: " << input_filename << "\n\n";
        pause();
        exit(EXIT_FAILURE);
        }
      else
        {
        for (int i = 0; i < array_size; i++)   // Load the array
          {
          inData >> things[i];
          }
        inData.close();
        }
      return;
      }
    
    //******************************************************
    // display_array
    //******************************************************
    
    void display_array(int things[], int array_size)
      {
    
      cout << "\n************************************";
      for (int i = 0; i < array_size; i++)
        {
        cout << "\nOffset: " << setw(2) << i;
        cout << " Memeber: " << setw(2) << i+1;
        cout << " value is: " << setw(3) << things[i];
        }
      cout << "\n************************************";
    
      return;
      }
    
    //******************************************************
    // bubble_sort_array
    //******************************************************
    
    void bubble_sort_array(int things[], int array_size)
      {
      bool moresortneeded;   // Flag to determine if sorting is completed
      int  temp;             // Used for swaping array members
    
      do
        {
        moresortneeded = false;
        for(int i = 0; i < array_size - 1; i++)
          {
          if(things[i] > things[i+1])
            {
            temp = things[i];
            things[i] = things[i+1];
            things[i+1] = temp;
            moresortneeded = true;
            }
          }
        }
      while(moresortneeded);
    
      return;
      }
    
    //******************************************************
    // pause
    //******************************************************
    
    void pause(void)
      {
      cout << "\n\n";
      system("PAUSE");
      cout << "\n\n";
      return;
      }
    
    //******************************************************
    // End of Program
    //******************************************************

    It's supposed to display grades ( character members ).

    It's counting the memebers; allocates the array; then loads the array. But something is wrong with the bubble_sort_array part. Where's the error?

    The input.txt has just those following values:

    A C F B D A W I B A C


    Thank you!

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    What kind of output do you get, any runtime errors? We need more info then just "something is wrong".

    The main concern I see is when you swap the values in the array at 'i' and 'i+1', but i+1 can be outside the array since 'i' goes all the way up to the size of the array-1 (essentially the last element, but i+1 is 1 element past the last element).

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    I don't get any output at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM