AedenCak3

(Q-3-2) DNA ratio of 'b' and 'c'

Dec 17th, 2021 (edited)
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. double ratio(char arr[])
  4. {
  5.     int b=0,c=0;
  6.     double ratio;
  7.     for(int i=0;i<100;i++)
  8.         {
  9.             if(arr[i]=='c')
  10.                 c++;
  11.             else if(arr[i]=='b')
  12.                 b++;
  13.         }
  14.     if(c==0)
  15.         return -1;
  16.     else
  17.         ratio=(double)b/c;
  18.  
  19. return ratio;
  20.  
  21.  
  22. }
  23.  
  24.  
  25. int main()
  26. {
  27.     char DNA[100];
  28.     printf("Input: ");
  29.     gets(DNA);
  30.  
  31.     double result=ratio(DNA);
  32.     if(result==-1)
  33.     {
  34.         printf("All characters are 'b'");
  35.          return 0;
  36.     }
  37.     else
  38.         printf("Ration of 'b' to 'c' is: %.3lf", result);
  39.  
  40.     return 0;
  41. }
  42.  
  43. &&
  44.  
  45. #include <stdio.h>
  46. void ratio(char str[])
  47. {
  48.     int count1 = 0, count2 = 0, x, y;
  49.  
  50.     for (int i = 0; i < 100; i++)
  51.     {
  52.         if (str[i] == 'b')
  53.         {
  54.             count1++;
  55.         }
  56.     }
  57.     for (int i = 0; i < 100; i++)
  58.     {
  59.         if (str[i] == 'c')
  60.         {
  61.             count2++;
  62.         }
  63.     }
  64.  
  65.     float r;
  66.     r = count1 / count2;
  67.  
  68.     printf("Ratio b and c is %d : %d or %.2f", count1, count2, r);
  69. }
  70.  
  71. int main()
  72. {
  73.     char DNA[100];
  74.  
  75.     printf("Enter random numbers of b and c:\n");
  76.     scanf("%s", DNA);
  77.  
  78.     ratio(DNA);
  79.  
  80.     return 0;
  81. }
Add Comment
Please, Sign In to add comment