Archived

This topic is now archived and is closed to further replies.

C++ help!

This topic is 5303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can''t think right now of a simple algorithm... can someone help me? I want a user to input a number, then this program will do a simple calculation where it adds the square from 1 to the number. ie... if user enters, 5 it should, do, (1*1) + (2*2) + (3*3) + (4*4) + (5*5) I started off with a user input and then a function. in the function, there is a for loop... for(counter=number;counter<0;counter--) but then, I dunno what to do after this.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

(define f
(lambda (n)
(if (= n 1)
1
(+ (f (- n 1)) (* n n)))))

(define g
(lambda (n k)
(if (= n 1)
(k 1)
(g (- n 1) (lambda (m)
(k (+ m (* n n))))))))


Take your pick, and yes I am bored.

Share this post


Link to post
Share on other sites
k, this was actually what I was working on from a book, and not homework...
**Write a program that accepts an integer and adds the square of all integers from 1 to the number entered.**

so far... I got...

#include <iostream>
using namespace std;

void SquareTotal(int toSquare);
int toSquare;
int main()
{
cout<<"this program will square from 1 to the number you have entered.\n"< cout<<"please enter a number: ";
cin>>toSquare;
if(toSquare <=0)
{
cout<<"please enter a non negative, non zero number."< cout<<"number: ";
cin>>toSquare;
}
cout<<"the number you have entered is: "<
SquareTotal(toSquare);

cout<<"the total is: "<
return 0;
}

void SquareTotal(int toSquare)
{
int total=0;
for (int counter =1;counter {
total = counter*counter;
}
}

but it doesn''t add from 1^2+2^2+3^2+...+n^2 and so forth.
can someone help me?

Share this post


Link to post
Share on other sites
You are neglecting to do anything meaningful with your function. You call a function with no return value and no alternate way to communicate its findings.

You should set the return type to an int, return total, assign the return value of the function to some variable in your main function, and output that variable. It''s simple really.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
thanks for the help,, but it still doesn''t do what it should.

I changed the code to...
#include <iostream>
using namespace std;
int SquareTotal(int toSquare);

int main()
{
int toSquare;
cout<<"this program will square from 1 to the number you have entered.\n"< cout<<"please enter a number: ";
cin>>toSquare;
if(toSquare <=0)
{
cout<<"please enter a non negative, non zero number."< cout<<"number: ";
cin>>toSquare;
}
cout<<"the number you have entered is: "<
SquareTotal(toSquare);

cout<<"the total is: "<
return 0;
}

int SquareTotal(int toSquare)
{
int total=0;
for (int counter =1;counter<=toSquare;counter++)
{
total +=counter*counter;
}
return total;
}





but the output is still...

this program will square from 1 to the number you have entered.

please enter a number: 5
the number you have entered is: 5
the total is: 5
Press any key to continue

Share this post


Link to post
Share on other sites
Uhm.. what's this code?

cout<<"the number you have entered is: "<
SquareTotal(toSquare);

cout<<"the total is: "<
return 0;
You don't print anything returned? Or does your code end up malformed by the forum? In that case you should use code or source tags when posting code.

PS. "a non negative, non zero number" -> "a positive number"?


[edited by - Jedyte on June 4, 2003 6:23:52 AM]

[edited by - Jedyte on June 4, 2003 6:24:36 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Jedyte
Or does your code end up malformed by the forum?



By choosing quote or edit on the original message, you can see it''s because they haven''t used the correct tags for the forum.

Share this post


Link to post
Share on other sites

#include <iostream>
using namespace std;
int SquareTotal(int toSquare);

int main()
{
int toSquare;
cout<<"this program will square from 1 to the number you have entered.\n"<<endl;
cout<<"please enter a number: ";
cin>>toSquare;
if(toSquare <=0)
{
cout<<"please enter a non negative, non zero number."<<endl;
cout<<"number: ";
cin>>toSquare;
}
cout<<"the number you have entered is: "<<toSquare<<endl;

SquareTotal(toSquare);

cout<<"the total is: "<<toSquare<<endl;

return 0;
}

int SquareTotal(int toSquare)
{
int total=0;
for (int counter =1;counter<=toSquare;counter++)
{
total +=counter*counter;
}
return total;
}

Share this post


Link to post
Share on other sites
quote:
please enter a number: 5
the number you have entered is: 5
the total is: 5

toSquare is 5
you print out toSquare
it prints 5

whats the problem?

[edited by - petewood on June 4, 2003 7:04:41 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
*cough homework cough*


Hah, a homework level question would be: A user will input a positive real number greater than or equal to 1, and a period which is greater than 0 and less than (positiveRealNumber - 1). Your program will calculate the square root of all values between from 1 to the positive real number INCLUSIVE, increasing by the period each iteration.

The catcher. Your program must run in at worst O(n log n ) time. Where n is (positiveRealNumber - 1)/period. Fun problem! =)

No non-user-defined functions (Except for non-formatted output) are allowed. In other words, calculate the square roots yourself.

So, if the user input 5.5 as the positiveRealNumber and 0.5 as the period.. you would calculate 1^(.5) + 1.5^(.5) + 2.0^(.5) + 3.0^(.5) ... + 5.5^(.5) and sum the results using a complexity not worse than O(n log n) -> n = 9



[edited by - haro on June 4, 2003 2:25:11 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by nobodynews
Your problem was you weren''t using the result.


now you''ve gone and given them the answer when all they needed to do was just look at the code and read it properly. I''d pointed out the problem already but gave them room to work it out for themselves.

Share this post


Link to post
Share on other sites
nother crit, nuttin personal but..

please POST your question as well, don''t just say help with C++.. peeves me having to go in here to see what the subject is..

if it is a question I know little to nothing about I won''t be wasting my time and yours.

I fseek, therefore I fam.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by drarem
please POST your question as well, don''t just say help with C++.. peeves me having to go in here to see what the subject is..



Yeah taking a few seconds to click on the subject and read the first line of the post is a real drag.

Share this post


Link to post
Share on other sites