Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
#include <iostream.h>
#include <conio.h>
void main()
  {
  clrscr();
  int a[2][3],row,col,max,p=0;
  for(row=0;row<2;row++)
    {
    for(col=0;col<3;col++)
      {
      cout<<"Enter values ";
      cin>>a[row][col];
      }
    }
  max=a[0][0];
  for(row=0;row<2;row++)
    {
    for(col=0;col<3;col++)
      {
      if(max<a[row][col])
        max=a[row][col];
       
      if(max==a[row][col])
        {
        p=row+col+1;
         
        }
      }
    }
  cout<<"Maximum value is "<<max<<endl;
  cout<<"Poistion "<<p;
  getch();
  }
 
The above program find the maximum value from 2 dimensional array
and also find the position of maximum value.
This program correctly finds the maximum value but not correctly finds the position of maximum value.
 
[edit]Code indented from flat version - OriginalGriff[/edit]
Posted 15-Dec-12 22:54pm
ALIWAZ407
Edited 15-Dec-12 23:02pm
v2
Comments
OriginalGriff at 16-Dec-12 5:05am
   
In what way does it not find the correct position? What is it giving and what were you expecting - try to give us an example.
jibesh at 16-Dec-12 5:05am
   
why do you need another check condition for setting the position. you may add the position assignment under max =a[row][col]; also row+col+1 ?? is that row +col?
OriginalGriff at 16-Dec-12 5:27am
   
Think about it - it finds the last instance of the max, not the first!

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The statement
      p=row+col+1;
is your cause of trouble. You are trying to squeeze a two-dimensional position into a single int value. That's possible, but in that case the mapping should be unique. With your mapping method position (1,2) and (2,1) would both be mapped to the same value of p.
 
Try the following instead:
    p = row * W + col;
W is the number of elements per row, in your case 3. Now every element of your array will produce a distinct p value.
 
Besides, the +1 in your method was probably just a way of numbering elements starting with 1 instead of 0. That might be a good idea for human output, but not if you are using the value of p in your program. Zero-based index values are a lot more convenient, particularly when you know that all your index values in a program are zero-based. In other words: Don't mix zero- and one-based.
 
As an alternative, you can store the position in two separate int variables, for example:
    int xMax = -1;
    int yMax = -1;
 
    // ... your nested loop

         if (a[row][col] > max)
         {
             max = a[row][col];
             xMax = col;
             yMax = row;
         }
 
 
Hope that helps.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 16 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100