I am mostly familiar with C++. :?: Please help. Question is below :


Write a program to discover how many numbers between 1 and 40 can be expressed as
the sum of 4 positive squares (e.g. 15 = 12 + 12 + 22 + 32).

I am mostly familiar with C++. :?: Please help. Question is below :


Write a program to discover how many numbers between 1 and 40 can be expressed as
the sum of 4 positive squares (e.g. 15 = 12 + 12 + 22 + 32).

Ok, a couple of problems...

Firstly, 12 + 12 + 22 + 32 does NOT equal 15 it is 78
Secondly, none of those 4 numbers are squares. The valid squares would be 1,4,9,16,25,36.
Thirdly, by the sounds of it you are having a problem with the maths, not the programming. Have you designed an algorithm for this yet? That would be your first step, than translate that into actual C++.

Colezy

I don't see the math bbCode. I think the OP means

15 = 1^2 +1^2 +2^2 +3^2 = 1 + 1 + 4 + 9 = 15

where ^ represents raising to a power.

To the OP : Pencil and paper till you see a pattern. Get the math down then turn the math into C++. You need to make an attempt and ask a specific question.

exactly vernon. i did come up with something.yes sir i am attempting it

int sum();

sum= x*x+(x+1)*(x+1)+(x+2)*(x+2)+(x+3)*(x+3)

(x=1,x<40,x++)

return 0
}


oki i am sorry i know i need to make a function and call it in the main program but i did try a bit there. please modify it as necessary. i think i am nearly there.

>> i know i need to make a function and call it in the main program

So make a function and call it in the main program. No one can help you debug what you wrote. The syntax has too many problems.

But if you want me to modify what you wrote, here goes.

int sum();

int sum()
{
    int sum;
    for(int x = 0; x < 40; x++)
    {
        sum= x*x+(x+1)*(x+1)+(x+2)*(x+2)+(x+3)*(x+3);
    }
    return sum;
}

int main()
{
    int returnValue = sum();
    return 0;
}

Now what?

yes. this is it,finally., problem solved. thank you :)

i know what i need to do. its just that i need more practice coz i make syntax mistakes a lot.

the code looks fine, just when i am trying to run this on dev c++ with
system ("pause");

so that i can see the answer but i am getting a lot of errors. can you please have a run and see if u get something.

thanks.

It's a bit hard to understand what you are saying. If you have compilation errors then it might be useful to post some specific examples as well as the code that I presume you have now modified

#include<iostream>

int sum();

int sum()
{
    int sum;
    for(int x = 0; x < 40; x++)
    {
        sum= x*x+(x+1)*(x+1)+(x+2)*(x+2)+(x+3)*(x+3);
    }
    return sum;
}

int main()
{
    int returnValue = sum();
    return 0;
    cout<<sum()<<endl;
system ("pause");

}

I want the program to show me the results. but its flashing too quickly. i would like to include a cout but its not doing the job. :(

forget this question, i am lost with it. i am going to tackle another one. will post if i am having trouble.

try including <stdlib>

Colezy

#include<iostream>

int sum();

int sum()
{
    int sum;
    for(int x = 0; x < 40; x++)
    {
        sum= x*x+(x+1)*(x+1)+(x+2)*(x+2)+(x+3)*(x+3);
    }
    return sum;
}

int main()
{
    int returnValue = sum();
    return 0;
    cout<<sum()<<endl;
system ("pause");

}

I want the program to show me the results. but its flashing too quickly. i would like to include a cout but its not doing the job. :(

Did you not notice that you are returning from main before you use cout?

Here is the amendment below...i am still getting errors there is a problem in the code. :(

Have a look guys.

#include<iostream>

int sum();

int sum()
{
    int sum;
    for(int x=0 ; x < 40; x++)
    {
        sum= (x*x)+((x+1)*(x+1))+((x+2)*(x+2))+((x+3)*(x+3));
    }
    return sum;
}

int main()
{
    int returnValue;
     returnValue= sum();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
    
    cout <<"The sum is: "<< returnValue<<endl;
    
    
    system ("pause");
    return 0;

}

>> I am mostly familiar with C++.

You are clearly not familiar with C++. All these errors would stand out if you were. You need to either put "using namespace std" after "#include <iostream>" or you need to replace "cout" with "std::cout" and replace "endl" with "std::endl".

Beyond the syntax, the code and the algorithm doesn't make any sense. My original post simply turned your code into something that compiled. The code WILL NOT WORK, even if you get the errors out, because the code has nothing to do with the problem.

You need to:

  1. Learn basic C++ syntax.
  2. Think about the problem and how to solve it. It is far more complex than the given code, even when you get the errors out. The code makes no sense.

Right now you're floundering. Take out pencil and paper and work through the problem mathematically till you see a pattern. Then take a few C++ tutorials and turn the math into code.

is it this ?

#include<iostream>
#include <conio.h>
#include <Windows.h>

using namespace std;


int main()
{
    int sum=0;
    for (int x=1;x<40;x++)
    {sum= x*x+(x+1)*(x+1)+(x+2)*(x+2)+(x+3)*(x+3);
    cout<<x<<".value="<<sum<<"\n";
    Sleep(1000);
}
system ("pause");

}

Vernon thanks for the tips. I mean C++ is the only programming language I know. But I am still learning it,by myself and guides from tutorials online and youtube, plus practice with programs in the dev c++. I am trying to solve tough(for me) questions. There is no teacher to guide me except you guys who are pro in this language.

Burcin Erek. I am looking at your code will post back soon. Thanks mate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.