Like Tree1Likes
  • 1 Post By laserlight

what am I doing wrong?

This is a discussion on what am I doing wrong? within the C Programming forums, part of the General Programming Boards category; I'm writing this program for yet another home work and I'm stuck on a very small part of it. I ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    what am I doing wrong?

    I'm writing this program for yet another home work and I'm stuck on a very small part of it. I wrote a short separate program that includes my problem. I added the program and what I get when I run it.
    the problem is with the last number on the second line.
    instead of "1234567" I get "1247038" and I have no idea where that number comes from.
    if I replace the 'i' with a '0' in the end of the "x_i6=..." line then everything works fine, but if I put back the 'i' (which is 0 in the 1st time the "for" runs) then this specific number returns.
    and what's even stranger is if print the 'i' with printf before doing the calculations then it also works fine, even though I don't change anything.
    help anybody?
    Attached Images Attached Images   

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The problem I have is you haven't said WHAT you're trying to do, and what algorithm you're trying to use to get that done. Posting code in plain text with code tags around it, is GREAT. Posting images of your code, is not.

    Declaring variables for your loop, inside the loop, is something I'm not familiar with. Best to declare your variables at the start of the function, or at least, outside a block of code - like a loop.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Quote Originally Posted by Adak
    Declaring variables for your loop, inside the loop, is something I'm not familiar with. Best to declare your variables at the start of the function, or at least, outside a block of code - like a loop.
    Unless there is some efficiency advantage to be had by moving the declaration and initialisation to before the loop, declaring variables to be used only in the loop body in the loop body is fine: it follows the rule of thumb to declare variables near first use.
    Salem likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 05:35 PM
  2. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  3. what is wrong?
    By timhxf in forum C Programming
    Replies: 2
    Last Post: 01-07-2007, 12:29 AM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-27-2005, 11:48 PM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM

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