1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Sorting 1 file into 3 Arrays

  1. Apr 9, 2006 #1
    The Problem (from the handout, word for word):
    Write a program that reads a list of integers from the file "numbers.in" into an array, which I will call numbers. Then sort the numbers into 3 groups, a group of odd numbers, a group of even numbers, and a group of negative numbers, use an array to hold the numbers in each group. Finally, you are to output the three groups per the example below. There could be up to 150 numbers in the file numbers.in, which is located in the folder:

    My Code ( no functions):
    Code (Text):

    # include <iostream>
    # include <cmath>
    # include <fstream>
    # include <iomanip>
    using namespace std;
    const int MAX = 150;
    int main ()
        cout << "Lab 8\t\t\tKevin O'Grady\n\n";
        ifstream fin("numbers.in");
            cerr << "Error opening input file. \n\n";
        ofstream fout("lab8a.out");
            cerr << "Error opening output file.\n\n";
        int even[MAX];
        int odd[MAX];
        int negative[MAX];
        int numbers[MAX];
        int index;
        int spotneg = 0;
        int spoteve = 0;
        int spotod = 0;
        fout << "Lab 8\t\t\tKevin O'Grady\n\n";
        fout << "This program sorts numbers into odd, even and negative groups. \n"
             << "The arrays are: \n"
             << setw(5) << "Even"
             << setw(5) << "Odd"
             << setw(10) << "Negative" << endl;
        fin >> numbers[index];
        while (!fin.fail() && index < MAX)
              if (numbers[index] < 0)
                 numbers[index] = negative[spotneg];
              else if (numbers[index]% 2 == 0)
                 numbers[index] = even[spoteve];
                 numbers[index] = odd[spotod];
              fout << setw(5) << even[index]
                   << setw(5) << odd[index]
                   << setw(10) << negative[index] << endl;
              fin >> numbers[index++];
        system ("pause");
        return 0;
    Where am I going wrong?
  2. jcsd
  3. Apr 10, 2006 #2
    1. You need to initialize "index", something like "int index = 0;" would be fine.
    2. I don't think it is a good idea to group the numbers and write onto file in the same loop. Group first and write later.
    3. The use of "even[index]", "odd[index]" and "negative[index]" will all return garbage since "index" is not the proper index for the three arrays.
  4. Apr 10, 2006 #3
    when you assign values into variables, it should read:

    negative[spotneg] = numbers[index];

    not the other way around (you aren't putting values from negative[] into numbers[], you want to sort the negative ones from numbers[] into negative[]
  5. Apr 10, 2006 #4
    Thanks to both of you!

    I was also told that my reference to index in the loop should be index-1, where would I put that?
  6. Apr 10, 2006 #5
    I am not sure what you meant by that.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Sorting 1 file into 3 Arrays
  1. Sorting Array (Replies: 6)