Like Tree1Likes
  • 1 Post By brewbuck

how can I do this withhout arrays or pointers

This is a discussion on how can I do this withhout arrays or pointers within the C Programming forums, part of the General Programming Boards category; Hello, I have this exercise : Make a programm where the user enters a number and the programm reverse the ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    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. #2
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    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. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    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. #4
    Registered User
    Join Date
    May 2010
    Posts
    230
    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. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    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. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    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. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    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. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    >>..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/
    Last edited by Bayint Naung; 05-30-2011 at 10:20 AM.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by CommonTater View Post
    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.
    Bayint Naung likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    There're tools like indent that can format your code.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    >>..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. #13
    Registered User
    Join Date
    May 2010
    Posts
    230
    For me there is no problem

    Roelof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers Vs Arrays
    By karthik537 in forum C Programming
    Replies: 2
    Last Post: 08-05-2010, 10:36 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. Pointers or Arrays?
    By magda3227 in forum C Programming
    Replies: 3
    Last Post: 07-09-2008, 09:52 AM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  5. Pointers and arrays
    By OttoDestruct in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2004, 12:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21