![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2005
Posts: 1
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|