C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-18-2009, 12:50 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 39
can i ask what is wrong with this code? [LOOPS only]

whats wrong with this code?
when i enter 10 as N.... it displays 81
when i enter 6 as N... its displays 25

can someone please edit or explain?

thanks!


Code:
#include <stdio.h>

void display(int n);

int main(void)
{
	int n;

	clrscr();

	printf("\nThis program computes and displays\n");
	printf("the sum of the first N perfect squares.");
	printf("\n\nEnter the value of N:\t");
	scanf("%d", &n);

	display(n);
	getche();
	return 0;

}

void display(int n)
{
	int i;
	int j;
	int sum = 0;

	for(i = 0, j = 0; i < ( n - 1 ); i++, j++);
	{
		printf("%d +", ( j * j ));
		sum = sum + (j * j);
	}

	printf("\n\n%d", sum);
}
Huskar is offline   Reply With Quote
Old 03-18-2009, 01:05 AM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
LOOK at this

Code:
for(i = 0, j = 0; i < ( n - 1 ); i++, j++); 
also note that your code makes 1 iteration less than required and starts with 0 instead of 1
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-18-2009, 03:55 AM   #3
Registered User
 
Join Date: Mar 2009
Posts: 39
hmm... can u suggest something please?
Huskar is offline   Reply With Quote
Old 03-18-2009, 04:38 AM   #4
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by Huskar View Post
hmm... can u suggest something please?
remove ;
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-18-2009, 04:40 AM   #5
Wheres the lesbians?
 
mike_g's Avatar
 
Join Date: Oct 2006
Location: UK
Posts: 1,219
In other words: Remove the semi colon. Set your loop to start at 1 and run until it equals n.

Also, since i and j are effectivly the same you should be able to remove one of the variables.
__________________
Senior highbrow doctor of authority.
mike_g is offline   Reply With Quote
Old 03-18-2009, 05:27 AM   #6
Registered User
 
Join Date: Mar 2009
Posts: 39
thanks mike...
i never reviewed the syntax since it compiled properly and was ran properly...
im polishing it now...

thanks a lot! =)
Huskar is offline   Reply With Quote
Old 03-18-2009, 02:05 PM   #7
Resu Deretsiger
 
Nightowl's Avatar
 
Join Date: Nov 2008
Location: /dev/null
Posts: 185
One way to notice this: when you entered 10, it gave you the square of 9, or (10-1). Same for when you entered 6.

This should tip you off that something is wrong with the logic, not the syntax . . . and that the error is probably something being set to 1 when it should be 0, vice versa, or a < rather than a <=, or vice versa.
__________________
Do as I say, not as I do . . .

Experimentation is the essence of programming. Just remember to make a backup first.

"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

Questions posted by these guidelines are more likely to be answered.

Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.
Nightowl is offline   Reply With Quote
Reply

Tags
code, loops, problems, program

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
what is wrong in this simple code vikingcarioca C Programming 4 04-23-2009 07:10 AM
Error msg regarding the Switch loop, What's wrong in my code? althagafi C Programming 7 08-06-2004 09:14 AM
what is wrong with this code please korbitz Windows Programming 3 03-05-2004 10:11 AM
what's wrong with the following code? catalyst C Programming 1 11-07-2003 04:30 AM
Interface Question smog890 C Programming 11 06-03-2002 05:06 PM


All times are GMT -6. The time now is 11:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22