this seems challenging
Write a function that accepts two input parameters and computes the result of raising the value of the ﬁrst input to the value of the second input, and returns the result. You are not allowed to use the multiplication operator(*)for performing multiplication, nor are you allowed to use any built-in C++ functionality that directly produces the exponentiation result.
Data Types: function works correctly for both inputs being positives, zeroes, or ones, and when the second input is negative. is there a if else if somewhere?
#include<iostream>
usingnamespace std;
int exponent(int, int);
int main()
{
int num1, //to store first number
num2, //to store second number
value = 0;
//read numbers
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
//call function
exponent(num1, num2);
//print value
cout << "The value of " << num1 << " and " << num2 << " is: " << value << endl << endl;
system("pause");
return 0;
}
//function definition
int exponent(int a, int b)
{
int result = 0;
for (int i = 0; i < x; i++)
{
result += y;
}
return result;
}
}
// Example program
#include <iostream>
/// Calculates x * y
/// (e.g. add the number "y" x times)
/// x >= 0
/// y can be negative.
int mult(int x, int y)
{
int result = 0;
for (int i = 0; i < x; i++)
{
result += y;
}
return result;
}
int main()
{
using std::cout;
cout << mult(3, 2) << '\n';
cout << mult(2, 3) << '\n';
cout << mult(4, -3) << '\n';
cout << mult(2, 0) << '\n';
cout << mult(0, 2) << '\n';
}
If the power can be a negative, integer, note that x^(-y) == 1.0/(x^y) (where ^ is exponentiation).
This will require the output to be a double, not an integer.
hes gotten a lot of help on the multiplier already. Lets see the OP's try on this one before we do another one... Its virtually identical to add and multiply anyway, as I said in his other thread.
Give it a try... you can do it...
nor are you allowed to use any built-in C++ functionality that directly produces the exponentiation result.
There are ways to get the exponentiation indirectly. You might want to look up how to do this with a slide rule. The procedure translates nicely into code.
I think some of you ought to remember this is the beginners forum and answer accordingly If you cant, go to other forums where your answers would be understood
I think some of you ought to remember this is the beginners forum and answer accordingly If you cant, go to other forums where your answers would be understood
I think you ought to remember that you are the beginner, and ought to consider that it is your understanding that needs adjustment.
You have been asking questions about basic, grade school mathematics, and you have been getting very kind and simple answers about it.
Perhaps if you were demonstrating some real effort to comprehend basic principles you would be less frustrated with the fact that everyone has been very generous with you, even going so far as to explicitly give you things you can Google around. Up to now, no one has said anything even remotely unkind to you.
So before you get all snotty and demanding, get your own butt in gear and try to learn something. Because most people won’t continue trying to help an arrogant, ungrateful berk.
@Alb13G,
I think @Ganado's answer is spot on in this instance, both in terms of what could be done, the route to get there, and the appropriate level of advice. Please don't suggest that the people who have answered your threads should "go to other forums".
Even if you were referring to the two other posts after mine (I don't see anything wrong with them), your post was still rude and demeaning. Doesn't really bother me, just be aware in the future that replies like your one before this won't help you get help.
Alb13G, if you're referring to my post, I didn't mean to sound insulting. There's an easy way to do this with logarithms. It's the basis of the slide rule and it revolutionized mathematical computation for centuries. I'm trying to get you to figure it out or look it up on your own, rather than just have me tell you
And, he has a point buried in there. Schools don't really teach anymore, so what I consider basic math and beginner stuff (like the logs, binary, and pointer stuff) may be considered advanced these days. I had pointers / lists/trees in a year long highschool programming class and my school's math had covered logs and binary in the 9th or 10th grade (though not well enough, I didn't find all the really cool stuff you can do with logs until much later). I still consider all that stuff to be first year programmer material and beginner friendly, but maybe that is not fair.
well, in that case, you are sort of an 'extra beginner' (no offense) because you are lacking a LOT of 'basic' math background as well as the coding side. The majority of coders start in college with calculus or near that level math background. This is GREAT; the folks that started earlier have a huge advantage later one, and a hard time early on.
Spend a couple of days with a book on logs. The number of things you can do in 3-4 lines of code with them is just astonishing. Most of it, you don't need to do any more, but about 25-30% of the tricks are quite efficient and useful in code.
As for the problem at hand, buried in this thread are 3 or 4 ways to solve the problem. See if you can do it yourself. Brute force works... base = 1; for(i = 0; i < exponent; i++) result*=base; ... use your multiply instead of *= as directed and you have the core program, just need to deal with negative exponent and whatever else
13, hmm? That’s 7^{th} grade, unless the Kindergarten cut-off date was missed or held back a year. In the USA, by the end of 6^{th} grade kids should be explicitly familiar with basic algebraic concepts like: how addition and multiplication are related, and how multiplication and exponential powers are related. Though, frankly, addition and multiplication concepts should be thoroughly understood by the end of 4^{th} grade.
I guest taught a basic programming unit in Camden (NJ) to 6^{th} graders, so I know it isn’t unheard of to take programming courses in elementary school. But it is unusual.
Of course, I don’t normally continue to respond to a topic when the OP has made my ignore list.
I have basic math skills and I'm taking algebra. they don't offer programming so I bought tony Gaddis's C++ book 8th ed for 3 dollars at the public library book sale and started learning on my own I found these challenging programs online and gave them a shot but to convert my math level to a programming language I just started was beyond me this one could simply be solved if I could use pow or *. I also taught myself how to fix and trouble shoot computers.im also learning networking all online and a free downloaded pdf CompTIA book what is OP don't bother with the ignore I wont be asking anything on this site again
algebra 1 is still basic skill set. But you are getting there. It really gets exciting after the set of calc1/2, advanced geometry, linear algebra 1, discrete math, and numerical methods 1. So many algorithms and computer useful stuff starts to come into play in those. Statistics is full of good stuff too.