Code this 1 2^2 + 3^3 4^4 + 5^5 -
Hi guys and girls
I was wondering if you guys would be able to help me with a question, hints, code...whatever, it would be greatly appreciated. It's due on Monday so please if you can help me please reply ASAP or alternatively just email me at dx_k9s@yahoo.com (i don't usually do this, but I'm desperate to get it done).
I like to thank you all in advance.
Here's the question...
A series of numbers are as follows.
1 2^2 + 3^3 4^4 + 5^5 -
Your task is to write a C++ program to compute and display the above series and the result (up to n terms, where n is obtained from the keyboard). The value of n must be less than 6. If n is greater than 6 or less than 0, then you need to output Invalid and terminate the program. You need to use a long int variable for this implementation.
A sample run of this program is as follows.
Enter n = 2
1 2^2 = -3
In the above example, the number 2 is entered from the keyboard to produce the series as 1 2^2.
thanks again
# 1 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Your task is to write a C++ program...
No, it is your task. We do not solve other peoples homework. Show us your work so far or ask specific questions, and we'll help, but nobody here will write your homework.
# 2 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Do the validation yourself.
int sign = 1;
int sum = 0;
for(i=1; i<=n; i++)
{
sum += sign * (int)pow(i, i);
sign *= -1;
}
cilu at 2007-11-9 0:43:28 >

# 3 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
but nobody here will write your homework.
Did I do something wrong? (Usually I don't do this...) Well, you are free to remove it if you want.
cilu at 2007-11-9 0:44:38 >

# 4 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Thanks so much... I love u guys.... ; )
# 5 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Hey guys is this right? But when i compiled it, it gave me an error message..."Line 22; parse error at end of input" i don't see anything wrong!
#include <iostream>
using namespace std;
int main()
{
int sign = 1;
int sum = 0;
int n;
cout << "Enter a value: ";
cin >> n;
for(double i=1; i<=n; i++)
{
sum += sign * (int)pow(i, i);
sign *= -1;
cout << sum; //is this right? or should it be sign?
return 0;
}
# 6 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Hey guys is this right? But when i compiled it, it gave me an error message..."Line 22; parse error at end of input" i don't see anything wrong!
#include <iostream>
using namespace std;
int main()
{
int sign = 1;
int sum = 0;
int n;
cout << "Enter a value: ";
cin >> n;
for(double i=1; i<=n; i++)
{
sum += sign * (int)pow(i, i);
sign *= -1;
cout << sum; //is this right? or should it be sign?
return 0;
}
Please use code tags when posting code!
You're missing a '}' to close the scope of your for-loop:
int main()
{
int sign = 1;
int sum = 0;
int n;
cout << "Enter a value: ";
cin >> n;
for(double i=1; i<=n; i++)
{
sum += sign * (int)pow(i, i);
sign *= -1;
}
cout << sum; //is this right? or should it be sign?
return 0;
}
# 7 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Thanks Matt
By the way i'm new to this forum so i still havn't discovered where the quote things is...sorry : (
# 8 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Please, use int, not double:
for(int i=1; i<=n; i++)
{
sum += sign * (int)pow(i, i);
sign *= -1;
}
pow is overloaded to take int arguments.
cilu at 2007-11-9 0:49:39 >

# 9 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
But Matt when i use int it gives me an error
"Line 16 : call of overloaded 'pow(int&, int&)' is ambiguous" but when i use double it's fine...
But i've just read the question again it told me to use long int.. I'm kinda caught out in the middle.
# 10 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Do you use CRT's pow, from math.h or STL's pow from from <complex>? The later takes only complex, so I guess I answered the question myself. However the first one has these overloads:
double pow(
double x,
double y
);
double pow(
double x,
int y
); // C++ only
double pow(
int x,
int y
); // C++ only
float pow(
float x,
float y
); // C++ only
float pow(
float x,
int y
); // C++ only
long double pow(
long double x,
long double y
); // C++ only
long double pow(
long double x,
int y
); // C++ only
float powf(
float x,
float y
);
You can cast it to double:
for(int i=1; i<=n; i++)
{
sum += sign * (int)pow((double)i, (double)i);
sign *= -1;
}
"Line 16 : call of overloaded 'pow(int&, int&)' is ambiguous" but when i use double it's fine...
What do you mean pow(&int, &int)?! What compiler are you using?
cilu at 2007-11-9 0:51:38 >

# 11 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
I'm using Quincy 2002 as the program and mingw as a compiler i think..
So can i do the same thing for long int?
# 12 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
I'm using Quincy 2002 as the program and mingw as a compiler i think..
If you don't specify what compiler you are using we assume it's a MS compiler, so please be more explicit from the very beginning next time.
So can i do the same thing for long int?
What same thing?
cilu at 2007-11-9 0:53:43 >

# 13 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Quote
for(int i=1; i<=n; i++)
{
sum += sign * (int)pow((double)i, (double)i);
sign *= -1;
}
Quote
Code
for(int i=1; i<=n; i++)
{
sum += sign * (int)pow((long int)i, (long int)i);
sign *= -1;
}
Code
# 14 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
To start a quote you must put QUOTE between []. To end a quote you must put /QUOTE between []. Same for CODE. Of you the buttons on the toolbar.
for(int i=1; i<=n; i++)
{
sum += sign * (int)pow((long int)i, (long int)i);
sign *= -1;
}
Yes, if you have an overload pow that takes long int params.
cilu at 2007-11-9 0:55:47 >

# 15 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
oh oo... it seems like long int doesn't work either... bugger. Stupid compiler
# 16 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Use the following function which gives n^n
long int power(int n)
{
long int ret=n;
for (int i=1; i<n; i++) {
ret *= n;
}
return ret;
}
T.G. at 2007-11-9 0:57:42 >

# 17 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Thanks T.G. and one more thing it asks me to "display the above series" as an output, do you think there is a looping way to do it or do i have to hardcode it?
# 18 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Thanks T.G. and one more thing it asks me to "display the above series" as an output, do you think there is a looping way to do it or do i have to hardcode it?
Don't hardcode. (How would you hardcode it anyway? :confused: When n is variable?) Of course you should display it in a loop.
cilu at 2007-11-9 0:59:49 >

# 19 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
I think it is possible to combine this printing into the loop that computes.
T.G. at 2007-11-9 1:00:50 >

# 20 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Hmmmmm.........
# 21 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
What about this solution?
template <int I, int J>
struct p
{
static const int ret = I* p<I, J-1>::ret;
};
template <int I>
struct p<I, 0>
{
static const int ret = 1;
};
template<int I>
struct q
{
static const int ret = q<I-1>::ret + ((I%2)?1:-1)*p<I, I>::ret;
};
template <>
struct q<1>
{
static const int ret = 1;
};
int main()
{
int j = q<6>::ret;
return 0;
}
:cool:
# 22 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Well Gabriel, for someone who writes his/her first program, like our OP is, I think your code can win the award for the most obfuscated code. I think you should take it slowly with the templates here, or will scare him enough to give up C++ and switch to VB. ;)
cilu at 2007-11-9 1:03:51 >

# 23 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Somebody's been reading Alexandrescu, haven't they? :rolleyes:
# 24 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Well Gabriel, for someone who writes his/her first program, like our OP is, I think your code can win the award for the most obfuscated code. I think you should take it slowly with the templates here, or will scare him enough to give up C++ and switch to VB. ;)In comp.lang.c++, this was the usual trick of "doing someone's homework", and that is to give a solution only an experienced C++ programmer would come up with. This way, if the student were to hand in the program, they will get questioned by the teacher, causing all sorts of problems for the student (and hopefully a lesson would be learned).
Regards,
Paul McKenzie
# 25 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
In comp.lang.c++, this was the usual trick of "doing someone's homework", and that is to give a solution only an experienced C++ programmer would come up with. This way, if the student were to hand in the program, they will get questioned by the teacher, causing all sorts of problems for the student (and hopefully a lesson would be learned).
Regards,
Paul McKenzie
I like it... If we do everyone's homework for them, we'll eventually have to do their job for them when they get to that point...
# 26 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
I like it... If we do everyone's homework for them, we'll eventually have to do their job for them when they get to that point...They'll never reach that point if they hand in some of the homework samples that I've seen on comp.lang.c++. Remember, what they did on comp.lang.c++ was specifically designed to backfire on the student.
Regards,
Paul McKenzie
# 27 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
In comp.lang.c++, this was the usual trick of "doing someone's homework", and that is to give a solution only an experienced C++ programmer would come up with. This way, if the student were to hand in the program, they will get questioned by the teacher, causing all sorts of problems for the student (and hopefully a lesson would be learned).
Isn't that mean? ;)
cilu at 2007-11-9 1:09:02 >

# 28 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Not only the student, but sometimes the teacher would not understand the solution.
(wouldn't he be embarrassed to ask the student then?)
:)
# 29 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Isn't that mean? ;)
No, it's a good lesson. Not to speak of that showing of every now and then is good to your health :D
# 30 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
In comp.lang.c++, this was the usual trick of "doing someone's homework", and that is to give a solution only an experienced C++ programmer would come up with.That's just plain brilliant. :D
/me is looking forward to the next homework question.
wien at 2007-11-9 1:12:02 >

# 31 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
#include <stdlib.h>
#include <stdio.h>
int calcSeries(int N)
{
int k1, k2, n = (N+1)/2;
if (N<1) exit(-1);
if (N%2)
{
k1 = k2 = 1;
}
else
{
k1 = -1;
k2 = 3;
}
return k1*n*(k2+2*(n-1));
}
int main()
{
for (int i=1; i<20; ++i)
printf("%d \t%d\n", i, calcSeries(i));
return 0;
}
# 32 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Gee thanx guys you make me feel heaps good...
But any way it's true... hard code means good practice
# 33 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Guys can you check this for me?
#include <iostream>
using namespace std;
int main()
{
int sign = 1;
int sum = 0;
int n;
int i;
cout << "Enter a value: ";
cin >> n;
if((n <= 6) && (n > 0))
{
for(double i=1; i<=n; i++)
{
sum += sign * (int)pow((double)i, (double)i);
sign *= -1;
}
}
else
{
cout << "Invalid\n";
}
cout << "1";
for (i=2, i<=n, i++)
{
if mod(i%2)==0
{
cout << "-";
}
else
cout << "+";
cout << i << "^" << i;
}
cout << "=" << answer << endl;
return 0;
}
# 34 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Don't mind that as yet i've got to fix it abit!
# 35 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
#include <stdlib.h>
#include <stdio.h>
int calcSeries(int N)
{
int k1, k2, n = (N+1)/2;
if (N<1) exit(-1);
if (N%2)
{
k1 = k2 = 1;
}
else
{
k1 = -1;
k2 = 3;
}
return k1*n*(k2+2*(n-1));
}
int main()
{
for (int i=1; i<20; ++i)
printf("%d \t%d\n", i, calcSeries(i));
return 0;
}
I will not try to understand what you did here! Did you and Gabriel started a competition for the most obfuscated code? There is a thread in the Chit Chat forum about the most obfuscated "Hello World" application. You might wanna try it!
cilu at 2007-11-9 1:17:01 >

# 36 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Okay i think this is it now...
#include <iostream>
using namespace std;
int main()
{
int sign = 1;
int sum = 0;
int n;
double i;
cout << "Enter a value: ";
cin >> n;
if((n <= 6) && (n > 0))
{
for(i=1; i<=n; i++)
{
sum += sign * (int)pow(i, i);
sign *= -1;
}
}
else
{
cout << "Invalid\n";
}
cout << "1";
for (int i=2; i<=n; i++)
{
if (i%2==0)
{
cout << "-";
}
else
cout << "+";
cout << i << "^" << i;
}
cout << "=" << sum << endl;
return 0;
}
# 37 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Oops supposed to be code not quote
# 38 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Guys can you check this for me?
Listen, I think you got the wrong impression. We are not your compiler. We won't do your homeworks. Is there something wrong with your code? What is it? What is the part you need help with?
cilu at 2007-11-9 1:20:03 >

# 39 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
I will not try to understand what you did here! Did you and Gabriel started a competition for the most obfuscated code? There is a thread in the Chit Chat forum about the most obfuscated "Hello World" application. You might wanna try it!
It's quite simple really. Differences in consecutive squares are easily calculated. It turns out that in the series, the difference increments by 4 each time. I split the cases between even and odd n:
Odd N:
Sum = 1^2 + (3^2 - 2^2) + (5^2 - 4^2) +...
Sum = 1 + 5 + 9 + 13 + ... (it now becomes obvious this is an arithmetic series)
(use equation for arithmetic series, re-order a few terms and we have:)
Sum = n*(1+2*(n-1)) ( where n = (N+1)/2 )
Even N:
Sum = -[(2^2 - 1^2) + (4^2-3^2) + (6^2 - 5^2) + ...]
Sum = -[3 + 7 + 11 + 15 + ...]
(again, this is an arithmetic series which can be re-written as:)
Sum = -n*(3+2*(n-1)) ( where n = (int)((N+1)/2) )
The equations for both even and odd are very similar, just a negative sign and one constant. That's where I got my equation:
k1*n*(k2+2*(n-1))
And once you understand the math (which I didn't explain b/c I weas looking for a reaction like yours ;) ), the code is perfectly legible! :D
# 40 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
It's quite simple really. Differences in consecutive squares are easily calculated. It turns out that in the series, the difference increments by 4 each time.
Hm... :sick:
1 - 3 + 5 - 7 + 9 - 11 + 13...
What is this?! :confused: The OP wants
1 2^2 + 3^3 4^4 + 5^5 - ...
that is:
1 - 4 + 27 - 256 + 3125 - ...
So, next time, be more careful... ;)
cilu at 2007-11-9 1:22:15 >

# 41 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
:blush: ooops. I misread it. In that case, the series is not that easy to simplify. LOL!
# 42 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Thanx guys
# 43 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
This is it... updated
/************************************************************************************
* *
* Filename: A2Part5.cpp *
* *
* Login: po028 *
* *
* Assignment No: 2 *
* *
* File Description: To compute and display the series 1 - 2^2 + 3^3 - 4^4 + 5^5 - ..*
* and the result. For example: Enter n = 2 *
* *
* Date Last Modified: 10/04/2005 *
************************************************************************************/
#include <iostream>
using namespace std;
int main()
{
int sign = 1; //for changing signs
int sum = 0; //make sure that the sum is 0
int n; //input
double i; //index
cout << "Enter a value: "; //get input
cin >> n;
if((n <= 6) && (n >= 0)) //check if input is in range
{ //if so do for loop
for(i=1; i<=n; i++) //for loop for calculating series
{
sum += sign * (long int)pow(i, i); //calculate number to power of number
sign *= -1; // swapping the signs
}
cout << "0"; //begin output in the series format
for (int i=1; i<=n; i++) //for loop for output as required
{
if (i%2==0) //if number is even use subtraction sign
{
cout << "-";
}
else //number is then odd, use addition sign
cout << "+";
cout << i << "^" << i; //output number to power of number format
}
cout << "=" << sum << endl; //output the result
}
else //number is not in range
{
cout << "Invalid\n";
}
return 0;
}
# 44 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
* *
* Filename: A2Part5.cpp *
* *
* Login: po028 *
* *
* Assignment No: 2
A bit off topic, but anyway: You shouldn't post your login here. That's one thing. The other is: did you spend a thought about the possibility of your teacher being a dev-archive member? It's not impossible, you know. :rolleyes:
# 45 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
It was a cut and paste job plus that teacher thing i doesn't bother me...With the login you guys gotta figure out which Uni i go to first...
# 46 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
It was a cut and paste job plus that teacher thing i doesn't bother me...With the login you guys gotta figure out which Uni i go to first...It's Cut&Paste University, isn't it?
# 47 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
int series_sum(const int &n)
{
int sum=0;
for(int i=1,sign=1,p,j;i<=n;sum+=sign*p,sign*=-1,++i)
for(j=1,p=1;j<=i;p*=i,++j);
return sum;
}
AvDav at 2007-11-9 1:29:15 >

# 48 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Hm... :sick:
1 - 3 + 5 - 7 + 9 - 11 + 13...
What is this?! :confused: The OP wants
1 2^2 + 3^3 4^4 + 5^5 - ...
that is:
1 - 4 + 27 - 256 + 3125 - ...
So, next time, be more careful... ;)
IMHO, KevinHall's solution was correct.
T.G. at 2007-11-9 1:30:21 >

# 49 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
IMHO, KevinHall's solution was correct.
That's because you're not reading the posts carefully... ;)
PS: Perhaps you want to run this...
int calcSeries(int N)
{
int k1, k2, n = (N+1)/2;
if (N<1) exit(-1);
if (N%2)
{
k1 = k2 = 1;
}
else
{
k1 = -1;
k2 = 3;
}
return k1*n*(k2+2*(n-1));
}
int powerSums(int N)
{
int sum = 0;
int sign = 1;
for(int i=1; i<=N; i++)
{
sum += sign*(int)pow(i, i);
sign *= -1;
}
return sum;
}
int main()
{
for (int i=1; i<=10; ++i)
{
printf("i = %d\n",i);
printf("[Kevin]\t%d\n", calcSeries(i));
printf("[Cilu]\t%d\n", powerSums(i));
}
return 0;
}
cilu at 2007-11-9 1:31:22 >

# 50 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Oh. It was stupid of me. thanks for the explanation.
T.G. at 2007-11-9 1:32:17 >

# 51 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Oh. It was stupid of me. thanks for the explanation.
That's ok. We all do mistakes. After all, we are only programmers... :D
cilu at 2007-11-9 1:33:19 >

# 52 Re: Code this 1 2^2 + 3^3 4^4 + 5^5 -
Thanx for all your help again