×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• 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.

# 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

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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!