Thread: Finding maximum value and its coresspoding number in 2d array

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    1

    Finding maximum value and its coresspoding number in 2d array

    I have 2 question regarding 2d array. I am whether my program i working in the way I am want or not. I have to make a program to store values in one column and numbers in second column. (Table is just an example not actual values and numbers).
    0.1 1
    -0.7 2
    0.2 5
    0.5 3
    Then call the number stored at maximum value (which is number 3 stored at Max value 0.5).
    I have stored values and numbers in array as mentioned below. Although it is not showing me any error. Just want to double confirm whether it is a right way to do so? Second thing how can I call this array to find out the number stored at maximum value?


    Code:
        double n[50][2];
        int val=0;
        int numb=0;
        static double MyValue = 0.0;
        static double number = 0.0;
        static double lastValue = 0.0;
        static double
        CalculateMyValue(Node* node, int a){ // a is taken as argument
        float b =0.3;
        int bd = CalculateNumber(node); //calling another program
        ......
        .....
                                   
        {
            MyValue = ((1-a)*LastValue);  //MyValue = {0.1,-0.7..}
            {
                number = bd;              //number = {1,2,..} 
                numb++;
            }
                  n[val][numb] = number;
              cout<<"Number = n[val][numb]  =  "<<n[val][numb]<<endl;
             val++;
             n[val][numb] = MyValue;
             cout<<"MyValue =  n[val][numb] =  "<<n[val][numb]<<endl;
             LastValue = MyValue;        
        }
        return MyValue;
        }  
      
     Here I want to call this array to find out the number stored at maximum value
    
    
        void CalculateNumber(Node* node)
        {
            ..............
            ..............
            else {
           Mymuber = ???     //number stored at maximum value in array
           }
         }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    You need to be aware that, for historical reasons, 2D arrays in C and C++ are quite complex things that don't work well and simply, as in most programming languages. There's also an unfortunate tendency to introduce them in chapter two or three of "C++ Programming 101" because 1D arrays are basic, and 2D arrays look at first sight like a natural extension.

    To get the maximum element, check the array for empty or null (width or height zero) and do something appropriate e.g. return Nan.
    Set maximum to element 0, 0. Iterate over the rows and columns, and if you find a higher element, set maximum to that.

    very simple. The issue is the array representation. The route many experienced programmers use is to simply set up a 1d array
    of width * height elements in memory and access by array[y*width+x]. Then it integrates with low level code, and all
    your problems simply disappear.

    But there are other solutions, eg you can create a 2D array in memory and pass a reference to it. Or you can mess
    about with templates. You seem to have gone the pointer route. Whilst this will work, what is a Node? Unless
    we can see the internals, we can't tell you how to set one up with 2D array data, or how to access that data
    within the "CalculateNumber" function.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the biggest number in an array.
    By LSD in forum C Programming
    Replies: 11
    Last Post: 02-02-2014, 08:59 AM
  2. Finding maximum and minimum number in 2D array
    By wonderwall in forum C Programming
    Replies: 4
    Last Post: 10-23-2011, 10:21 PM
  3. Replies: 10
    Last Post: 04-26-2009, 02:46 PM
  4. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 02:31 PM
  5. Finding the maximum in an array
    By Turtal in forum C Programming
    Replies: 4
    Last Post: 11-15-2006, 07:07 AM

Tags for this Thread