Thread: Help With Arrays

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Post Help With Arrays

    /*I want to load arrays and display arrays that will have numbers 1-25 in this format

    1 2 3 4 5
    6 7 8 9 10
    11 12 and so on...

    i have got 2 functions
    1st one will load arrays n second one will display */

    Code:
     
    
    void loadArray() 
    { 
    int r, c, cnt=1; 
    
    for( r=0; r<5; r++ ) 
    for( c=0; c<5; c++ ) 
    data[r][c] = cnt++; 
    } 
    
    void displayArray( ) 
    { 
    int r, c, temp; 
    
        cout << "The 5 x 5 2D array:\n\n"; 
    
        for (r=0;r<5;r++) 
       { 
           for(c=0;c<5;c++) 
           { 
            cout << " " << data[r][c]; 
           } 
       } 
    
    displayArray(); 
    getchar(); 
    
    pause(); 
    }
    //i want to display arrays when ever i call func DisplayArrays();

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Well, displayArray is recursive.. with no sign of stopping. Therefore, if you call it once itt'l continue to output the data... forever.

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    so tell me wat to do
    n u didnt actually answere my question witch was
    how would i display the array in 5x5 format

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    void displayArray( ) 
    { 
    int r, c, temp; 
    
        cout << "The 5 x 5 2D array:\n\n"; 
    
        for (r=0;r<5;r++) 
       { 
           for(c=0;c<5;c++) 
           { 
            cout << " " << data[r][c]; 
           }
            cout << endl;
       } 
    
    getchar(); 
    
    pause(); 
    }
    Ooops, sorry about that There ya go

  5. #5
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    thx now let me see if it works




    it works thx man
    Last edited by dayknight; 05-04-2002 at 08:18 PM.

  6. #6
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    ok now i am stuck on adding rows n colums

    Code:
    void sumRows( )
    {
    	int	r, c, row;
    	
    	cout << "Enter row number:\t ";
    	cin  >> row;
    
    	for (r=0;r<5;r++)
    	{
    		for(c=0;c<5;c++)
    		{
    			csum = data[row][c] + data[row][c];
    		}
    	}
                   cout << " "<<csum;
    	cout << "\n\nSwapping done:\n";
    	displayArray();
    }
    codewarrior gives me this error:

    illegal operrand
    and it is on the semicolon


    and i was also having trouble showing the array backwards
    (it is not that i dont know, i have done this program b4, but i keep forgetting how i did.)
    Last edited by dayknight; 05-04-2002 at 08:50 PM.

  7. #7
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Is the sumRow function supposed to output the sum of a row, or swap elements in that row?

    To output the array backwards just do

    for (int i = 4; i >= 0; i--)
    {
    for (int ii = 4; ii >= 0; ii--)
    {
    cout << data[i][ii];
    }
    cout << endl;
    }

  8. #8
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    the sumRow is suppose to output the sum of the row that is inputted by the user
    for example
    sum of row 1 is 15
    Last edited by dayknight; 05-05-2002 at 02:14 PM.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    Code:
    for(c=0;c<5;c++)
    		{
    			csum = data[row][c] + data[row][c];
    		}
    try this instead csum+=data[row];
    or csum=csum+data[row];
    also don't forget to declare csum
    Last edited by rip1968; 05-05-2002 at 03:55 PM.

  10. #10
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    i can seem to get it to work
    my
    int csum[5]; // it is global
    and it is giving me the same error when i tried with all those different methods.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    if csum is a global array of 5 integers each holding the sum of a row then try this instead
    csum[row]=csum[row]+data[row,c];

  12. #12
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Question

    here is the code:

    Code:
    void sumRows( )
    {
    	int	r, c, row;
    	
    	cout << "Enter row number:\t ";
    	cin  >> row;
    
    	for (r=0;r<5;r++)
    	{
    		for(c=0;c<5;c++)
    		{
    		csum[row]=csum[row]+data[r,c];
    		}
    	}
    	cout << "\n\nSwapping done:\n";
    	displayArray();
    }
    here is the error promped by CODEWARrior
    Error : illegal implicit conversion from 'int *' to
    'int'
    !U1_A4_2.CPP line 193 csum[row]=csum[row]+data[r,c];

    it gave me the same error when i tried
    csum[row]=csum[row]+data[row,c];

    and my csum is an array ie
    csum[5] // it is global

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