Code Fragments!?!?

Sep 21, 2015 at 5:05am
I need help finding the code fragments in these examples. I'm new to programming so an explanation would be great too!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

1. They may be logic or syntax errors only (not style errors) 

min_approx = flooring( min_exact
                       + 15
                       / .5 )
               - 15;


2.  Explain what is wtong with the following Code fragment.  It has nothing to do with the comment.

double rate;
long start_popul, end_popul;
// fill in start and end _popul with data
// (...probably using a cin)
rate = end_popul / start_popul;


what is the name of the method we use to fix this situation and show how it would be applied here?
Last edited on Sep 21, 2015 at 6:36pm
Sep 21, 2015 at 7:09am
I need help finding the code fragments in these examples.

A code fragment is simply a piece of code that doesn't make up the whole program. You have posted two code fragments. They are fragments because they are not complete programs.
Sep 21, 2015 at 6:35pm
These are 2 seperate problems. I got the first one, but I'm still stuck on the second. We have to explain what is wrong with the following code fragment.
Sep 21, 2015 at 6:43pm
Why don't you put the code fragment into a program and see what it does. Be sure to set start_popul and end_popul to some different values and print the resulting rate. You will find that the rate is not what you might expect.
Sep 22, 2015 at 1:07am
Yeah I think I got this one now. Would you have to use a static_cast?

rate = static_cast<double>(end_popul/start_popul);
Sep 22, 2015 at 10:47am
Would you have to use a static_cast?
Yes, you you have it in the wrong place. static_cast<double>(end_popul/start_popul) still does the integer division and then converts the result to double. You want to convert the operands of the division to double. To do that, you only have to convert 1 and then the compiler will convert the other:
static_cast<double>(end_popul) / start_popul
Last edited on Sep 22, 2015 at 10:47am
Topic archived. No new replies allowed.