How do I "read-in" numbers from the left? | | |
I have something like this:
printf("Enter numbers: ");
scanf ("%i", &number);
If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:
right_number = number % 10;
/* print the number */
number = number / 10;
Well, i have the above setup in a loop, so i keep getting the right
most digit printed out. However, they're all backwards since i am
reading in the right-most digit first. How do i make it so it reads
the left-most first, and then proceeds to print? Or is there a way to
flip the right most ones around afterwards?
I can only use printf, scanf and switch functions right now.
Thx. | | | | re: How do I "read-in" numbers from the left?
gk245 wrote:[color=blue]
> I have something like this:
>
> printf("Enter numbers: ");
> scanf ("%i", &number);
>
> If the user put in a bunch of numbers (like 4568), instead of just one
> number, i know that you can extract the right most digit with this:
>
> right_number = number % 10;
> /* print the number */
> number = number / 10;
>
> Well, i have the above setup in a loop, so i keep getting the right most
> digit printed out. However, they're all backwards since i am reading in
> the right-most digit first. How do i make it so it reads the left-most
> first, and then proceeds to print? Or is there a way to flip the right
> most ones around afterwards?
>
> I can only use printf, scanf and switch functions right now.
>
> Thx.
>
>[/color]
if you want to read digits from left to right, c provides recursives functions...
Xavier | | | | re: How do I "read-in" numbers from the left?
on 2/20/2006, serrand supposed :[color=blue]
> gk245 wrote:[color=green]
>> I have something like this:
>>
>> printf("Enter numbers: ");
>> scanf ("%i", &number);
>>
>> If the user put in a bunch of numbers (like 4568), instead of just one
>> number, i know that you can extract the right most digit with this:
>>
>> right_number = number % 10;
>> /* print the number */
>> number = number / 10;
>>
>> Well, i have the above setup in a loop, so i keep getting the right most
>> digit printed out. However, they're all backwards since i am reading in
>> the right-most digit first. How do i make it so it reads the left-most
>> first, and then proceeds to print? Or is there a way to flip the right most
>> ones around afterwards?
>>
>> I can only use printf, scanf and switch functions right now.
>>
>> Thx.
>>
>>[/color]
>
> if you want to read digits from left to right, c provides recursives
> functions...
>
> Xavier[/color]
Well, thats the thing. I am not allowed to use those functions, only
printf, scanf, switch, and if-else statments. | | | | re: How do I "read-in" numbers from the left?
gk245 wrote:[color=blue]
> on 2/20/2006, serrand supposed :
>[color=green]
>> gk245 wrote:
>>[color=darkred]
>>> I have something like this:
>>>
>>> printf("Enter numbers: ");
>>> scanf ("%i", &number);
>>>
>>> If the user put in a bunch of numbers (like 4568), instead of just
>>> one number, i know that you can extract the right most digit with this:
>>>
>>> right_number = number % 10;
>>> /* print the number */
>>> number = number / 10;
>>>
>>> Well, i have the above setup in a loop, so i keep getting the right
>>> most digit printed out. However, they're all backwards since i am
>>> reading in the right-most digit first. How do i make it so it reads
>>> the left-most first, and then proceeds to print? Or is there a way to
>>> flip the right most ones around afterwards?
>>>
>>> I can only use printf, scanf and switch functions right now.
>>>
>>> Thx.
>>>
>>>[/color]
>>
>> if you want to read digits from left to right, c provides recursives
>> functions...
>>
>> Xavier[/color]
>
>
> Well, thats the thing. I am not allowed to use those functions, only
> printf, scanf, switch, and if-else statments.
>
>[/color]
then you can try something like
....
if ( (int)(n/100000) != 0) printf ("%d", (int)(n/100000));
if ( (int)(n/10000) != 0) ...
....
not very nice... but it works with your req ...
Despite... nothing to do with standard c ...
Xavier | | | | re: How do I "read-in" numbers from the left?
gk245 wrote:[color=blue]
> on 2/20/2006, serrand supposed :
>[color=green]
>> gk245 wrote:
>>[color=darkred]
>>> I have something like this:
>>>
>>> printf("Enter numbers: ");
>>> scanf ("%i", &number);
>>>
>>> If the user put in a bunch of numbers (like 4568), instead of just
>>> one number, i know that you can extract the right most digit with this:
>>>
>>> right_number = number % 10;
>>> /* print the number */
>>> number = number / 10;
>>>
>>> Well, i have the above setup in a loop, so i keep getting the right
>>> most digit printed out. However, they're all backwards since i am
>>> reading in the right-most digit first. How do i make it so it reads
>>> the left-most first, and then proceeds to print? Or is there a way to
>>> flip the right most ones around afterwards?
>>>
>>> I can only use printf, scanf and switch functions right now.
>>>
>>> Thx.
>>>
>>>[/color]
>>
>> if you want to read digits from left to right, c provides recursives
>> functions...
>>
>> Xavier[/color]
>
>
> Well, thats the thing. I am not allowed to use those functions, only
> printf, scanf, switch, and if-else statments.
>
>[/color]
it seeems to bee an algorithmic problem...
Find the left most digit then proceed...
if ((int)(n/100...0) != 0)
{
/**/ then extract one by one digit with substractions ...
}
else
better :
k = scanf ("%c%c%c%c%c%c%c%c%c", &n);
k == number of digits... then go on
Xavier | | | | re: How do I "read-in" numbers from the left?
gk245 wrote:[color=blue]
> I have something like this:
>
> printf("Enter numbers: ");
> scanf ("%i", &number);
>
> If the user put in a bunch of numbers (like 4568), instead of just one
> number, i know that you can extract the right most digit with this:
>
> right_number = number % 10;
> /* print the number */
> number = number / 10;
>
> Well, i have the above setup in a loop, so i keep getting the right
> most digit printed out. However, they're all backwards since i am
> reading in the right-most digit first. How do i make it so it reads
> the left-most first, and then proceeds to print? Or is there a way to
> flip the right most ones around afterwards?
>
> I can only use printf, scanf and switch functions right now.
>
> Thx.[/color]
It seems you could input the number as a string using scanf() even
though using scanf has a bug (but you cannot use fgets). Once you have
the input, start accessing the left most number using index 0, 1, and
so on..
So, the code snippet could be:
scanf("%s", str);
str[0] = .... /* To acess the first digit */
Now if you want that character as a numeral digit you could do
str[index]-'0' (Assuming ASCII).
Hope that helps. | | | | re: How do I "read-in" numbers from the left?
Jaspreet expressed precisely :[color=blue]
> gk245 wrote:[color=green]
>> I have something like this:
>>
>> printf("Enter numbers: ");
>> scanf ("%i", &number);
>>
>> If the user put in a bunch of numbers (like 4568), instead of just one
>> number, i know that you can extract the right most digit with this:
>>
>> right_number = number % 10;
>> /* print the number */
>> number = number / 10;
>>
>> Well, i have the above setup in a loop, so i keep getting the right
>> most digit printed out. However, they're all backwards since i am
>> reading in the right-most digit first. How do i make it so it reads
>> the left-most first, and then proceeds to print? Or is there a way to
>> flip the right most ones around afterwards?
>>
>> I can only use printf, scanf and switch functions right now.
>>
>> Thx.[/color]
>
> It seems you could input the number as a string using scanf() even
> though using scanf has a bug (but you cannot use fgets). Once you have
> the input, start accessing the left most number using index 0, 1, and
> so on..
>
> So, the code snippet could be:
>
> scanf("%s", str);
> str[0] = .... /* To acess the first digit */
>
> Now if you want that character as a numeral digit you could do
> str[index]-'0' (Assuming ASCII).
>
> Hope that helps.[/color]
Hmm, %s i might be able to use somehow. However, i can't use arrays.
:-(. | | | | re: How do I "read-in" numbers from the left?
gk245 wrote:[color=blue]
> Jaspreet expressed precisely :[color=green]
> > gk245 wrote:[color=darkred]
> >> I have something like this:
> >>
> >> printf("Enter numbers: ");
> >> scanf ("%i", &number);
> >>
> >> If the user put in a bunch of numbers (like 4568), instead of just one
> >> number, i know that you can extract the right most digit with this:
> >>
> >> right_number = number % 10;
> >> /* print the number */
> >> number = number / 10;
> >>
> >> Well, i have the above setup in a loop, so i keep getting the right
> >> most digit printed out. However, they're all backwards since i am
> >> reading in the right-most digit first. How do i make it so it reads
> >> the left-most first, and then proceeds to print? Or is there a way to
> >> flip the right most ones around afterwards?
> >>
> >> I can only use printf, scanf and switch functions right now.
> >>
> >> Thx.[/color]
> >
> > It seems you could input the number as a string using scanf() even
> > though using scanf has a bug (but you cannot use fgets). Once you have
> > the input, start accessing the left most number using index 0, 1, and
> > so on..
> >
> > So, the code snippet could be:
> >
> > scanf("%s", str);
> > str[0] = .... /* To acess the first digit */
> >
> > Now if you want that character as a numeral digit you could do
> > str[index]-'0' (Assuming ASCII).
> >
> > Hope that helps.[/color]
>
> Hmm, %s i might be able to use somehow. However, i can't use arrays.
> :-(.[/color]
You surely have lots of restrictions in place. If you cannot use
arrays, then I guess Xavier did give a solution in a previous post. You
could input the number character by character and then try and use them
to solve your problem. | | | | re: How do I "read-in" numbers from the left?
"Jaspreet" <jsingh.oberoi@gmail.com> wrote in message
news:1140480229.168954.251910@g47g2000cwa.googlegr oups.com...[color=blue]
> Now if you want that character as a numeral digit you could do
> str[index]-'0' (Assuming ASCII).[/color]
As people pointed out to me in some other thread, this does not apply just
to ASCII. It is generally true because the Standard ensures it. | | | | re: How do I "read-in" numbers from the left?
"gk245" <topsoil@mail.com> wrote in message
news:mn.a4067d627f0b04be.49793@mail.com...[color=blue]
> I have something like this:
>
> printf("Enter numbers: ");
> scanf ("%i", &number);
>
> If the user put in a bunch of numbers (like 4568), instead of just one
> number, i know that you can extract the right most digit with this:
>
> right_number = number % 10;
> /* print the number */
> number = number / 10;
>
> Well, i have the above setup in a loop, so i keep getting the right
> most digit printed out. However, they're all backwards since i am
> reading in the right-most digit first. How do i make it so it reads
> the left-most first, and then proceeds to print? Or is there a way to
> flip the right most ones around afterwards?
>
> I can only use printf, scanf and switch functions right now.
>
> Thx.[/color]
I do not know if you are "allowed" this but anyway take a look, printdec()
is recursive:
#include <stdio.h>
void printdec(unsigned int i)
{
if (i/10) printdec(i/10);
printf("%d",i%10);
fflush(stdout);
}
int main(void)
{
unsigned int number;
printf("Enter numbers: ");
scanf ("%d", &number);
printdec(number);
printf("\n");
return 0;
}
It can print non-negative numbers in their decimal representation.
I tried to keep your own code too, even if it is not that safe. You can work
out the rest. | | | | re: How do I "read-in" numbers from the left?
Jaspreet wrote:
[color=blue]
> It seems you could input the number as a string using scanf() even
> though using scanf has a bug (but you cannot use fgets). Once you have
> the input, start accessing the left most number using index 0, 1, and
> so on..
>
> So, the code snippet could be:
>
> scanf("%s", str);
> str[0] = .... /* To acess the first digit */
>
> Now if you want that character as a numeral digit you could do
> str[index]-'0' (Assuming ASCII).[/color]
ASCII is irrelevant to the validity of the
(str[index]-'0') expression.
--
pete | | | | re: How do I "read-in" numbers from the left?
gk245 wrote:[color=blue]
> I have something like this:
>
> printf("Enter numbers: ");
> scanf ("%i", &number);
>
> If the user put in a bunch of numbers (like 4568), instead of just one
> number, i know that you can extract the right most digit with this:
>
> right_number = number % 10;
> /* print the number */
> number = number / 10;
>
> Well, i have the above setup in a loop, so i keep getting the right most
> digit printed out. However, they're all backwards since i am reading in
> the right-most digit first. How do i make it so it reads the left-most
> first, and then proceeds to print? Or is there a way to flip the right
> most ones around afterwards?
>
> I can only use printf, scanf and switch functions right now.
>
> Thx.
>
>[/color]
from left to right or right to left, without arrays, without any extra call but scanf
int n, m, i;
char c;
n = 0;
m = 0;
i = 1;
while (scanf ("%c", &c), c != '\n')
{
n = 10*n + (c-'0');
m += i*(c-'0');
i *= 10;
}
printf ("N = %d %d\n", n, m);
Xavier | | | | re: How do I "read-in" numbers from the left?
serrand formulated the question :[color=blue]
> gk245 wrote:[color=green]
>> I have something like this:
>>
>> printf("Enter numbers: ");
>> scanf ("%i", &number);
>>
>> If the user put in a bunch of numbers (like 4568), instead of just one
>> number, i know that you can extract the right most digit with this:
>>
>> right_number = number % 10;
>> /* print the number */
>> number = number / 10;
>>
>> Well, i have the above setup in a loop, so i keep getting the right most
>> digit printed out. However, they're all backwards since i am reading in
>> the right-most digit first. How do i make it so it reads the left-most
>> first, and then proceeds to print? Or is there a way to flip the right most
>> ones around afterwards?
>>
>> I can only use printf, scanf and switch functions right now.
>>
>> Thx.
>>
>>[/color]
>
> from left to right or right to left, without arrays, without any extra call
> but scanf
>
> int n, m, i;
> char c;
> n = 0;
> m = 0;
> i = 1;
> while (scanf ("%c", &c), c != '\n')
> {
> n = 10*n + (c-'0');
> m += i*(c-'0');
> i *= 10;
> }
> printf ("N = %d %d\n", n, m);
>
> Xavier[/color]
wow, thats impressive. 8-o | | | | re: How do I "read-in" numbers from the left?
pete wrote:[color=blue]
> Jaspreet wrote:
>[color=green]
> > It seems you could input the number as a string using scanf() even
> > though using scanf has a bug (but you cannot use fgets). Once you have
> > the input, start accessing the left most number using index 0, 1, and
> > so on..
> >
> > So, the code snippet could be:
> >
> > scanf("%s", str);
> > str[0] = .... /* To acess the first digit */
> >
> > Now if you want that character as a numeral digit you could do
> > str[index]-'0' (Assuming ASCII).[/color]
>
> ASCII is irrelevant to the validity of the
> (str[index]-'0') expression.
>
> --
> pete[/color]
Yes realised after I had posted. Not sure why I wrote "assuming ASCII".
Apologies for that. | | | | re: How do I "read-in" numbers from the left?
gk245 wrote:[color=blue]
> I have something like this:
>
> printf("Enter numbers: ");
> scanf ("%i", &number);
>
> If the user put in a bunch of numbers (like 4568), instead of just one
> number, i know that you can extract the right most digit with this:
>
> right_number = number % 10;
> /* print the number */
> number = number / 10;
>
> <snip>
>
> I can only use printf, scanf and switch functions right now.
>[/color]
#include <stdio.h>
int main (void)
{
int num = 0, revnum = 0, digit = 0;
printf ("Enter a number: ");
scanf ("%i", &num);
while (num > 0)
{
digit = num % 10;
revnum = revnum*10 + digit;
num /= 10;
}
printf ("Reversed number is %d\n", revnum);
return 0;
} | | | | re: How do I "read-in" numbers from the left?
Sirius Black has brought this to us :[color=blue]
> gk245 wrote:[color=green]
>> I have something like this:
>>
>> printf("Enter numbers: ");
>> scanf ("%i", &number);
>>
>> If the user put in a bunch of numbers (like 4568), instead of just one
>> number, i know that you can extract the right most digit with this:
>>
>> right_number = number % 10;
>> /* print the number */
>> number = number / 10;
>>
>> <snip>
>>
>> I can only use printf, scanf and switch functions right now.
>>[/color]
>
> #include <stdio.h>
>
> int main (void)
> {
> int num = 0, revnum = 0, digit = 0;
>
> printf ("Enter a number: ");
> scanf ("%i", &num);
>
> while (num > 0)
> {
> digit = num % 10;
> revnum = revnum*10 + digit;
> num /= 10;
> }
>
> printf ("Reversed number is %d\n", revnum);
> return 0;
> }[/color]
I might be reading this the wrong way, but if revnum is set to zero
from the beginning, then revnum*10 will always equal zero. So the
expression 'revnum = revnum*10 + digit' is just goint to set revnum to
the digit's value..... i don't see why revnum*10 is needed since all
its saying is that 0*10 + digit.
Apologies if i am wrong...
thanks. | | | | re: How do I "read-in" numbers from the left?
gk245 wrote:[color=blue]
> Sirius Black has brought this to us :[color=green]
>> #include <stdio.h>[/color]
> I might be reading this the wrong way, but if revnum is set to zero from
> the beginning, then revnum*10 will always equal zero.[/color]
No, if you check the code right, you'll see that "revnum*10" is used to
deslocate the current number to the left, so the last digit can be setted...
This is a typical university exercise haha, I have mine one here yet,
but I quit the university in the same year =]
#include <stdio.h>
int main(int argc, char *argv[]){
int n, r = 0;
printf("\nentre com o numero: ");
scanf("%d", &n);
for(n = abs(n); n; r = r * 10 + n % 10, n /= 10);
printf("o inverso eh: %d", r);
return 0;
}
--
Jonas Raoni Soares Silva http://www.jsfromhell.com | | | | re: How do I "read-in" numbers from the left?
gk245 wrote:[color=blue]
> Sirius Black has brought this to us :[color=green]
>>
>> #include <stdio.h>
>>
>> int main (void)
>> {
>> int num = 0, revnum = 0, digit = 0;
>>
>> printf ("Enter a number: ");
>> scanf ("%i", &num);
>>
>> while (num > 0)
>> {
>> digit = num % 10;
>> revnum = revnum*10 + digit;
>> num /= 10;
>> }
>>
>> printf ("Reversed number is %d\n", revnum);
>> return 0;
>> }[/color]
>
> I might be reading this the wrong way, but if revnum is set to zero
> from the beginning, then revnum*10 will always equal zero. So the
> expression 'revnum = revnum*10 + digit' is just goint to set revnum to
> the digit's value..... i don't see why revnum*10 is needed since all
> its saying is that 0*10 + digit.[/color]
"Walk the code" with pen and paper, marking all changes of variables
code | num | revnum | digit |
------------------------+------+--------+-------+
initialization | 0 | 0 | 0 |
scanf | 4568 | 0 | 0 |
digit = num % 10 | 4568 | 0 | 8 |
revnum = ... | 4568 | 0*10+8 | 8 |
num /= 10 | 456 | 8 | 8 |
(loop)
digit = num % 10 | 456 | 8 | 6 |
revnum = ... | 456 | 8*10+6 | 6 |
num /= 10 | 45 | 86 | 6 |
(repeat ...) ... ... ...
printf | 0 | 8654 | 4 |
------------------------+------+--------+-------+
--
If you're posting through Google read <http://cfaj.freeshell.org/google> | | | | re: How do I "read-in" numbers from the left?
Pedro Graca formulated on Wednesday :[color=blue]
> gk245 wrote:[color=green]
>> Sirius Black has brought this to us :[color=darkred]
>>>
>>> #include <stdio.h>
>>>
>>> int main (void)
>>> {
>>> int num = 0, revnum = 0, digit = 0;
>>>
>>> printf ("Enter a number: ");
>>> scanf ("%i", &num);
>>>
>>> while (num > 0)
>>> {
>>> digit = num % 10;
>>> revnum = revnum*10 + digit;
>>> num /= 10;
>>> }
>>>
>>> printf ("Reversed number is %d\n", revnum);
>>> return 0;
>>> }[/color]
>>
>> I might be reading this the wrong way, but if revnum is set to zero
>> from the beginning, then revnum*10 will always equal zero. So the
>> expression 'revnum = revnum*10 + digit' is just goint to set revnum to
>> the digit's value..... i don't see why revnum*10 is needed since all
>> its saying is that 0*10 + digit.[/color]
>
>
> "Walk the code" with pen and paper, marking all changes of variables
>
> code | num | revnum | digit |
> ------------------------+------+--------+-------+
> initialization | 0 | 0 | 0 |
> scanf | 4568 | 0 | 0 |
> digit = num % 10 | 4568 | 0 | 8 |
> revnum = ... | 4568 | 0*10+8 | 8 |
> num /= 10 | 456 | 8 | 8 |
> (loop)
> digit = num % 10 | 456 | 8 | 6 |
> revnum = ... | 456 | 8*10+6 | 6 |
> num /= 10 | 45 | 86 | 6 |
> (repeat ...) ... ... ...
> printf | 0 | 8654 | 4 |
> ------------------------+------+--------+-------+[/color]
doh...i forgot that it was adding the digit, was just looking at revnum
= revnum * 10.
Thanks, i like the table approach. Will use it quite a bit, i am sure.
^^ | | | | re: How do I "read-in" numbers from the left?
On Tue, 21 Feb 2006 05:38:56 +0200, "stathis gotsis"
<stathisgotsis@hotmail.com> wrote:
[color=blue]
> I do not know if you are "allowed" this but anyway take a look, printdec()
> is recursive:
>
> #include <stdio.h>
>
> void printdec(unsigned int i)
> {
> if (i/10) printdec(i/10);
> printf("%d",i%10);
> fflush(stdout);
> }
>[/color]
If you have printf (with %u or probably %d) you don't need your
recursive logic. But all you need is putchar ('0' + i%10).
Doing the fflush() for each digit is probably wasteful.
[color=blue]
> int main(void)
> {
> unsigned int number;
>
> printf("Enter numbers: ");
> scanf ("%d", &number);
>[/color]
#if stdclc
Prompt not ending with newline (and perhaps even with) is not strictly
guaranteed to appear if you don't fflush(stdout).
%d is technically incorrect for unsigned int, use %u.
Or use an int variable; it will converted on the call next.
#endif
[color=blue]
> printdec(number);
> printf("\n");[/color]
This could also be a putchar ('\n').
[color=blue]
>
> return 0;
> }
>
> It can print non-negative numbers in their decimal representation.
> I tried to keep your own code too, even if it is not that safe. You can work
> out the rest.
>[/color]
- David.Thompson1 at worldnet.att.net |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|