can i ask what is wrong with this code? [LOOPS only]

This is a discussion on can i ask what is wrong with this code? [LOOPS only] within the C Programming forums, part of the General Programming Boards category; whats wrong with this code? when i enter 10 as N.... it displays 81 when i enter 6 as N... ...

  1. #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);
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    hmm... can u suggest something please?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    Quote Originally Posted by Huskar View Post
    hmm... can u suggest something please?
    remove ;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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.

  6. #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! =)

  7. #7
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  4. what's wrong with the following code?
    By catalyst in forum C Programming
    Replies: 1
    Last Post: 11-07-2003, 03:30 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread


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