How to Compose Digits into Integer Descriptions?

In summary: Please give me some idea to start.In summary, you should create an array of strings, with each string having the name "ones","tens","hundreds",... and loop through the character array normally, but loop through the "place name" array differently-- start in the middle (where appropriate), and work backwards, rather than forwards.
  • #1
darkvalentine
12
0
Object: Write a program that read digits and composes them into integers. For example, when you read 123( in characters) the program should print: "123(in integers) is 1 hundred and 2 tens and 3 ones".

My first attempt is first read the number as a string then convert its characters to integers then store them in another int vector. To print the result, I used switch to determine how many items are there in the vector (i.e if read 1234 (4 items) then print 1 thousand 2 hundred...) but then I realize it not a good way. How about if people read in 10 characters??

Please give me some idea to start.
 
Technology news on Phys.org
  • #2
darkvalentine said:
Object: Write a program that read digits and composes them into integers. For example, when you read 123( in characters) the program should print: "123(in integers) is 1 hundred and 2 tens and 3 ones".

My first attempt is first read the number as a string then convert its characters to integers then store them in another int vector. To print the result, I used switch to determine how many items are there in the vector (i.e if read 1234 (4 items) then print 1 thousand 2 hundred...) but then I realize it not a good way. How about if people read in 10 characters??

Please give me some idea to start.

Don't ask homework questions :P

Proceeding along your original route of storing the char input and transforming it into an int vector:

Instead of switch/case, I'd create an array/vector of strings, with each string having the name "ones","tens","hundreds",...

Then just create a loop that runs backwards from the length of the input string down.

A couple details: you can determine how big an array is using sizeof( yourArray ). You might also want to do a preliminary parse to remove things like commas from the input.
 
  • #3
darkvalentine said:
Object: Write a program that read digits and composes them into integers. For example, when you read 123( in characters) the program should print: "123(in integers) is 1 hundred and 2 tens and 3 ones".

My first attempt is first read the number as a string then convert its characters to integers then store them in another int vector. To print the result, I used switch to determine how many items are there in the vector (i.e if read 1234 (4 items) then print 1 thousand 2 hundred...) but then I realize it not a good way. How about if people read in 10 characters??

Please give me some idea to start.

Here we go:

Code:
#!/usr/bin/perl
$_=<>;
s/\D+//gs;
print "$_ (in integers) is";
@_=split //;
for(0..$#_) { print " ".(($_>0)?"and ":"")."$_[$_] ".(qw/one ten hundred thousand ten-thousand hundred-thousand million ten-million hundred-million billion ten-billion hundred-billion trillion ten-trillion hundred-trillion quadrillion/)[$#_-$_] . (($_[$_]==1)?"":"s"); }
print "\n";

Seriously, though:

1) Homework = not here, see the sticky.
2) Clean your input (make sure there are no other characters like decimal points, commas, other characters, etc.)
3) Make one array of names (as suggested above), like "one", "ten", "hundred", etc. However, note: there's no "s" on the end. There's a difference when you print them between saying "5 hundreds", "1 hundred", or "0 hundreds". Only print an "s" after the place's name if the value is 1!
4) Make a second array of the characters that were input
5) Loop through the character array normally, but loop through the "place name" array differently-- start in the middle (where appropriate), and work backwards, rather than forwards.

DaveE
 

FAQ: How to Compose Digits into Integer Descriptions?

What is "Programming Digits to Integers"?

"Programming Digits to Integers" is the process of converting numerical values represented as digits into integer data types that can be used in computer programming.

Why is "Programming Digits to Integers" important?

Converting digits to integers is important because computers can only understand and manipulate numbers in their binary form, which is represented by 0s and 1s. By converting digits to integers, we are providing the computer with a way to understand and perform calculations on numerical data.

What are the different ways to program digits to integers?

There are multiple ways to program digits to integers, depending on the programming language and the specific task at hand. Some common methods include using the "atoi" function, the "parseint" method, or manually converting the digits to integers using mathematical operations.

What are some common errors to watch out for when programming digits to integers?

One common error is trying to convert a string of characters that cannot be interpreted as a number, which will result in an error. It is important to check for input validation and handle potential errors when converting digits to integers.

How can "Programming Digits to Integers" be applied in real-world scenarios?

Converting digits to integers is essential in many real-world scenarios, such as financial transactions, data analysis, and scientific calculations. It allows for precise and efficient manipulation of numerical data in various fields, including finance, engineering, and computer science.

Similar threads

Replies
4
Views
2K
Replies
8
Views
1K
Replies
2
Views
1K
Replies
11
Views
3K
Replies
6
Views
1K
Replies
4
Views
4K
Back
Top