Thread: Problem with a sequence

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Problem with a sequence

    Teh first term is 1, the second term is 2, and for all n>=3 the nth term is equal to term (n-1) plus term n/2, where n/2 is rounded down.

    Program should prompt for n, and then display the nth term in the sequence. it can be assumed n is 50 or less. the program must contain a loop that keeps reading new values for n and then prints the nth term. The program show stop when n=0.

    Code:
    //tj wright 11.1
    #include <stdio.h>
    
    void main (void)
    {
    int q,m,n,x[50];
    
               for (m=1;m<=6;m++)
    
               {
               printf("enter n");
    scanf ("%d", &n);
    if (n==0) goto end;
    
    x[1]=1;
    x[2]=2;
    
    for (q=3;q<=50;q++)
    {
    x[q]=x[q-1]=x[q/2];
    }                     
    
    printf("The number is %d\n",x[n]);
    
    }
    end:
    }

    When I run the program, it asks me to enter n, then I go to scan it in, and the program exits and points me to the x[2]=2; line. I am trying to trace the program and see where it is going wrong, but I cannot tell.

    Can anyone tell me where I am going wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    void main is wrong, see the FAQ.

    Using goto in such a short program is very poor style. Use appropriate loop structures.

    The indentation is shoddy. Cleaning this up will help you follow the program flow.

    > for (q=3;q<=50;q++)
    This steps off the end of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    "this steps off the end of the array"

    What does that mean?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Obvious mistake:
    Code:
      x[q]=x[q-1]=x[q/2];

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    I can't even get it to compile, it gives me:

    error at line 23 (end label at end of compound statement
    warning at line 3 (void main(void)) return type of 'main' is not 'int'

    Last time i checked, "goto" was highly discouraged. Here, you can either use:
    return; //supposing you don't fix the return type
    return 0; //supposing you fix the return type to be int like it's supposed to be
    exit(0); //works either way, but I'd generally encourage you to use "return 0;"

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    I could use break; instead of the goto, and it would work fine.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by astrodude15 View Post
    "this steps off the end of the array"

    What does that mean?
    If you have something like:
    Code:
    {
      int nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      int i;
    
      for(i = 0;i < 11;++i)
        printf("&#37;d\n", nums[i]);
    }
    When i gets to 10 in the loop, it's stepped off the end of the array. The only valid indexes are 0 through 9 for that array.
    Last edited by itsme86; 05-30-2007 at 04:14 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    A better solution would be to change your main loop from a for loop to a while loop.

    You could change this:
    Code:
    for (m=1;m<=6;m++)
    to this:
    Code:
    while(n != 0)
    Then you wouldent even need to use a goto or break to end the program.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Thank you all very much for your help and sharing your knowledge!

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by itsme86 View Post
    Code:
      int nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    Shouldn't that give an warning about an "excess elements in initializer" or somesuch? It does for me.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Yeah, it should. I got a little carried away with the initializer
    If you understand what you're doing, you're not learning anything.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also:
    Code:
    sourceFile.cpp:5: `main' 
       must return `int'
    sourceFile.cpp: In 
       function `int main(...)':
    sourceFile.cpp:27: label 
       must be followed by statement
    A goto label must precede a statement. You can't just have one at the end of a block. However, you can add a NULL statement if you wish:
    Code:
    end: ;
    }
    Not that I'm advocating goto. It's a bad idea to use goto.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. time or display problem I am not sure!
    By hamsteroid in forum C Programming
    Replies: 3
    Last Post: 04-09-2007, 01:52 PM
  4. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM