# Pretty new to C and am looking for some help

Printable View

• 10-06-2003
CrazyCanuck
Pretty new to C and am looking for some help
I guess to start i will post what my teacher is wanting me to program...

Note that leading zeros are NOT printed. For example, the number 02244 is NOT printed as zero two two four four. The correct output is two two four four. However, the value for the number 0 is printed as a single zero. The words printed out are separated by single space. Here is the algorithm in pseudo-code (variables in italics):
Code:

main()
print instructions to the user
get number to print and store as inputNumber
if inputNumber is zero
then print the word "zero"
otherwise {
if inputNumber is less than zero
then        print the word "minus"
make the number positive
set remainder to inputNumber
set divisor to 10,000
loop until divisor is zero {
if inputNumber is greater than or equal to divisor
then        set digit to remainder divided by divisor
set remainder to remainder MOD divisor
pass digit to function PrintDigit()
set divisor to divisor divided by 10
}
}
PrintDigit()
switch on digit {
case 0:        print "zero"
case 1:        print "one"
.
.
.
case 9:        print "nine"
default:        print " unknown digit detected "
}

Your program MUST:
---Use the given pseudo-code algorithm DO NOT invent your own algorithm. No marks will be given if you diverge from the given algorithm.
---use "scanf() to read in the numbers from the user.

---------------------

So basically I have to turn that all into code, but im kinda stuck and cant figure it out. Ive got it all done up until it asks me to pass digit to function printdigit. After that point im kinda messed up, im not sure how to do that and set up the switch.

Any help to point me in the right direction would be much appreciated.
• 10-07-2003
quzah
Welcome to the board. It's best to read the posts at the top, which cover the FAQ, how to use code tags, and the general rules, if you haven't done so. (It looks like you did some at least, you actually used code tags, which is more than most.)

You should next post your attempt at the code, and where you're having problems. From your post, I'm just guessing that you're having problems with the way a switch works.

Do you know how a switch works? If not, I'll show you real quick:
Code:

switch( some_integer )
{
case some_integer_value:
one_or_more_statments;
break; /* optional, required if you don't want cascading cases. You don't in your example */
case another_value:
...repeat...
}

In your case, you'll want to be passing it a single digit. Thus, you'll need a total of 11 statements. (You'll want one for each single digit numerical value, and one for the default one, which will be your error message if they pass something other than 0 through 9.)

If you don't know how to pass variables by value to functions, it goes something like this:
Code:

void myfunction( int argument )
{
...do stuff with argument...
}

... stuff ...

int x = 4;
myfunction( x ); /* valid */
myfunction( 9 ); /* valid */
myfunction( 4.5 ); /* not valid, not an int */
myfunction( 'a' ); /* valid, the value of 'a' evaluates to an integer */

Have fun.

Quzah.
• 10-07-2003
CrazyCanuck
Thanks

Heres the code I have done so far, I think most of its right but without getting the switch working I havent actually been able to test it. Hopefully with you tips in your last post I will be able to figure this stuff out. Not sure why I always mess up writing my own code but usually dont have problems reading other peoples code and fixing there errors.

Code:

#include <stdio.h>

void PrintDigit();
void main(void)
{
int inputNumber, remainder, divisor, digit;  // Declaring variables

printf("Please enter a number:\t"); // Have user enter in a number
scanf("%d", &inputNumber);

if (inputNumber == 0)        // If 0 is entered display the word zero
printf("Zero");

else {
if (inputNumber < 0) // Check to see if the value entered is less then 0
printf("Minus");
inputNumber = (inputNumber * -1);  // Change value to a positive

remainder = inputNumber;
divisor = 10000;

while (divisor > 0){
if (inputNumber >= divisor)
digit = remainder / divisor;
remainder = remainder % divisor;
// Here is where im supposed to pass digit to printdigit function
divisor = divisor /10;}
}

// Switch
PrintDigit();        // working and getting this line right
switch () { // along with  this line
case 0:        printf("zero ");
break;
case 1:        printf("one ");
break;
case 2:        printf("two ");
break;
case 3:        printf("three ");
break;
case 4:        printf("four ");
break;
case 5:        printf("five ");
break;
case 6:        printf("six ");
break;
case 7:        printf("seven ");
break;
case 8:        printf("eight ");
break;
case 9:        printf("nine ");
break;
default:        printf(" unknown digit detected ");
break;}
}

• 10-07-2003
DrZoidberg
You forgot to set a few { and }.

Code:

#include <stdio.h>

void PrintDigit();
void main(void)
{
int inputNumber, remainder, divisor, digit;  // Declaring variables

printf("Please enter a number:\t"); // Have user enter in a number
scanf("%d", &inputNumber);

if (inputNumber == 0)        // If 0 is entered display the word zero
printf("Zero");

else {
if (inputNumber < 0) // Check to see if the value entered is less then 0
{
printf("Minus");
inputNumber = (inputNumber * -1);  // Change value to a positive
}

remainder = inputNumber;
divisor = 10000;

while (divisor > 0){
if (inputNumber >= divisor)
{
digit = remainder / divisor;
remainder = remainder % divisor;
PrintDigit(digit);
}
divisor = divisor /10;}
}

}

void PrintDigit(int digit) {
switch (digit) {
case 0:        printf("zero ");
break;
case 1:        printf("one ");
break;
case 2:        printf("two ");
break;
case 3:        printf("three ");
break;
case 4:        printf("four ");
break;
case 5:        printf("five ");
break;
case 6:        printf("six ");
break;
case 7:        printf("seven ");
break;
case 8:        printf("eight ");
break;
case 9:        printf("nine ");
break;
default:        printf(" unknown digit detected ");
break;}

}