# problem finding the largest number from a selected row of an matrix

Page 1 of 1

## 5 Replies - 906 Views - Last Post: 15 February 2012 - 09:20 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=266716&amp;s=22267fa0ae581a77aab9e70293d59059&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 horsx

Reputation: 0
• Posts: 2
• Joined: 14-February 12

# problem finding the largest number from a selected row of an matrix

Posted 14 February 2012 - 05:40 PM

Hi!
im trying to find the largest number from a selected row of an matrix. My code is ok until i insert negative numbers. I don't know what to write for solvinng this...

```#include<iostream>
#include<cstdlib>
using namespace std;
int main ()
{

int rang=10,i,j, M[rang][rang],n,k,max=0;

do{

do{cout<<"Insert the matrix rank(max. 10): "; cin>>n; }
while(n<1||n>rang);

cout<<"Insert elements: "<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++){cout<<"M["<<i<<"]["<<j<<"]= "; cin>>M[i][j];}
cout<<endl;

for(int i=0; i<n;i++){ for(int j=0;j<n;j++) cout<<"\t"<<M[i][j]; cout<<"\n\n";}

do{ cout<<"Select a row: "; cin>>k;}
while(k<0||k>n);

for(j=0;j<n;j++)
if (max<M[k-1][j]){max=M[k-1][j];}

cout<<"The largest number of the "<<k<<"  row is "<<max<<endl ;

cout << "Try again? (Y/N): ";
cout << endl;
}

}
```

Is This A Good Question/Topic? 0

## Replies To: problem finding the largest number from a selected row of an matrix

### #2 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: problem finding the largest number from a selected row of an matrix

Posted 14 February 2012 - 06:09 PM

'max' starts out at 0 so it will never be less than a negative value hence it will always say '0' if all the values are negative; you need to set 'max' to the first value in the row before the loop. that way 'max' starts out with a possible answer and not something invalid(like 0 in an all negative row).

while that is a problem you have several more.

1) you need an indentation style; i took the liberty of formatting your code(so I could read it)
```#include<iostream>
#include<cstdlib>

using namespace std;

int main () {
int rang=10,i,j, M[10][10],n,k,max=0;
do{
do {
cout<<"Insert the matrix rank(max. 10): "; cin>>n;
} while(n < 1 || n > rang);

cout<<"Insert elements: "<<endl;
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
cout<<"M["<<i<<"]["<<j<<"]= ";
cin>>M[i][j];
}
}
cout<<endl;

for(int i=0; i<n;i++) {
for(int j=0;j<n;j++) {
cout<<"\t"<<M[i][j];
}
cout<<"\n\n";
}

do {
cout<<"Select a row: ";
cin>>k;
} while(k<0||k>n);

for(j=0;j<n;j++) {
if (max<M[k-1][j]) {
max=M[k-1][j];
}
}
cout<<"The largest number of the "<<k<<"  row is "<<max<<endl ;
cout << "Try again? (Y/N): ";
cout << endl;
}

```

2) you can't create an array with a value not known at compile time.
```int rang=10,i,j, M[rang][rang],n,k,max=0;
```

that is illegal in all standards of C++, only C99 and C11 offer variable length arrays.
```const int rang = 10;
int i, j, M[rang][rang], n, k, max=0;

```

then rang is known at compile time this way.

3) you redeclare(shadow) i and j a few times; just declare them were they are used(in for loops). shadowing variables leads to bad things.

This post has been edited by ishkabible: 14 February 2012 - 06:14 PM

### #3 horsx

Reputation: 0
• Posts: 2
• Joined: 14-February 12

## Re: problem finding the largest number from a selected row of an matrix

Posted 15 February 2012 - 03:37 AM

Thanks for the answer and for solving the other problems.
So from what you are saying the code should be
```max=M[k-1][j]
for(int j=0;j<n;j++) {
36
if (max<M[k-1][j]) {
37
max=M[k-1][j];
38
}
39
}
40
cout<<"The largest number of the "<<k<<"  row is "<<max<<endl ;

```

I have another question. If i type a character when i insert the matrix rank or elements i'm stuck in a infinite loop. What should i do to avoid that?

### #4 jimblumberg

Reputation: 5441
• Posts: 16,967
• Joined: 25-December 09

## Re: problem finding the largest number from a selected row of an matrix

Posted 15 February 2012 - 07:30 AM

ishkabible, on 14 February 2012 - 07:09 PM, said:

2) you can't create an array with a value not known at compile time.
```int rang=10,i,j, M[rang][rang],n,k,max=0;
```

that is illegal in all standards of C++, only C99 and C11 offer variable length arrays.

Variable Length Arrays are only guaranteed in C99, in C11 support is not required. See the following section of the C11 draft standard.

Quote

6.10.8.3 Conditional feature macros
The following macro names are conditionally deﬁned by the implementation:
...
_ _STDC_NO_VLA_ _ The integer constant 1, intended to indicate that the
implementation does not support variable length arrays or variably
modiﬁed types.

Jim

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: problem finding the largest number from a selected row of an matrix

Posted 15 February 2012 - 07:33 AM

Quote

I have another question. If i type a character when i insert the matrix rank or elements i'm stuck in a infinite loop. What should i do to avoid that?

### #6 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: problem finding the largest number from a selected row of an matrix

Posted 15 February 2012 - 09:20 AM

@Jim: good catch, I guess when I read about it I didn't look closely enough at it.