5 Replies - 500 Views - Last Post: 15 February 2012 - 09:20 AM Rate Topic: -----

#1 horsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
char answer;

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): "; 
cin >> answer;
cout << endl;
}
while(answer == 'Y' || answer == 'y');

}


Is This A Good Question/Topic? 0
  • +

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

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • 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;
	char answer;
	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): ";
		cin >> answer;
		cout << endl;
	} while(answer == 'Y' || answer == 'y');
}




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.
instead it should be...
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

Was This Post Helpful? 1
  • +
  • -

#3 horsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,424
  • Joined: 25-December 09

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

Posted 15 February 2012 - 07:30 AM

View Postishkabible, 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 defined by the implementation:
...
_ _STDC_NO_VLA_ _ The integer constant 1, intended to indicate that the
implementation does not support variable length arrays or variably
modified types.


Jim
Was This Post Helpful? 1
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • 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?

FAQ Link
Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1