2d arrays: H e l p!

This is a discussion on 2d arrays: H e l p! within the C++ Programming forums, part of the General Programming Boards category; hi all, i am having difficulty understanding how to use 2d arrays in any of my functions... I know how ...

  1. #1
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    2d arrays: H e l p!

    hi all,
    i am having difficulty understanding how to use 2d arrays in any of my functions...
    I know how to find the max[] for any array, but when the arr[][] comes into the picture i'm getting messed up!

    here's what my max function is: (my array has 4 rows, 6 cols) and the content has been inputted and printed by previous functions.
    I'm not sure my function or call are correct either..
    for some reason the logic is passing me, hope someone can explain.thanks, Michele

    Code:
    //prototype
    int Maxsearch(int arr2d[][cols], int SIZE);
    
    //call
     Maxsearch(arr2d, SIZE);
    
    
    
    //Part3 : Function definition: Largest Int & Address
    	int Maxsearch(int arr2d[][cols], int MAX){
    
    		outfile << "\n\n";
    		outfile<<setw(60)<< "Part 3 - Largest Integer & Address:" << "\n\n" << endl;
    	    outfile<< setw(30) << endl;
    		MAX = arr2d[0][6];
    		int MAXi = 0;
    		for ( int i = 0; i < MAX; i++ ){
    			if (arr2d[i][6] > MAX){
    				MAX = arr2d[i][6];
    				MAXi = i;
    			}
    				
    			outfile<< MAX << setw(4) << MAXi << endl;
      }
    			return 0;
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Two things, if cols isn't a const global variable, I don't think this will compile. If it isn't you need to do this in the definition:
    Code:
    //Part3 : Function definition: Largest Int & Address
    	int Maxsearch(int arr2d[][6], int MAX)
    Second, if I'm to understand you correctly, then are you looking for the greatest number out of the entire array, or just one column of the array? If you want the whole thing, you need two nested for loops like so:
    Code:
    for (int x=0; x<rowsize; x++)
    {
      for (int y=0; y<colsize; y++)
      {
         if (arr2d[x][y]>max)
         {
            max=arr2d[x][y];
            MAXx=x;
            MAXy=y;
         }
       }
    }
    If you just want to check the 6th column, then your code for finding the max looks fine to me.

  3. #3
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    still

    am still needing help,
    i am so not getting this...
    could you explain what your nested loop is doing?
    and also if my code for just checking on row is okay why am I not getting anything close to an answer?
    (I do have a const int cols global)
    Thanks Michele
    very frustrated newcomer

  4. #4
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    more ?

    i don't get what you loop tells me...
    I already know what that max rows and cols are they are const.
    I want to know what the largest integer stored in the array and it's location (as an prdered pair).
    i don't see it..and i feel so stupid.
    M

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    could you explain what your nested loop is doing?
    It traverses ("checks all elements") the array. The max variable contains the max value found in the array and MAXx and MAXy stores the "cordinates" for the max variable.



    PJYelton forget´t to assign max to the first element in the array
    Code:
    max = arr2d[0][0];
    int MAXx, MAXy;
    //Implement PJYelton´s code
    return arr2d[MAXx][MAXy];
    Add this to the function Maxsearch and it should work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 06:06 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-27-2008, 12:03 AM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 05:34 AM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 07:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21