For loop and exponents

This is a discussion on For loop and exponents within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm caught in a somewhat difficult question in my C++ course at college. The question pertains to for loops ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Cool For loop and exponents

    Hello,

    I'm caught in a somewhat difficult question in my C++ course at college. The question pertains to for loops and the calculation of exponents. The question is as follows:

    Start with integer objects n and product. Initialize product = 1 and use the loops to output the powers (n^1, n^2... n^6) for exponents 1 through 6
    This specific question deals with the for loop. I'm not sure how to go about performing this calculation. First, I could increment the variable product while product is <= to 6 to get all six exponents recognized. But then, where would the answer for each respective operation be stored. I'm not sure how to calculate exponents either without using a massive set of if-else statements, which may or may not be permitted on this question (I don't believe its permitted)

    Thank you for your help,
    Travis

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It is your assignment so I wouldn't want to do it for you but here is some code that you can examine:

    Code:
    int main(void) {
        int i, array[6];
    
        for(i = 0; i < 6; i++) 
            array[i] = 1<<i;
    
        return 0;
    }
    Before you wet yourself thinking I just did it for you I remind you that this only does what you want for powers of 2. If you are permitted to do so I would make a recursive function. If not I would just put a nested loop inside the main for that would do the calculations. This is one of the first assignments I've heard of that has real world utility.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Thank you for your help, but however, we haven't gotten into arrays yet (thats next chapter) so arrays are thrown out lol

    anyway to do it without an array?

    we have just stepped foot in for loops (this is an introduction to C++ course) and we havent reached recursive functions yet (that is also next chapter)

    thank you,
    Travis
    Last edited by TrazPFloyd; 10-15-2002 at 02:51 AM.

  4. #4
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Try to display the following using for loops, and you might start to get an idea of how they work, and how to use them:

    *
    **
    ***
    ****
    *****

    then do the reverse:

    *****
    ****
    ***
    **
    *

    See if you can get each of them to display using 'for' loops. That should be a start in the right direction
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Thank you very much, I figured out how to do the triangle *'s. I'm starting to formulate ideas on how to go about this. A nested for loop makes sense but I'm not quite sure about the implementation... should I use another object as a control object in the nested for loop (besides n?)

    Thanks for your help guys, it's really appreciated

    Travis

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Talking

    Okay guys, check this code out and see if you believe it's okay for the problem required. I believe I got it:

    Code:
    #include <iostream.h>
    
    void main()
    {
    	long n, product, exponentSum;
    	cout << "Define n--";
    	cin >> n;
    	exponentSum = n;
    	cout << n << endl;
    	for (product=1; product < 6; product++)
    	{
    		exponentSum *= n;
    		cout << exponentSum << endl;
    
    	}
    }
    Thank you guys again for your help... let me know if you believe this is thumbs up or down

    Travis

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exponents are LAAAME
    By Sm33zy in forum C Programming
    Replies: 8
    Last Post: 11-21-2008, 10:10 PM
  2. Fraction Exponents
    By ninjaturtle[k9] in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2004, 11:46 AM
  3. non-interger exponents
    By brane_sail in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2003, 01:41 AM
  4. God, i feel like a n00b again. How do i do exponents?
    By Inquirer in forum C++ Programming
    Replies: 13
    Last Post: 09-01-2003, 09:41 PM
  5. how to use exponents
    By guitargatler in forum C++ Programming
    Replies: 11
    Last Post: 02-01-2003, 09:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21