please c++ help: my program has a fault

Jul 3, 2011 at 1:51pm
thanks
Last edited on Jul 3, 2011 at 5:29pm
Jul 3, 2011 at 2:30pm
Ahrg! you need to put code tags!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
	int i,j;
	int sum1=0,sum2=0;
	int n;
	for(n=100;n<1000;n++) {
		for(i=n-1;i>0;i--)
			sum1 +=i;
		for(j=n+1;sum1>sum2;j++)
			sum2+=j;
		if(sum1==sum2)
			printf(" n may be %d\n\n ",n);
	}
	system("PAUSE");
	return 0;
}
Last edited on Jul 3, 2011 at 2:31pm
Jul 3, 2011 at 4:14pm
1+2+3+......+(n-2)+(n-1)=(n+1)+(n+2)+....+m

In addition; 99<n<1000

What about "m"? Are there any restrictions on "m"?

If the goal is to find an "n" such that the sum from 1 to n-1 is equal to the sum (n+1)+(n+2)+...+m, for some m >= n + 1, then your code is correct except for one problem: you should be setting sum1 and sum2 each back to zero at the beginning of the main loop.
Jul 3, 2011 at 4:20pm
Yeah, it's important to know what m is. And just a heads up - don't duplicate threads or some members might get annoyed.

http://cplusplus.com/forum/general/45969/
http://cplusplus.com/forum/general/45969/
http://cplusplus.com/forum/general/45977/
Jul 3, 2011 at 4:35pm
m is any number
only important thing is

sum1=sum2

sum1=1+2+3+......+(n-2)+(n-1)
sum2=(n+1)+(n+2)+....+m


99<n<1000
Jul 3, 2011 at 4:47pm
closed account (D80DSL3A)
@Xander314. You missed one. http://www.cplusplus.com/forum/beginner/45976/
He has at least 4 threads going on this problem.
Jul 3, 2011 at 4:47pm
Regardless, I gave you the answer in my previous post.

shacktar wrote:
you should be setting sum1 and sum2 each back to zero at the beginning of the main loop.
Jul 3, 2011 at 5:23pm
thank for your answers. program give the correct answer.

shacktar (136) Jul 3, 2011 at 7:47pm
Regardless, I gave you the answer in my previous post.

shacktar wrote:
you should be setting sum1 and sum2 each back to zero at the beginning of the main loop.

Jul 3, 2011 at 7:39pm
OP, please don't delete your posts. Now no-one else can benefit from the help in this thread.

I don't think anyone gave you a full solution, so I don't know what you are afraid of. Surely your lecturer doesn't mind you asking for a hint? And if he/she does then you should probably have gone to him/her in the first place rather than breaking his/her rules and posting here. Just saying ^^

The details are all in this article, lovingly recorded for us all by Albatross
http://cplusplus.com/articles/oGLN8vqX/
Topic archived. No new replies allowed.