user3424954
user3424954

Reputation: 75

finding minimum of a subarray while the value might be only 1 or 2 or 3

I have a problem with the following code:

#include<stdio.h>
main()
  {
  int len,no,n,a,result;
  int start[100],end[100];
  int width[100];

  printf("enter the lenghth of the road : \n");
  scanf("%d",&len);

  printf("enter the width of the road ,in unitwise format for the given length : \n");
  for(n=0;n<len;n++)
    scanf("%d",&width[n]);

  printf("enter the no of testcases : \n");
  scanf("%d",&no);

  printf("for each testcase enter the starting and  end point : \n");
  for(n=1;n<=no;n++){
    printf("for testcase %d enter the start and end point respectively",n);
    scanf("%d %d",&start[n],&end[n]);}

    for(n=1;n<=no;n++){
        for(a=start[n];a<=end[n];a++){
        result=3;
        if(width[a]<result)
          result=width[a];
        }

      printf("for testcase %d the vehicle that can be used is %d",n,result);
      printf("\n");}
    }

I am not getting the right result although I feel I have applied right logic in finding minimum width. What is wrong with this logic?

Upvotes: 1

Views: 41

Answers (1)

ruhungry
ruhungry

Reputation: 4706

Put this line

result=3;

before your loop.

Upvotes: 1

Related Questions