Thread: help with a simple program

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    12

    Question help with a simple program

    Hello guys,
    I'm kind of stuck here trying to do some homework...writing a simple program which
    1. displays numbers from 1-5.
    2. user picks a number (1-5).
    3. the programs displays the available numbers except the number the user picked.
    4. the program runs until the last number is picked and displays a message that there are no available numbers left.

    Could you suggest me how to approach this problem?
    So far, I've been able to do 1,2. but not 3 and 4..

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    We would be glad to help. Show us what you've got so far, and where you're stuck, and we can offer advice from there.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you'll allow me to be silly...

    Quote Originally Posted by holler View Post
    Hello guys,
    I'm kind of stuck here trying to do some homework...writing a simple program which
    1. displays numbers from 1-5.

    So far, I've been able to do 1,2. but not 3 and 4..
    Code:
    printf("%d and %d\n", 3, 4);

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    Quote Originally Posted by Matticus View Post
    If you'll allow me to be silly...

    Code:
    printf("%d and %d\n", 3, 4);
    Sure..
    It only works when I enter numbers one by one from 1-5.

    Code:
    int choice,tries,i;
       int nums[5] = {1,2,3,4,5};
    
    
       printf("Available numbers:\n");
       for(i=0; i<5; i++)
       {
           printf("Numbers: %d\n", nums[i]);
       }
    
    
       LOOP:
        for(tries=0; tries<5; tries++)
        {
                printf("Enter a number you want to pick: (1-5)\n");
                scanf("%d", &choice);
                if(choice>5)
                {
                    printf("wrong number. Try again!\n");
                    goto LOOP;
                }
           for(i=0; i<5; i++)
           {
                if(choice==nums[i])
                    {
                        nums[i]++;
                        continue;
                    }
                printf("Numbers: %d\n", nums[i]);
           }
        }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps start with
    Code:
    int picked[5] = { 0 };
    When the user chooses a number 'num', you do
    picked[num] = 1;

    When it comes to displaying numbers to choose, you do
    if ( picked[n] != 1 ) print n
    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.

  6. #6
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    Quote Originally Posted by Salem View Post
    Perhaps start with
    Code:
    int picked[5] = { 0 };
    When the user chooses a number 'num', you do
    picked[num] = 1;

    When it comes to displaying numbers to choose, you do
    if ( picked[n] != 1 ) print n

    Thanks for this. Though I'm here to learn, with the limited knowledge that I have, still can't implement your suggestion.
    So far, I was able to accomplish only this chunk of code:

    Code:
    int len,choice,i,j;
       int nums[5] = {1,2,3,4,5};
    
    
       printf("Available numbers:\n");
       for(i=0; i<5; i++)
           printf("Numbers: %d\n", nums[i]);
    
    
        len = 5;
    
    
        do
        {
            printf("\nChoose a number: or press '6' to exit)\n");
            scanf("%d",&choice);
    
    
            for(i=0;i<len;i++)
            {
                if(nums[i]==choice)
                {
                    for(j=i;j<len;j++)
                    nums[j]=nums[j+1];
                    len--;
                }
            }
    
    
            printf("\nNumbers left:\n");
                for(i=0;i<len;i++)
                {
                    printf("Numbers: %d \n",nums[i]);
                    if(nums[i]== '\0') // want to check if the array is empty to display the message below
                        printf("none left\n"); 
                }
    
    
        }
        while(choice<6);
    I understand that the exit condition isn't the best one, could you suggest anything?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(nums[i]== '\0') // want to check if the array is empty to display the message below
    That happens when len == 0
    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.

  8. #8
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    Thank you! Sharing my latest progress.
    The program executes while there are free numbers left and stops when the array is empty.

    Code:
    int len,choice,i,j;
       int nums[5] = {1,2,3,4,5};
    
    
       printf("Available numbers:\n");
       for(i=0; i<5; i++)
           printf(" %d", nums[i]);
    
    
        len = 5;
    
    
        do
        {
            LOOP:
            printf("\nChoose a number: \n");
            scanf("%d",&choice);
            if(choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)
            {
                printf("The number you entered doesn't exist. Try again.\n");
                goto LOOP;
            }
                for(i=0;i<len;i++)
                {
                    if(nums[i]==choice)
                    {
                        for(j=i;j<len;j++)
                        nums[j]=nums[j+1];
                        len--;
                    }
                }
                    printf("\nNumbers left:");
                        for(i=0;i<len;i++)
                        {
                            printf(" %d",nums[i]);
                        }
            }
            while(len!=0);
            printf(" none\n");
            getch();

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Do not ever use goto. Use a loop instead or some other conditional statement to return to that point of the code.
    Do not use getch() it's non-standard and not globally supported on all platforms. Use getchar() instead.
    Double Helix STL

  10. #10
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    I see. Replaced goto with continue and getch() with getchar().

    Code:
        do
        {
            printf("\nChoose a number: \n");
            scanf("%d",&choice);
            if(choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)
            {
                printf("The number you entered doesn't exist. Try again.\n");
              continue; // instead of goto
            }
                for(i=0;i<len;i++)
                {
                    if(nums[i]==choice)
                    {
                        for(j=i;j<len;j++)
                        nums[j]=nums[j+1];
                        len--;
                    }
                }
                    printf("\nNumbers left:");
                        for(i=0;i<len;i++)
                        {
                            printf(" %d",nums[i]);
                        }
            }
            while(len!=0);
            printf(" none\n");
            getchar(); // instead of getch();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM

Tags for this Thread