Sponsor Message:
Non Aviation Forum
My Starred Topics | Profile | New Topic | Forum Index | Help | Search 
I Need C++ Help, Any Programmers?  
User currently offlineLehpron From United States of America, joined Jul 2001, 7028 posts, RR: 21
Posted (10 years 4 months 2 weeks 3 days 14 hours ago) and read 1313 times:

Here's our assignment:

"An open box is to be made from a piece of rectangle cardboard by cutting out squares of equal size from the four corners and bending up the sides. What size should the squares be to obtain a box with largest possible volume?

Design a class to define a specific object to solve the above problem.

The program is worth 20 points and the due date is March 12, 2004 at noon.


BTW, that's PST local to the west coast of USA, it is 230pm March 11th here.

I got the algorithm and math done (derivative of volume function set to zero to find max volume) but can't seem to translate it into code, I do not understand how to use a CLASS function. All of the examples I have seen and used do not make sense, they have not given me a user-input example.

My book is not helpful, it says, "Classes are the standard unit of programming, they are like a blueprint – reusable, objects are instantiated (created) from the class. For example, a house is an instance of a "blueprint" class"

Giving this example:

struct Time {
int hour;
int minute;
int second;
};

I figure I start with:

struct Cutlength {
double cut;
double length;
double width;
};

...because none of the length, width or the cut are integers, they get the double. From there I don't know where to go. I got the program in a do-while with if-then-else done, but the teacher wants classes, I don't get classes!

[Edited 2004-03-11 23:33:35]


The meaning of life is curiosity; we were put on this planet to explore opportunities.
16 replies: All unread, jump to last
 
User currently offlineSebolino From France, joined May 2001, 3681 posts, RR: 4
Reply 1, posted (10 years 4 months 2 weeks 3 days 13 hours ago) and read 1296 times:

Perhaps:

Build a class "Open box" with a method to calculate its volume. Make a loop to create an open box making varying all you can (Length and whatever you call the other param). Find the extremum during the loop.

No math, that's just an iterative test.


Does it make sense ?


User currently offlineKlaus From Germany, joined Jul 2001, 21406 posts, RR: 54
Reply 2, posted (10 years 4 months 2 weeks 3 days 13 hours ago) and read 1290 times:

I can´t give you a complete C++ course in here...

But basically, a C++ "class" is roughly the following:

- It creates a "name space" to make every name (functions, types etc.) "local" to this class to evade conflicts with equally named functions or types in other classes / name spaces.

- Defining a class is basically a spruced-up structure type definition; Instead of typedef struct ... you declare a class with object variables (corresponding to conventional structure elements). Every object is a structure that contains those elements.

- In addition to the "typedef-similar" effect, a class declaration tells the compiler which functions can be applied to objects of this class; When invoking an object function

obj.func(par1, par2);

The compiler can just look at what class the object belongs to and it knows which "func()" to call, even if other classes have functions of the same name. Within that object function´s declaration, the object´s components can be accessed like local variables (makes it a little easier to write).

- When an object is declared, the "constructor" function is automatically called to initialize it (very useful!) and when it is deleted or falls out of scope, the "destructor" function is called (even more useful!!).

- In addition, the class can define "static" functions or variables which are not part of every object but just exist once for the class (like an object counter or similar stuff).

- Then there´s the whole inheritance business which makes things still more interesting, but also more complex...

For all the syntax and semantics there´s no way around the reference book by Bjarne/Stroustrup; But maybe you can get away with what you can find on the net...


User currently offlineTrekster From , joined Dec 1969, posts, RR:
Reply 3, posted (10 years 4 months 2 weeks 3 days 13 hours ago) and read 1285 times:

im just going to go HUH??????????

My mate is doing a couse at uni, and he talks about c++ alot, always goes over my head


User currently offlineLehpron From United States of America, joined Jul 2001, 7028 posts, RR: 21
Reply 4, posted (10 years 4 months 2 weeks 3 days 13 hours ago) and read 1277 times:

Sebolino, that Open Box sounds like math to me, like some function to find the max vol. I already got that part, if that was what you meant:

maxvol = (width - 2 * cut) * cut * (length- 2 * cut)
-- max volume occurs when the derivative of the function equals zero and the use of the quadratic formula must be implimented to find cut and since there are two values for c, set one c1 and second as c2 --

d/dc (4*c^3 -4*c^2*(w+l) +w*l*c) = 12*c^2 - 8*c*(w+l) + w*l

c1 = (-y - sqrt (y * y - 4 * x * z))/(2 * x);
c2 = (-y + sqrt (y * y - 4 * x * z))/(2 * x);

-- it turns out that c2 will always be greater than either the length or width; hence, c1 is the longest practical length.--

Klaus, that reads like it would be from the text, and is somewhat porportionately undersandable, but I need an working example, like something asking enter two numbers to add them or something. A template is what I need.

Would "obj.func(par1, par2);" become -->> "max.cut(length, width);" ?



The meaning of life is curiosity; we were put on this planet to explore opportunities.
User currently offlineKlaus From Germany, joined Jul 2001, 21406 posts, RR: 54
Reply 5, posted (10 years 4 months 2 weeks 3 days 12 hours ago) and read 1269 times:

I just had the impression that all the "fluff" that´s being thrown around about classes had you confused and I wanted to infuse some actual explanations...  Wink/being sarcastic


The basic structure would be something like this:


in box.h:

class box
{
private: // only class functions are allowed to see these:

// object variables (once for every object)
double volume;
double cutout;

// class variables (only once for the whole class)
static long int nobj = 0; // init object counter

// constructor declaration and inline implementation (macro-like)
void box() { volume = 0; cutout = 0; nobj++; };

// destructor declaration and inline implementation
void ~box() { volume = 0; cutout = 0; nobj--; };


public: // everybody can see this:

double getvolume(double trycutout); // object function declaration


// static function (not applied to an object):
static long int getnobj() { return nobj; };


}


in box.cpp:

// implement object function (box:: "steps into" the class namespace)
double box::getvolume(double trycutout)
{

// uses the "volume" component of the object like a local var...
double result = volume +-*/ ... trycutout; // do something here...

return result;
}


in main.cpp:

#include "box.h";

int main()
{
box mybox; // declares object of class box (invokes constructor)

// apply object function:
double whatever = mybox.getvolume(17.5);

// call class function (again, box:: "steps into" the class namespace, since
// the compiler has no object to know the class from)
long int object_count = box::getnobj();
// object_count will now be 1 (the constructor for "mybox" has
// incremented the object count from 0 to 1)

// "mybox" will die here: the destructor is called automatically!
}



See? (No warranty; I´ve been out of C++ for a few weeks now.)

(Sorry for the formatting; I´ve got no nerve to debug a.net right now.)

[Edited 2004-03-12 01:16:36]

User currently offlineLehpron From United States of America, joined Jul 2001, 7028 posts, RR: 21
Reply 6, posted (10 years 4 months 2 weeks 3 days 11 hours ago) and read 1259 times:

That's okay dude, I got this so far, zero errors according to compiler but the numbers are off in the exe file:

#include
#include
using namespace std;

// Time abstract data type (ADT) definition

class Volume
{
public:
Volume(); // constructor
void setVolume( double, double);// set length, width
void printCut();
void printMaxVol();
double cut;
private:
double length;
double width;
};

Volume::Volume() { length = width = 0; }

void Volume::setVolume( double l, double w)
{
length = ( l >= 0 ) ? l : 0;
width = ( w >= 0 ) ? w : 0;
}

void Volume::printCut()
{
cut = ((8) * ( width + length) - (sqrt ((8*(width + length)) * (8*( width + length)) - 48 * width * length))/24);
cout << cut << endl;
}

void Volume::printMaxVol()
{
printCut();
cout << (width - 2 * cut) * cut * (length - 2 * cut) << endl;
}

int main()
{
Volume v;

cout << "The Cutlngth from the corner is ";
v.printCut();
cout << "nThe Maximun volume is ";
v.printMaxVol();

v.setVolume( 13, 27);
cout << "nnCut after setVolume is ";
v.printCut();
cout << "nVolume after setVolume is ";
v.printMaxVol();

cout << endl;
return 0;
}





******* I've been here for, crap, 6 hours(!), I'm gtg eat b4 my next class in another 3 hrs. Could anyone debug this for me 'less it's askin too much? Big grin

I'll be back here in the morning, that's 16 hrs from now.



The meaning of life is curiosity; we were put on this planet to explore opportunities.
User currently offlineKlaus From Germany, joined Jul 2001, 21406 posts, RR: 54
Reply 7, posted (10 years 4 months 2 weeks 3 days 11 hours ago) and read 1255 times:

No, sorry; Got my own work to worry about.

I don´t know how literal your copied code is, but this:

void setVolume( double, double);// set length, width

isn´t right for all I know. You´d need to specify formal names as well:

void setVolume( double l, double w);// set length, width

The #includes will probably have real arguments as well...

If it compiles okay but gives a bad result, use the debugger and single-step your code. That´s always a good idea anyway.

Good luck!


User currently offlineQb001 From Canada, joined Apr 2000, 2053 posts, RR: 4
Reply 8, posted (10 years 4 months 2 weeks 3 days 10 hours ago) and read 1247 times:

Listen, I'm not very familiar with C++, but I can hold my own in Java, which is a close cousin to C++.

I understand what you have to do, no problem.

I wonder how you'll derive the 3rd degree function though, unless there is a function in the math library that does that. Otherwise, you may have to program your own function to derive; it's a pain in the neck, but it can be done.

The way I see it would be something like (pseudo-code):

main(){
calc = new calcObject();

input width and length here...

calc.setParameters(width, length);
calc.calculateOptimalVolume();
double sizeOfPieceToRemove = calc.getSizeOfPieceToRemove();
double optimalVolume = calc.getOptimalVolume();

output sizeOfPieceToRemove and optimalVolume here...

return;
}

class calcObject{
private double width, length, sizeOfPieceToRemove, optimalVolume;

calcObject(){
}

public void setParameters(double w, double l){
this.width = w;
this.length = l;
return;
}

public void calculateOptimalVolume(){
this.sizeOfPieceToRemove = calcSizeOfPieceToRemove();
this.optimalVolume = calcOptimalVolume();
return;
}

public double getSizeOfPieceToRemove(){
return this.sizeOfPieceToRemove
}

public double getOptimalVolume(){
return this.optimalVolume
}

private double calcSizeOfPieceToRemove(){
//This is where you write the code to calculate the size
// of the piece to remove, using this.width and this.length
return result of the calculation;
}

private double calcOptimalVolume(){
//This is where you write the code to calculate the volume
// you get by removing the pieces, using this.sizeOfPieceToRemove
return result of the calculation;
}
}


This is Java style of doing it, but if you understand this, you could probably easily translate it into C++.



Never let the facts get in the way of a good theory.
User currently offlineLehpron From United States of America, joined Jul 2001, 7028 posts, RR: 21
Reply 9, posted (10 years 4 months 2 weeks 2 days 18 hours ago) and read 1225 times:

Okay, I kinda see the java pattern, like I said I got no errors but now the math is altogether wrong, I hope it is just a matter of parentheses.

And I copied the code completely, maybe indent problems cuz a.net doesn't allow tabs as far as I see. those '// text' just means that the program will ignore anything after those slashes, they are just comment lines.

This stuff is due in 91 minutes, and my results for a 8x11 sheet of cardboard should be a cut about = 0.62 and max volume of 44, I keep getting a cut 146 long and a volume in the millions....

perhaps my math is misplaced...?


I added a user input to it, as it was required (those hyphens are just an attempt to indent, not part of code):


-cout << "Enter a length (unitless): " << endl;
- cin >> l;
-cout << endl;
-cout << "Enter a width (unitless): " << endl;
- cin >> w;
-cout << endl;
-
-
-
-Volume v;
- v.setVolume(w,l);
-cout << "The Cutlength from the corner is ";
- v.printCut();
-cout << "nThe Maximun volume is ";
- v.printMaxVol();





[Edited 2004-03-12 19:36:53: DAMMIT my cut equation is wrong!]

[Edited 2004-03-12 19:42:26]


The meaning of life is curiosity; we were put on this planet to explore opportunities.
User currently offlineLehpron From United States of America, joined Jul 2001, 7028 posts, RR: 21
Reply 10, posted (10 years 4 months 2 weeks 2 days 17 hours ago) and read 1220 times:

I'm done, it works, yay!

thank you everyone, chances are I won't learn from procrastination but I guess that is one mistake I have to do a bunch of times b4 i get the point Big grin



