I am just preparing to take a quiz and he said some examples will be kind of like this and I was just tryin to get some input on if these answers I have are the right ones..

he said just do what the statement says, no further work, just little snippets;

1. Output the value of the variable, x, in a field of 10 spaces
cout<< setw(10) << x;

2. Read 2 float values from a stream, inFile, into variables x & y
I'm not sure how to write this one...

3. Increase the value of variable x by variable y
x= x+y;

4. Output the value of x and leave the cursor on the same line
cout<<x;

5. Add x to y, double it and store the value in z
z= (x+y) * 2;

6. Show the exact output of this code:
x = 7.987654321;
cout << fixed << showpoint
<< setprecision(2) << setw(5) << x;

7.98

Write code to:
open a file called data.dat with a stream called myFile
myFile.open("data.dat")

read 3 integers into vars x, y and z
?? is it int x,y,z; ??

calculate the sum and average and store in vars sum and avg
output all 5 values (x, y, z, sum, avg) on 5 separate lines
I'm not sure ??

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by WaltP

You're looking fairly good.

2. You are given an istream named inFile. You need to create two floats: one named x and the other named y. Remember that cin is also an istream (so you can use your file stream just like you use cin).

6. Remember, 7.987... rounds to 7.99. Also, I presume your output has a space in front of it: " 7.99"

7. Yes, it's `int x, y, z;` , but now that you've opened "data.dat" you need to read a value for x, then a value for y, then a value for z. Remember, myFile is an istream.

Surely you know how to calculate the sum and average of x, y, and z?

Hope this helps.

And:

3. Increase the value of variable x by variable y
x= x+y;

There's another way to do this. Look at double operators.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.