# Thread: how can I do this withhout arrays or pointers

1. ## how can I do this withhout arrays or pointers

Hello,

I have this exercise :

Make a programm where the user enters a number and the programm reverse the number.
I must work with 2 ,3, 4 or more numbers.

Hint : Use a do loop that repeatly divides with 10 stopping when it reaches 0.

Now im thinking how I can store the numbers.
Normally I would use a array but that's chapter 8 and im working in chapter 5.
Pointers are later discussed in the book.

Now I wonder how I can store the numbers. I can make a variable number 1 , number2 and so on but I don't know how big the number is that the user enters.

Anyone with a idea ?

Roelof

2. Do you need to store more than one number at a time? I do not see that requirement in the exercise. If not you can just use a loop to read each number and an inner loop to reverse it, then overwrite the variable with the next number.

3. Originally Posted by roelof
Hello,

I have this exercise :

Make a programm where the user enters a number and the programm reverse the number.
I must work with 2 ,3, 4 or more numbers.

Hint : Use a do loop that repeatly divides with 10 stopping when it reaches 0.

Now im thinking how I can store the numbers.
Normally I would use a array but that's chapter 8 and im working in chapter 5.
Pointers are later discussed in the book.

Now I wonder how I can store the numbers. I can make a variable number 1 , number2 and so on but I don't know how big the number is that the user enters.

Anyone with a idea ?

Roelof
Hey Roelof... think back to the problem when you were trying to break a number down to binary, and it kept printing out backwards... Your answer is there... (actually this is a real easy one...)

4. Hello,

I have looked back and use there a array of chars. char[++] = number%2
I wonder if this can be working without a array because in this nook array are not described.
I agree with a array it's a real easy one.

Roelof

5. Originally Posted by roelof
Hello,

I have looked back and use there a array of chars. char[++] = number%2
I wonder if this can be working without a array because in this nook array are not described.
I agree with a array it's a real easy one.

Roelof
Recall, the array was the FIX you used to stop it from printing backwards... but isn't that what you want this time... reverse printing?

Seriously... You should be able to do this in less than 10 lines...

6. Hello Commontator,

You are right.
I was thinking way to far.
This is mu solution :
Code:
#include        <stdio.h>
#include        <stdlib.h>

int
main ( int argc, char *argv[] )
{
int number;

printf("Enter a number : ");
scanf ("%d", &number);
printf ("Het nummer %d is omgekeerd : ", number);
do
{
printf ("%d", number%10);
number = number/10 ;
}
while (number !=0) ;

return 0 ;
}                               /* ----------  end of function main  ---------- */
Roelof

7. Yep... overthinking is a constant risk in programming... The simplest solution is usually the best.

Just one comment... I see you've taken to putting the return types of functions on a separate line... as you did with int and main ... This is not wrong from the standpoint of the compiler, it actually doesn't care. But you may want to think about whether it's confusing to humans who may have to read your source code at some point. Clarity does matter. The whole return value on it's own line thing is a linux trick that gives way to an inherrent weakness in the grep search utility... so I'd suggest giving a little thought to which is more important... readable code or grep's weaknesses...

Nice job on the assignment...

8. Oke,

I think I choose for readable code.
I will look if I can adapt the template that the vim plugin uses.

Roelof

Roelof

9. >>..own line thing is a linux trick that gives way to an inherrent weakness in the grep search utility
I can't find which post of OP mentioned grep ??
Ctags - Wikipedia, the free encyclopedia
http://www.thegeekstuff.com/2009/04/...-code-browser/

10. Originally Posted by CommonTater
Just one comment... I see you've taken to putting the return types of functions on a separate line... as you did with int and main ... This is not wrong from the standpoint of the compiler, it actually doesn't care. But you may want to think about whether it's confusing to humans who may have to read your source code at some point. Clarity does matter. The whole return value on it's own line thing is a linux trick that gives way to an inherrent weakness in the grep search utility... so I'd suggest giving a little thought to which is more important... readable code or grep's weaknesses...
I think the readability of it subjective. What's important is knowing why you're doing it the way you're doing it, not just because you saw some other code written that way and decided to emulate it.

After twenty years I find that my eyes tend to filter away the style and I see the code directly, so this sort of thing isn't a big deal for me. What I do have trouble with is changing the style I write the code (for instance, to match the style of some code I'm modifying). I don't have an emotional attachment to my style but it's pretty much hard-wired and it takes mental effort to go against it.

11. There're tools like indent that can format your code.

12. Originally Posted by Bayint Naung
>>..own line thing is a linux trick that gives way to an inherrent weakness in the grep search utility
I can't find which post of OP mentioned grep ??
Ctags - Wikipedia, the free encyclopedia
Ctags and Taglist: Convert Vim Editor to Beautiful Source Code Browser for Any Programming Language
He didn't... I did... I noticed he's using a formatting style that several here have touted as allowing "faster searching with grep". I merely commented that for some (like me) this is slightly confusing... althought the compiler doesn't care a whit. I suggested he give some thought to it before settling on it...

Why is this a problem?

13. For me there is no problem

Roelof