The meaning of life is curiosity; we were put on this planet to explore opportunities.
User currently offlineKlaus From Germany, joined Jul 2001, 21406 posts, RR: 54
Reply 11, posted (10 years 4 months 2 weeks 2 days 17 hours ago) and read 1210 times:

Fine. Glad to have been of any help. Good luck!

User currently offlineJetService From United States of America, joined Feb 2000, 4798 posts, RR: 11
Reply 12, posted (10 years 4 months 2 weeks 2 days 16 hours ago) and read 1204 times:

C++ is for pussies. 10 bucks if you can identify this.

ssFrom:="Proof"
ARRAY STRING(20;llbottom;4)
llbottom{1}:="Get path..."
llbottom{2}:="Get ticket..."
llbottom{3}:="Get specs..."
llbottom{4}:="Get info..."
llbottom:=1
USE SET("all")
C_BOOLEAN(Wcolor)
ARRAY STRING(3;llEmp;0)
ARRAY STRING(45;llAcccount;0)
ARRAY STRING(8;llDirNo;0)
ARRAY STRING(40;llDirname;0)
ARRAY STRING(10;llsize;0)
ARRAY INTEGER(lloffice;0)
ARRAY STRING(20;llDesignat;0)
  `ARRAY STRING(20;llArtID;0)
ARRAY STRING(40;llHeading;0)
ARRAY TEXT(llComment;0)
ARRAY STRING(2;llChkMar;0)
ARRAY TEXT(llNote;0)
ARRAY REAL(llRECID;0)
ARRAY REAL(llRECID;Records in selection([TRAFFIC]))
ARRAY STRING(15;llptrStr;0)
ARRAY STRING(15;llptrStr;Records in selection([TRAFFIC]))
initPendList
ARRAY STRING(10;llLoca;0)
ARRAY STRING(10;llLoca;Records in selection([TRAFFIC]))
ARRAY STRING(4;llboutton;0)
ARRAY STRING(4;llboutton;Records in selection([TRAFFIC]))
ARRAY BOOLEAN(llcolor;0)
ARRAY BOOLEAN(llcolor;Records in selection([TRAFFIC]))
ARRAY BOOLEAN(llcolor2;0)
ARRAY BOOLEAN(llcolor2;Records in selection([TRAFFIC]))
If (Macintosh control down)
TRACE
End if
SELECTION TO ARRAY([TRAFFIC]OPERATOR_NO;llEmp;[TRAFFIC]ACCOUNT;llAcccount;[TRAFFIC]DIRECTORY_NO;llDirNo;[TRAFFIC]DIRECTORY;llDirname;[TRAFFIC]UDAC;llsize;[TRAFFIC]OFFICE_NO;lloffice;[TRAFFIC]HEADING;llHeading;[TRAFFIC]COMMENT;llComment;[TRAFFIC]NOTE;llnote;[TRAFFIC]NOTE_CHECKMARK;llChkMar;[TRAFFIC]DESIGNATIONCODE;llDesignat)
For ($i;1;Records in selection([TRAFFIC]))
GOTO SELECTED RECORD([TRAFFIC];$i)
llRECID{$i}:=Record number([TRAFFIC])
If (bActual)
llptrStr{$i}:=GET_FIELD_PTR_ACTUAL ("STRING")
Else
llptrStr{$i}:=GET_FIELD_PTR ("STRING")
End if
llLoca{$i}:=WLOCASTRING->
llboutton{$i}:=WLOCABUT->
llcolor{$i}:=wcolor
llcolor2{$i}:=wcolor2
End for
$error:=AL_SetArraysNam (ADDLIST;1;15;"llLoca";"llEmp";"llAcccount";"llChkMar";"llDirNo";"llDirname";"llsize";"lloffice";"llHeading";"llDesignat";"llComment";"llNote";"llboutton";"llRECID";"llptrStr")
$error:=AL_SetArraysNam (ADDLIST;16;1;"llcolor")
$error:=AL_SetArraysNam (ADDLIST;17;1;"llcolor2")
AL_SetRowOpts (ADDLIST;0;1;0;0;1)
AL_SetMiscOpts (ADDLIST;0;0;";")
AL_SetWidths (ADDLIST;1;12;?col1;?col2;?col3;?col4;?col5;?col6;?col7;?col8;?col9;?col10;?col11;?col12)
AL_SetStyle (ADDLIST;0;"Geneva";9;0)
AL_SetHdrStyle (ADDLIST;0;"Geneva";9;1)
AL_SetHeaders (ADDLIST;1;12;"Loc.";"T#";"Account";"N";"Dir Nº";"Directory";"Size";"Office";"Heading";"Designation";"Comments";"Note")
For ($i;1;Size of array(llRECID))
If (llcolor{$i})
AL_SetRowColor (ADDLIST;$i;"RED";0;"";0)
End if
If (llcolor2{$i})
AL_SetRowColor (ADDLIST;$i;"WHITE";0;"BLACK";0)
End if
If (llcolor{$i}) & (llcolor2{$i})
AL_SetRowColor (ADDLIST;$i;"RED";0;"BLACK";0)
End if
If (llptrStr{$i}="NONE")
AL_SetRowColor (ADDLIST;$i;"RED";0;"RED";0)
End if
QUERY([CREATIVEPURP];[CREATIVEPURP]ACCOUNT=llAcccount{$i})
If (Records in selection([CREATIVEPURP])>0)
AL_SetRowColor (ADDLIST;$i;"MAGENTA";0;"";0)
End if
End for
AL_SetColOpts (ADDLIST;1;0;0;4;0;0;0)
  `AL_SetSort (ADDLIST;0;1;1;0;1;0;0;0;1;0;0;0;0;-1)
AL_SetSort (ADDLIST;-15;2;3;5)
VPEND:=Size of array(llrecid)
vtot:=vpend+vdone
theUser:=Current machine owner





"Shaddap you!"
User currently offlineLehpron From United States of America, joined Jul 2001, 7028 posts, RR: 21
Reply 13, posted (10 years 4 months 2 weeks 2 days 16 hours ago) and read 1203 times:

It reminds me of FORTRAN 77 ( learned in 2000), could be QBasic (learned in 1997), i donno.

What is it?



The meaning of life is curiosity; we were put on this planet to explore opportunities.
User currently offlineJetService From United States of America, joined Feb 2000, 4798 posts, RR: 11
Reply 14, posted (10 years 4 months 2 weeks 2 days 14 hours ago) and read 1193 times:

It's 4th dimension (4D). Database and application development.

I believe it origiated in France.



"Shaddap you!"
User currently offlineQb001 From Canada, joined Apr 2000, 2053 posts, RR: 4
Reply 15, posted (10 years 4 months 2 weeks 2 days 10 hours ago) and read 1187 times:

Indeed, 4th dimension is from France. I think it is mostly used in the Mac world.

If you a real programmer, then you gotta try Brainfuck !

Here's the famous "Hello World" in Brainfuck.

>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]
<.#>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++[
<++++>-]<+.[-]++++++++++.



Never let the facts get in the way of a good theory.
User currently offlineKlaus From Germany, joined Jul 2001, 21406 posts, RR: 54
Reply 16, posted (10 years 4 months 2 weeks 2 days ago) and read 1172 times:

And I thought Perl was ugly!  Nuts

Top Of Page
Forum Index

This topic is archived and can not be replied to any more.

Printer friendly format

Similar topics:More similar topics...
Any Veteranarians Here? I Need Help Badly. posted Sun Dec 8 2002 03:27:40 by Airlinelover
Any Songwriters/rewriters Out There? Need Help! posted Thu Jun 13 2002 06:58:53 by Airlinelover
Native Spanish Speakers: I Need Help posted Sun Nov 19 2006 05:48:43 by Tsaord
I Need Help With Copyrights And Radio Broadcasts posted Thu Nov 16 2006 23:01:51 by FlyingNanook
Need Help Getting Broncos-Cardinals Tickets posted Tue Nov 7 2006 00:13:41 by HALFA
I Need Help With A Matrix Problem... posted Fri Oct 20 2006 18:00:22 by Iamcanadian
Dating Advice, Need Help! posted Wed Oct 18 2006 00:16:59 by Speedbird747BA
Need Help On US Immigration Policy... posted Sun Oct 15 2006 02:49:03 by Boeinglover24
Need Help With V=IR (and TEC's On A Computer) posted Fri Oct 13 2006 00:38:37 by Lehpron
Need Help To ID A Song (techno) posted Tue Oct 3 2006 04:40:25 by Luisde8cd