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 :) -
#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;
-
}
-
23 3333
you pow10 function calls itself recursivly -
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
you pow10 function calls itself recursivly -
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
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...
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..
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...
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 :)
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: -
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?
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>
That's what I've been thinking.. but he specifically said to use it as an integer..
and not to include the math.h library.. I can get it to work fine using the library..
That's what I've been thinking.. but he specifically said to use it as an integer..
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?
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?
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?
Behold the power of recursion. -
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: -
int pow10(int m)
-
{
-
int i, n = 1;
-
for( i = 0; i < m; i++) n *= 10;
-
return n;
-
}
-
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
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!
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!
Behold the power of recursion. -
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: -
int pow10(int m)
-
{
-
int i, n = 1;
-
for( i = 0; i < m; i++) n *= 10;
-
return n;
-
}
-
AHHHHHH *the angelic chorus sings!* My eyes, ouch, the power is so bright! Where are my sunglasses?
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.
AHHHHHH *the angelic chorus sings!* My eyes, ouch, the power is so bright! Where are my sunglasses?
Heh, yeah...Recursion is so tasty!
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: -
int pow10(int m)
-
{
-
if(m == 0) return 1;
-
if(m == 1) return 10;
-
return pow10(m-1); // Should be something else here...
-
}
-
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...
Ouch, busted Motoma. Shame has befallen you. Your shame index moves from green to yellow! "Elevated threat of shame"
CRIKEY I'M A BUM! -
int pow10(int m)
-
{
-
if(m == 0) return 1;
-
if(m == 1) return 10;
-
return 10 * pow10(m-1); // GANON JUST PWNED MY N00B ARSE
-
}
-
CRIKEY I'M A BUM! -
int pow10(int m)
-
{
-
if(m == 0) return 1;
-
if(m == 1) return 10;
-
return 10 * pow10(m-1); // GANON JUST PWNED MY N00B ARSE
-
}
-
XD
As they used to say,
lol served.
We all make mistakes, and today was your (un)lucky day!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
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 *...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |