Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,550 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,560 people online right now. Registration is fast and FREE... Join Now!




array c++ help ?

2 Pages V  1 2 >  
Reply to this topicStart new topic

array c++ help ?

tikotiko
12 Nov, 2007 - 07:21 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
Write a function that will receive a two-dimensional array (matrix) A of integers and, then, determine those elements of the array that their values are maximum in their respective rows and columns. Assume that the stored values are distinct. Print the subscripts of those elements. Example,

3 4 8 5

2 9 10 1

7 12 6 11



A[1][2]= 10 and A[2][1]= 12 satisfy the requirements and their subscripts should be printed.





CODE
#include<iostream.h>
#include<conio.h>

int matrix[3][4],row,col,maxrows=3,maxcols=4;
int maxrow1,maxrow2,maxrow3,maxcol1,maxcol2,maxcol3,maxcol4;

main()
{

/* enternig the martix elements */

for(row=0;row<maxrows;row++)
{
    for(col=0;col<maxcols;col++)

    {
    cout<<"Enter the Array Elements=["<<row<<","<<col<<"] ";
    cin>>matrix[row][col];

    }
    }


/* matrix completed */

/* dislay the elements of the matrix with sub-scripts */

cout<< "The values Entered into Matrix are="<<endl;

  for(row=0;row<maxrows;row++)
{
    for(col=0;col<maxcols;col++)

    {
    cout<<"\t"<<matrix[row][col];

    }
          cout<<endl;
    }


/* maximum element of row1 is displayed */

maxrow1=matrix[0][0];
for(int i=0;i<4;i++)
{
if( matrix[0][i]>matrix[0][0])
{
  int temp=matrix[0][i];
    maxrow1=temp;

   }
}
cout<<"\nMaxrow1 element="<<maxrow1;


/* maximum element of row2 is displayed */

maxrow2=matrix[1][0];
for(int i=0;i<4;i++)
{
if( matrix[1][i]>matrix[1][0])
{
  int temp=matrix[1][i];
    maxrow2=temp;

   }
}
cout<<"\nMaxrow2 element="<<maxrow2;

/* maximum element of row3 is displayed */

maxrow3=matrix[2][0];
for(int i=0;i<4;i++)
{
if( matrix[2][i]>matrix[2][0])
{
  int temp=matrix[2][i];
    maxrow3=temp;

   }
}
cout<<"\nMaxrow3 element="<<maxrow3<<endl;;



/* maximum colums */

/* maximum element of col1 is displayed */

maxcol1=matrix[0][0];
for(int i=1;i<3;i++)
{
if( matrix[i][0]>matrix[0][0])
{
  int temp=matrix[i][0];
    maxcol1=temp;

   }
}
cout<<"\nMaxcol1 element="<<maxcol1;


/* maximum element of col2 is displayed */

maxcol2=matrix[0][1];
for(int i=1;i<3;i++)
{
if( matrix[i][1]>matrix[0][1])
{
  int temp=matrix[i][1];
    maxcol2=temp;

   }
}
cout<<"\nMaxcol2 element="<<maxcol2;

/* maximum element of col3 is displayed */

maxcol3=matrix[0][2];
for(int i=1;i<3;i++)
{
if( matrix[i][2]>matrix[0][2])
{
  int temp=matrix[i][2];
    maxcol3=temp;

   }
}
cout<<"\nMaxcol3 element="<<maxcol3;

/* maximum element of col4 is displayed */

maxcol4=matrix[0][3];
for(int i=1;i<3;i++)
{
if( matrix[i][3]>matrix[0][2])
{
  int temp=matrix[i][3];
    maxcol4=temp;

   }
}
cout<<"\nMaxcol4 element="<<maxcol4;


    return 0;
}




the program is not working properly , any help would be much appreciated.

This post has been edited by jjhaag: 12 Nov, 2007 - 07:46 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Array C++ Help ?
12 Nov, 2007 - 07:45 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Please post source code using the code tags like this: code.gif. I've changed it in your orginal post, but remember for next time.

In what way is it not working? Compiler errors? Linkers errors? Unexpected output? We can probably give better and faster help if you gave a better idea of what's giving you trouble.
User is offlineProfile CardPM
+Quote Post

tikotiko
RE: Array C++ Help ?
12 Nov, 2007 - 08:07 PM
Post #3

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
QUOTE(jjhaag @ 12 Nov, 2007 - 08:45 PM) *

Please post source code using the code tags like this: code.gif. I've changed it in your orginal post, but remember for next time.

In what way is it not working? Compiler errors? Linkers errors? Unexpected output? We can probably give better and faster help if you gave a better idea of what's giving you trouble.






the output I got is not what I have to look for . I need to determine the elements of the array that their values are maximum in their respective rows and columns and to print out their subscripts..
User is offlineProfile CardPM
+Quote Post

tikotiko
RE: Array C++ Help ?
13 Nov, 2007 - 08:57 AM
Post #4

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
any help to solve this problem ???
User is offlineProfile CardPM
+Quote Post

