First test tomorrow! some help questions!

Hey guys, so I have been acing all my programs so far and getting perfect scores but I have a couple of questions.

I am having my first C++ test tomorrow and it will include programming, writing sophisticated equations if you were doing it through an input, and solving problems like a compiler (int/double).

I feel as though I have my program skills set for this test but my main question is for the other 2.

1. In regards to writing sophisticated equations do you guys have any tips?
Side Question:lets say you had e^3^t(rare case but I'm curious) would it be written as " exp(3,t); " are you able to add a comma showing that it is to be a power of 3? Or could you declare 3^t by doing double blah = pow(3,t) and then doing exp(blah).

2. When it comes to solving problems like a compiler I am completely confused. I am sure in some early C++ courses you guys have had problems like these. Where you are meant to find the answer even though there are a bunch of int's and doubles in the actual equation:

For example:
3/5 * (1.25-2) + 4/3 * (2-(4*2)) + exp(3/5) + 3/4.

Integer answer = -4 double answer = -4.25

If this was given, what would be considered the correct answer?


--- One last question, my latest program I have been working with cin

1
2
3
4
5
6
7
8
9
10
11
12
13
double degrees, wavelength, wavelengthE, wavelengthS, wavelengthM, kelvin, mars;
int sun, earth;
sun=5727;
earth=14;
mars=0.46;
const int WEINCONSTANT= 2897; // the units for wein's constant is microns/Kelvins
kelvin=273.15; // For Converting Celcius to Kelvin
output<<"Please Insert Degrees In Celcius to calculate the Maximum Wavelength:"<< endl;
cout<<"Please Insert Degrees In Celcius to calculate the Maximum Wavelength:"<< endl;
cin >> degrees;
wavelength= WEINCONSTANT / (degrees+kelvin);
output<<"\nThe Maximum Wavelength is = " <<wavelength<< " at "<<degrees<<" Degrees Celcius"<<endl;
cout<<"\nThe Maximum Wavelength is = " <<wavelength<< " at "<<degrees<<" Degrees Celcius"<<endl;


Assuming I have 5 different values I want to enter. Instead of inserting one, getting the answer, and then restarting the program.. how can I make it so that after getting an answer, I can ask:

"If you want to try another value please type 1, if not type 0"

If you press 1, it will go through the program again.

Is that possible without doing something like if's or for loops (haven't gotten there yet so I want to avoid it) or are if's and for loops the only way?


Thanks in advance guys, I know its a lot to answer and sorry for grammatical errors typing from college before I leave!
Last edited on
In regards to writing sophisticated equations do you guys have any tips?
Try not to?

Side Question:lets say you had e^3^t(rare case but I'm curious) would it be written as " exp(3,t); " are you able to add a comma showing that it is to be a power of 3?
No.
Assuming you were using right-associative exponentiation, e^(3^t) -> exp(pow(3,t))

For example:
3/5 * (1.25-2) + 4/3 * (2-(4*2)) + exp(3/5) + 3/4.

Integer answer = -4 double answer = -4.25

If this was given, what would be considered the correct answer?
General tips:
1. Without a decimal point, a number is always an integer. For example, 5.
2. Always evaluate an expression as eagerly as possible; don't simplify. Remember that generally a/b*c != a.
3. When the operands of an expression are of differing types, the smaller type is converted to the bigger type. Thus, 1/2.0 == 1.0/2.0 == 0.5.

Is that possible without doing something like if's (haven't gotten there yet so I want to avoid it) or are if's the only way?
It's the only way. if is the basic decision structure.
Thank You helios for the great response.

My question is back at the: 3/5 * (1.25-2) + 4/3 * (2-(4*2)) + exp(3/5) + 3/4.

I see what you are saying. With that in mind, does that technically mean that this would then have a double answer? Seeing how 3/5 and 4/3 are double as well it would almost cause a "ripple" to all the int's and ultimately be a double answer?

Am I correct in assuming that?


Edit: actually it looks like I'm wrong... just from seeing this done, why is it that all the fractions = 0 except 4/3 which ='s 1... is that because they are integers?
Last edited on
With that in mind, does that technically mean that this would then have a double answer?
Looks that way.

Edit: actually it looks like I'm wrong... just from seeing this done, why is it that all the fractions = 0?
Remember that a/b=0 when / is integer division and b>a. Also, in those cases a%b=a.

EDIT: I've thought of a way to do that other thing without using if, but it's even more complicated.
Last edited on
It seems I get it, I also keep assuming that because it's an integer it will round up or something but all it does as an integer is chop off everything after the decimal point.
Do a lot of schools just not teach proper spacing and indentations in code? Just genuinely curious.
They do teach but it is minimal. In fact they like to focus a lot more time on data structures and algorithms cuz it is these that drive a lot of programs underlying.
I just know at my school, spacing and readability of code is stressed. Points are docked majorly for poor spacing, even if the rest of the programming is good.
Ok I agree. But would a commercial world employer place poor spacing as higher precedence over the rest of the programming which is driving the business logic that make monies for the company?

There is always a gap between academia and the commercial world. Academia focus on many stuff for complete-ness but the commercial world are driven by time to market and profits so they have monies to pay their employees isn't it ?

Will this gap be sealed? Well it depends on how often the academia staff interact with the commercial world staff isn't it? Let the academia staff work for 1 month in the commercial world and then come back and tell ppl which has higher precedence :)

PS I do note readability is important but in the context of many factors like program deliverables and date-lines, it is not the first in the priority list. After all company can always engage other ppl to do maintenance of those no spacing programs.
Well, as Knuth once said: programs are meant to be read by humans, and only incidentally for computers to execute.
Topic archived. No new replies allowed.