# Array Help!!!!

• 05-05-2005
marshel
Array Help!!!!
This is my code. It is a two Dimensional Array.
The program reads in 25 numbers from a data file then is supposted to sort them. All the code is simple, I used all the code in another program that does a one Dimensional Array. I am new to C++ so any help would be VERY VERYN Greatful.
The numbers are 1 thru 25 in random order
:confused:

Code:

```#include <iostream> #include <fstream> #include <string> using namespace std; int main() { //Array used in this program.         int array[5][5];         int x;         int y;         int numin;         int temp; //File read in and use.         fstream fin ("random numbers.txt");         for (x = 0; x < 5; x++)                         for(y = 0; y < 5; y++)                 {                         fin >> numin;                         array[x][y] = numin;                 } //Loop; sort array and print to screen         x = 0;         while (x < 5)         if (array[x][y] < array[x+1][y-1])         {         temp = array[x+1][y-1] ;         array[x+1][y-1] = array[x][y];         temp = array[x+1][y-1];         array[x][y] = temp;         }                         else         {                 x++; y++;                 cout<<"["<<x<<"]" "["<<y<<"]" << " ";                    cout << "\n";         }                 //Write to a file.         fstream fout("sorted.txt");         fout <<" sorted: ";                                                         return 0; }```
• 05-05-2005
When make posts for help remember to say what the problem is.
• 05-05-2005
marshel
Sorry Everyone. the problem with this program is that when I complie it, it only sort's 1 - 10, and not all 25 numbers in the data file. I used a while loop in the first program and just copied the first program over into this one, then added the additional [y] and the for loop for ( y = 0; y < 5; y++0 ). If you do a array[x+1][y+1] in the while loop when you complie it nothing happens, but if you change it to array[x+1][y-1] throughout the whole while loop it complies only 1 thru 10. I've have been stuck on tis for about a week and I have tried and asked everyone I know and know one has any answers. Please Help me if you all can.
• 05-05-2005
7stud
In your sorting section, y starts off as 5 because that was its value when you finished reading in data.
• 05-05-2005
marshel
Not sure I know what you mean " In your sorting section, y starts off as 5 because that was its value when you finished reading in data."
• 05-05-2005
7stud
1) What value do you want y to be when you start sorting?

2) Is y a variable that was used earlier in your program?

3) If y is a variable that you used earlier in your program, what value does y have just before you start sorting?
• 05-05-2005
marshel
I would like y to be set to zero when I start sorting.

Y is a new variable I added to this program, the first program uses only x variable

I am steping through the program now, when I go into my while loop

array[x+1][y-1] = 10

array[x][y] = 6

x = 0

y = 5

I need a different loop for my sorting to work, but I can not figure one out.
• 05-05-2005
marshel
When I step through my program it starts my while loop with the above post and then skips through it until it goes around 10 times.
• 05-05-2005
7stud
Quote:

Y is a new variable I added to this program, the first program uses only x variable

Quote:

2) Is y a variable that was used earlier in your program(i.e. before you started sorting)?

3) If y is a variable that you used earlier in your program, what value does y have just before you start sorting?

4) Why did you assign 0 to x right before you started sorting?
• 05-05-2005
marshel
y is a new variable

the value of y is 5 before i start sorting

I don't know why I assigned 0 to x before i start sorting, it just works like I said I'm new a this and just need a little help from someone who knows C++
• 05-05-2005
7stud
Quote:

y is a new variable
Incorrect.

Quote:

the value of y is 5 before i start sorting
Correct. You might want to think about how y can be equal to 5 if it is a new variable that you created for the sorting section of your code. You also might want to consider that you said you wanted y to equal 0 when you started sorting, but it equals 5. What can you do to correct that state of affairs?

Quote:

I don't know why I assigned 0 to x before i start sorting, it just works
I would insert a cout statment before you assign 0 to x, and display x and y. What are their values? What do you want them to be? How can you get them to be what you want?