3 Replies - 451 Views - Last Post: 04 September 2012 - 05:07 PM Rate Topic: -----

#1 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Need help in initializing my increment operator

Posted 04 September 2012 - 04:37 PM

I'm trying to make a program that asks for a starting and final temperature then finds the velocity at the given temperature. But i need to use the increment operator so that i can get the velocity at each temperature between the two numbers. For example if the numbers were 5 and 10, then i need to find the velocity at teach temp between 5 and 10 degrees.


#include <iostream>
using namespace std;
int main ()
{
	int starttemp;
	int endtemp;
	double velocity;
	int tempchange;
	int count;
	

	cout << "Please enter the initial temperature\t";
	cin >> starttemp;
	cout << endl;
	cout << "Please enter the final temperature\t";
	cin >> endtemp;
	cout << endl;

	tempchange = endtemp - starttemp;
	velocity = 331.3 + 0.61 * tempchange;

	cout << "At" << starttemp << "degrees Celsius the velocity of sound is " << velocity << endl;

	while ( count ++ <= endtemp )
	{
		cout << "At" << count << "degrees Celsius the velocity of sound is " << velocity << endl;
	}

	cout << "At" << endtemp << "degrees Celsius the velocity of sound is " << velocity << endl; 

	return 0;

}




Is This A Good Question/Topic? 0
  • +

Replies To: Need help in initializing my increment operator

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: Need help in initializing my increment operator

Posted 04 September 2012 - 04:54 PM

Why not use a simple for loop between start temp and end temp, and compute the appropriate velocity for a given temperature with in the loop?

Something like this which computes y = x2:
for(int x = 0; x < 5; x++)
{
    int y = x * x;
    cout << "x: " << x << " y: " << y " << endl;
}


Was This Post Helpful? 0
  • +
  • -

#3 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: Need help in initializing my increment operator

Posted 04 September 2012 - 05:00 PM

View PostSkydiver, on 04 September 2012 - 04:54 PM, said:

Why not use a simple for loop between start temp and end temp, and compute the appropriate velocity for a given temperature with in the loop?

Something like this which computes y = x2:
for(int x = 0; x < 5; x++)
{
    int y = x * x;
    cout << "x: " << x << " y: " << y " << endl;
}



you confused me with the int y = x*x. I am assuming the "X" refers to "count" in my code. What does the "Y" stand for velocity?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: Need help in initializing my increment operator

Posted 04 September 2012 - 05:07 PM

Yes.

In your code, you would use a variable named velocity instead of y. You would also use a variable named temperature instead of x. Obviously, you would use a different formula, than the one I used. :) Using the for loop you would make temperature range from the start temperature to the end temperature by incrementing the value each time through the loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1