C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 29th, 2005, 08:50 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 14
Send a message via AIM to Geo.Garnett
Help debug a for loop

I have two nested for loops inside a while loop. I need help fixing a problem with the functionality of it. It runs through the process once but not the full amount which should be three times.
Heres the code and I hope I commented enough. any help would be nice.
Code:
#include <cstdlib>
#include <iostream>
#include <ctime>
//use a pointer to store the first two copies of the chosen dice
//then use a const int to return back the dice.

using namespace std;

int main()
{
    int d = 5;//number of dice used in for loops
    int dice[d];//variable used to store the initial dice roll
    int pick[d];//pointer to the chosen dice
    int i = 0,j = 0,amount = 1;//used to run the for, and while loops
    int choose = 0;//used to choose to keep or clear
    int seed = time(NULL);
    srand(seed);//make sure random # generator is outside loop for functionality
    while ( amount <= 3 )//suppose to continue until three turns but is not
    {
    for( i ; i <= d; i++ )
    {
         dice[i] = (rand() % 6) + 1;
         cout<<dice[i]<<endl;
         }
         for( j , i; j <= d; j++ , i++ )
         {
              cout<<"-----------\n";
              cout<<dice[j]<<endl;
              cout<<"-----------\n";
              cout<<"1] to keep \n"
                  <<"2] to clear \n";
              cin>>choose;
              if( choose == 1 )
              {
                  pick[i] = dice[j];
                  d--;
                  }
                  else
                  {
                      pick[i] = dice[j] = 0;
                      }
                      
              }
              amount++;
                      }
    
    system ("PAUSE");                  
    return 0;
}

The objective is to choose six random numbers then choose which ones you would like to keep and which ones to clear. then it should go through the process again until either all dice are filled or amount == 3. The game is yahtzee if you know it then you'll understand the concept
__________________
---Official Member Of The Itsacon Fan Club---
Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer.

Reply With Quote
  #2  
Old December 29th, 2005, 11:13 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 13 m 30 sec
Reputation Power: 14
Send a message via Yahoo to BloodlustShaman
I don't think you can put a for statement in a while statement.

I tried many things but it doesn't work sorry buddy but my advice try not using for statement in the while.

I looked over my book and saw it never used for statement in the while.
Sorry hoped I could have helped but I can't.
Comments on this post
Itsacon disagrees: Incorrect: you can put one in the other. Have a look at my post below.

Reply With Quote
  #3  
Old December 30th, 2005, 05:51 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,029 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Quote:
Originally Posted by Geo.Garnett
I have two nested for loops inside a while loop. I need help fixing a problem with the functionality of it. It runs through the process once but not the full amount which should be three times.
Heres the code and I hope I commented enough. any help would be nice.

The objective is to choose six random numbers then choose which ones you would like to keep and which ones to clear. then it should go through the process again until either all dice are filled or amount == 3. The game is yahtzee if you know it then you'll understand the concept


Can't refuse help to one of the fanclub, now can I?

But firstly: Bloodlustshaman, putting while's in for's, or for's in while's, or while's in while's or for's in for's is all allowed. It doesn't make your code readable, and eats up memory like hell, but you can nest 'em infinitely if you want.


On to the code:
Seems your primary problem is the fact you don't reset the i and j counters, but there are some other small things I'd like to point out, so I'm gonna do my normal walkthrough, line by line. (Nothing like a good dissection over breakfast )

cpp Code:
Original - cpp Code
  1. int d = 5;//number of dice used in for loops
  2. int dice[d];//variable used to store the initial dice roll
  3. int pick[d];//pointer to the chosen dice
  4.  

This is dangerous. It is correct that in C++ you can use variables as dimensions for arrays, but it makes your code overly slow, as the compiler has to convert this to run-time malloc calls. Which isn't necesary, since the number of dice is fixed. Better do this with a constant.

cpp Code:
Original - cpp Code
  1. int i = 0,j = 0,amount = 1;//used to run the for, and while loops
  2.  

I've noticed before you use two types of progress counters, 0 up-to the number, and 1 up-to-and-including the number
Best to pick one, and stick with it, cause this gets you confused. Proof is in this code, where you start at 0 for the dice, and then go up-to-and-including, causing you to throw 6 dice (which gets you disqualified )
My personal preference is the 0 up-to variant, since this is also the way arrays are indexed, meaning you don't have to convert.

cpp Code:
Original - cpp Code
  1. for( i ; i <= d; i++ )
  2. /*-and-*/
  3. for( j , i; j <= d; j++ , i++ )

This is where the whole thing probably falls apart, when it comes to the second for-loop, i is alread d+1, since that was the moment the first for-loop terminated. Next time through the while-loop, this also applies to j. This causes your arrays to be incorrectly indexed, which can lead to anything from weird numbers on your screen, to a simple straightforward BSOD.

cpp Code:
Original - cpp Code
  1. pick[i] = dice[j];
  2. d--;
  3. /*-and-*/
  4. pick[i] = dice[j] = 0;

Another problem. The way you implemented this, it is possible to overwrite earlier picked dice. What if I pick the first die both times? It'll only store the last one.

Finally, another rule-wise problem, aside from the afore-mentioned 6 dice, IIRC, you're allowed to 'unpick' dice after your second throw (throw them again for the last throw), this is not possible in your code

I took the liberty of rewriting the whole bunch (breakfast can be boring sometimes), have a look at this:

cpp Code:
Original - cpp Code
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <ctime>
  4. #define NUM_DICE 5
  5. #define NUM_TURNS 2
  6. //  NOTE that NUM_TURNS is only 2! You only get to pick dice twice, the final roll is final!
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     int dice[NUM_DICE][2];      //    2-column array, one for the value, one to set if it's picked or not
  13.     int i, turn, choose;            //  no init values necesary
  14.  
  15.     srand(time(NULL));        //    randomize randomizer :-)
  16.  
  17.     for(i = 0; i < NUM_DICE; i++)
  18.         dice[i][1] = -1;            //  pre-set all dice to 'not picked'
  19.  
  20.     for(turn = 0; turn < NUM_TURNS; turn++)   //   using a for loop here, since we need an increment each time anyway
  21.     {                              // also note the use of 0-up-to logic, and the constant defined above.
  22.         cout << "Roll nr. " << (turn + 1) << endl;  //    fancy output :-)
  23.         for(i = 0; i < NUM_DICE; i++)
  24.         {
  25.             if(dice[i][1] < 0)
  26.                 dice[i][0] = (rand() % 6) + 1;
  27.             cout << dice[i][0] << endl;
  28.         }
  29.  
  30.         for(i = 0; i < NUM_DICE; i++)
  31.         {
  32.             cout << "-----------\n";
  33.             cout << dice[i][0] << endl;
  34.             cout << "-----------\n";
  35.             cout << "1] to keep \n2] to clear \n";
  36.             cin>>choose;
  37.             if( choose == 1 )
  38.                 dice[i][1] = 1;
  39.             else
  40.                 dice[i][1] = -1;
  41.         }
  42.     }
  43.  
  44.     cout << "Final result:" << endl;
  45.     for(i = 0; i < NUM_DICE; i++)
  46.         cout << dice[i][0] << endl;
  47.  
  48.     system ("PAUSE");                 
  49.     return 0;
  50. }


Hope this helps
Comments on this post
Geo.Garnett agrees: Your the man.
BloodlustShaman agrees: Nice stuff and thanks for telling me the facts but for the # define I have never seen them and if
you explain them that would also help a lot
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 592

Reply With Quote
  #4  
Old December 30th, 2005, 01:09 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 14
Send a message via AIM to Geo.Garnett
Well Itsacon all's I can say is I think my problem is that I was trying so hard to do it a certain way that it screwed me up in actually doing it the best possible way. Im not to good with using # define I know what they do but I dont really understand the concept of how you use them correctly yet. If you can explain it in laymens terms for me I would also appreciate that too. Nice work by the way, but I hate to just copy and paste it to my program though . When I started I never thought that a simple game like yahtzee would be that difficult to program. I've been getting frustrated lately because it seems like I have to weed through all the bs in some of the books im reading to get to the important stuff.

Thank you for the help and as always it is much appreciated, I should be paying you to go to school, lol ...

Thanks for attempting BLS any help is always appreciated buddy.

Quote:
Im not to good with using # define I know what they do but I dont really understand the concept of how you use them correctly yet.

Ok, Looking over your code I think I understand it a little better. Is using the #define almost like making a constant variable?

Last edited by Geo.Garnett : December 30th, 2005 at 01:30 PM. Reason: final thought

Reply With Quote
  #5  
Old December 30th, 2005, 01:22 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 14
Send a message via AIM to Geo.Garnett
Quote:
It doesn't make your code readable, and eats up memory like hell, but you can nest 'em infinitely if you want.

So basically what you are saying is that this is bad practice or is it ok if its done right?

Reply With Quote
  #6  
Old December 30th, 2005, 06:42 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,029 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Post

#define is a so-called pre-processor statement.
(Note: If you see experienced hackers talk about 'the CPP', they mean the C Pre-Processor, not C++)

The pre-processor is the first stage a program goes through during compilation, it includes include files, removes comments and whitespace from your code, and handles #define's.

A #define is nothing but a way to tell a compiler to replace all instances of A in the code with B.

So where I say
Code:
#define NUM_DICE 5
I'm telling the Pre-Processor that everytime he sees 'NUM_DICE' in my code, he can replace it with '5'.
Of course, I could just type '5' in my code, but then, when I have to modify the game to use 4 dice, I have to edit all those places, not to mention the trouble if I make a typo somewhere, and suddenly at one place, it uses 6 dice instead of 5 (see my comment about bad array indexing in the earlier post).

It is also possible to use #define's in a more complex way, called macros.
Take this (very commonly used) example:
Code:
#define max(a,b) (((a)>(b))?(a):(b))

Here I define a macro, that returns the maximum value of 2 'things'. I say things, cause it'll work with every variable type, struct or object that has the greater-than operator ( > ) defined (especially useful in C++).

Again, what it does, is replace every instance of max(something1, something2) with (((something1)>(something2))?(something1) : (something2)), which is a simple inline function to return the maximum value of 2 somethings.
Here the #define recognises the function-like buildup of the first parameter (with the parentheses () ) and takes the elements in between as arguments for its second part.

Code:
Homework
Rewrite the macro above to return the MINIMUM value of two somethings.
Call it min(a,b)


Of course, macros should only be used in very specific cases, usually you're better off with either typing the function out, or by using a real function.

2 last warnings when using #define's (and macros):
  • Just like #include, do NOT terminate the line with a semicolon ( ; ) all lines starting with a hash (#) are terminated with a line break, if you want to stretch them over multiple lines, put an escape character (backslash, \ ) at the end of the line, this will escape the linebreak, and the #define will continue on the next line.
  • If you make a macro, ALWAYS put every argument in the second part between INDIVIDUAL parentheses (like the (a) and (b) in my max(a,b) example). This is to make sure everything goes right if you enter an expression as either a of b.

All this may be a bit much all at once, but just practice a bit with it, and look at other peoples code, it's all pretty straightforward in the end. I know of people who've written complete programs in the CPP


Quote:
Originally Posted by Geo.Garnett
So basically what you are saying is that this is bad practice or is it ok if its done right?


It is not optimal, but not really bad either. Like I said, your program may suffer a slight performance loss, but a good optimizer might even spot this, and replace it by a constant allocation anyway.
However, you know I'm a sticker for ANSI-C in most cases, and this is not valid there, so I don't use it.
Should you do it right (which you did by the way) this wouldn't present any problems though. G++ didn't even give me a warning over it (and I always compile with -Wall, another good coding practice ), so don't worry.

Reply With Quote
  #7  
Old December 30th, 2005, 08:38 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 14
Send a message via AIM to Geo.Garnett
Well I did my home work but im off on the median number, I had trouble for some reason to get it to calculate correctly everytime. My brain is running to fast to work right but here is what I got. and by the way man, I appreciate you taking the time with me, and walking me through everything like you do every time.
Code:
#include <cstdlib>
#include <iostream>
#define min(a) (((a)<(b))?(a):(b))
#define max(b) (((b)>(a))?(b):(a))
#define median(c) ((c) = (((a)+(b)) / 2))

using namespace std;

int main()
{
    int a;   
    int b;
    int c;
    cout<<"Enter first number\n";
    cin>>a;
    cout<<"Enter a second number\n";
    cin>>b;
    cout<<"-------------------\n";
    cout<<"Max entered is: "<<max(b)<<endl;
    cout<<"Minimum entered is: "<<min(a)<<endl;
    cout<<"Median number is: "<<median(c)<<endl;    
    system("PAUSE");
    return EXIT_SUCCESS;
}

The median is screwed but it uses the #defines like it should atleast. For some reason I have the most trouble with the easy stuff because Im concentrating on the harder things lol

Preciate the help man.

Last edited by Geo.Garnett : December 30th, 2005 at 08:43 PM.

Reply With Quote
  #8  
Old December 31st, 2005, 05:28 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,029 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
What's going wrong it the fact you only have one argument with every macro.

If you want a function (or macro) to give you the maximum of two values, you need to give 'em both values. Same goes for the median. You only specify the target variable, and neither of the others (for which it has to calculate the maximum). On the whole, specifying a target in a macro is not practical, it minimises its uses, and makes it less readable.

Also, the 'name' of the variables in the macro is of no consequence, and doesn't have to have any bearing on the names in the program. So if you define a macro max(a,b), that doesn't mean you can only use it on variables called a and b. if you use it like max(firstvariable, arrayindex[i]), it'll still work fine, just like a function.

So the corrected version of your homework would be:
cpp Code:
Original - cpp Code
  1. #include <cstdlib>
  2. #include <iostream>
  3. #define min(a,b) (((a)<(b))?(a):(b))
  4. #define max(b,b) (((b)>(a))?(b):(a))
  5. #define median(a,b) (((a)+(b)) / 2)
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     int a;   
  12.     int b;
  13.     cout << "Enter first number\n";
  14.     cin >> a;
  15.     cout << "Enter a second number\n";
  16.     cin >> b;
  17.     cout << "-------------------\n";
  18.     cout << "Max entered is: " << max(a,b) << endl;
  19.     cout << "Minimum entered is: " << min(a,b) << endl;
  20.     cout << "Median number is: " << median(a,b) << endl;   
  21.     system("PAUSE");
  22.     return EXIT_SUCCESS;
  23. }


Code:
Homework
Write a macro that takes two arguments.
These represent the straight sides of a triangle.
Have the macro calculate the length of the 3rd side.
This can be done using Pythagoras (a^2 + b^2 = c^2).
You can use the math.h library for square roots.

Reply With Quote
  #9  
Old December 31st, 2005, 05:11 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 14
Send a message via AIM to Geo.Garnett
I have failed boss. I tried to do this project but for some reason my math.h is not calculating to the second power for some reason, or Im just using it incorrectly but heres is what little I got. I also got carried away with looking through include files because now that I understand a little about define and ifndef and stuff I can kinda read the includes and make sense of them a little.
Code:
#include <cstdlib>
#include <iostream>
#include <math.h>
#define Path(c,a,b) ((c)=((a)^2)+((b)^2))

using namespace std;

int main()
{
    int line1;
    int line2;
    int line3;
    cout<<"Enter length of the first line : ";
    cin>>line1;
    cout<<"Enter the second length : ";
    cin>>line2;
    Path(line3,line1,line2);
    line3 = line3^2;
    cout<<line3;
    system("PAUSE");
    return EXIT_SUCCESS;
}

I know this isn't correct but I thought I would show you what I got so you can least see that I attempted. I know c should also be squared but I couldn't even get my compiler to do a single square root right let alone a formula more complex. For some reason it only adds the two variables together instead of doing the number squared???

By the way have a good new year and be safe tonight and watch out for da coppers too, I already got a 200$$ ticket today, and almost got another because I went through three traffic stops today in the matter of about four hours.

Reply With Quote
  #10  
Old January 1st, 2006, 10:52 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,029 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Have a look here for the different functions available from math.h

the ^ is not the 'power' function, its a bitwise xor. powers are done with the 'pow()' function, like pow(x, 2) to calculate x^2.

So the correct macro could have been:

cpp Code:
Original - cpp Code
  1. #include <math.h>
  2. #define path(a,b) (sqrt(pow((a),2)+pow((b),2)))
  3. /* -or- */
  4. #define path(a,b) (sqrt(((a)*(a))+((b)*(b))))
  5. /* pow(x,2) is the same as x*x */


Also, like I said before, it's not necesary to put the 'c' in the macro, just have the macro be the calculation, and assign it to the variable you want.

so:
cpp Code:
Original - cpp Code
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <math.h>
  4. #define path(a,b) (sqrt(pow((a),2)+pow((b),2)))
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int line1;
  11.     int line2;
  12.     cout<<"Enter length of the first line : ";
  13.     cin>>line1;
  14.     cout<<"Enter the second length : ";
  15.     cin>>line2;
  16.     cout << "length of longest side = " << path(line1, line2) << endl;
  17.     system("PAUSE");
  18.     return EXIT_SUCCESS;
  19. }

Reply With Quote
  #11  
Old January 2nd, 2006, 01:03 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 14
Send a message via AIM to Geo.Garnett
That was what I was doing wrong, I havent used the math library very often so its fairly new to me. This is definetly a function I am goint to start exploring with. Could have just did the a * a deal and that would have worked but like I said earlier I get so caught up trying to do something one way that I forget about the obvious. Thanks for all your help Itsacon..

Reply With Quote
  #12  
Old January 2nd, 2006, 03:04 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 13 m 30 sec
Reputation Power: 14
Send a message via Yahoo to BloodlustShaman
Kool stuff Itsacon yo Itsacon assign more homework cause I am late to this stuff and I want to give these things a try so plz assign some more homework

Damn Warcraft 3 it takes time of programming and doesn't give u time to look at the furoms to see whats new.

Reply With Quote
  #13  
Old January 3rd, 2006, 06:37 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,029 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Post

Assignment 1.
Rewrite the following code so it uses defines at places where you think it would be wise. And when I say rewrite, I mean rewrite, as long as the output is the same, I don't care what you do with it.

cpp Code:
Original - cpp Code
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int i, list[100];
  6.  
  7.     list[0] = 0;
  8.    
  9.     for(i = 1; i < 100; i++)
  10.         list[i] = list[i - 1] + i;
  11.  
  12.     for(i = 99; i > -1; i--)
  13.         printf("%d\n", list[i]);
  14.  
  15.     return 0;
  16. }


PM me the result, so you lot can't cheat by looking at each other's answers :-)
When I have them from both of you, I'll post them, with comments (if necessary).

PS: BloodlustShaman, are you a member yet?

Last edited by Itsacon : January 3rd, 2006 at 02:22 PM.

Reply With Quote
  #14  
Old January 3rd, 2006, 06:38 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,029 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Assignment 2.

Write a macro that calculates the rms value of 3 variables.
The rms value (root-mean-square) is calculated as follows: take the square power of each value, calculate the average of the squares, then take the square root of the average. Look here for more info.
The macro must replace only the RIGHT-HAND part of the calculation, so in the code, it should be used as:
Code:
rms_variable = RMS_3(val1, val2, val3);

where RMS_3 is the name of the macro.

Same story: PM me the result.

Reply With Quote
  #15  
Old January 3rd, 2006, 10:22 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 14
Send a message via AIM to Geo.Garnett
Im working on the homework right now, but I'll turn it in tonight cause I got work today Just to let ya know. I am doing it though.

Reply With Quote
  #16  
Old January 3rd, 2006, 07:00 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 13 m 30 sec
Reputation Power: 14
Send a message via Yahoo to BloodlustShaman
K I barely saw this post now first I am doing assingment 1 before I experiment with the assignment 2 also do I have to be in ur club?
Comments on this post
Geo.Garnett agrees: if you want to be cool like me you would, lol =)

Reply With Quote
  #17  
Old January 4th, 2006, 02:02 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,029 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Bloodlust, assignment 1 was about the use of defines, what you did was a macro, see this post for the difference (mostly, a define has no arguments).
I'll still look at your answer, and see if it works, but you might want to enter another solution as well. It's good practice :-)

As for the fan-club, of course you don't have to be a member. But you can... :-)

Reply With Quote
  #18  
Old February 14th, 2006, 10:57 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 13 m 30 sec
Reputation Power: 14
Send a message via Yahoo to BloodlustShaman
Quote:
Originally Posted by Itsacon
Bloodlust, assignment 1 was about the use of defines, what you did was a macro, see this post for the difference (mostly, a define has no arguments).
I'll still look at your answer, and see if it works, but you might want to enter another solution as well. It's good practice :-)

As for the fan-club, of course you don't have to be a member. But you can... :-)


Alright first is first sorry about bringing this topic up but this as not been finished and I just don't drop projects just cause I can't do them.

Also I haven't been active in the furoms cause of Finals and a lot of tests in my classes. But I am back.

So let me get this straight what you wanted us to do was set it to a integer.

So for example I could have used the define function to make it equal to 100 then. In the int main()
I place that instead of the 100

#define DEFINE_NUM 100
list[DEFINE_NUM]

like that? is that what u wanted us to do just define a it to a integer and place it and play with the function?

Is that what I needed to do?
Cause I was just wondering.
Also srry about bringing this topic up after it hasnt been touched.

Just trying to atleast get passing grades

P.S. wth ill join ur club.

Reply With Quote
  #19  
Old February 15th, 2006, 01:32 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,029 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Quote:
Originally Posted by BloodlustShaman
So let me get this straight what you wanted us to do was set it to a integer.

So for example I could have used the define function to make it equal to 100 then. In the int main()
I place that instead of the 100

#define DEFINE_NUM 100
list[DEFINE_NUM]


That was indeed the idea, simple, ey? But don't get cocky, it was only assignment 1

How did your finals go?


PS: Welcome! Have a beer in the Clubhouse

Reply With Quote
  #20  
Old February 15th, 2006, 08:18 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 13 m 30 sec
Reputation Power: 14
Send a message via Yahoo to BloodlustShaman
Quote:
Originally Posted by Itsacon
How did your finals go?

PS: Welcome! Have a beer in the Clubhouse


Alright. I did better than I thought, I thought I was going to flunk them all. But what was making my not be active were those test after cause all year we hardly had test. But after finals I had over 15 test. And for me that is A LOT!!

But now I am back.

Okay now I'll give assignment 2 a shot
dont expect that today nor tomorow cause I was in the internet for 3 HOURS trying to log in this place.[a long story].

P.S. Better be good beer

Reply With Quote
  #21  
Old February 15th, 2006, 10:06 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 13 m 30 sec
Reputation Power: 14
Send a message via Yahoo to BloodlustShaman
Okay so I am working on it and I got a problem but I will try to fix it myself first before posting here but I was wondering if a define can return 2 numbers.
I hope u understand what i ask.

such as
# define mm(a,b) (blah blah blah do some stuff)

int main()
{

blah blah blah
mm(x,y);

-it need to return 2 of those numbers changed

could that happen i just want a yes or a no
plz dont give examples(well just not yet )

Reply With Quote
  #22  
Old February 16th, 2006, 12:48 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,029 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 55 m 59 sec
Reputation Power: 16
Send a message via ICQ to Itsacon
Remember that a define doesn't return anything! It replaces stuff. It's like an advanced 'Search & Replace action, done before compilation of the program.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help debug a for loop


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap