1 Question 1 You are given a sorted array of integers The nu

1. Question 1: You are given a sorted array of integers. The numbers in the array are pairwise distinct, namely no value appears more than once. Your goal is to nd if there is an index 1 k n so that A[k] = k. (Assume that the rst index is 1).

Solution

#include <stdio.h>

int main(void)
{
   int i;
   int n;
   int a[1000];
   printf(\"enter no. of integers in array\ \");
   scanf(\"%d\",&n);
   printf(\"enter %d no.s in sorted order\ \",n);
   for(i = 1; i < n; i++)
   {
      scanf(\"%d\ \", &a[i]);
   }
   printf(\"\ \");
   for(i = 1; i <= n; i++)
   {
      if(a[i] == i)
      {
         printf(\"yes index %d = a[%d]\ \",i,i);
      }
      else
      {
         printf(\"no index %d != a[%d]\ \",i,i);
      }
   }
}

The above program is written in c language. First, scan the number of integers user want to check. Then, provide read the numbers input by users using scanf() function. To check if the given number is equal to its index or not, check each number using if-else statements. If the number is equal to index, the output will be \"yes\", if the number is not equal to index, the output will be \"no\".

1. Question 1: You are given a sorted array of integers. The numbers in the array are pairwise distinct, namely no value appears more than once. Your goal is to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site