How do I "read-in" numbers from the left?

G

gk245

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.
 
S

serrand

gk245 said:
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.

if you want to read digits from left to right, c provides recursives functions...

Xavier
 
G

gk245

if you want to read digits from left to right, c provides recursives
functions...

Xavier

Well, thats the thing. I am not allowed to use those functions, only
printf, scanf, switch, and if-else statments.
 
S

serrand

gk245 said:
Well, thats the thing. I am not allowed to use those functions, only
printf, scanf, switch, and if-else statments.

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
 
S

serrand

gk245 said:
Well, thats the thing. I am not allowed to use those functions, only
printf, scanf, switch, and if-else statments.

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
 
J

Jaspreet

gk245 said:
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.

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.
 
G

gk245

Jaspreet expressed precisely :
gk245 said:
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.

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.

Hmm, %s i might be able to use somehow. However, i can't use arrays.
:-(.
 
J

Jaspreet

gk245 said:
Jaspreet expressed precisely :
gk245 said:
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.

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.

Hmm, %s i might be able to use somehow. However, i can't use arrays.
:-(.

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.
 
S

stathis gotsis

Jaspreet said:
Now if you want that character as a numeral digit you could do
str[index]-'0' (Assuming ASCII).

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.
 
S

stathis gotsis

gk245 said:
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.

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.
 
P

pete

Jaspreet wrote:
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).

ASCII is irrelevant to the validity of the
(str[index]-'0') expression.
 
S

serrand

gk245 said:
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.

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
 
G

gk245

serrand formulated the question :
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

wow, thats impressive. 8-o
 
J

Jaspreet

pete said:
Jaspreet said:
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).

ASCII is irrelevant to the validity of the
(str[index]-'0') expression.

Yes realised after I had posted. Not sure why I wrote "assuming ASCII".
Apologies for that.
 
S

Sirius Black

gk245 said:
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.

#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;
}
 
G

gk245

Sirius Black has brought this to us :
#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;
}

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.
 
J

Jonas Raoni

gk245 said:
Sirius Black has brought this to us :
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.

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;
}
 
P

Pedro Graca

gk245 said:
Sirius Black has brought this to us :

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.


"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 |
------------------------+------+--------+-------+
 
G

gk245

Pedro Graca formulated on Wednesday :
"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 |
------------------------+------+--------+-------+

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.

^^
 
D

Dave Thompson

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);
}
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.
int main(void)
{
unsigned int number;

printf("Enter numbers: ");
scanf ("%d", &number);
#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
printdec(number);
printf("\n");

This could also be a putchar ('\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.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top