tikotiko
RE: Array C++ Help ?
14 Nov, 2007 - 08:36 PM
Post #5

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
Any help guys on how to solve this problem. blink.gif
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Array C++ Help ?
14 Nov, 2007 - 08:56 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
You've got a problem with your comparison statements. You assign the value of the first element in a given row or column to a test variable prior to looping through the rest of them, which is good. However, you then ignore that variable in the test conditions, and instead compare the value to the first element in the array. That would only work if the values are ascending in all rows and columns. You need to compare the current value to the maximum value instead - not just the first value.

A corrected subset is shown below to give you an idea:
CODE
    maxrow1=matrix[0][0];
    for (int i=0; i<4; i++) {
        if (matrix[0][i]>maxrow1) {
            maxrow1=matrix[0][i];
        }
    }


You also had a weird temporary variable in there in your assignment that you didn't need - you can assign the value in the current position directly to the maxrowX variable, without an intermediary.

You'll need to correct each of your comparisons, but that should give you an idea how to go about it. Hope it helps,

-jjh
User is offlineProfile CardPM
+Quote Post

tikotiko
RE: Array C++ Help ?
14 Nov, 2007 - 09:29 PM
Post #7

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
thanks jjhaag , but I'm not getting the right output I need to determine the elements of the array that their values are maximum in their respective rows and columns and to print out their subscripts..
A[1][2]= 10 and A[2][1]= 12 satisfy the requirements and their subscripts should be printed
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Array C++ Help ?
14 Nov, 2007 - 09:39 PM
Post #8

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
QUOTE(tikotiko @ 14 Nov, 2007 - 10:29 PM) *

thanks jjhaag , but I'm not getting the right output I need to determine the elements of the array that their values are maximum in their respective rows and columns and to print out their subscripts..
A[1][2]= 10 and A[2][1]= 12 satisfy the requirements and their subscripts should be printed


Well, those values will be printed...along with the maxima for each of the other rows and columns. If the problem is that you aren't outputting the subscripts, you'll need to store the appropriate subscript when you find the maximum - much as you did with the actual maximum value, but with the indices this time. Create some new variables to store the indices (one for the row index and one for the column index, for each of your maxrowX and maxcolX variables), and store the row and column index each time you find a higher value.
User is offlineProfile CardPM
+Quote Post

tikotiko
RE: Array C++ Help ?
15 Nov, 2007 - 11:35 AM
Post #9

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
Hi jjhaag I'm not getting the right output:
this is a screen_shot of what I got , but I need to get like this A[1][2]= 10 and A[2][1]= 12 satisfy the requirements and their subscripts should be printed.





Attached File(s)
Attached File  screen_shot.bmp ( 1004.05k ) Number of downloads: 30
User is offlineProfile CardPM
+Quote Post

tikotiko
RE: Array C++ Help ?
21 Nov, 2007 - 10:01 PM
Post #10

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
I'm only getting the maximum of the rows and the naximum of the colouns buy what I'm looking for is to get the numbers that are maimmum in ther respective rows and colouns like in this example :

10 is maximum in the its respective row (2) and its respective colouns (3) therefore the program should output
A[1][2]=10 , and same thing for A[2][1]=12 .

any help would be much appreciated .
User is offlineProfile CardPM
+Quote Post

juti
RE: Array C++ Help ?
22 Nov, 2007 - 01:02 AM
Post #11

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 9


My Contributions
I dont' think you really need so much code. Here is my proposal. I hope it is reasonable to you. If not, you can ask me.
CODE

#include <iostream>
#include<conio.h>
using namespace std;
int matrix[3][4],row,col,maxrows=3,maxcols=4;
int position, counter,maxrow;

main()
{

/* enternig the martix elements */

for(row=0;row<maxrows;row++)
{
    for(col=0;col<maxcols;col++)

    {
                                
    cout<<"Enter the Array Elements=["<<row<<","<<col<<"] ";
    cin>>matrix[row][col];

    }
    }


/* matrix completed */

/* dislay the elements of the matrix with sub-scripts */

cout<< "The values Entered into Matrix are="<<endl;

  for(row=0;row<maxrows;row++)
{
    for(col=0;col<maxcols;col++)

    {
    cout<<"\t"<<matrix[row][col];

    }
          cout<<endl;
    }


/* maximum element of row1 is displayed */
for(int j=0;j<3;j++)
{   position=0;
    counter=0;
    maxrow=matrix[j][0];
    for(int i=0;i<4;i++)
    {
       if( matrix[j][i]>maxrow)
       {
          maxrow=matrix[j][i];
          position=i;
       }

    }
    for(int k=0;k<3;k++)
    {
            if(maxrow>matrix[k][position])
              counter++;
    }
    if (counter==2)
            cout<<"A["<<j<<","<<position<<"]="<<maxrow<<endl;
}


    return 0;
}

User is offlineProfile CardPM
+Quote Post

tikotiko
RE: Array C++ Help ?
22 Nov, 2007 - 04:25 AM
Post #12

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 32


My Contributions
thank you so much I got it now..

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/7/09 09:44PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month