Thread: Find no of times a no is repeated in a array.

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    India
    Posts
    35

    Find no of times a no is repeated in a array.

    Write a program to accept 20 no's into a array and then find
    how many times a particular no is repeated in the array

    What am i doing wrong?

    Code:
    main()
    {
    printf("Enter 20 no");
    int a[20],i=0,b=0,count=0,d=0,j=0;
    for(i;i<=20;i++)
    {
    scanf(" %d ",&a[i]);
    }
    
    printf("Which no would you like to find? ");
    scanf(" %d",&b);
    for(j;j<=20;j++)
    {
    if(a[j]==b)
    {
      int count=count+1;         
    }
    else
    {}
    }               
    printf("The no %d repeated %d time in the given array \n",b,count);
    getch();
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    1) You have shadowed the count variable. It is defined once in main() and another time (locally) within an enclosed block. Remove the keyword "int" from line 16.

    2) Your first for loop reads 21 values into a 20 element array. The second loops over those 21 values. Both blocks therefore exhibit undefined behaviour (and a common symptom of that is a program crash). Change the termination condition so the variable is < 20, not <= 20. It is a good idea to initialise the index in the for loop, via for (i = 0; i < 20; ++i) .....

    Also, to increment count, an alternative syntax is "++count" rather than "count = count + 1".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    India
    Posts
    35
    Did the changes you asked but it still takes the input of the 'no to be found' before the question is displayed...although i have the printf for the question before the scanf!!

    Code:
    /*Write a program to accept 20 no into a array and then find 
    how many times a particular no is repeated in the array*/
    main()
    {
    printf("Enter 20 no \n");
    int a[20],i=0,b=0,count=0,d=0,j=0;
    for(i;i<20;++i)
    {
    scanf(" %d ",&a[i]);
    }
    //Mentioned Issue Below
    printf("Which no would you like to find? \n ");
    scanf(" %d",&b);
    for(j;j<20;++j)
    {
    if(a[j]==b)
    {
      ++count;         
    }
    else
    {}
    }               
    printf("The no %d repeated %d time in the given array \n",b,count);
    getch();
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Remove the trailing space from the format string when reading a[i].
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    India
    Posts
    35
    Thank you grumpy,that worked perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 08-09-2011, 12:55 PM
  2. Replies: 25
    Last Post: 10-31-2009, 01:45 AM
  3. Replies: 16
    Last Post: 11-12-2008, 12:02 AM
  4. count/display non-repeated element of array
    By azsquall in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2008, 09:42 AM
  5. Times Table with Two-Dimensional Array
    By jmb272 in forum C Programming
    Replies: 10
    Last Post: 10-02-2007, 02:22 AM