Thursday, September 13, 2018

STRUCTURE 33


  • Problem Description

    Declare a structure fraction that has two fields numerator and denominator .Create two variables and and compare them using function. Return 0 if the two variable are equal ,-1 if the fraction is less than the second and 1 otherwise. You may convert a fraction into a floating point number for your convenience

    Input and Output Format:

    Refer sample input and output for formatting specification.

    All float values are displayed correct to 2 decimal places.

    All text in bold corresponds to input and the rest corresponds to output.
  • CODING ARENA
  • #include <stdio.h>
    struct fraction
    {
      int n,d;
    };
    int compare(struct fraction a,struct fraction b)
    {
      float val1=(float)(a.n)/(float)(a.d);
      float val2=(float)(b.n)/(float)(b.d);
      if(val1<val2)
        return -1;
      else if(val1==val2)
        return 0;
      else
        return 1;
    }
    int main()
    {
      struct fraction a,b;
      scanf("%d%d",&a.n,&a.d);
      scanf("%d%d",&b.n,&b.d);
      int val=compare(a,b);
      if(val==0)
        printf("%d/%d is equal to %d/%d",a.n,a.d,b.n,b.d);
      else if(val==-1)
        printf("%d/%d is smaller than %d/%d",a.n,a.d,b.n,b.d);
      else 
        printf("%d/%d is greater than %d/%d",a.n,a.d,b.n,b.d);

    return 0;
    }
  • Test Case 1

    Input (stdin)
    12 12
    
    12 22
    
    
    Expected Output
    12/12 is greater than 12/22
  • Test Case 2

    Input (stdin)
    23 44
    
    99 23
    
    
    Expected Output
    23/44 is smaller than 99/23

2 comments: