Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 13th, 2005, 1:38 AM   #1
stabule
Newbie
 
Join Date: Nov 2005
Posts: 1
Rep Power: 0 stabule is on a distinguished road
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?
stabule is offline   Reply With Quote
Old Nov 13th, 2005, 6:55 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 13th, 2005, 10:22 AM   #3
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4 Benoit is on a distinguished road
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
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Nov 14th, 2005, 4:55 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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.
jim mcnamara is offline   Reply With Quote
Old Nov 15th, 2005, 8:49 AM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,464
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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;
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Nov 15th, 2005, 9:32 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 15th, 2005, 10:49 AM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,464
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I like DaWei's approach moreso than my own... mine lacks error handling and validation for sure.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:38 AM.

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