Question

In: Computer Science

C++ Questions: 7. What is a potential problem with the code below: int x=4.5; 8. Write...

C++ Questions:

7. What is a potential problem with the code below:

int x=4.5;

8. Write a C++ code snippet to input a whole number from the user and output double the number to the screen.

9. True/False. Assume x is an int which has been declared and assigned a valid value. The value of x is guaranteed to change after this code snippet:

if (x<0)
  x--;
else 
  x++;

10. Consider the below code snippet. Will it compile? What is a likely error?

int score = 0;
if (score = 0)
  cout << "We had a shutout today!\n";

11. Write a loop which prints out the even numbers from 0-100, including 0 and 100, to the screen.   

12. Same as Question 11, but count down from 100 to 0, even numbers only.

Solutions

Expert Solution

7)

int x = 4.5, its called as an lossy onversion because the 4.5 is the float value and it is getting converted into the integer value so that lossy of data occurs, if we try to print the value of x it will prints 4

8)
#include <stdio.h>

int main()
{
int x;
printf("Enter the value of x : ");
scanf("%d", &x); // read the value
  
double y = (double) x; // convert it into double
printf("Double : %lf", y); // print the double value

return 0;
}

9)

True, Yeah the x value will change at the end of the execution of the program, If it falls in the IF condition it will get decremented or in the else condition it will get incremented

10)

Yeah the code will compile successfully

if(score = 0), in the if condition the score is initialized with the value 0, where the if condition returns false and the statements inside the if statements will not execute

Likely error in this case we can simply called a logical flaws errors, where we are initialize score with the 0 instead of comparing them, where the logical execution of the program will disrupts

in the above code we can re-write the if condition as :

if(score == 0) // now the compare the score with the value 0

11)
#include <stdio.h>

int main()
{
printf("Even numbers are : \n");
for(int i = 0; i <= 100; i++)
{
if(i % 2 == 0) // checking if it is even number
{
printf("%d ", i); // then print the number
}
}

return 0;
}
12)


#include <stdio.h>

int main()
{
int count = 0;
printf("No of Even numbers are : ");
for(int i = 0; i <= 100; i++)
{
if(i % 2 == 0) // checking if it is even number
{
count++; // then increment the count
}
}
printf("%d", count);
return 0;
}


Related Solutions

C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
For the matrix X answer the questions below. X = ( 4 8 8 ; 3...
For the matrix X answer the questions below. X = ( 4 8 8 ; 3 6 − 9 ), this is a 2x3 matrix (a) Calculate XX’ and compute the corresponding Eigen values and Eigen vectors. (b) Obtain the singular-value decomposition of the matrix X. Note that: Eigen values for X’X are: λ1 =150, λ2 = 120, and λ3 = 0. The corresponding Eigen vectors are: eigenvector1 = 1/√30 (1 2 5), eigenvector2 = 1/√6 (1 2 -1), and...
What is the Θ( ) for each code segment below? (a) for( int i = 1,...
What is the Θ( ) for each code segment below? (a) for( int i = 1, i<= n, i = i*2){       some constant operation } (b) for( int i = 1, i<= n, i++){      for( int j = 2*i, j<=n, j++){           some constant operation      } } (c) for( int i = 1, i<= n, i = i*2){      for( int j = 1, j<=n, j = j*2){           some constant operation      } }
After the code executes (in C++), what will the linked list 8->4->3->7->5 look like? (8 is...
After the code executes (in C++), what will the linked list 8->4->3->7->5 look like? (8 is the head) 1a) Node* newNode = new Node(26); newNode->next = head->next; head = newNode; 1b) Node* curNode = head; for(int i=0; i<2; i++) curNode = curNode->next; Node* temp = curNode->next; curNode->next = curNode->next ->next; delete temp; temp = nullptr;
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100;...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100; i++) myAry = i + 2; for(int i=100; i>0; i--) cout << myAry[i] << '\t'; The first for loop assigns myAry 99 values and the null character. The second for loop prints out myAry elements backwards. The index in the second for loop is out of bounds. Only the first loop needs the null character. Q3. A value returning function that takes one parameter,...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that generates and stores a specified number of random numbers each of whose values are between 0 and 1000 in an array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT