Thread: Array Help!!!!

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    Post 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


    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;
    }

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    When make posts for help remember to say what the problem is.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    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.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    In your sorting section, y starts off as 5 because that was its value when you finished reading in data.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    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."

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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?
    Last edited by 7stud; 05-05-2005 at 07:46 PM.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    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.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    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.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Y is a new variable I added to this program, the first program uses only x variable
    Your utmost concentration is needed when answering these questions. I am not asking about some hypothetical program you wrote last week. I am asking you about the program you posted.

    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?
    Last edited by 7stud; 05-05-2005 at 08:01 PM.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    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++

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    y is a new variable
    Incorrect.

    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?

    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?
    Last edited by 7stud; 05-05-2005 at 08:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM