Question

Using C++ What is type casting (page 103) and why is it used incorrectly in the...

Using C++

  1. What is type casting (page 103) and why is it used incorrectly in the following code?
int books = 30;
int months = 4;
double perMonth;
perMonth = static_cast<double> (books/months);
cout << perMonth << endl;
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Type Casting: It is a way to change the data type explicitly by a user.

Why it is USed??

Some times we assume the data type of some particular type, but in between, we might need to switch a different type so as to be more precise or the new type is more valid.

In this example:

We start with books which is an integer

months which is also an integer.

Now perMonth is defined as double which is the ratio of books and months.

This only makes sense that perMonth is double so it is defined double.

Expected Answer = 7.5

Actual Answer when we run the code will be 7 because we are dividing two integers.

In c++ when two integers are divided the output is always an integer value.

SO What is wrong in this declaration!

There is casting applied after the division has taken place instead it should be done before the division.

SO the correct code is as follows

Make books as double LIKE THIS

#include <iostream>
using namespace std;

int main() {
   // your code goes here
   int books = 30;
   int months = 4;
   double perMonth;
   perMonth = static_cast<double> (books) / (months);
   cout << perMonth << endl;
   return 0;
}

OR Make months as double LIKE THIS

#include <iostream>
using namespace std;

int main() {
   // your code goes here
   int books = 30;
   int months = 4;
   double perMonth;
   perMonth = (books) / static_cast<double> (months);
   cout << perMonth << endl;
   return 0;
}

OR Make months and books both as double LIKE THIS

#include <iostream>
using namespace std;

int main() {
   // your code goes here
   int books = 30;
   int months = 4;
   double perMonth;
   perMonth = static_cast<double> (books) / static_cast<double> (months);
   cout << perMonth << endl;
   return 0;
}

All the three above codes are perfectly fine and will give 7.5 as a result.

Please comment for any further assistance.

Add a comment
Know the answer?
Add Answer to:
Using C++ What is type casting (page 103) and why is it used incorrectly in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A. What is the output of the following C++ code fragment? (all variables are of type...

    A. What is the output of the following C++ code fragment? (all variables are of type int) int count-1; int y-100; while (count 3) y=y-1 ; count+t cout << y << endl; cout<< count <<endl What is the value of x after control leaves the following while loop? (all variables are of type int) B. int x0 while (x < 20) sum- sum+x cout << sum<< endl;

  • What is the output of the following code? int num = 17;                           //Line 1 double...

    What is the output of the following code? int num = 17;                           //Line 1 double gpa = 3.85;                  //Line 2 bool done;                               //Line 3 done = (num == static_cast<int>( (2 * gpa + 9.3)));    //Line 4 cout << "The value of done is: "<< done << endl;     //Line 5

  • c++ Consider the following statement string str "Now is the time for the party!" What is...

    c++ Consider the following statement string str "Now is the time for the party!" What is the output of the following statements? (Assume that all parts are independent of each other.) a. cout <str·size ( ) end 1 ; b. Cout << str. substr (7, 8) <<endl: c. string: :size type indstr.find'£') string s str. substr (ind 4, 9); d. cout << str insert (11,"best " <<endl e. str.erase (16, 14) str.insert (16, "to study for the exam? ") cout...

  • c++ only. Please follow directions. What does the following program print and why? Comment each line...

    c++ only. Please follow directions. What does the following program print and why? Comment each line of code to explain what it is doing.   #include <iostream> using namespace std; int main() { int track[ ] = { 10, 20, 30, 40 }; int * ptr; ptr = track; track[1] += 30; cout << * ptr << " "; *ptr -= 10; ptr++; cout << * ptr << " "; ptr += 2; cout << * ptr << " "; cout...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • PLEASE TYPE OUT IN TEXT (please no pdf or writing) C++ CODE Consider the following program...

    PLEASE TYPE OUT IN TEXT (please no pdf or writing) C++ CODE Consider the following program in which the statements are in the incorrect order. Rearrange the statements in the following order so that the program prompts the user to input: The height of the base of a cylinder The radius of the base of a cylinder The program then outputs (in order): The volume of the cylinder. The surface area of the cylinder Format the output to two decimal...

  • C++ Questions Please answer quickly! 1. Please use this info to answer next questions -> 2. Please give reasoning 4. Explain why, I think it is 4 but i also think it might be 0. You need to wri...

    C++ Questions Please answer quickly! 1. Please use this info to answer next questions -> 2. Please give reasoning 4. Explain why, I think it is 4 but i also think it might be 0. You need to write a program that uses the speed of light in a vacuum (299792458 meters per second) Realizing that this is a very large value, you run this code to see how large a value you can store as an int, a long...

  • using the source code at the bottom of this page, use the following instructions to make...

    using the source code at the bottom of this page, use the following instructions to make the appropriate modifications to the source code. Serendipity Booksellers Software Development Project— Part 7: A Problem-Solving Exercise For this chapter’s assignment, you are to add a series of arrays to the program. For the time being, these arrays will be used to hold the data in the inventory database. The functions that allow the user to add, change, and delete books in the store’s...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • 51. What is the output of the following code snippet? int number = 0; int ptr_num...

    51. What is the output of the following code snippet? int number = 0; int ptr_num -&number ptr_num 60; number-80 cout < "ptr num << endl b, 60 c. 80 d. the address of number Answer 52. What is the output of the following code snippet? double num-0.0; double* ptr = &num; num = 15.0; ptr ptr 15.0 cout << num <<"ptr <<endl; a. 15 15 b. 15 30 С. 30 15 d. 30 30 Answer: 53. What is the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT