×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help with multi-diemensional array

Help with multi-diemensional array

Help with multi-diemensional array

(OP)
Hello,

I have a problem with the following code

int main()
{
int i,j,max;
int a[3][3]={{85,99,77},
{80,73,74},
{89,33,88}};

for (i=0;i<3;i++){
max=0;
printf (" \n");
for (j=0;j<3;j++)
if (max<a[i][j])
max=a[i][j];
printf ("%d the maximum is %i ",a[i][j],max);
}
}

I want the output to be the ALL numbers in arrays and the maximum number of each row I tried and tried but I couldn't know where is the problem,and here is the output
80 the maximum is 99
89 the maximum is 80
0 the maximum is 99

any help pls

RE: Help with multi-diemensional array

The 3rd line of your output is:
... is 89
(please, look at your program output again;)

It's a wrong way to get max row values: it does not work for negative values. Right way:

CODE

#define N 3
int maxval;
int a[N][N] = { ...
...
for (i = 0; i < N; ++i) {
    maxval = a[i][0];
    for (j = 1; j < N; ++j) {
        if (maxval < a[i][j])
            maxval = a[i][j];
    }
    printf("Row %d: maxval %d\n",i,maxval);
} 
Yet another mistake: reference to i,j-th element after loop in printf call. Think: j == 3 after loop was terminated and you are trying to print incorrectly indexed array elements:

CODE

a[0][3] ... a[1][3] ... a[2][3] 

Next time use CODE tag for your snippets. Press help (?) button on the posting form to get help on this forum TGML layout tags...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close