#include <iostream>
#include <stdlib.h>
#include <fstream.h>
void main(void)
{
char a[32];
cout << "Please enter a file to read from: " << endl;
cin >> a;
ifstream f;
f.open(a);
if(f.fail())
{
cout << "Error opening file" << endl;
return 0;
}
typedef struct{
float height;
float Eg;
float Ek;
float v;
} Datum;
datum entrys[50];
while(f.eof() != true)
{
f >> entrys.height;
im stuck and would like some advice
Here is what i need to do:
You are given the starting height and mass of a roller coaster. You are then given the height of the
track at various points in time. The program should calulate the gravitational potential energy, the kinetic
energy, and the velocity of the rollercoaster at that point. The data will be given to you in a file. The first
line will have the weight of the coaster in kilograms. The second will have the original height. After that,
it will be consecutive heights (in metres) of points along the track. ( you don’t need to care how far apart
these points are horizontally). Output all the data (including the givens) in tabular form to a file, that is: Height — Eg — Ek — v.
ok the code i have so far is:
I know that this requires some basic knowledge of physics, but im more concerned about the structure of my code, im not sure if im headed in the right direction, any help is appreciated. thanks
[edited by - talith on November 23, 2002 2:13:54 AM]
Sounds like homework to me.
For starters, your code doesn''t seem really complete. The while loop isn''t closed and there''s nothing else after it.
Hope this helps
Seriously now, little bit of advice, I''d put all my definitions at the same place instead of scattering typedefs and the likes all over the place in the middle of your main().
For starters, your code doesn''t seem really complete. The while loop isn''t closed and there''s nothing else after it.
Hope this helps
Seriously now, little bit of advice, I''d put all my definitions at the same place instead of scattering typedefs and the likes all over the place in the middle of your main().
Heya,
A good step would be to just clean up your code as it is quite messy. It would also be good to comment things, even though this is a small project it is a good habbit to get into.
All i''ve done is cleaned this up a bit, added some comments and changed a couple of things...
Cheers,
Morgan
A good step would be to just clean up your code as it is quite messy. It would also be good to comment things, even though this is a small project it is a good habbit to get into.
#include <iostream> // Include I/O Header
#include <stdlib.h> // Include StdLib Header
#include <fstream.h> // Include File I/O Header
int main(void) // Main Function
{
char a[32]; // Declaration of Char a
cout << "Please enter a file to read from: " << endl;
cin >> a; // Input Dest. File
ifstream f; // Locate File
f.open(a); // Open File
if(f.fail()) // If error
{
cout << "Error opening file" << endl;
return 0;
}
typedef struct{ float height;
float Eg;
float Ek;
float v;
} Datum;
datum entrys[50];
while(f.eof() != true)
{
f >> entrys.height;
}
return(0);
}
All i''ve done is cleaned this up a bit, added some comments and changed a couple of things...
Cheers,
Morgan
Cheers,Morgan
// move this up here, cuz It looks better
struct datum
{
float height;
float Eg;
float Ek;
float v;
};
int main(void)
{
char a[32];
cout << "give me a file name" << endl;
cin >> a;
ifstream file(a);
datum entries[50];
int weight;
file >> weight;
// change this to a for loop, so if there are more entries than
// 50, we don''t bomb out.
int x;
for(x=0; x<50; ++x)
{
if( file.eof() ) break;
file >> entries[x].height; // forgot the [x]
}
entries[0].Eg = 9.8*entries[0].height*weight;
entries[0].v = 0; //assuming this is where the coaster starts
entries[0].Ek = 0;
for(x=1; x<50; ++x)
{
entries[x].Eg = 9.8*entries[x].height*weight;
entries[x].Ek = entries[0].Eg - entries[x].Eg;
entries[x].v = sqrt( (ke*2)/m );
}
return 0;
}
Youll have to add in your own output, since I''m too lazy to do it for you...
--- insert signature here ---
struct datum
{
float height;
float Eg;
float Ek;
float v;
};
int main(void)
{
char a[32];
cout << "give me a file name" << endl;
cin >> a;
ifstream file(a);
datum entries[50];
int weight;
file >> weight;
// change this to a for loop, so if there are more entries than
// 50, we don''t bomb out.
int x;
for(x=0; x<50; ++x)
{
if( file.eof() ) break;
file >> entries[x].height; // forgot the [x]
}
entries[0].Eg = 9.8*entries[0].height*weight;
entries[0].v = 0; //assuming this is where the coaster starts
entries[0].Ek = 0;
for(x=1; x<50; ++x)
{
entries[x].Eg = 9.8*entries[x].height*weight;
entries[x].Ek = entries[0].Eg - entries[x].Eg;
entries[x].v = sqrt( (ke*2)/m );
}
return 0;
}
Youll have to add in your own output, since I''m too lazy to do it for you...
--- insert signature here ---
--- insert signature here ---
I noticed no one actually closed the file when they were done.
The past was unknown, the future was predicted.
The past was unknown, the future was predicted.
the future is just like the past, just later. - TANSTAAFL
Thanks for the help, its not homework, just something a friend gave me to do to help learn C++.
char a[32]; // Declaration of Char a
Lol, you know, there is such a thing as over-commenting. Also, if you were going to comment this line, you should state what the variable will be used for, not re-stating exactly what the line does. Actually, ''a'' should be replaced with a more intuitive variable name.
As for cleaning up your code, I''ll admit, I''m a HUGE fan of OO programming. I would create a roller-coaster class, with a constructor you can supply a filename, so:
ROLLERCOASTER::ROLLERCOASTER(char* filename)
{
// code here
}
Then I would create a separate function to calculate all that crap you wanted at a given point.
ENERGY ROLLERCOASTER::CalculateEnergy(int trackNode)
{
// calculate stuff, put into ENERGY structure.
}
The ENERGY structure would replace your ''Datum'' struct, which is painfully vague. You could then reference it like so:
Energy.Kinetic = ...
Actually, while you''re at it, I''d make ENERGY an object, and create functions to funnel energy from one member to another, so you would subtract from the potential energy, and add to the kinetic, for example.
You would also want to do a function like:
void ROLLERCOASTER::OutputToFile(char* filename)
{
// code here, would call CalculateEnergy once for every
// track node.
}
Lol, you know, there is such a thing as over-commenting. Also, if you were going to comment this line, you should state what the variable will be used for, not re-stating exactly what the line does. Actually, ''a'' should be replaced with a more intuitive variable name.
As for cleaning up your code, I''ll admit, I''m a HUGE fan of OO programming. I would create a roller-coaster class, with a constructor you can supply a filename, so:
ROLLERCOASTER::ROLLERCOASTER(char* filename)
{
// code here
}
Then I would create a separate function to calculate all that crap you wanted at a given point.
ENERGY ROLLERCOASTER::CalculateEnergy(int trackNode)
{
// calculate stuff, put into ENERGY structure.
}
The ENERGY structure would replace your ''Datum'' struct, which is painfully vague. You could then reference it like so:
Energy.Kinetic = ...
Actually, while you''re at it, I''d make ENERGY an object, and create functions to funnel energy from one member to another, so you would subtract from the potential energy, and add to the kinetic, for example.
You would also want to do a function like:
void ROLLERCOASTER::OutputToFile(char* filename)
{
// code here, would call CalculateEnergy once for every
// track node.
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement