Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Counting occurrence of numbers in C (http://www.programmingforums.org/showthread.php?t=6960)

stabule Nov 13th, 2005 1:38 AM

Counting occurrence of numbers in C
 
Hi all.

I need help creating a prog. in C that can count the occurrence of numbers. The prog. should go like this:

Enter input 1 = 223335
Enter input 2 = 2

Output = The number 2 appears twice in "223335".

The first input can be any 6 numbers and the second input should only be 1 number.

Could someone help me out?

DaWei Nov 13th, 2005 6:55 AM

Investigate the modulus and division operators. Also, a trip to forum's rules/FAQ would be in order, and a "How to Post...." thread. There's plenty of help available here if you don't denigrate our rules and preferences.

Benoit Nov 13th, 2005 10:22 AM

The easiest way to do this is have the first number inputted as a string, and the second one as a character, and just search through the string with a loop

jim mcnamara Nov 14th, 2005 4:55 PM

DaWei - that's okay - this was posted on every C forum out there... with about the same lack of success from a homework poster's perspective - no completely working code.

Sometimes I wish they's stop teaching C out there so we'd get something approximating actual questions. Instead of homework.

Infinite Recursion Nov 15th, 2005 8:49 AM

here it is in C++... surely you can convert it to C...

:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main (void)
{
    string num = "";
    char search = '0';
    int i = 0;
    int count = 0;
   
    cout << endl << "Enter Number: ";
    cin >> num;

    cout << endl << "Enter Search Value: ";
    cin >> search;
   
    for (i = 0; i < num.length(); i++)
    {
        //cout << num[i] << endl;
        if ( num[i] == search )
        {
            count++;
        }
    }
   
    cout << endl << "Number of occurences: " << count << endl;
   
    system("PAUSE");
    return 0;
}


DaWei Nov 15th, 2005 9:32 AM

The reason I suggest division and the modulus operator is because your post specifies "numbers". If you use a string, then you'll have to validate it first, which somewhat tosses the "easiest" description. If you acquire the information using scanf, you'll get validation or failure as part of the package. Counting the occurrence of a digit with division/modulus is every bit as simple as counting with a pointer.
:

#include <stdio.h>
#include <stdlib.h>

int failure (char *why)
{
    fprintf (stderr, "%s\n", why);
    return 1;
}
int main (int argc, char *argv [])
{
    int theNumber;
    int theDigit;
    int count = 0;

    // Getting and validating input
    printf ("Enter a number: ");
    if (scanf ("%d", &theNumber) != 1) return failure ("Not a number");
    theNumber = abs (theNumber);
    printf ("Enter a single digit: ");
    if (scanf ("%d", &theDigit) != 1) return failure ("Not a number");
    theDigit = abs (theDigit);
    if (theDigit > 9) return failure ("Too many digits");

    // Counting digits
    while (theNumber)
    {
        if ((theNumber % 10) == theDigit) count++;
        theNumber /= 10;
    }

    // Presenting results
    printf ("The digit %d occurs %d times\n", theDigit, count);
    rewind (stdin);
    getchar ();
    return 0;
}


Infinite Recursion Nov 15th, 2005 10:49 AM

I like DaWei's approach moreso than my own... mine lacks error handling and validation for sure.


All times are GMT -5. The time now is 2:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC