Help fix failed assignment

I am noob taking a C++ class, I need a little bit of help figuring out the correct answers to two question I got wrong.

What is the value of n after the assignment n = j - j / i * i ?

My wrong answer was, Basically the value of "n" is the result to the math problem. Example Below. (he said my answer was wrong because I did not provide a requested value) I wonder if he wanted me to tell him my answer of 5?

#include <iostream>

using namespace std;

int main()
{
int j = 5;
int i = 10;
int n = j - j / i * i;

cout << n;
return 0;
}

Given a variable float f, what would be the value of f after assigning f = 24;

My wrong answer was the books definition of float. (once again he said it was wrong because I did not provide the requested value) Upon further review I think the correct answer is 2.4E1???

Any help would be greatly appreciated.

Thanks
The answer to the first question is j%i for positive values and the answer to the second one should be 24.0f (2.4E1 is a double literal, not a float literal).

When an assignment asks for the result of an expression involving variables, then a general answer is required. An example won't do, unless the assignment says otherwise.
Last edited on
I would have thought the first answer would always be zero.

j - j is always going to make the numerator zero;

If j, for example is 3. 3 - 3 = 0. 0 / whatever = 0.

If j was -3, then it'd be -3 -- 3, which equals - 3 + 3 = 0. 0 / whatever = 0.


Forget that. Operator precedence. :-P
Last edited on
Weeeell... perhaps that's why I edited it four minutes prior to your comment there. ;-P
Part of the problem I am having with this assignment is the directions seem poorly written and or I just do not comprehend them.

Apparently I am supposed to use the code he posted in the middle of page, that seemed out of place to me. This was found after question 5, I think it you should have been under the first question.

#include <iostream>
int main (void)

{
int n, i =5;
j = 8;
n = j - j / i * i ;
cout << "The result is " << "----->" << n;
return 0;
}

Last edited on
Your thread title is right on. You need to help your teacher fix his failed assignment. I hate tests that test you on how the test should be interpreted. His example won't even compile since j and cout are undeclared. I hope you're not paying too much for his instruction.
I am paying like 700 bucks for the class, well the GI Bill footing the bill.

Here is the directions he posted.

Assignment 2

Reading and Exercises

* Read Lesson 2 (online notes)

* Read Chapter 2, pages 29-76, from the textbook. Pay special attention to operations like % (ask me if any doubts)


Programming with Words

Given the following program, answer the questions below:

a) What is the value of n after the assignment n = j - j / i * i ?

b) What is the use of #include <iostream> ?

c) If you have int x and do x = 2.45, the compiler will issue a warning, but it will compile. What is the effect of that assignment ?

d) Why would the compiler issue a warning? What could be "dangerous" about that assignment? how come a warning is not issued when you assign an int value to a float variable ?

e) Given a variable float f, what would be the value of f after assigning f = 24;


#include <iostream>

int main (void)

{

int n, i =5, j = 8;

n = j - j / i * i ;

cout << "The result is " << "----->" << n;

return 0;

}



C++ Program

Write a program that prints to cout, separated by a space, the digits of any three digit integer. You will need one variable, named, for example, value, for the number (int value) to be broken down into its digits. You might also need other variables to store the digits as you get them.

The structure of your program could be:

#include <iostream>

int main()

{ int number;

int digit1, digit2, digit3; // place holders for the digits.

cout << "Type a three digit number\n";

cin >> number;


// your code here


return 0;

}

Hint: You can do this by using repeated divisions by 10, and remembering that dividing two integers always resuts in an integer. For
example 6/5 would yield a result of 1. You can use the % operation

If you input a number like 345 your ouput should be 3 4 5 (its three digits separated by a space)
Last edited on
Then he extended the due date and is allowing people to fix mistakes, which is good.

Athar are you sure its 24.0f because from what I read in my book the anwnser seems like it should be 2.4E1.

I copied pasted part of the book below


To deal with decimal numbers, C++ provides the floating-point data type, which we discuss in this section. To facilitate the discussion, let us review a concept from a high school or college algebra course.You may be familiar with scientific notation. For example:

43872918 = 4.3872918 * 107 {10 to the power of seven}
.0000265 = 2.65 * 10-5 {10 to the power of minus five}
47.9832 = 4.79832 * 101 {10 to the power of one}

To represent real numbers, C++ uses a form of scientific notation called floating-point notation. Table 2-3 shows how C++ might print a set of real numbers using one machine’s interpretation of floating-point

Real Number C++ Floating-Point Notation Examples

75.924 = 7.592400E1
0.18 = 1.800000E-1
0.0000453 = 4.530000E-5
-1.482 = -1.482000E0
7800.0 = 7.800000E3
Last edited on
the anwnser seems like it should be 2.4E1.

Like I said, 2.4E1 is a double literal, a float variable can't ever take that value. It's 24.0f (or 2.4e1f if you insist on that notation).

Floating point numbers are indeed represented as mantissa+exponent internally, but how you display them is another matter. See this article for more information:
http://en.wikipedia.org/wiki/Single_precision_floating-point_format
Last edited on
Your right Athar, I read more of the book and it better explained it, thanks for the help.
So I did a lot of studying and re-submitted my assignment last night. I got a lower grade than last time because he found mistakes he let slip by last time... I emailed him this morning for the first time asking him for personal help, but I have not heard back from him yet, and the paper is due tonight. I have read the chapter in the book and have been trying to find a clue about the two questions below but I am having now luck.


If you have int x and do x = 2.45, the compiler will issue a warning, but it will compile. What is the effect of that assignment?

Why would the compiler issue a warning? What could be "dangerous" about that assignment? how come a warning is not issued when you assign an int value to a float variable ?

PS anyone no how to make the font on codeblocks bigger so my tired eyes can see it?
anyone no how to make the font on codeblocks bigger so my tired eyes can see it?

In Settings/Editor.

If you have int x and do x = 2.45, the compiler will issue a warning, but it will compile. What is the effect of that assignment?

The value is truncated to 2, the rest follows from that.
Topic archived. No new replies allowed.