Need help with pow10 and "Access Violation" error msg

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbaldridge
    New Member
    • Feb 2007
    • 7

    Need help with pow10 and "Access Violation" error msg

    I'm just starting my first C++ class and have a very basic question. We are to write a program that is going to be using the pow10(int m) function and we are not to use the pow10 function from the math library. I'm unfamiliar with the function so wrote a simple program to see it's output. I am receiving the following error however.
    " An Access Violation (Segment Fault) raised in your program."

    If anyone can explain what's causing this I would greatly appreciate it :)

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int pow10(int m);   //function prototype
    
    int main()
    {  
       int num, ans;
       
       cout<<"Enter an integer: ";                       
       cin>>num;                                          
       ans = pow10(num);                              
       cout<<"The 10th power of "<<num<<" is "<<ans<<endl; 
       system("PAUSE");
       return 0;                                          
    }
    
    int pow10(int m)                                  
    {  
       int result;
       result = pow10(m);                          
       return result;                                 
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you pow10 function calls itself recursivly
    Code:
    int pow10(int m)                                  
    {  
       int result;
       result = pow10(m);     << calls itself!!                     
       return result;                                 
    }
    so it will run out of memory and give a segmentation error or similar

    did you mean to test it with pow() from the maths library
    Code:
       result = pow(10, m);

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Originally posted by horace1
      you pow10 function calls itself recursivly
      Code:
      int pow10(int m)                                  
      {  
         int result;
         result = pow10(m);     << calls itself!!                     
         return result;                                 
      }
      so it will run out of memory and give a segmentation error or similar

      did you mean to test it with pow() from the maths library
      Code:
         result = pow(10, m);
      Haha, nice catch. I was sitting in front of my compiler changing ints to longs and messing with variables, I didn't even see that ... :( I need more coffee...

      Comment

      • pbaldridge
        New Member
        • Feb 2007
        • 7

        #4
        The instructions for our program specifically say to not use the function from the math library so I was attempting that.. not very well obviously :) Here is the actual instruction for that portion..

        "The function pow10 computes and returns the integer 10 raised to the m_th power, e.g. pow10(3) computes and returns 1000. Compute this using a for loop. Do not use the pow function in the math library.
        *************** *************** *************** *************** *************** *************** */
        int pow10(int m)"

        Hope that helps.. I've looked everywhere and can't seem to get this one..

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by pbaldridge
          The instructions for our program specifically say to not use the function from the math library so I was attempting that.. not very well obviously :) Here is the actual instruction for that portion..

          "The function pow10 computes and returns the integer 10 raised to the m_th power, e.g. pow10(3) computes and returns 1000. Compute this using a for loop. Do not use the pow function in the math library.
          *************** *************** *************** *************** *************** *************** */
          int pow10(int m)"

          Hope that helps.. I've looked everywhere and can't seem to get this one..
          have you looked into using a shift? If you shift bits one to the left, you are raising it to the second power...

          Comment

          • pbaldridge
            New Member
            • Feb 2007
            • 7

            #6
            The program itself is actually fairly simple and could be done many easier ways, but the professor is having us use specific functions in order to get acquainted with them.. hence he wants us to use the pow10(int m) function.. I just can't figure how to use it in a for loop but not use the function from the math lib and not call the function from within itself. Sorry I'm very new to this so I'm sure my explanation is more confusing than the problem itself :)

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by pbaldridge
              The program itself is actually fairly simple and could be done many easier ways, but the professor is having us use specific functions in order to get acquainted with them.. hence he wants us to use the pow10(int m) function.. I just can't figure how to use it in a for loop but not use the function from the math lib and not call the function from within itself. Sorry I'm very new to this so I'm sure my explanation is more confusing than the problem itself :)
              Are there any constraints in the pow10() function?

              If you're to use a while loop to control the pow10() function, i believe that your teacher expects the implementation to contain either:

              Code:
              int pow10(int x) {
                  return x*x;
              }
              
              or 
              
              int pow10(int x) {
                  return /*and i'm blanking on how to do shift left, something using the <<, but it does the same thing as x*x */;
              }
              and then your main contains a while loop that will go while counter < 10.

              Or am I missing one of the constraints?

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Ok, and after I finally bothered to read up on what I was talking about...

                pow10 returns a double. You're just using the wrong type (and you dont' need to define the function yourself).

                Edit:: but you do need to include <math.h>
                Last edited by sicarie; Feb 13 '07, 03:35 PM. Reason: adding library

                Comment

                • pbaldridge
                  New Member
                  • Feb 2007
                  • 7

                  #9
                  That's what I've been thinking.. but he specifically said to use it as an integer..
                  Code:
                  int pow10(int m)
                  and not to include the math.h library.. I can get it to work fine using the library..

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Originally posted by pbaldridge
                    That's what I've been thinking.. but he specifically said to use it as an integer..
                    Code:
                    int pow10(int m)
                    and not to include the math.h library.. I can get it to work fine using the library..
                    Ok, so you need to use a for loop to run a function ten times. That function, after ten times, will return the number to the tenth power. So if you are calling the function ten times, it does the same thing ten times. What is that?

                    Comment

                    • pbaldridge
                      New Member
                      • Feb 2007
                      • 7

                      #11
                      Actually I don't need to run it 10 times.. I simply need the function to return a random number within a certain range.. the pow10 function is supposed to determine that range. Basically we're creating a set of multiplaction problems for the user to solve.. The use will input the max number of digits for the integers at the beginning.. then using a random function and the pow10 function we create a problem for the user to solve.. i.e:
                      [Sample Output]
                      Enter max digits: 3

                      1) 402 x 223 = 89646 //user inputs solution

                      Correct!
                      [/Sample Output]

                      From what I gather the pow10 function is simply going to use the max digits to establish the range used in the random function.

                      I hope that made sense?

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        Originally posted by pbaldridge
                        Actually I don't need to run it 10 times.. I simply need the function to return a random number within a certain range.. the pow10 function is supposed to determine that range. Basically we're creating a set of multiplaction problems for the user to solve.. The use will input the max number of digits for the integers at the beginning.. then using a random function and the pow10 function we create a problem for the user to solve.. i.e:
                        [Sample Output]
                        Enter max digits: 3

                        1) 402 x 223 = 89646 //user inputs solution

                        Correct!
                        [/Sample Output]

                        From what I gather the pow10 function is simply going to use the max digits to establish the range used in the random function.

                        I hope that made sense?
                        Ok, now I'm definitely confused. You teacher wants you to become familiar with the functions in the math.h library, but implement them differently? The pow10() function is named because it returns the number you passed to it, to the power of 10. You can do that without running it 10 times, but it seemed (from the request for the for loop, which can be inside or outside the function. You can either use a for loop inside to multiply a number by itself 10 times, or call the funciton 10 times.)

                        Unless pow10 is not supposed to return the number to the power of 10?

                        Comment

                        • Motoma
                          Recognized Expert Specialist
                          • Jan 2007
                          • 3236

                          #13
                          Behold the power of recursion.

                          Code:
                          int pow10(int m)
                          {
                            if(m == 0) return 1;
                            if(m == 1) return 10;
                            return pow10(m-1);
                          }
                          But being that your instructions said a for loop:

                          Code:
                          int pow10(int m)
                          {
                            int i, n = 1;
                            for( i = 0; i < m; i++) n *= 10;
                            return n;
                          }

                          Comment

                          • pbaldridge
                            New Member
                            • Feb 2007
                            • 7

                            #14
                            Motoma you are a genius!! Thank you ever so much.. worked perfectly :)
                            Thanks for all the help Sicarie.. sorry for being so confusing.. I'll get better I promise :D

                            Comment

                            • sicarie
                              Recognized Expert Specialist
                              • Nov 2006
                              • 4677

                              #15
                              Originally posted by pbaldridge
                              Motoma you are a genius!! Thank you ever so much.. worked perfectly :)
                              Thanks for all the help Sicarie.. sorry for being so confusing.. I'll get better I promise :D
                              I'm sure I wasn't reading clearly as well - please post anything else you get stuck on - we'll get better together!

                              Comment

                              Working...