C++ help - Chicagoland Sportbikes
Chicagoland Sportbikes
 
Open Forum This forum is for all off-topic discussion.

 
LinkBack Thread Tools Display Modes
post #1 of 19 (permalink) Old 02-08-2008, 01:27 PM Thread Starter
shhhhhhhhh!
 
sponge's Avatar
 
Join Date: Apr 2004
Location: Houston, TX
Posts: 1,227
Location: Houston, TX
Sportbike: suzuki
Years Riding: yesterday
How you found us: Nabisco
           
C++ help

With all the IT and computer guys here, someone must be fluent in C++, anyone wants help out a little?

I am taking this class and just realize that how frustrating C++ can be. I am half way done, but kind of not meeting the assgnment requirements.
The code is about while loop with decision statement (while,,,if,,,,else,,,)

Please let me know and I will semd you the requirements and code which I have so far. Of course reward can be arranged (I am not wealthy, but I can buy you a beer or 4 next time around )

Thanks in advance.
sponge is offline  
Sponsored Links
Advertisement
 
post #2 of 19 (permalink) Old 02-08-2008, 01:32 PM
habitual line stepper
 
Jack's Avatar
 
Join Date: Apr 2002
Location: Wisconsin
Posts: 3,235
Location: Wisconsin
Sportbike: F4,Brutale,Warrior,DRZ
Years Riding: X
How you found us: .
           
Send a message via AIM to Jack
Post the code up. Several people on the board are fluent in C++ and likely to help.
Jack is offline  
post #3 of 19 (permalink) Old 02-08-2008, 01:44 PM Thread Starter
shhhhhhhhh!
 
sponge's Avatar
 
Join Date: Apr 2004
Location: Houston, TX
Posts: 1,227
Location: Houston, TX
Sportbike: suzuki
Years Riding: yesterday
How you found us: Nabisco
           
ok, please don't laugh too hard with my stupid codes,,,,,
********************************************
Requirments:
Calculations
Use the following formulas for your calculations:

average velocity = change of position / time interval

average acceleration = change of velocity / time interval

Processing Requirements
At the top of your C++ source code, include a documentation box that resembles the ones from programs 1 and 2.

Notice (in the Sample Output below) that there are line numbers before each prompt for a position measurement. But also notice that the number does not change when invalid data is entered.

Sample Output
Motion Analysis

Enter the uniform time interval in seconds: 2.5

1. Enter a position in feet (negative value to quit): 2

Average velocity (this interval): 0.80 feet/second
Average velocity (all intervals): 0.80 feet/second
Average acceleration: 0.32 feet/second/second

2. Enter a position in feet (negative value to quit): 5

Average velocity (this interval): 1.20 feet/second
Average velocity (all intervals): 1.00 feet/second
Average acceleration: 0.16 feet/second/second

3. Enter a position in feet (negative value to quit): 4

Invalid value: 4 is less than the current position of 5. Try again.

3. Enter a position in feet (negative value to quit): 12

Average velocity (this interval): 2.80 feet/second
Average velocity (all intervals): 1.60 feet/second
Average acceleration: 0.64 feet/second/second

4. Enter a position in feet (negative value to quit): -1

************************************************** ***
Codes

