Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. // Solved: P1. Declare an integer with name (v) and initialize it with 2, in the global scope.
  5. int v = 2;
  6.  
  7. // P2. Declare  an integer with name (u) and initialize it with v + 5, in the global scope.
  8. int u=v+5;
  9.  
  10.  
  11. // Solved: P3. Given the function declaration (header), return the square of a given double (x).
  12. double square( double x )
  13. {
  14.     return x * x;
  15. }
  16.  
  17. // P4. Given the function declaration (header), return the cube of a given double (x).
  18. double cube( double x )
  19. {
  20.     return x * x * x;
  21. }
  22.  
  23. // P5. Using for or while loop, compute the summation of integers from 1 to 15.
  24. // Hints: before entering the loop, you need to declare and initialize a variable to accumulate the values.
  25. int sumOfFirst15Integers (int sum)
  26. {
  27.    sum=0; int i=0;
  28.    while(i<=15)
  29.    {
  30.        sum+=i;
  31.        i++;
  32.    }
  33.    return sum;
  34. }
  35.  
  36. // P6. Using while loop, compute the summation of first 15 odd integers (starting from 1).
  37. // Hints: before entering the loop, make a 3 variables:
  38. // 1- Counter for iteration over integers starting from 1,
  39. // 2- Accumulater for summing odd integers,
  40. // 3- And a variable that counts the odd integers, so the loop terminates once it reaches 15.
  41.  
  42. // Hints: use the expression ( x % 2 ) == 1 in if conditions to determine whether (x) is odd or even.
  43. int sumOfFirst15OddIntegers(int sum)
  44. {
  45.     sum=0; int i=0; int j=0;
  46.         while(j<15)
  47.         {
  48.             if(i%2==0)
  49.             {
  50.                 i++;
  51.             }
  52.             else
  53.             {
  54.                 sum+=i;
  55.                 i++;
  56.                 j++;
  57.             }
  58.          
  59.         }
  60.     return sum;
  61. }
  62.  
  63. // You know that our DNA is double stranded. Each base has its complementary base.
  64. // Adenine (A) binds to Thymine (T)
  65. // Guanine (G) binds to Cytocine (C)
  66. // Example:
  67. // A-G-T-A-T-C-A-A-C
  68. // T-C-A-T-A-G-T-T-G
  69.  
  70. // If you don't know what DNA is? see this short video: https://www.youtube.com/watch?v=zwibgNGe4aY
  71.  
  72. // P7. Given the following implemented function with (if-else if-else) that returns the complementary base,
  73. // Convert this function to do the same logic with (switch-case).
  74.  
  75. namespace dna
  76. {
  77.  char complementaryBase( char base )
  78.  {
  79.     switch (base)
  80.     {
  81.     case 'A' :
  82.         {
  83.         return 'T';
  84.         break;
  85.         }
  86.     case 'C':
  87.     {
  88.         return 'G';
  89.         break;
  90.     }
  91.     case 'G':
  92.     {
  93.         return 'C';
  94.         break;
  95.     }        
  96.     case 'T':
  97.     {
  98.         return 'A';
  99.         break;
  100.     }
  101.     }
  102.  }
  103. }
  104. // P8. In the previous problem, enclose the function complementaryBase inside a namespace with name (dna).
  105.  
  106. int main()
  107. {
  108.  
  109.     std::cout << "u = " << u << std::endl;
  110.     std::cout << "The square of u = " << square( u ) << std::endl;
  111.     std::cout << "The cube of u = " << cube( u ) << std::endl;
  112.  
  113.     std::cout << "The sum of integers from 1 to 15 = " << sumOfFirst15Integers() << std::endl;
  114.     std::cout << "The sum of first 15 odd integers = " << sumOfFirst15OddIntegers() << std::endl;
  115.  
  116.     std::cout << "The complementary base of 'A' = "
  117.               << dna::complementaryBase( 'A' ) << std::endl;
  118.  
  119.     std::cout << "The complementary base of 'C' = "
  120.               << dna::complementaryBase( 'C' ) << std::endl;
  121.  
  122.     std::cout << "The complementary base of 'G' = "
  123.               << dna::complementaryBase( 'G' ) << std::endl;
  124.  
  125.     std::cout << "The complementary base of 'T' = "
  126.               << dna::complementaryBase( 'T' ) << std::endl;
  127.  
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement