#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Unhappy Problem with a program


    Hi all, can you help me to understand what is the fault with this not working program?? :(

    Thank you a lot :)



    #include <stdio.h>
    #define N 7

    main()

    {
    int n, j;
    int a[N];

    int i=0;

    printf ("Enter a number: ");
    scanf ("%d", &n);

    for (j=2; j<=n || n==1; j++)
    {
    while ((i<=N) || (n==0) || (n%j!=0))
    {
    if (n%j==0)
    {
    a[i]=j;
    n/=j;
    i++;
    }
    else
    break;
    }
    }
    i=0;
    for (i=0; i<=N; i++);
    printf ("a[%d]=%d\n", i, a[i]);

    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,733
    Rep Power
    1959
    You tell us what is wrong, then we can help you to solve the problem.

    Do you get any error message? please post it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by MrFujin
    You tell us what is wrong, then we can help you to solve the problem.

    Do you get any error message? please post it.
    It doesn't work. Any number I enter it doesn't print anything
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by deliabeatles
    It doesn't work. Any number I enter it doesn't print anything
    No error messages
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    The first problem is formatting. You need to use code tags to preserve your code's indentation, but your indentation style is very poor. Here's a better style:
    Code:
    #include <stdio.h>
    #define N 7
    
    int main()
    {
        int n, j;
        int a[N];
    
        int i=0;
    
        printf ("Enter a number: ");
        scanf ("%d", &n);
    
        for (j=2; j<=n || n==1; j++)
        {
            while ((i<=N) || (n==0) || (n%j!=0))
            {
                if (n%j==0)
                {
                    a[i]=j;
                    n/=j;
                     i++;
                }
                else
                    break;
            }
        }
        i=0;
        for (i=0; i<=N; i++);
            printf ("a[%d]=%d\n", i, a[i]);
        return 0;
    }
    The second problem is this: main()
    You are implicitly declaring main to return an int, which means that you are promising the compiler that you will return an int, but then you break your promise and don't return anything. You need to explicitly declare main to return int and then actually do return an int -- returning 0 signals successful completion, non-zero for terminating because of an error. I've added those corrections and highlighted them in red.

    There you go. If you think that there might be anything else wrong with it, please explain why you think that. Telling us what the program is supposed to do would also help.

    PS
    The array, a, has N elements indexed by 0 though (N-1) inclusive. You are trying access N+1 elements as well as writing N+1 elements. That means that you are writing outside the bounds of the array and clobbering whatever 4 bytes is next to it in memory. Depending on how the local variables are arranged in memory, that means that you are overwriting either i or j, which is probably messing up your calculations.

    Instead of
    for (i=0; i<=N; i++);
    you need to write
    for (i=0; i<N; i++);
    Similarly, in the while condition you need to write
    (i < N)
    Last edited by dwise1_aol; January 30th, 2013 at 03:28 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    The program writes a number's factorization.
    I tried to write int main () and return 0, to replace i<=N with i<N but te continue to not work.
    Sorry, but I haven't understood yet what is the problem :( ...can you explain me?
  12. #7
  13. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,733
    Rep Power
    1959
    then it start debugging.

    Try adding more printf so you can see what happens in each "step".

    Look out for missing {} at the else part in the loop if you add more code lines to it. ;)
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    I have understood what was the error.
    Thanks you very much, you gave me a great help! :D

IMN logo majestic logo threadwatch logo seochat tools logo