Doubt in Sample Ques 7

48 views
Skip to first unread message

Rohan Saluja

unread,
Dec 16, 2020, 3:39:02 PM12/16/20
to Discussion forum for Introduction to Programming in C
Can anyone please explain what is going on in this function?
Why Blank 4 is 1?
Doubt Sample q7.jpg

rs rk

unread,
Dec 17, 2020, 9:17:16 AM12/17/20
to Discussion forum for Introduction to Programming in C, Rohan Saluja
Yes, the code seems to be incorrect.
#include <stdio.h>
int is_contiguous ( int a[], int size )
{
  int i;
  for(i=0;i<size-1;i++){
    if(a[i+1]-a[i]!=1){ /* not strictly increasing */
    break;
    }
  }
  if ( i == size ){ /* strictly increasing */ //should be size-1
  return 0;//should be return 1;
  }
  if ( i == 0){
    for(i=0;i<size-1;i++){
      if(a[i]-a[i+1]!=1){ /* not strictly decreasing */
        return 0;
      }
  }
  return 1;//should be return 0;
  } else{
      return 0;
    }
}
int main() {
  int a[5];
  int size = 5;
  a[0] = 1;
  a[1] = 2;
  a[2] = 3;
  a[3] = 4;
  a[4] = 5;
  int ans;
  ans = is_contiguous(a, size);
  printf("%i\n", ans);// 0 is printed but as per the question 1 should be printed
  return 0;
}

Reply all
Reply to author
Forward
0 new messages