Thread: Need immmediate help with program (basic C++ stuff)

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Question Need immmediate help with program (basic C++ stuff)

    Hello,
    I've writing a function to find the three largest integers in a 3rd dimensional array which has #0 to 9 stored exclusively. However, I keep getting the same highest # (usually 9) stored in max_store[]. I have tried everything, and I don't know where to go next. If some one could please offer me some help, I'd really appreciate it. I need this asap. Thank you so much.

    Code:
    void find_max(int fun[ROW][COL][LAYER]) {
       
       int num, r, c, l, k;
       int max=0;
       int max_store[3]={-1};
    
       for (num=0; num < 3; num++) {
          for (r=0; r < ROW; r++) {
             for (c=0; c < COL; c++) {
                for (l=0; l < LAYER; l++) {
                   for (k=0; k < 3; k++) {                         
                      if (max < fun[r][c][l] && max_store[k] != max)
                         max = fun[r][c][l];
                   }
                       
                }
                
             }  
              
          }
          
          max_store[num] = max; 
          max = 0;
         
          
       }
    Justin

    P.S. If you want to look at entire source file, I attached it to this thread.
    Last edited by YevGenius; 04-30-2003 at 09:54 PM.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Write a method to check if a number exists in an array.
    Then use that here :
    Code:
    if (max < fun[r][c][l] && !NumExistsInArray( max_store, 3 ) )
          max = fun[r][c][l];
    The error with your code lies in the fact that it never really checks to see if a number has already been chosen.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Unhappy Still unable to get to work!

    Hi,
    I have tried creating a method to check if a number exists in an array but still I am not getting the proper solutions. If someone could please write me that method, I would really appreciate it.


    Code:
    int max_store[3]={-1};
    
    bool NumExistsInArray(int max_store[3], const int qt, int fun[ROW][COL][LAYER]) {
                                             
       
       for (int r=0; r < ROW; r++) {
          for (int c=0; c < COL; c++) {
             for (int l=0; l < LAYER; l++) {
                for (int m=0; m < qt; m++) {
                   if (max_store[m] == fun[r][c][l]) {
                      return true;
                   }
                  
                }    
                
             }   
                     
          }
            
       }  
       return false;
    } 
    
    void find_max(int fun[ROW][COL][LAYER]) {
       
       int max=0;   
       int num;
    
       for (num=0; num < 3; num++) {
          for (int r=0; r < ROW; r++) {
             for (int c=0; c < COL; c++) {
                for (int l=0; l < LAYER; l++) {
                                               
                   if (max < fun[r][c][l] && !NumExistsInArray( max_store, 3, fun ))
                      max = fun[r][c][l];
                     
                }
                
             }  
              
          }
          max_store[num] = max; 
          max = 0;
         
          
       }
    Justin

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You need to...

    Oh! Wait a minute. You needed immediate help. Sorry, I was too late. Never mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  2. Advice on writing a basic encryption program?
    By osiris^ in forum C Programming
    Replies: 2
    Last Post: 09-10-2007, 02:02 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 02:35 AM
  5. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM