Can any one help me with this code?

abbd1990

abbd1990

@abbd1990-WLnI62 Oct 25, 2024

hi to all
please can u help me to find the problems in this program

this program will find the min element in quarter1 of square array and the max element of quarter3 and replace them

q1 , q2
q4 , q3

#include<stdio.h>
main()
{
 int a[10][10],i,j,min,max,lmin,lmax,n,m ;
 scanf("%d",&n) ;
 scanf("%d",&m) ;
 for (i=0;i<n;i++)
  for (j=0;j<m;j++)
   scanf("%d",&a[i][j]) ;
 for(i=0;i<n/2;i++) 
  for(j=0;j<m/2;j++)
   min = a[i][j] ;
  for(i=0;i<n/2;i++) 
  for(j=0;j<m/2;j++)
  if ( min > a[i][j] )
   a[i][j]=min ;
   lmin=a[i][j] ;
  for(i=n/2;i<n;i++)
  for(j=m/2;j<m;j++)
  max = a[n/2][m/2] ;
    for(i=n/2;i<n;i++)
  for(j=m/2;j<m;j++)
  if ( max < a[i][j] ) 
  {
   a[i][j] = max ;
   lmax=a[i][j] ;
  }
  a[i][j] = lmax; 
  lmax = lmin ;
  lmin = a[i][j] ;
  for(i=0;i<n;i++)
  {
  for(j=0;j<m;j++)
   printf("%d",a[i][j] ) ;
  printf("\n") ;
  }
}


thank u

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • silverscorpion

    silverscorpion

    @silverscorpion-iJKtdQ Mar 22, 2009

    ah, you seem to have messed up the program in many places. I'll indicate the mistakes. Convince yourself that they are wrong..

    Lines 10,11,12 are not required. If they are executed, they'll simply assign a[n/2,m/2]
    to be the minimum number, which is not true. Instead, initialise min to a high number. Similarly for lines 18,19,20. Remove them and set max to a small number, say zero at the start.

    line 16: It should be min=a[j];

    line 17: It should be lmin=min;

    line 25: It should be max=a[j];

    line 26 is not required.
    there are many more. I'm posting the correct version of this program below.. Check it up.

  • silverscorpion

    silverscorpion

    @silverscorpion-iJKtdQ Mar 22, 2009

    #include<stdio.h>
    main()
    {
    int a[10][10],i,j,min=99,max=-99,lmin,lmax,n,m ;
    scanf("%d",&n); scanf("%d",&m) ;
    for (i=0;i<n;i++)
    for (j=0;j<m;j++)
    scanf("%d",&a[j]) ;
    for(i=0;i<n/2;i++)
    for(j=0;j<m/2;j++)
    if ( min > a[j] )
    min=a[j];
    lmin=min;
    for(i=n/2;i<n;i++)
    for(j=m/2;j<m;j++)
    if ( max < a[j] )
    max=a[j] ;
    lmax=max;
    for(i=0;i<n;i++)
    {
    for(j=0;j<m;j++)
    printf("%d\t",a[j] ) ;
    printf("\n") ;
    }
    for(i=0;i<n/2;i++)
    for(j=0;j<m/2;j++)
    if(a[j]==lmin)
    a[j]=lmax;
    for(i=n/2;i<n;i++)
    for(j=m/2;j<m;j++)
    if(a[j]==lmax)
    a[j]=lmin;
    printf("\n\n\n") ;
    for(i=0;i<n;i++)
    {
    for(j=0;j<m;j++)
    printf("%d\t",a[j] ) ;
    printf("\n") ;
    }
    }



    I've not commented the code as i'm sure you can understand it without trouble.
    Cheers!!