In: Computer Science
C++ EXERCISES
(a) Given int a = 5, b = 2, c = 4, and d = 5; determine the value of the expression:
d % b * c > 5 || c % b * d < 7.
(b) Which repetition statement is preferred for user data input and its validation?
(c) Write a for statement to populate an array, double val[NUMCOUNT], for the following case: Use a counter named double count that has an initial value of 16.2, a final value of 0.4, and a decrement of 0.2 and populate the val[] array with the value of count.
What is the size, NUMCOUNT, of the array val[]?
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
For the given statement d % b * c > 5 || c % b * d < 7
Let's debug it one by one.
according to operator precedence , it will execute like below
first : d % b * c > 5
second : c % b * d < 7
after that OR of these two statements will be the final result.
Now resolve first one
d%b*c here * will execute first and % after that.
=d % b * c > 5
=5%2*4>5
=5%8>5
=0>5
=false
now let's see second statement.
c % b * d < 7
again lets see the value of
c % b * d<7
it will be
=4%2*5<7
=4%10<7
=0<7
=true
Hence final result will be or of these two output
that is false OR true= true
Hence it will result = 1
c++ output confirmation
2)
Do While loop is preferred for user input and validation.
suppose there is a requirement that ask a user number that should be greater than 0
than these can be handle easily with the help of while loop as below
do{
cin>>number;
}while(number<=0)
Above code will keep asking for number until positive number is not entered.
3)
In order to get the size, Following calculation can be used.
Starting value is : 16.2
Ending value is : .4
Difference is : 16.2-.4 = 15.8
so the value of NUMCOUNT willl be = 15.8/.2+1
= 79+1
=80
extra one is added due to zero base index.
And the for loop would be as below
#include <iostream>
using namespace std;
int main(void) {
//initialize num count
int NUMCOUNT=77;
//declare array of double
double val[NUMCOUNT];
//index for subscripting
int index=0;
//loop for
for(double count=16.2;count>=0.4;count-=.2){
val[index] = count;
index++;
}
return 0;
}