int main()
{
float timeinterval;
int intervalcount=1;
float position, positiona, positionb;
float avgvelocity, avgvelocityb;
float avgvelocityall;
float avgacceleration;

cout <<"Please enter the uniform time interval in second: ";
cin >> timeinterval;
cout <<"Please enter a postion in feet (negative value to quit): ";
cin>> positiona;

avgvelocity= (double)(positiona)/timeinterval;
avgvelocityall=(double)(positiona)/(timeinterval*intervalcount);
avgacceleration=(double)(avgvelocity)/(timeinterval);
intervalcount++;

cout < cout < cout <
cout< cout <<"Please enter a postion in feet (negative value to quit): ";
cin>> positionb;

while (positiona>0 and positionb>0)
{
if (positionb {
cout < }
else
{
position=positionb-positiona;
avgvelocityb= (double)(position)/timeinterval;
avgvelocityall=(double)(positionb)/(timeinterval*intervalcount);
avgacceleration=(double)(avgvelocityb-avgvelocity)/(timeinterval);
intervalcount++;
cout < cout < cout < }
cout < cout <<"Please enter a postion in feet (negative value to quit): ";
cin>> positionb;
}
return 0;
}

Thanks again
sponge is offline  
post #4 of 19 (permalink) Old 02-08-2008, 04:08 PM
habitual line stepper
 
Jack's Avatar
 
Join Date: Apr 2002
Location: Wisconsin
Posts: 3,235
Location: Wisconsin
Sportbike: F4,Brutale,Warrior,DRZ
Years Riding: X
How you found us: .
           
Send a message via AIM to Jack
See if this gives you a few ideas. You'll have to debug it because I just whipped through it and didn't even compile it. Obviously you'll have to format the output to the user. Creating an object would be a nice touch and clean it up a little.

Code:
int IntervalCount = 1;
float TimeInterval = 0;
float Position = 0;
float NewPosition = 0;
float Velocity = 0;
float NewVelocity = 0;
float Acceleration = 0;

// get the uniform time interval from the user
cout <<"Please enter the uniform time interval in second: ";
cin >> TimeInterval;

// if user enters negative value here, we're done so exit.
if (TimeInterval < 0)
	return 0;			

// get user's first position relative to zero
cout <<"Please enter a postion in feet (negative value to quit): ";
cin>> NewPosition;

// loop through getting new positions until the user enters a negative value
while (NewPosition > 0)
{

	// Check input for valid position
            // note: assumes Position = 0 for first run
	if (NewPosition < Position)
	{
		// write try again message since input is not valid
		// Invalid value: 4 (NewPosition) is less than the current position of 5 (Position). Try again.

	}	
	else	// input is valid, so make calculations and display 
	{
		// average velocity = change of position / time interval
		NewVelocity = (NewPosition - Position) / TimeInterval;

		// average velocity for all runs
		// change of position / time interval
		Velocity = NewPosition / TimeInterval; // assumes original starting position of 0

		// average acceleration = change of velocity / time interval
		Acceleration = (NewVelocity - Velocity) / TimeInterval;	


		//OUTPUT THE CALCULATIONS HERE: NOTE VALUES HAVE TWO DECIMAL PLACES
		//Average velocity (this interval): NewVelocity feet/second
		//Average velocity (all intervals): Velocity feet/second
		//Average acceleration: Acceleration feet/second/second


		Velocity = NewVelocity;
		Position = NewPosition;
		IntervalCount++;
	}

             // Get the next position from the user 
             // NOTE: USE IntervalCount HERE 
	cout < cout < cout < 
	cout< cout <<"Please enter a postion in feet (negative value to quit): ";
	cin>> NewPosition;

}

return 0;

Last edited by Jack; 02-08-2008 at 04:10 PM.
Jack is offline  
post #5 of 19 (permalink) Old 02-08-2008, 04:15 PM
 
Join Date: Apr 2004
Posts: 1,416
           
i'm a java developer with just enough knowledge of c++ to know i hate pointers. with that said, where's the rest of the if conditional?

while (positiona>0 and positionb>0)
{
if (positionb
{
cout <<<"invalid value ! "<<<" is less than current value " << positiona<< " try again ";
}
PaikyPoo is offline  
post #6 of 19 (permalink) Old 02-08-2008, 04:16 PM
teets
 
Join Date: Apr 2006
Location: teets
Posts: 7,411
Location: teets
Sportbike: teets
Years Riding: teets
How you found us: teets
           
Send a message via AIM to Flounder
Dayam.. this shit reminds me of college. I haven't done any Java or C++ programming in years. I can write you a PERL script though... or some SQL perhaps

teets
Flounder is offline  
post #7 of 19 (permalink) Old 02-08-2008, 04:17 PM
The Benchwarmer
 
Village Idiot's Avatar
 
Join Date: Jun 2007
Location: Chicago
Posts: 14,580
Location: Chicago
Sportbike: Ninja
Years Riding: Long
How you found us: Dong
           
if youre going to have and statements shouldnt it be double parathensis?

while (positiona>0 and positionb>0)

vs

while ((position > 0) && (positionb>0))

?

Men will never be free until the last king is strangled with the entrails of the last priest - Denis Diderot
Village Idiot is offline  
post #8 of 19 (permalink) Old 02-08-2008, 04:41 PM
Registered User
 
DieselBoy's Avatar
 
Join Date: Aug 2004
Location: Naperville IL
Posts: 701
Location: Naperville IL
Sportbike: 08 R6 , CRF250R
Years Riding: 1
How you found us: chicagorussionriders
           
Quote:
Originally Posted by Jack View Post
See if this gives you a few ideas. You'll have to debug it because I just whipped through it and didn't even compile it. Obviously you'll have to format the output to the user. Creating an object would be a nice touch and clean it up a little.

Code:
int IntervalCount = 1;
float TimeInterval = 0;
float Position = 0;
float NewPosition = 0;
float Velocity = 0;
float NewVelocity = 0;
float Acceleration = 0;

// get the uniform time interval from the user
cout <<"Please enter the uniform time interval in second: ";
cin >> TimeInterval;

// if user enters negative value here, we're done so exit.
if (TimeInterval < 0)
	return 0;			

// get user's first position relative to zero
cout <<"Please enter a postion in feet (negative value to quit): ";
cin>> NewPosition;

// loop through getting new positions until the user enters a negative value
while (NewPosition > 0)
{

	// Check input for valid position
            // note: assumes Position = 0 for first run
	if (NewPosition < Position)
	{
		// write try again message since input is not valid
		// Invalid value: 4 (NewPosition) is less than the current position of 5 (Position). Try again.

	}	
	else	// input is valid, so make calculations and display 
	{
		// average velocity = change of position / time interval
		NewVelocity = (NewPosition - Position) / TimeInterval;

		// average velocity for all runs
		// change of position / time interval
		Velocity = NewPosition / TimeInterval; // assumes original starting position of 0

		// average acceleration = change of velocity / time interval
		Acceleration = (NewVelocity - Velocity) / TimeInterval;	


		//OUTPUT THE CALCULATIONS HERE: NOTE VALUES HAVE TWO DECIMAL PLACES
		//Average velocity (this interval): NewVelocity feet/second
		//Average velocity (all intervals): Velocity feet/second
		//Average acceleration: Acceleration feet/second/second


		Velocity = NewVelocity;
		Position = NewPosition;
		IntervalCount++;
	}

             // Get the next position from the user 
             // NOTE: USE IntervalCount HERE 
	cout < cout < cout < 
	cout< cout <<"Please enter a postion in feet (negative value to quit): ";
	cin>> NewPosition;

}

return 0;
Few corrections :

Define new variable TotalTime = 0;
Define new variable avgVelocity = 0;

Add TotalTime += TimeInterval; inside the loop and before any of calculations
Replace Volocity = NewPosition/TimeInterval ; with
avgVelocity = NewPosition/TotalTime;

avgVelocity is your avg velocity (not Velocity)

CCS 787(ex). 08r6
DieselBoy is offline  
post #9 of 19 (permalink) Old 02-08-2008, 04:46 PM Thread Starter
shhhhhhhhh!
 
sponge's Avatar
 
Join Date: Apr 2004
Location: Houston, TX
Posts: 1,227
Location: Houston, TX
Sportbike: suzuki
Years Riding: yesterday
How you found us: Nabisco
           
thanks everyone, (Jack especially). I will play around more with this until I am absolutely sick of it (or lose it).
sponge is offline  
post #10 of 19 (permalink) Old 02-08-2008, 07:33 PM
Registered User
 
BusaDave's Avatar
 
Join Date: Apr 2002
Location: Naperville
Posts: 5,002
Location: Naperville
Sportbike: 2002 Turbo Hayabusa
Years Riding: Lost Track
How you found us: Tony
           
I don't see much that's C++ about this code other than the use of cout.

Dave
BusaDave is offline  
post #11 of 19 (permalink) Old 02-08-2008, 07:35 PM
Registered User
 
BusaDave's Avatar
 
Join Date: Apr 2002
Location: Naperville
Posts: 5,002
Location: Naperville
Sportbike: 2002 Turbo Hayabusa
Years Riding: Lost Track
How you found us: Tony
           
Quote:
Originally Posted by burntash View Post
if youre going to have and statements shouldnt it be double parathensis?

while (positiona>0 and positionb>0)

vs

while ((position > 0) && (positionb>0))

?
Not needed...see order of precedence.

Dave
BusaDave is offline  
post #12 of 19 (permalink) Old 02-08-2008, 07:52 PM
The Benchwarmer
 
Village Idiot's Avatar
 
Join Date: Jun 2007
Location: Chicago
Posts: 14,580
Location: Chicago
Sportbike: Ninja
Years Riding: Long
How you found us: Dong
           
been a few years

Men will never be free until the last king is strangled with the entrails of the last priest - Denis Diderot
Village Idiot is offline  
post #13 of 19 (permalink) Old 02-08-2008, 11:18 PM
habitual line stepper
 
Jack's Avatar
 
Join Date: Apr 2002
Location: Wisconsin
Posts: 3,235
Location: Wisconsin
Sportbike: F4,Brutale,Warrior,DRZ
Years Riding: X
How you found us: .
           
Send a message via AIM to Jack
Quote:
Originally Posted by DieselBoy View Post
Few corrections :

Define new variable TotalTime = 0;
Define new variable avgVelocity = 0;

Add TotalTime += TimeInterval; inside the loop and before any of calculations
Replace Volocity = NewPosition/TimeInterval ; with
avgVelocity = NewPosition/TotalTime;

avgVelocity is your avg velocity (not Velocity)
Good point, I missed that in the calculation. The real problem here is that the time interval to get the average velocity over all runs is TimeInterval * IntervalCount.

So no need for additional variables. Try replacing this line

Velocity = NewPosition / TimeInterval; // assumes original starting position of 0

with this line

Velocity = NewPosition / (TimeInterval * IntervalCount); // assumes original starting position of 0


If it makes it more readable, you could rename my Velocity variable to OverallVelocity.



Also, you can delete this line:

Velocity = NewVelocity;

It's not needed.
Jack is offline  
post #14 of 19 (permalink) Old 02-08-2008, 11:20 PM
habitual line stepper
 
Jack's Avatar
 
Join Date: Apr 2002
Location: Wisconsin
Posts: 3,235
Location: Wisconsin
Sportbike: F4,Brutale,Warrior,DRZ
Years Riding: X
How you found us: .
           
Send a message via AIM to Jack
Quote:
Originally Posted by BusaDave View Post
I don't see much that's C++ about this code other than the use of cout.
Agreed. It's basically C not C++. I wasn't sure they are working with classes yet. But if you are, you could wrap all the variables and calculations in a class with properties. It would clean things up a bit and make it more readable, but it's not really needed.
Jack is offline  
post #15 of 19 (permalink) Old 02-08-2008, 11:23 PM
habitual line stepper
 
Jack's Avatar
 
Join Date: Apr 2002
Location: Wisconsin
Posts: 3,235
Location: Wisconsin
Sportbike: F4,Brutale,Warrior,DRZ
Years Riding: X
How you found us: .
           
Send a message via AIM to Jack
Also noticed,

while (NewPosition > 0)

should be

while (NewPosition >= 0)
Jack is offline  
post #16 of 19 (permalink) Old 02-09-2008, 12:58 AM
[Online]
 
Fracker's Avatar
 
Join Date: Mar 2005
Location: Chicago
Posts: 3,695
Location: Chicago
Sportbike: Multistrada 1100S; CBR600RR
Years Riding: Since 2004
How you found us: CRR
           
Quote:
Originally Posted by PaikyPoo View Post
i'm a java developer with just enough knowledge of c++ to know i hate pointers.
I'm too drunk to code, but I have to comment on this. Pointers is the best thing in C++. This is the sweetness, the love and the dao. Fuck, pointers are pure sex .

- Stas

Thou shalt not disfigure the soul.
Fracker is offline  
post #17 of 19 (permalink) Old 02-09-2008, 01:30 AM
Registered User
 
BusaDave's Avatar
 
Join Date: Apr 2002
Location: Naperville
Posts: 5,002
Location: Naperville
Sportbike: 2002 Turbo Hayabusa
Years Riding: Lost Track
How you found us: Tony
           
Quote:
Originally Posted by Fracker View Post
I'm too drunk to code, but I have to comment on this. Pointers is the best thing in C++. This is the sweetness, the love and the dao. Fuck, pointers are pure sex .
Don't need C++ for pointers either. Works just fine in C.

Jack, I noticed your conditional mistake, but decided not to nit-pick. I prefer people do their own homework anyway.

Dave
BusaDave is offline  
post #18 of 19 (permalink) Old 02-09-2008, 11:24 AM
habitual line stepper
 
Jack's Avatar
 
Join Date: Apr 2002
Location: Wisconsin
Posts: 3,235
Location: Wisconsin
Sportbike: F4,Brutale,Warrior,DRZ
Years Riding: X
How you found us: .
           
Send a message via AIM to Jack
Another thing to consider, is since the user enters the TimeInterval you should do a test for TimeInterval==0? If it is zero you should prompt them to enter a positive number or negative to exit. TimeInterval can not be zero since it wouldn't make sense and you would get a division by zero if you tried to do the calculations.
Jack is offline  
post #19 of 19 (permalink) Old 02-09-2008, 11:28 AM Thread Starter
shhhhhhhhh!
 
sponge's Avatar
 
Join Date: Apr 2004
Location: Houston, TX
Posts: 1,227
Location: Houston, TX
Sportbike: suzuki
Years Riding: yesterday
How you found us: Nabisco
           
Man, you guys are hardcore on this. Thanks a whole bunch. I will now go see some hot bikes and weird people walking around in their riding jackets in the middle of dead winter, then come back and work on this again.
sponge is offline  
Reply

Quick Reply
Message:
Options

Register Now



In order to be able to post messages on the Chicagoland Sportbikes forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.

User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.

Password:


Confirm Password:
Email Address
Please enter a valid email address for yourself.

Email Address:
OR

Log-in










Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page
Display Modes
Linear Mode Linear Mode



Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

 
For the best viewing experience please update your browser to Google Chrome