Jump to content
  • Advertisement

Archived

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

PartyOmNi

C++ help!

This topic is 5552 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
Advertisement
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
ehm..


long getVal(int n)
{
long result = 0;
for(int i = 1; i <= n; i++)
result += i*i;
return result;
}



Hopefully I misunderstood your question.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
*cough homework cough*

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!