473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 :)

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int pow10(int m);   //function prototype
  6.  
  7. int main()
  8. {  
  9.    int num, ans;
  10.  
  11.    cout<<"Enter an integer: ";                       
  12.    cin>>num;                                          
  13.    ans = pow10(num);                              
  14.    cout<<"The 10th power of "<<num<<" is "<<ans<<endl; 
  15.    system("PAUSE");
  16.    return 0;                                          
  17. }
  18.  
  19. int pow10(int m)                                  
  20. {  
  21.    int result;
  22.    result = pow10(m);                          
  23.    return result;                                 
  24. }
  25.  
Feb 13 '07 #1
23 3333
horace1
1,510 Expert 1GB
you pow10 function calls itself recursivly
Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)                                  
  2. {  
  3.    int result;
  4.    result = pow10(m);     << calls itself!!                     
  5.    return result;                                 
  6. }
  7.  
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
Expand|Select|Wrap|Line Numbers
  1.    result = pow(10, m);  
  2.  
Feb 13 '07 #2
sicarie
4,677 Expert Mod 4TB
you pow10 function calls itself recursivly
Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)                                  
  2. {  
  3.    int result;
  4.    result = pow10(m);     << calls itself!!                     
  5.    return result;                                 
  6. }
  7.  
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
Expand|Select|Wrap|Line Numbers
  1.    result = pow(10, m);  
  2.  
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...
Feb 13 '07 #3
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..
Feb 13 '07 #4
sicarie
4,677 Expert Mod 4TB
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...
Feb 13 '07 #5
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 :)
Feb 13 '07 #6
sicarie
4,677 Expert Mod 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. int pow10(int x) {
  2.     return x*x;
  3. }
  4.  
  5. or 
  6.  
  7. int pow10(int x) {
  8.     return /*and i'm blanking on how to do shift left, something using the <<, but it does the same thing as x*x */;
  9. }
  10.  
and then your main contains a while loop that will go while counter < 10.

Or am I missing one of the constraints?
Feb 13 '07 #7
sicarie
4,677 Expert Mod 4TB
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>
Feb 13 '07 #8
That's what I've been thinking.. but he specifically said to use it as an integer..
Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)
  2.  
and not to include the math.h library.. I can get it to work fine using the library..
Feb 13 '07 #9
sicarie
4,677 Expert Mod 4TB
That's what I've been thinking.. but he specifically said to use it as an integer..
Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)
  2.  
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?
Feb 13 '07 #10
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?
Feb 13 '07 #11
sicarie
4,677 Expert Mod 4TB
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?
Feb 13 '07 #12
Motoma
3,237 Expert 2GB
Behold the power of recursion.

Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)
  2. {
  3.   if(m == 0) return 1;
  4.   if(m == 1) return 10;
  5.   return pow10(m-1);
  6. }
  7.  
But being that your instructions said a for loop:

Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)
  2. {
  3.   int i, n = 1;
  4.   for( i = 0; i < m; i++) n *= 10;
  5.   return n;
  6. }
  7.  
Feb 13 '07 #13
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
Feb 13 '07 #14
sicarie
4,677 Expert Mod 4TB
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!
Feb 13 '07 #15
Motoma
3,237 Expert 2GB
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
Glad to help, and welcome to TheScripts!
Feb 13 '07 #16
RedSon
5,000 Expert 4TB
Behold the power of recursion.

Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)
  2. {
  3.   if(m == 0) return 1;
  4.   if(m == 1) return 10;
  5.   return pow10(m-1);
  6. }
  7.  
But being that your instructions said a for loop:

Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)
  2. {
  3.   int i, n = 1;
  4.   for( i = 0; i < m; i++) n *= 10;
  5.   return n;
  6. }
  7.  
AHHHHHH *the angelic chorus sings!* My eyes, ouch, the power is so bright! Where are my sunglasses?
Feb 13 '07 #17
sicarie
4,677 Expert Mod 4TB
AHHHHHH *the angelic chorus sings!* My eyes, ouch, the power is so bright! Where are my sunglasses?
Haha, yeah, that is a very elegant solution, Motoma.
Feb 13 '07 #18
Motoma
3,237 Expert 2GB
AHHHHHH *the angelic chorus sings!* My eyes, ouch, the power is so bright! Where are my sunglasses?
Heh, yeah...Recursion is so tasty!
Feb 13 '07 #19
Ganon11
3,652 Expert 2GB
I'm not sure if the recursive function is better than the for... function - it seems like it would take a lot more time to call a function 3 times than to multiply a number 3 times. Regardless, both work.

EDIT:

Nevermind. The recursive function, as is, will return 10 if m >= 1, or 1 if m == 1. Look carefully:

Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)
  2. {
  3.   if(m == 0) return 1;
  4.   if(m == 1) return 10;
  5.   return pow10(m-1); // Should be something else here...
  6. }
  7.  
If m >= 1, then the function returns its own value for m - 1 - but doesn't change this value at all. This situation will end in m == 1 and pow10(1) == 10. There has to be one more thing added...
Feb 13 '07 #20
RedSon
5,000 Expert 4TB
Ouch, busted Motoma. Shame has befallen you. Your shame index moves from green to yellow! "Elevated threat of shame"
Feb 13 '07 #21
Motoma
3,237 Expert 2GB
CRIKEY I'M A BUM!

Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)
  2. {
  3.   if(m == 0) return 1;
  4.   if(m == 1) return 10;
  5.   return 10 * pow10(m-1); // GANON JUST PWNED MY N00B ARSE
  6. }
  7.  
Feb 13 '07 #22
Ganon11
3,652 Expert 2GB
CRIKEY I'M A BUM!

Expand|Select|Wrap|Line Numbers
  1. int pow10(int m)
  2. {
  3.   if(m == 0) return 1;
  4.   if(m == 1) return 10;
  5.   return 10 * pow10(m-1); // GANON JUST PWNED MY N00B ARSE
  6. }
  7.  
XD

As they used to say,

lol served.

We all make mistakes, and today was your (un)lucky day!
Feb 13 '07 #23
Motoma
3,237 Expert 2GB
I'm not sure if the recursive function is better than the for... function - it seems like it would take a lot more time to call a function 3 times than to multiply a number 3 times.
You are, of course, entirely right; the recursive solution would not be an efficient solution to this problem.

However, the fact that a recursive solution was the first idea that popped into my head would bring tears of joy to one of my old professors eyes.
Feb 13 '07 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Oliver | last post by:
I have a database developed fully in Access 2000 which I make available to users of both Access 97 and Access 2000. I use a split front end/back end arrangement; with the BE in Access 97. When I...
4
by: Jerry | last post by:
Hi, I have an app which retrieves data from a sql server table and displays it on a datagrid. If 2 sessions of this app are running and 2 users try to update the same record at about the same...
2
by: Agnes | last post by:
I got a simple form and using databinding manager to do the add new Now , my big trobule is . I can update the 'addnew' record, However, after I new the record, and then amend it , it got...
7
by: Tom | last post by:
Hello all: I have a method that does a POST to a secured website using HttpWebRequest. It worked when logging in the site, but it failed with an HTTP prococol violation error when it is used to...
1
by: Khadim | last post by:
I m using HTTWebResponse which is running smoothly on my system which is behing a proxy server. When I run the application with Live IP it gives "HTTP Protocol violation error" I can't use...
5
by: Bible Bob | last post by:
I can not get Access to load. It starts and then pops up an error message that says "A problem occured while Microsoft Office access was communicating with the OLE server or ACtiveX Control. ...
3
by: Scott McDermott | last post by:
I have an application that is making an HTTP request with HttpWebRequest.GetRequest. Unless I set 'httpWebRequest useUnsafeHeaderParsing="true"' in the web.config, I get a 'The server committed a...
4
by: tuxman | last post by:
Hi all. First of all let me apologize by my english. I've googled a lot about my problem, but I had not find anything conclusive. I have the following piece of code: vector < map < int , char *...
2
by: jthep | last post by:
I'm trying to get this piece of code I converted from C to work in C++ but I'm getting an access violation error. Problem occurs at line 61. Someone can help me with this? The function...
4
by: evenlater | last post by:
Anybody know how to prevent the annoying Access 2007 "Getting Started with Access" page from showing up when the database is closed using DoCmd.Quit? My database is used in a terminal server...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.