Jump to content

store getint() integers to a list


samifox

Recommended Posts

Hi

As my “beginner” membership state has been void (thankfully in some philosophic way :twisted:) I was forced tolearn the ABC of autolisp , so please be kind to me :D

 

After reading help I start inventing tasks for my self-using the expressions I have learned.

Im trying to ask the user to type 5 integers and then store them into a list

 

The following code get endless loop

(I code it by my totally self :P)

 

[font=Calibri][size=3];_ ask the user to type 5 integers[/size][/font]
[size=3][font=Calibri] ;_put them into a list[/font][/size]
[font=Calibri][size=3](defun C:demo (/ i)[/size][/font]
[font=Calibri][size=3]  (setq i 1)[/size][/font]
[font=Calibri][size=3]  (while[/size][/font]
[size=3][font=Calibri]    (and[/font][/size]
[size=3][font=Calibri]      (setq lst '(getint ("Type 5 numbers")))[/font][/size]
[size=3][font=Calibri]      (<= i 5)[/font][/size]
[size=3][font=Calibri]      (setq i (1+ i))[/font][/size]
[size=3][font=Calibri]    ) ;_and[/font][/size]

[size=3][font=Calibri]  ) ;_while[/font][/size]
[size=3][font=Calibri]) ;_defun`[/font][/size]

 

Why?

Link to comment
Share on other sites

First of all:

 

(setq lst '(getint ("Type 5 numbers")))

to

(setq lst (getint "\nType 5 numbers: "))

 

And what do you mean by 5 numbers? a five digit integer? or single digit at every loop?

 

Be wary of getint on the former

Type 5 numbers: 56236

Requires an integer between -32768 and 32767.

 

So what will it be? 5 digit number or single?

Link to comment
Share on other sites

Like Pbe If you want say multiple integers eg 1 12 3456 2 34 567 then you can create a loop and when you press enter twice it will stop this way can be 1 or 5 or as many as you want also can be fixed number required also. 12345 67890 please advise.

Link to comment
Share on other sites

I want the user to enter number between 1 to 100 and then I will start to play with this list 45 56 12 64 87 . The way it will be doesn't matter as I just learning. Of you u want to show few ways to solve it god bless

Link to comment
Share on other sites

A simple method is a while loop check for nil entry

 

(setq ansint 0) ; dummy to start
(while (/= ansint nil)
(setq ansint (getint "\Enter integer press enter to stop"))
(setq intans (cons ansint intans))

)

Link to comment
Share on other sites

Sample:

 

(defun C:demo (/ i lst)
 (while
   (and
     (< (length lst) 5)
     (setq i (initget 7) i (getreal "\nEnter number from 1 to 100 : "))
           )
   (if
     (<= i 100)
     (print (Setq lst (cons (fix i) lst)))
     (princ "\n<<Number out of range>>")
     )
   )
 lst
 (princ)
 )

 

Why getreal you ask? you will know if ever you want to be prompted for large numbers. :)

 

HTH

Link to comment
Share on other sites

I would suggest perhaps:

(defun c:5num ( / i l n )
   (setq i 1)
   (while
       (and
           (< i 6)
           (setq n (1-100 (strcat "\nEnter number " (itoa i) ": ")))
       )
       (setq i (1+ i)
             l (cons n l)
       )
   )
   (reverse l)
)

(defun 1-100 ( m / n )
   (while
       (and
           (setq n (getint m))
           (not (<= 1 n 100))
       )
       (princ "\nNumber must be between 1 and 100.")
   )
   n
)

Link to comment
Share on other sites

A simple method is a while loop check for nil entry

 

(setq ansint 0) ; dummy to start
(while (/= ansint nil)
(setq ansint (getint "\Enter integer press enter to stop"))
(setq intans (cons ansint intans))



)

its the same as the while getint() does, is it not?

Link to comment
Share on other sites

Only 5 digit numbers : [for learning purposes]

 

(defun C:demo2 (/ i lst)
 (while
   (and
     (< (length lst) 5)
     (setq i (initget 7)
           i (getreal "\nEnter 5 digit number: "))
     )
   (if
     (< [color="blue"]9999[/color] i 100000)
     (print (Setq lst (cons (fix i) lst)))
     (princ "\n<<Number out of range>>")
     )
   )
 lst
 (princ)
 )

Edited by pBe
number doesnt compute
Link to comment
Share on other sites

ahhhh yes numbers will be the death of me LM :lol:

 

:)

 

Here is another way to determine the number of digits:

(defun c:5dig ( / d n )
   (while
       (and
           (setq n (getreal "\nEnter a 5-digit number: "))
           (/= 5 (setq d (digits (setq n (fix n)))))
       )
       (princ (strcat "\n" (itoa n) " has " (itoa d) " digits."))
   )
   n
)

(defun digits ( n )
   (1+ (fix (/ (log n) (log 10))))
)

Link to comment
Share on other sites

(defun digits ( n )
   (1+ (fix (/ (log n) (log 10))))
)

 

Very nice Lee :thumbsup: I knew there's a math solution to that besides using the usual operators.

 

Can you explain the function log in a way everyone else can understand it?

Link to comment
Share on other sites

Very nice Lee :thumbsup: I knew there's a math solution to that besides using the usual operators.

 

Cheers pBe :)

 

Can you explain the function log in a way everyone else can understand it?

 

How long have you got? :lol:

 

To briefly explain, the AutoLISP log function returns the natural logarithm (usually written 'ln') of the supplied number, that is, the logarithm with base e (2.718...), or the inverse exponential function.

 

For logarithms in general we have:

log_a(b) = x  =>  a^x = b

That is, the logarithm base a of b results in a number such that a raised to that number will equal b.

 

In my function, I am calculating a value x such that:

n = 10^x

Using a logarithm of base 10 this can be easily calculated (since log_x(x) = 1):

n = 10^x
log_10(n) = log_10(10^x)
log_10(n) = x(log_10(10))

=> x = log_10(n)

However, since we only have access to the natural logarithm function (log), we must proceed in the following way:

n = 10^x
log(n) = log(10^x)
log(n) = x(log(10))

=> x = log(n)/log(10)

Link to comment
Share on other sites

 

To briefly explain, the AutoLISP log function returns the natural logarithm (usually written 'ln') of the supplied number, that is, the logarithm with base e (2.718...), or the inverse exponential function....

 

Excellent, I may not know what that means now but in time I will. :) Thank you Lee, appreciate the lesson.

 

Cheers :beer:

Link to comment
Share on other sites

Excellent, I may not know what that means now but in time I will. :) Thank you Lee, appreciate the lesson.

 

Cheers :beer:

 

You're very welcome.

 

There's far too much information on logarithms than could be explained in a forum post, but if you are interested in learning more, the Khan Academy have a whole section on the topic:

 

https://www.khanacademy.org/math/algebra/logarithms-tutorial

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...