urgent c++ code

Pages: 12
May 18, 2011 at 10:34am
write aprogram that input two members x and y and calculates x^y.
May 18, 2011 at 10:46am
another homework.
May 18, 2011 at 11:07am
Powers of integers? Think about how 4^5 is calculated. Perhaps it will give you insight into how to calculate powers in the general integer case.

If it is for non integer powers, then it is extremely complicated to write the algorithm yourself.
May 18, 2011 at 2:12pm
another homework.


Well, yeah, but this is urgent!!!

Ha! What's the old quote? "Lack of planning on your part does not constitute an emergency on my part."

@tagir: This is very easy to code. You must have something at this point. Post it here so we can see you're trying and help you complete it. We won't do your homework for you though.
May 18, 2011 at 3:04pm
This is how we end up with people applying to jobs and not knowing how to code even simple things like the "FizzBuzz" program.
May 18, 2011 at 4:43pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int x=0;
    int y=0;
    int z=1;
    
    cout<<"input x"<<endl;
    cin>>x;
    cout<<"input y"<<endl;
    cin>>y;
    
    
    for(int a=0; a<y; a++)
    {z=z*x;}
    cout<<z<<endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



should work
May 18, 2011 at 4:47pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main () {
	int base, pow;
	int product = 1;
    cout << "Enter your base: ";
	cin >> base;
	cout << "\nEnter your power: ";
	cin >> pow;
	for (int i =0; i < pow; i++) {
		product *= base;
	}
	cout << "\n"<< product;
    return 0;
}


This will work, but packetpirate is right you really need to try and figure this out yourself first.
May 18, 2011 at 4:47pm
@Kizlbot and hackthisred While I am sure you mean well in providing that code, it is ultimately more educational for everyone if we merely give suggestions instead. I hate to say it but posting solutions to people's homework will not make you any friends on here :/

Please don't take this personally - it's just a heads up :D

Regards
-Xander314
Last edited on May 18, 2011 at 4:48pm
May 18, 2011 at 4:54pm
Thanks Xander314, but I was a very visual or learn by example individual... I always found it easier to understand a concept by actually seeing a working example... NOT by people giving me riddles in a subject I don't have a total handle on... but thanks for the heads up
May 18, 2011 at 5:08pm
No problem :) To be honest I am probably one of the people who gives riddles for fear of spoon feeding...

As code goes, yours is pretty good. Some people actually give code samples which are riven with bad coding practise (you haven't :) ), which is understandably frowned upon.

What you said is of course correct - people learn in different ways and to be honest, had you just added one or two comments I doubt I'd have said anything at all.

Thanks for taking my prejudices in good spirit XD
Last edited on May 18, 2011 at 5:08pm
May 18, 2011 at 5:13pm
it's all good =)
and thanks for the complements
May 18, 2011 at 5:26pm
Doesn't the cmath library have a function for exponents? I believe its pow(x,y) or something like that.
May 18, 2011 at 5:29pm
Yes. However I assume OP is expected to write a power function him/herself. Otherwise this would be a very very easy assignment.
May 18, 2011 at 7:07pm
This "If you just SHOW me how to do it, I'll LEARN how to do it" idea is wishful thinking at best.

Can you read a bunch of novels and then sit down and write one?
How much time do you have to spend in museums before you can paint?
Will a years worth of MTV make you a Guitar Hero???

A good example that's EXPLAINED WELL will bring you up to speed on a general concept. But after that:
If you want to be a writer, write a lot.
If you want to be a painter, paint everything you see.
If you want to get to Carnegie Hall, practice, practice, practice.
And if you want to be a good programmer, code your brains out!!
Also don't forget that half of programming is critical thinking and the only way to develop that skill is to solve problems. Insight can't be taught, only learned. (Otherwise it would be called "outsight")
May 18, 2011 at 7:19pm
cnoeval wrote:
solve problems

That's what Project Euler is for XD

@OP If you want some problems to try, of increasing difficulty, check out
http://www.projecteuler.net/
May 18, 2011 at 7:56pm
Yeah, but that's more for math-based problems. For general programming problems, he should try SPOJ or Cprogramming.com's help-free challenges.
May 19, 2011 at 5:34am

@Xander314, generally people new to the forum give full solutions with code to motivate themselves but with time they learn that we are not here to do homework. :)
May 19, 2011 at 1:51pm
Newbies are also often very excited about what they have just learned and want to show off their new knowledge. You can't blame them for that, it's just human nature. I wish they would at least comment their solutions well and try to explain how and why they solved the problem the way they did. That way both sides would potentially learn something. I say potentially because I suspect most recipients of the free solution just put their name on it and hand it in (more beer drinking time, WooHoo!).
May 20, 2011 at 6:25pm
yes, include the library <math.h> which will call a function essentially a function similar to the one i wrote.
1
2
3
4
5
6
7
#include <iostream>
#include <math.h>
using namespace std;
main(){
   cout << pow(2, 3); // 2^3 = 8
return 0;
}


I thought he needed to actually figure out how to write, the code NOT call a library function... my bad
Last edited on May 20, 2011 at 6:25pm
May 20, 2011 at 6:31pm
@hackthisred, I think he does have to write it himself. He hasn't specified, but I imagine no homework would be that easy...
Pages: 12