Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. i will answer question in-order (top to bottom), so let's begin with the adventure of problem solving.
  2.  
  3. 1.In accordance with C++ programming, variables is a name which is given to a memory location but value is the data stored in that variable.Value stored in a variable can be changed during execution of a program, for a clear view see the attachment below. for example: int sepa In Ctt, for scamples and not Value in nuum 21 Variable & If this is not mentioned then some garleage va2.a.Modulo(%): a%b means remainder when a is divided by b.
  4.  
  5. b.Division(/): a/b is rounded off integer when a is divided by b when a and b both are integer value(for example 1).
  6.  
  7. a/b is floating point number when a is divided by b when either (a) or (b) or (both) is/are float value(for example 1.0).
  8.  
  9. so the answers for the problems are:
  10.  
  11. (2.1).0(as when 10 is divided by 2 remainder is 0).
  12.  
  13. (2.2).3(as when 23 is divided by 4 remainder is 3).
  14.  
  15. (2.3).0(as when 36 is divided by 6 remainder is 0).
  16.  
  17. (2.4).2(as this is integer division both operands are integer).
  18.  
  19. (2.5).2.6(as this is (float/integer) division answer is floating point number).
  20.  
  21. 3.Here we follow operator precedence rule which defines precedence of some operators over others(i.e, they are performed first) and when some operators belongs to same precedence then operations are performed from left to right.
  22.  
  23. precedence is as follows:
  24.  
  25. () then * / % then + - , so using these operators above problem is solved, please see the attachment below.( 12-*/. +1)*2/6+ 543*8/22-716)-3) о 12 +1)* 2/6s з( 8/2 - 3) 2 (3) - +5+3* (Ч-3) 3 6 / ++3°1) +S+3 Ч u0) -4.static_cast<data_type> is a simple type of cast in c++, it is a compile time cast operator.it performs implicit conversions like(int to float etc.)Static_cast Cint> (78+ static cast (float> (15%)/2). static-cart cint> (708+ Static Cast flood o); static - cast int> (708);5.see the attachment below,line 5 has invalid syntax in the code.#include <iostream> using namespace stf; int main() { Charch; (cin <ch; coutch; return o; & correct syntax is cinch6.OUTPUT will be "southern" not "southern miss", as cin in c++ read string only till the space it assumes that space is the end of the string.
  26.  
  27. 7.to read input string with spaces in a single string variable using cin we use getline(cin,str) instead of cin>>str where str is string variable.
  28.  
  29. 8.OUTPUT: x=37 y=86 z=0.56
  30.  
  31. 9.a) 1.
  32.  
  33. b)1.
  34.  
  35. c)1.
  36.  
  37. d)1.
  38.  
  39. e)1.
  40.  
  41. 10.!(x<0) becomes true when (x<0) is false as ! is a logical operator representing boolean NOT so when x>=0.0 or x>=0 the equation will be true for all possible x>=0.
  42.  
  43. 11.OUTPUT: ########83 as setfill is used to fill character with setw which decides the field width in the output.
  44.  
  45. 12.n will be printed as 9 as while loop will break when n is incremented from 7 to (+2) 9.
  46.  
  47. 13.OUTPUT:
  48.  
  49. ONE: 54.000000 positive number TWO: 54.000000
  50. ONE: -77.000000 TWO: -77.000000
  51.  
  52. 14.the code will not produce any output it will give TLE(time limit exceeded) as there are two conditions in while loop in which if any one is correct it will execute and one of the conditions is counter<5 and counter is 0 and never changed so result in TLE.
  53.  
  54. 15.'#' is printed 7 times "#######" as for loop will execute 10 times from i =0 to i=9 and for i=2 , i=4, i=6 the inner if condition will run and continue will skip the print statement.
  55.  
  56. 16.secret(15) will return 6, and another(6,3) will return 0, as for loop will not execute as i(=6) <=b(3) which fails.
  57.  
  58. 17.OUTPUT: 45 1 , x will be same as 45 but y will change to 1 from 43 as in the function call it is passed by reference so in the function there will be the same copy of y as in main but a local copy of x so changes are accordingly.
  59.  
  60. 18.function which returns 0(if argument is zero),-1(if argument is odd),1(if argument is even) is given below.
  61.  
  62. //starts here
  63.  
  64. int fun(int x){
  65. if(x==0)//check for zero before even as results are defined accordingly
  66. return 0;
  67. if(x % 2==0)// check for even
  68. return 1;
  69. else//all other integers are odd
  70. return -1;
  71. }//ends here
  72.  
  73. 19.below is the implementation of function required for addition of two fractions and the answer fraction is stored in n3 and d3 which are passed in function by reference.(using two helper function as mentioned).
  74.  
  75. //function to find gcd(greatest common divisor) of two numbers.
  76. int gcd(int a,int b){
  77. if (a==0)
  78. return b;
  79. return gcd(b%a, a);
  80. }
  81. // Function to convert the obtained fraction
  82. // into it's simplest form
  83. void simplest_form(int &d3,int &n3){
  84. int highest_common_factor=gcd(n3,d3);// Finding gcd of d3 and n3
  85. // converting both terms into simpler forms by dividing them by highest common factor.
  86. d3 = d3/highest_common_factor;
  87. n3 = n3/highest_common_factor;
  88. }
  89.  
  90. //function to add two fractions
  91. void addFraction(int n1,int d1, int n2,int d2, int &n3, int &d3){
  92. // Finding gcd of d1 and d2
  93. d3=gcd(d1,d2);
  94. // denominator of final fraction obtained
  95. // finding LCM of d1 and d2
  96. d3 = (d1*d2)/d3;
  97. //using hint mentioned in the problem
  98. // Numerator of the final fraction obtained
  99. n3 = (n1)*(d3/d1) + (n2)*(d3/d2);
  100.  
  101. //calling function simplest_form to convert final fraction into it's simplest form.
  102. lowest(d3,n3);
  103. }
  104.  
  105. 20.OUTPUT ARRAY : 4 4 8 8 according to the problem(after if and for loop execution).
  106.  
  107. All the 20 problems has been answered above so please take a look it took a huge time so just look at them interestingly and please mention any possible error from my side if you find it , i will improve on them. and please upvote if you like my answers. it was a fun doing all these programming based questions. i will be very thankful if you give a feedback (maybe positive but can be negative, both appreciated).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement