Thread: function with loop within a loop

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    8

    function with loop within a loop

    I'm trying to get this program to print the sum of the table. I think I need a double loop, but I'm not sure how to go about this.

    Code:
    #include<stdio.h>#include<math.h>
    
    
    int power_table(int startingint, int endingint, int exponent, int sumvalue); //fucntion prototype
    
    
    int main()
    { 
        char line[10];
        int startingint = 1;
        int endingint = 1;
        int exponent = 1;
        int x = 1;
        int sumvalue = 1;
    
    
        printf("Enter the starting integer value:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &startingint);
    
    
        printf("Enter the ending integer value:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &endingint);
        
        printf("Enter the exponent:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &exponent);
        
        power_table(startingint, endingint, exponent, sumvalue);
    }
    
    
    int power_table(int startingint, int endingint, int exponent, int sumvalue) //function that prints the table of exponent values
    {
        int x = 1;
        int sumvalue = 1;
        printf("x   x^n     sum\n");
        printf("---------------\n");
        
        for(x=startingint; x<=endingint; x++)    //for loop that computes the table of exponent values
        {
    
        startingint = x;
        exponent = pow(startingint, x);
        sumvalue = exponent + exponent;
        printf("%d   %d   %d\n", startingint, exponent, sumvalue);
    
        }
    }
    Last edited by rabert1; 04-16-2012 at 08:05 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If I understand your purpose then you don't need a double loop. However, your attempt is very, very broken. Look at it more carefully and try again.

    To clarify your goal, what should be printed with the following invocation:

    power_table(1, 5, 2, 0);

    (I believe that the last parameter is not actually needed.)
    Last edited by oogabooga; 04-16-2012 at 08:38 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    Thanks for responding. To clarify my goal, I want to add the sum of exponent values. So If the user enters
    1 for starting value
    3 for ending value
    and 2 for exponent, the table would look like:

    value____value squared
    1___________1
    2___________4
    3___________9
    ----------------
    ___________14 = sum of squared values

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    pseudo Code:
    Code:
    sum=0
    for(startingvalue<=ending value){
    value_squared=sqrt(startingvalue)
    print(startingvalue __ value_squared)
    sum+=value_squared
    startingvalue++
    }
    print(sum)
    Last edited by camel-man; 04-16-2012 at 09:12 PM.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    Thanks for responding. I have two questions.

    1) s
    um+=value_squared //how does this += work
    2) Why am I short 1 value when summing the exponents when I run the code?

    Code:
    #include<stdio.h>#include<math.h>
    
    
    int power_table(int startingint, int endingint, int exponent, int sumvalue); //fucntion prototype
    
    
    int main()
    { 
        char line[10];
        int startingint = 1;
        int endingint = 1;
        int exponent = 1;
        int x = 1;
        int sumvalue = 1;
    
    
        printf("Enter the starting integer value:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &startingint);
    
    
        printf("Enter the ending integer value:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &endingint);
        
        printf("Enter the exponent:\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &exponent);
        
        power_table(startingint, endingint, exponent, sumvalue);
    }
    
    
    int power_table(int startingint, int endingint, int exponent, int sumvalue)
    {
        int x = 1;
        int sumvalue = 1;
        printf("x   x^n     sum\n");
        printf("---------------\n");
        
        for(x=startingint; x<=endingint; x++)
        {
            
        startingint = x;
        exponent = pow(startingint, x);
        printf("%d   %d\n", startingint, exponent,);
        sumvalue+=exponent;
            
        }
        printf("The sum of the exponent values = %d\n", sumvalue);
    }



  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Line 37 should give you an error because you are re declaring sumvalue.. Aside from that you should also set sumvalue = 0...
    sumvalue+=exponent works just like this sumvalue= sumvalue+exponent the onlly difference is it is shorter to write

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Sorry for the double post but you also want to change this
    Code:
    startingint = x;
        exponent = pow(startingint, x);
        printf("%d   %d\n", startingint, exponent,);
        sumvalue+=exponent;
    exponent should go into the pow function like this aVariable=(int) pow(startingint, exponent);

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Start by turning up your warning levels and fixing the problems it tells you about.
    Then post the code and we'll tell you about the remaining problems.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. function loop help
    By akahizzle in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2011, 11:15 AM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. How to loop a function
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-14-2002, 12:52 PM