Thread: Please help me (urgent)

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Unhappy Please help me (urgent)

    The question is as follows:

    Write a program to prompt the user to input five integers in the range [0,9] and then output the followings:

    1. the numbers in ascending order.
    2. the numbers that occur three times in the user input.
    3. the numbers that occur twice in the user input.

    Example: For the user input: 2 3 5 5 3,
    1. the sorted order is: 2 3 3 5 5
    2. no numbers occur three times
    3. the numbers that occur twice are: 3 5

    One possible idea:
    Declare an array (size of 5) of integers named hand for storing the five user inputs.

    Declare an array (size of 10) of integers named count for counting the number of occurrences of each possible value in the user input.

    Example:
    count becomes [0 0 1 2 0 2 0 0 0 0 ] for the user input: 2 3 5 5 3

    Then print the sorted array using count.
    Use the array count to check for values that occur twice or three times.

    i have done the first question

    but i have no idea with question 2 and 3
    please help me

    my program is as follows:

    #include<stdio.h>



    void main()

    {
    int hand[4],count[10]={0,1,2,3,4,5,6,7,8,9},j,i=0,temp;
    for (i=0;i<4;i++)
    {
    do {printf("Enter the num%d:",i);
    scanf(" %d",&hand[i]);
    } while (hand[i]<0 || hand[i]>9);
    }


    for(j=0;j<4;j++)

    {

    for(i=0;i<4;i++)

    {

    if(hand[i]>hand[i+1])

    {

    temp=hand[i];

    hand[i]=hand[i+1];

    hand[i+1]=temp;

    }

    }

    }

    printf("The sorted order is");

    for(i=0;i<4;i++)

    printf("%3d",hand[i]);
    getch();

    }

  2. #2
    samp005
    Guest
    Declare an array (size of 10) of integers named count for counting the number of occurrences of each possible value in the user input.

    Example:
    count becomes [0 0 1 2 0 2 0 0 0 0 ] for the user input: 2 3 5 5 3


    just declare an array ARRAY[10]
    then when ever you read a number in you say ARRAY[number-1]++

    Then when you want to print out only numbers that are in twice you do a loop like this
    Code:
    for(i = 0; i < 10; i++)
    {
         if(ARRAY[i] == 2)
              printf("%d", i+1);
    }
    you should use code tags when you post your code, they make it a lot easier to read

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In your haste, you forgot to read the important things. Don't post as "urgent", and please use code tags.


    >void main()
    is wrong. Use
    >int main(void)


    If you know how to loop through the array, you know how to count...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    for(j=0;j<4;j++)
    {
        for(i=0;i<4;i++)
        {
            if(hand[i]>hand[i+1])
            {
                temp=hand[i];
                hand[i]=hand[i+1];
                hand[i+1]=temp;
           }
        }
    }
    In the last for-loop run with i=3 hand[i+1] will cause trouble, because hand[4] is out of bounds.
    You've declared hand as an array of size 4. That means the array goes from index 0 to n-1 = 3, with n = 4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 10:38 PM
  3. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM