Print 

Author Topic: C++ Help!  (Read 6086 times)

Offline Goalie

  • Posts Too Much
  • *****
  • Posts: 2462
  • Congrats Jordan Lynch, 3rd place in Heisman!!!
    • York Set/Tech Crew
C++ Help!
« on: March 04, 2008, 03:33:55 PM »
Calling all computer geeks!!!!

in my C++ class, i have to devise a program that uses several functions, each function only does one job.  i.e. asking the user to input a number then returning that number to main() or another function.

Most of the problem is not a problem.  Just these two functions, and i'll give you the quote from the assignment sheet:

Quote
void doRange()
This function will call the getValue() function 4 times in order to prompt the user for values between 0 and 10000, pass the four values returned by the getValue() calls to the calcRange() function, and then display the result that is returned by the calcRange() function.

int calcRange( int number1, int number2, int number3, int number4 )
This function will calculate a range amoung 4 integer values. It takes four arguments: the integers that should be used in the range calculation. It returns an integer value which is the range of the 4 numbers. The range of the numbers is the difference between the largest of the 4 numbers and the smallest of the 4 numbers.

I have a few ways to go around this, but I'm trying to find a better, shorter solution.  1. Use around 12 if/else if statements to find out which numbers are at the top and bottom of the range.  2. Compare the numbers as they are inputted by the user (in the DoRange function).  3. Calculate the range between each integer and return the largest range. (will need to use absolute values to make like easier).

Unfortunately, all of these require me to use between 6-12 if statements in order to get the correct answer.  I am leaning towards #3 right now.  If you guys have any other suggestions on how to approach this, let me know.
thanks in advance!
You blame me?  Remember it had to get past 10 other players before I saw the ball.

Thanks to Spidey for this sig!                                                                                                                  .

Offline BFM_Edison

  • Captain
  • *
  • Posts: 3074
Re: C++ Help!
« Reply #1 on: March 04, 2008, 03:41:34 PM »
I don't know C++, but I know Java and I'm pretty sure it's simple enough it would be done in a similar way. calcRange is the actual thing that's a method, and it has to take in four integers, so the one where you do stuff each time something is inputted won't work under the requirements. What you want to do is this for only six if statements:
Create two new integers, large and small
Set both large and small equal to number1
if number2 > large
large = number2
if number3 > large
large = number3
if number4 > large
large = number4

You'd do the same thing with small except with a less-than sign. You'd subtract small from large and have your range. Now, if you know how to make arrays and for loops, I could make it much simpler.
« Last Edit: March 04, 2008, 03:45:38 PM by bfm_Edison »
52.87   60.07   46.40   72.73   68.23   55.10   98.27   84.73

Offline Bleach

  • Senior Poster
  • ****
  • Posts: 702
Re: C++ Help!
« Reply #2 on: March 04, 2008, 05:54:00 PM »
also, in php there are case statements, where you have different cases, and depending on each case you do something, im not sure if C++ has this, but if it did using these case statments you could do edisons steps but youd only have to use 3 if statements rather then 6
A part of the winning teams for season 9, 10, 13, 17, 18 and now 19. Thanks for all of the great memories guys :D

Offline BFM_Edison

  • Captain
  • *
  • Posts: 3074
Re: C++ Help!
« Reply #3 on: March 04, 2008, 06:00:56 PM »
Do you mean switch statements?
switch(variable)
case 1:
blah
break;
case 2:
blah
break;
default:
moo
break;

That type of thing? Although I think if you did it like that, you'd decrease the if statements but still increase the switch statements, so it wouldn't have much of an overall effect.
52.87   60.07   46.40   72.73   68.23   55.10   98.27   84.73

Offline BFM_Hydra

  • BFM Admin
  • *
  • Posts: 10013
  • BFM Rules Guru and Forum Pwner.
Re: C++ Help!
« Reply #4 on: March 04, 2008, 06:31:54 PM »
...you and your switch statements, Edison ::)


BFM_Hydra Signature: Miser
Soccer Trophy: Igor
Scrim Season VII Trophy: H@ngm@n

Thanks guys!

TESTIMONIAL
"Hydra knows the forums like a spider knows it's web." - BFM_Jimmy 5/9/09


"Hydra, 300 posts to you is like pocket money to the Monopoly guy." - BFM_Jimmy 11/9/09

Offline BFM_Edison

  • Captain
  • *
  • Posts: 3074
Re: C++ Help!
« Reply #5 on: March 04, 2008, 06:38:39 PM »
Lol. Gotta love the default case.
52.87   60.07   46.40   72.73   68.23   55.10   98.27   84.73

Offline Goalie

  • Posts Too Much
  • *****
  • Posts: 2462
  • Congrats Jordan Lynch, 3rd place in Heisman!!!
    • York Set/Tech Crew
Re: C++ Help!
« Reply #6 on: March 04, 2008, 06:48:57 PM »
If there are case/switch statements, we haven't gotten that far yet.  there is another class after this one (Intermediate Programming in C++), which might, if C++ has it, cover case/switch statements.

I guess I was being too impatient.  Edison's first post will probably help the most.  I was trying to get it done in one shot instead of having to use several if statements, and in my mind I made it much harder than it was supposed to be.  I will use that suggestion though.

I also know that C++ writing programs each have their own pre-defined functions, which a range program might be in that mix.  But I use a different writing program than the TAs, so I doubt that one function on my program will be the same on the TA's.

(lol, moo)
You blame me?  Remember it had to get past 10 other players before I saw the ball.

Thanks to Spidey for this sig!                                                                                                                  .

Offline AR~Archon

  • Senior Poster
  • ****
  • Posts: 846
  • No surrender, no retreat
    • MacFire (Xfire for Mac)
Re: C++ Help!
« Reply #7 on: March 04, 2008, 08:25:46 PM »
I agree with Edison's approach, it should be the simplest and least prone to error.

Yes, C++ does support a 'switch' statement, but the 'case' conditions must be constant values.  That is, they must be known at compile-time.  You can't put expressions that require evaluation into a 'case'.  You have to use if/else if/else.
The minstrel boy to the war has gone...
Understanding is a three edged sword.  Your side, their side, and the truth.
Who are you?  What do you want?  Why are you here?  Where are you going?




Offline MrMxyzptlk

  • Posts Too Much
  • *****
  • Posts: 9208
  • Never backward,           always forward!
    • My 5th Dimensional Homepage
Re: C++ Help!
« Reply #8 on: March 04, 2008, 08:29:10 PM »



int calcRange( int number1, int number2, int number3, int number4 )
{

  int[] nums = {number1, number2, number3, number4};

  int low = number1;
  int high = number1;

  for (n=2, n<=4, n++)
  {

      if (nums[n] > high)  high == nums[n];
      if (nums[n] < low)  low = nums[n];
     
  }

return( high - low );

}


Not compiled or tested or anything, and there's no bounds or error-checking....

BTW, with a few minor mods mine has the advantage of working for ANY SIZED array of int's, once the developer understands the meaning of "general purpose programming" and passes an array instead of a fixed # arguments list!


(Yeah, yeah, I didn't need to put number1 into the array, but for the cost of doing it you get MUCH more readable code....)


« Last Edit: March 04, 2008, 08:39:34 PM by MrMxyzptlk »
Mr. Mxy's current Word Corner word is catachresis    

Offline Goalie

  • Posts Too Much
  • *****
  • Posts: 2462
  • Congrats Jordan Lynch, 3rd place in Heisman!!!
    • York Set/Tech Crew
Re: C++ Help!
« Reply #9 on: March 04, 2008, 08:47:57 PM »
Mxy's is the For-loop version of Edison's.  I can't relaly decide which to use, they're both so much better than mine!

thanks guys, this program is gonna be so much easier.  My mind was just making it seem hard.

BTW, I don't suppose you could give me the code for the rest of the program while you're at it?  I'll give you guys the link to the assignment... :evil:

Gotta watch out for the plagarism police!  :ninja: :siren: :ninja: :siren: :ninja:
You blame me?  Remember it had to get past 10 other players before I saw the ball.

Thanks to Spidey for this sig!                                                                                                                  .

Offline BFM_Edison

  • Captain
  • *
  • Posts: 3074
Re: C++ Help!
« Reply #10 on: March 04, 2008, 09:08:45 PM »
I wasn't sure if he had done arrays yet, which is why I alluded to that better solution in my post ;-) And is that C++? It looks exactly like Java. The only difference is that in Java, as I recall, when you fill an array like that you use rectangular brackets as opposed to curly brackets. I think an ArrayList would be better, cus you don't have to set a fixed size for it before loading the things in :-P Of course, you'd have to change the objects within an ArrayList to int, but that's simple.

By the way, I'm currently working on a program that schedules students for classes. The most annoying part, though, will be creating the text files with the requests and classes and whatnot, since I'll want a couple hundred students preferably :'(
52.87   60.07   46.40   72.73   68.23   55.10   98.27   84.73

Offline AR~Archon

  • Senior Poster
  • ****
  • Posts: 846
  • No surrender, no retreat
    • MacFire (Xfire for Mac)
Re: C++ Help!
« Reply #11 on: March 04, 2008, 10:36:51 PM »
Yeah, the array approach is somewhat more general.  There are several errors in your C++ code, though, Mxy :ninja:.

I purposefully avoid initializing arrays inside functions like that because I've been bit by different C++ compilers on different platforms not properly supporting it.  Code like this would be more portable:

Code: [Select]
int numbers[4];
numbers[0] = number1;
numbers[1] = number2;
numbers[2] = number3;
numbers[3] = number4;

Of course, passing a different set of arguments would be even better (as Mxy said), but you gotta stick with the assignment.

And is that C++? It looks exactly like Java.

In this regard they would look nearly identical.  The array declaration syntax is different.  Your original approach (sequential if statements) would probably be identical in both languages.

BTW, I don't suppose you could give me the code for the rest of the program while you're at it?  I'll give you guys the link to the assignment... :evil:

Nice try :)  (!
The minstrel boy to the war has gone...
Understanding is a three edged sword.  Your side, their side, and the truth.
Who are you?  What do you want?  Why are you here?  Where are you going?




Offline Daigoro

  • Posts Too Much
  • *****
  • Posts: 1807
  • shishogan...swordsman's eyes
Re: C++ Help!
« Reply #12 on: March 05, 2008, 08:46:43 AM »
 :munch:
Sincerely,
Daigoro

Offline MrMxyzptlk

  • Posts Too Much
  • *****
  • Posts: 9208
  • Never backward,           always forward!
    • My 5th Dimensional Homepage
Re: C++ Help!
« Reply #13 on: March 05, 2008, 12:06:41 PM »


(ROFL, D'!  ^^! :siderofl: )


- Yes, there are cosmetic errors in my pseudocode.... (That's why there are junior programmers and compilers, you know!  ;) ) Heck, I didn't even throw in all the REQUIRED commentary!  :P  :winkgrin:

- 'Better learn about arrays BEFORE you even learn about "for" loops!!  ::)

- Given the extraordinary optimizations that have gone into today's compilers (pretty much ALL of them) and into the CPU devices themselves, if you're ever going to do any "small" sequence more than two times your better off using a loop.... (In fact, tight loops are also the fastest code for today's CPUs to execute, since it stays in L1 cache....)

- Tale: I once wrote a "case()" statement that was SO HUGE it exceeded the compiler's MEMORY JUMP RANGE and busted up our (Fortune 1000 Co's) COMMERCIAL, SHIPPING COMPILER!  :siderofl: :siderofl: (The down side: It produced NO ERROR during compilation, it just jumped to some "random (okay addr-32KB... Ya KB, I'm OLD! But then, our entire OS ran in 4MB! ::) )" location in memory during execution and continued on its merry way!  . . . Debug THAT bugger!!!  :o )

« Last Edit: March 05, 2008, 12:09:54 PM by MrMxyzptlk »
Mr. Mxy's current Word Corner word is catachresis    

Offline Goalie

  • Posts Too Much
  • *****
  • Posts: 2462
  • Congrats Jordan Lynch, 3rd place in Heisman!!!
    • York Set/Tech Crew
Re: C++ Help!
« Reply #14 on: March 05, 2008, 01:53:02 PM »
Okay, so i need more help now.  I will go to my professor, i just want you guys' input:

For some reason my program (called Portable Dev C++) does not make string variables the same way the TA's program (Quincy 2005) does.  The assignment calls for me to type:
string menu();
Here's the error: C:\users\blahblahblah\HW5.cpp:17: error: 'string' does not name a type
And later: c:\users\blahblahblah\HW5.cpp:34: error: 'string' does not name a type
(first one is when i declare the function Menu() before main() and the second one is where I open the function Menu())
but Dev C++ does not like this.  Any reason why?  And any way around this?  Or am i doing it completely wrong?

I can easily bypass this problem by going to a computer lab on campus, but I don't want to have to do that each time I need a string.  Much easier for me to stay in my room and type it, like I am now!

Also, we don't cover strings with respect to arrays till later in the semester, so I can't use those yet.  (I looked ahead in the lecture notes, I was curious about arrays)
You blame me?  Remember it had to get past 10 other players before I saw the ball.

Thanks to Spidey for this sig!                                                                                                                  .

Print