473,513 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting # of single digits with arrays?

I've been trying to learn arrays, and I was wondering how one would go
about making a program that would take from the user as many single
digit numbers (0 - 9) as the user wants to enter, and then the program
would output the number of times each digit was inputted, (and only the
digits that were inputted). I've been toying around with this all day,
but can't seem to nail it. I've been trying to use an array as a
counter, and then putting it in a loop. Not a whole lot of luck so far,
but I'm probably doing it wrong.. Any ideas?

Nov 1 '06 #1
9 2502
tm*****@gmail.com wrote:
I've been trying to learn arrays, and I was wondering how one would go
about making a program that would take from the user as many single
digit numbers (0 - 9) as the user wants to enter, and then the program
would output the number of times each digit was inputted, (and only the
digits that were inputted). I've been toying around with this all day,
but can't seem to nail it. I've been trying to use an array as a
counter, and then putting it in a loop. Not a whole lot of luck so far,
but I'm probably doing it wrong.. Any ideas?
Is your problem with the algorithm or with the control flow?

int array[10] = {0};
....get user number 0...9
array[user_number]++;

It sounds like homework, so show what you've got if you need more help.

--
Scott McPhillips [VC++ MVP]

Nov 1 '06 #2

tm*****@gmail.com wrote:
I've been trying to learn arrays, and I was wondering how one would go
about making a program that would take from the user as many single
digit numbers (0 - 9) as the user wants to enter, and then the program
would output the number of times each digit was inputted, (and only the
digits that were inputted). I've been toying around with this all day,
but can't seem to nail it. I've been trying to use an array as a
counter, and then putting it in a loop. Not a whole lot of luck so far,
but I'm probably doing it wrong.. Any ideas?
Maybe you could post your source code. You might be on the right track,
but could have some misconceptions about how to proceed. It sounds like
you might have idea.

Nov 1 '06 #3

Scott McPhillips [MVP] wrote:
tm*****@gmail.com wrote:
I've been trying to learn arrays, and I was wondering how one would go
about making a program that would take from the user as many single
digit numbers (0 - 9) as the user wants to enter, and then the program
would output the number of times each digit was inputted, (and only the
digits that were inputted). I've been toying around with this all day,
but can't seem to nail it. I've been trying to use an array as a
counter, and then putting it in a loop. Not a whole lot of luck so far,
but I'm probably doing it wrong.. Any ideas?

Is your problem with the algorithm or with the control flow?

int array[10] = {0};
...get user number 0...9
array[user_number]++;

It sounds like homework, so show what you've got if you need more help.

--
Scott McPhillips [VC++ MVP]
The algorithm wasn't the problem, so I guess it was the control flow?
I'm just trying to practice using arrays as I have a test coming soon.
I think I may have my problems figured out all anyways. I will post
everything up if I run into more problems. Thanks

Nov 1 '06 #4
tman88g wrote:
I've been trying to learn arrays, and I was wondering how one would go
about making a program that would take from the user as many single
digit numbers (0 - 9) as the user wants to enter, and then the program
would output the number of times each digit was inputted, (and only the
digits that were inputted). I've been toying around with this all day,
but can't seem to nail it. I've been trying to use an array as a
counter, and then putting it in a loop. Not a whole lot of luck so far,
but I'm probably doing it wrong.. Any ideas?
A hint:

++array['7' - '0'];

Replace one of those constants with one of your input variables.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 1 '06 #5
ughh, the same problem has been resurfacing. Everything compiles and
executes, but I get some debug error. "Run-Time Check Failure #2 -
Stack around the variable 'numArray' was corrupted." I was getting some
other run-time error message before, so I wasn't sure what I was doing
wrong. All I have left that I want to figure out is how to output only
the count of the digits that are entered, but I think I have the basic
idea covered. Here is my code, (don't be too hard)...

#include <iostream>
using namespace std;

void printNumDigits(int Array[])
{
cout << "\nThere are " << Array[0] << " 0's. " << endl;
cout << "There are " << Array[1] << " 1's. " << endl;
cout << "There are " << Array[2] << " 2's. " << endl;
cout << "There are " << Array[3] << " 3's. " << endl;
cout << "There are " << Array[4] << " 4's. " << endl;
cout << "There are " << Array[5] << " 5's. " << endl;
cout << "There are " << Array[6] << " 6's. " << endl;
cout << "There are " << Array[7] << " 7's. " << endl;
cout << "There are " << Array[8] << " 8's. " << endl;
cout << "There are " << Array[9] << " 9's. " << endl << endl;
}

int main()
{
int num;
int numArray[10] = {0};

do
{
cout << "Enter an one-digit number, or 10 to quit: ";
cin >num;

numArray[num]++;

} while ( num <= 9 );
if ( num = 10 )
{
printNumDigits(numArray);
}

else if ( num 10 )
{
cout << "One-digit numbers only! " << endl << endl;
}

return 0;
}


tman...@gmail.com wrote:
Scott McPhillips [MVP] wrote:
tm*****@gmail.com wrote:
I've been trying to learn arrays, and I was wondering how one would go
about making a program that would take from the user as many single
digit numbers (0 - 9) as the user wants to enter, and then the program
would output the number of times each digit was inputted, (and only the
digits that were inputted). I've been toying around with this all day,
but can't seem to nail it. I've been trying to use an array as a
counter, and then putting it in a loop. Not a whole lot of luck so far,
but I'm probably doing it wrong.. Any ideas?
>
Is your problem with the algorithm or with the control flow?

int array[10] = {0};
...get user number 0...9
array[user_number]++;

It sounds like homework, so show what you've got if you need more help.

--
Scott McPhillips [VC++ MVP]

The algorithm wasn't the problem, so I guess it was the control flow?
I'm just trying to practice using arrays as I have a test coming soon.
I think I may have my problems figured out all anyways. I will post
everything up if I run into more problems. Thanks
Nov 1 '06 #6
As soon as I ask around for possible solutions, I end up solving the
problem myself. It figures... Thanks for the help anyways guys.
tman...@gmail.com wrote:
Scott McPhillips [MVP] wrote:
tm*****@gmail.com wrote:
I've been trying to learn arrays, and I was wondering how one would go
about making a program that would take from the user as many single
digit numbers (0 - 9) as the user wants to enter, and then the program
would output the number of times each digit was inputted, (and only the
digits that were inputted). I've been toying around with this all day,
but can't seem to nail it. I've been trying to use an array as a
counter, and then putting it in a loop. Not a whole lot of luck so far,
but I'm probably doing it wrong.. Any ideas?

>
Is your problem with the algorithm or with the control flow?
>
int array[10] = {0};
...get user number 0...9
array[user_number]++;
>
It sounds like homework, so show what you've got if you need more help.
>
--
Scott McPhillips [VC++ MVP]
The algorithm wasn't the problem, so I guess it was the control flow?
I'm just trying to practice using arrays as I have a test coming soon.
I think I may have my problems figured out all anyways. I will post
everything up if I run into more problems. Thanks
Nov 1 '06 #7

<tm*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
As soon as I ask around for possible solutions, I end up solving the
problem myself. It figures... Thanks for the help anyways guys.
You did figure to use == isntead of = in your if statement right?
>

>tman...@gmail.com wrote:
Scott McPhillips [MVP] wrote:
tm*****@gmail.com wrote:
I've been trying to learn arrays, and I was wondering how one
would go
about making a program that would take from the user as many single
digit numbers (0 - 9) as the user wants to enter, and then the
program
would output the number of times each digit was inputted, (and only
the
digits that were inputted). I've been toying around with this all
day,
but can't seem to nail it. I've been trying to use an array as a
counter, and then putting it in a loop. Not a whole lot of luck so
far,
but I'm probably doing it wrong.. Any ideas?


Is your problem with the algorithm or with the control flow?

int array[10] = {0};
...get user number 0...9
array[user_number]++;

It sounds like homework, so show what you've got if you need more
help.

--
Scott McPhillips [VC++ MVP]

The algorithm wasn't the problem, so I guess it was the control flow?
I'm just trying to practice using arrays as I have a test coming soon.
I think I may have my problems figured out all anyways. I will post
everything up if I run into more problems. Thanks

Nov 1 '06 #8
>tman...@gmail.com wrote:
Scott McPhillips [MVP] wrote:
tm*****@gmail.com wrote:
I've been trying to learn arrays, and I was wondering how one
would go
about making a program that would take from the user as many single
digit numbers (0 - 9) as the user wants to enter, and then the
program
would output the number of times each digit was inputted, (and only
the
digits that were inputted). I've been toying around with this all
day,
but can't seem to nail it. I've been trying to use an array as a
counter, and then putting it in a loop. Not a whole lot of luck so
far,
but I'm probably doing it wrong.. Any ideas?


Is your problem with the algorithm or with the control flow?

int array[10] = {0};
...get user number 0...9
array[user_number]++;

It sounds like homework, so show what you've got if you need more
help.

--
Scott McPhillips [VC++ MVP]

The algorithm wasn't the problem, so I guess it was the control flow?
I'm just trying to practice using arrays as I have a test coming soon.
I think I may have my problems figured out all anyways. I will post
everything up if I run into more problems. Thanks

<tm*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
As soon as I ask around for possible solutions, I end up solving the
problem myself. It figures... Thanks for the help anyways guys.
Please don't top post in this newsgroup. Okay, now that you got it, I would
of done it a bit different.

I would of had the user input a whole string, then go through the stream
adding up the digits. What if you wanted to see how many of any character
they put int?

Pseudo code:

std::string Line;
std::cout << "Enter a line:: ";
std::cin >Line;

unsigned int Characters[256] = (0);
// Exercise for the reader, use a std::vector instead and preallocate 256
spaces

for ( int i = 0; i < Line.length(); ++i )
{
Characters[Line[i]]++;
}

for ( int i = 0; i < 256; ++i )
{
if ( Characters[i] 0 )
{
if ( i >= 32 && i < 128 )
std::cout << "You entered " << Characters[i] << " " <<
static_cast<char>( i ) << "'s" << std::endl;
else
std::cout << "You entered " << Character[i] << " chars with the ASCII
value " << i << std::endl;
}
}
Nov 1 '06 #9
tm*****@gmail.com wrote:
int num;
int numArray[10] = {0};

do
{
cout << "Enter an one-digit number, or 10 to quit: ";
cin >num;

numArray[num]++;

} while ( num <= 9 );
You've got a bug here that will cause an out-of-bounds access error. If
the user enters 10 your code attempts to increment numArray[10], which
does not exist.

--
Scott McPhillips [VC++ MVP]

Nov 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3228
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
1
1864
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
7
4789
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
12
8528
by: nikNjegovan | last post by:
So i have a tachometer that I can communicated with via UART which gives me a character array of ascii values in the following form: Standard ascii 7 characters including decimal point such that...
11
2219
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a...
1
2065
by: TC | last post by:
I am getting small math errors when I use a query to convert a column's data type from Text to Single. I am using a calculated column with the following expression. HighSchoolGPA:...
2
4013
by: djzbor | last post by:
Any who can help!! let i have a number 16.0230045 i want to count the fractional part of this number means i want to get the number of digits of the fractional part of this number and store each...
8
5254
by: xiaolim | last post by:
i making a simple program to count the different kinds of characters in a text file and then display them out, however i only manage to count the total numbers of characters. #include...
1
3297
by: vikastcser | last post by:
Hi All , I have a table where four rows are there and each row has two tds.one td is for label and two image icons and other td is used for keeping numeric values. I am able to accomodate digits...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.