Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 267 268 [269] 270 271 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 908128 times)

Zrk2

  • Bay Watcher
  • Emperor of the Damned
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4020 on: February 28, 2013, 06:20:39 pm »

So, I've just started learning to program (I'm taking Intro to Programming) and I'm stuck on a question. I have to write a program that will solve for the voltage through a resistor for every value of temperature between 0 and 100, and print out the voltage for every temperature value that is a multiple of 10. The question reads as follows:

The resistor R represents a temperature sensor enclosed in the beaker. The resistance R, in
Ω, is related to the temperature T, in °C, by the equation
r=r0+kt
where R0 = 50 and k = 0.5.

The voltmeter displays the value of the voltage across the sensor
vm=r*vs/(rs+r)

The voltage vm indicates the temperature, T, of the water according to the equation
t=150*vm/(20-vm)-100

Write a C++ program that prints a table showing the meter voltage corresponding to
water temperatures varying from 0 °C to 100 °C in increments of 10 degrees.

So far I've done this:
Spoiler: Code (click to show/hide)

I tried incrementing t nine more times in order to insure it reaches 10, but that gave me the same error compiling; syntax error: identifier "t."
Logged
He's just keeping up with the Cardassians.

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4021 on: February 28, 2013, 06:23:30 pm »

C++ looks like someone made a language out of math. D:
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4022 on: February 28, 2013, 06:24:24 pm »

Ok before I even start to look at that, your IF statement is fuck. Use the modulus operator instead.

Code: [Select]
if (t % 10 == 0)While you do that, I will bother to actually read the code.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4023 on: February 28, 2013, 06:24:49 pm »

C++ looks like someone made a language out of math. D:
That's mostly because the variable names are ripped straight out of the equations involved :P If the variable names weren't so horribly obtuse and the code were commented, that wouldn't look so bad.

Twiggie

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4024 on: February 28, 2013, 06:26:43 pm »

use a for loop instead of a while loop, and dont use a double for the counter (not sure if this will actually break anything, its just better practice to use ints if you dont explicitly need a double).

remove the initialisation of t that you already have and use

for(int t = 0; t < 101; t +=10) {

}

in place of the while loop. then get rid of that hideous if statement, and move the
 r=rnot+k*t;
   vm=r*vs/(rs+r);
   t=150*vm/(20-vm)-100;

inside the loop , before the  cout statement (since you want to recalculate the voltage of the meter every time you change the temperature)
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #4025 on: February 28, 2013, 06:27:30 pm »

If the variable names weren't so horribly obtuse and the code were commented, that wouldn't look so bad.

If you're doing the former, you should have no need for the latter :-D if code needs comments to be understood it isn't very good code ;-)
Logged

Zrk2

  • Bay Watcher
  • Emperor of the Damned
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4026 on: February 28, 2013, 06:45:09 pm »

I can't use the modulus function there, I get an error; it has to be an l-value. What if I wrote a separate variable for t%10? It appears to work.

I've updated and commented the code. At this point it compiles but prints nothing.
Spoiler: Still Code (click to show/hide)
Logged
He's just keeping up with the Cardassians.

Twiggie

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4027 on: February 28, 2013, 06:48:38 pm »

ok, just take out the t increments. you're incrementing t by 10 in the for loop (thats what the t += 10 does), so you dont need to do that. also that if statement is now irrelevant since t will always be 10*x (since you're incrementing it by 10 starting from 0) and so t%10 will always be 0.

you also have a double declaration of t.

EDIT: I started hyperventilating when i saw all those t++s -.-
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4028 on: February 28, 2013, 06:49:42 pm »

I'm... Not fully convinced you have a full and working understanding of how loops work.
Get rid of all of those t++ statements as well as the if (a=0) and the first t declaration.

Damn ninjas.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4029 on: February 28, 2013, 06:51:00 pm »

Damn ninjas indeed.

Zrk2

  • Bay Watcher
  • Emperor of the Damned
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4030 on: February 28, 2013, 06:53:10 pm »

I'm... Not fully convinced you have a full and working understanding of how loops work.
Get rid of all of those t++ statements as well as the if (a=0) and the first t declaration.

Damn ninjas.

Not really. They were introduced last week and this is my first real experience with them.
Logged
He's just keeping up with the Cardassians.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4031 on: February 28, 2013, 06:54:40 pm »

You should start a bit more simple with playing with them before you go all out then.
Make a program that prints the numbers from 1 to 10.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4033 on: February 28, 2013, 11:03:54 pm »

How much are we allowed to help you?
Spoiler: Commented solution (click to show/hide)
« Last Edit: February 28, 2013, 11:06:40 pm by MagmaMcFry »
Logged

Zrk2

  • Bay Watcher
  • Emperor of the Damned
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4034 on: March 01, 2013, 10:05:28 am »

How much are we allowed to help you?
Spoiler: Commented solution (click to show/hide)

I don't know, but I managed to fix it. If anyone cares to take a look at my solution, here it is:
Logged
He's just keeping up with the Cardassians.
Pages: 1 ... 267 268 [269] 270 271 ... 796