How Can I Correctly Sort and Output Arrays from a Single File in C++?

In summary, the problem is that the code does not properly group the numbers into odd, even and negative groups.
  • #1
kahless2005
46
0
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:
# 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");
	if(fin.fail())
	{
		cerr << "Error opening input file. \n\n";
		exit(1);
	}
	ofstream fout("lab8a.out");
	if(fout.fail())
	{
		cerr << "Error opening output file.\n\n";
		exit(1);
	}
    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];
             spotneg++;
          }
          else if (numbers[index]% 2 == 0)
          {
             numbers[index] = even[spoteve];
             spoteve++;
          }
          else
          {
             numbers[index] = odd[spotod];
             spotod++;
          }
          fout << setw(5) << even[index]
               << setw(5) << odd[index]
               << setw(10) << negative[index] << endl;
          fin >> numbers[index++];
    }
    fin.close();
    fout.close();
    system ("pause");
    return 0;
}

Where am I going wrong?
 
Physics news on Phys.org
  • #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.
 
  • #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[]
 
  • #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?
 
  • #5
kahless2005 said:
I was also told that my reference to index in the loop should be index-1, where would I put that?

I am not sure what you meant by that.
 

Related to How Can I Correctly Sort and Output Arrays from a Single File in C++?

1. What is the purpose of sorting 1 file into 3 arrays?

The purpose of sorting 1 file into 3 arrays is to organize and categorize data in a more efficient and structured manner. This can help with data analysis and retrieval, as well as make it easier to manipulate and work with the data.

2. How do you decide which data goes into each array?

The decision of which data goes into each array depends on the specific parameters and criteria set by the scientist. This could include numerical values, alphabetical order, or other factors depending on the type of data being sorted.

3. What are the potential benefits of sorting data into multiple arrays?

Sorting data into multiple arrays can provide several benefits, such as easier data management and analysis, improved data accuracy, and faster data retrieval. It can also help identify patterns and relationships within the data that may not have been apparent before.

4. Are there any limitations or challenges when sorting data into multiple arrays?

One limitation of sorting data into multiple arrays is that it can be time-consuming and require a lot of processing power, especially when dealing with large datasets. Additionally, if the criteria for sorting are not well-defined, it can lead to errors or inaccurate results.

5. Can the process of sorting data into multiple arrays be automated?

Yes, the process of sorting data into multiple arrays can be automated using computer programs or scripts. This can save time and reduce the risk of human error. However, it still requires careful consideration and planning in order to ensure accurate and meaningful results.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
7K
  • Programming and Computer Science
Replies
20
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top