Here the val should print the max number but its printing some address. When i tried to print while in loop, it printed the largest number but in the next line it again printed the address.

please tell me whats wrong.
Thanks

#include<stdio.h>
#include<conio.h>
int main()
{
  int arr[5][5]={ 2,4,8,11,15,
                  6,8,5,8,7,
                  3,5,1,21,72,
                  28,95,62,8,2,
                  4,1,7,6,8};
  int i,j,val;
  
  val=arr[0][0];
  for(i=0;i<5;i++)
  {
     for(j=0;j<5;j++)
     if(val<arr[i][j+1])
      {
        val=arr[i][j+1];
      }
                          
    else
     {
       continue;  
     }
  }
printf("\n %d ",val); 
getch();
}

A few notes, this is C so main return an integer and you should define your array like below.

#include<stdio.h>

int main()
{
  int arr[5][5]={ {2,4,8,11,15},{6,8,5,8,7},{3,5,1,21,72},{28,95,62,8,2},{4,1,7,6,8}};
  int i,j,val;
  
  val=arr[0][0];
  for(i=0;i<5;i++)
  {
     for(j=0;j<5;j++)
     if(val<arr[i][j+1])
      {
        val=arr[i][j+1];
      }
  }
printf("\n %d ",val); 
return 0;
}

This worked correctly on my machine.

You're letting j run outside the boundary of the array on the last loop.

Keep it < 5, always. Also, < should be > in your comparison, if you want the largest number. Otherwise, you'll get the smallest number in the array.

You badly initialized your 2 dimensional array that's all. You should initialize it as gerard4143 did.
Though for some reasons your program worked, try not too run outside the boundary of an array becuase you will get weird numbers in some cases. So compare like this

if(val<arr[i][j])
      {
        val=arr[i][j];
      }

and not

if(val<arr[i][j+1])

Sorry, mkab. That won't work.

j+1 is the standard C idiom used in bubble sort, and all kinds of other algorithms that check adjacent elements in an array.

if SIZE = size of the array, then

j = 0; j < SIZE; j++ is simply wrong.
j = 0; j < SIZE - 1; j++, then array[j] can be correctly compared with array[j+1]

Any reference to array is an error, in C, and no logic exists that will make it correct.

Yes, his program may run right, since errors in C do not guarantee a program will crash. It may even run correctly, for a long time (in some cases, the bug is never detected).

With a larger array, I believe I could crash his program in a heartbeat, but that's irrelevant. We want the logic to be correct.

i agree with adak .
the bug is in your second for loop, it goes out of range of array and access to memory location or we can say take a garbage value ......

There were three bugs:

1) Gerard found the big bug, which was the improper initialization of the array values.

2) The index out of range bug

and

3) The < operator needed to be changed to > if you wanted the largest number in the array.

The #2 error was not causing a run-time problem, but you see this all the time with C programs. The pointer or index for the array, goes out of bounds.

Asserts can help keep that down, but everyone should know that special care must be taken to test that the index referencing the array, must be carefully tested at the low and the high ends of the array range, for this type of error.

Thanks! to all ...................... :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.