0

The problem with my code is that it is not identifying my function, I am not sure if the function is incorrect or written with the wrong syntax. What I have tried is to create a new array for the location of the largest index but it doesn't seem to work.

#include <iostream>
#include <iomanip>

using namespace std;

void locateLargest(const double a[][4], int location[]);

const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;

int main(){

    int location [ROW_SIZE][COLUMN_SIZE];

    double matrix [ROW_SIZE][COLUMN_SIZE];

    double input;

    cout<<"Enter the array: "<< endl;
    
    for (int i = 0; i < ROW_SIZE; i++){
        for(int j = 0; j < COLUMN_SIZE; j++){
            cin>>input;
            matrix[i][j] =  input;
        }
    }

     for(int i = 0; i < ROW_SIZE; i++){
        for(int j = 0; j < COLUMN_SIZE; j++){
            cout<< setw(4)<<matrix[i][j]<< " ";
        }
         cout<< endl;
    }

    locateLargest(matrix, location)
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Why do you want to return a 3x4 matrix for just a row+column pair? – acraig5075 Apr 07 '21 at 06:26
  • 1
    Why do you compare all elements with `a[0][0]`? You should compare all elements with the current max value. `location` should contain the position and not the value. You need something like `if(a[location[0]][location[1]] < a[i][j]) – Thomas Sablik Apr 07 '21 at 06:27
  • 1
    Don't you mean for location to be `int location[2]`? – acraig5075 Apr 07 '21 at 06:28
  • You have a function named `locateLargest`. You may want to answer these questions. Do you want it to **return** the location or to **print** the location? If you want to return a location, what **is** a location (how you represent it)? – n. m. could be an AI Apr 07 '21 at 08:26

2 Answers2

2

You can keep track of the max value's indices while iterating through the matrix.

void max_idx(const double (&arr)[RS][CS]) {
  double curr_max = arr[0][0];
  size_t max_i = 0, max_j = 0;

  for (size_t i = 0; i < RS; ++i) {
    for (size_t j = 0; j < CS; ++j) {
      if (curr_max < arr[i][j]) {
        curr_max = arr[i][j];
        max_i = i;
        max_j = j;
      }
    }
  }
  cout << "Largest value is at (i=" << max_i << ", j=" << max_j << ")\n";
}

Demo

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • 1
    A replacement for the two-level for-loop is using `std::max_element` and regard it as one large array, the get the row and column number by index calculation – prehistoricpenguin Apr 07 '21 at 07:04
  • @prehistoricpenguin "and regard it as one large array" this would be undefined behaviour. In order to regard it as one large array, it needs to *be* one large array. – n. m. could be an AI Apr 07 '21 at 10:54
  • @n.'pronouns'm. The reason for undefined behaviour here that I can recall is that the argument is cast from some large 2-d array, then the memory layout does not continue for different rows. I think it's not the case in this question. – prehistoricpenguin Apr 07 '21 at 12:06
  • @prehistoricpenguin It is not a question of memory layout. The behaviour is undefined simply because the standard says so. It is discussed in multiple posts on this site e.g. [this](https://stackoverflow.com/questions/7269099/may-i-treat-a-2d-array-as-a-contiguous-1d-array) and many of its duplicates (be sure to read all the answers and comments). – n. m. could be an AI Apr 07 '21 at 12:48
  • @n.'pronouns'm. Thank you so much, I have read the answers and comments, now I know it's a common mistake. – prehistoricpenguin Apr 07 '21 at 12:58
0

First of all, you have to make sure that your code is consistent : in the prototype of your locateLargest function, location is a one-dimensional array but in your main() function it is a two-dimensional one.

This is how I would write this :

#include <iostream>
#include <iomanip>

using namespace std;

void locateLargest(double** a, int* location);

const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;

int main()
{
    int location [2];
    double* matrix [ROW_SIZE];
    for(int s= 0; s< ROW_SIZE; s++)
    {
        matrix[s]= new double[COLUMN_SIZE];
    }

    double input;

    cout<<"Enter the array: "<< endl;

    for (int i = 0; i < ROW_SIZE; i++)
    {
        for(int j = 0; j < COLUMN_SIZE; j++)
        {
            cin>>input;
            matrix[i][j] =  input;
        }
    }

    for(int i = 0; i < ROW_SIZE; i++)
    {
        for(int j = 0; j < COLUMN_SIZE; j++)
        {
            cout<< setw(4)<<matrix[i][j]<< " ";
        }
         cout<< endl;
    }

    locateLargest(matrix, location);
}

void locateLargest(double** a, int* location)
{
    int i, j;
    double maxVal= a[0][0]; location[0]= location[1]= 0;

    for(i = 0;i < ROW_SIZE; i++)
    {
        for(j = 0; j < COLUMN_SIZE; j++)
        {
           if(maxVal < a[i][j])
            {
               location[0] = i;
               location[1]= j;
               maxVal= a[i][j];
            }
        }
    }
    cout << "The location of the largest element is at ("<< location[0] << " , "<<
    location[1] <<" ) . it is : "<< maxVal<<endl;
}

max represents the maximum value of your matrix's elements, you first set it to be equal to the first element and then compare it to each element of the matrix. Each time you find an element that is larger than max, you assign his value to max and his position to location and at the end of the iterations, you have the largest value and his location.

mqbaka mqbaka
  • 235
  • 1
  • 7