1

As homework, I must swap letters in a given string. I already figured out how to do this, but not how to display them at once. it involves a for loop. so if I include disp x in the for loop, it displays them between parentheses and a space, but I want them all together, so instead of

"a"

"b"

"c"

I want "abc". Is there a way to do this? Should I push the variable into an array and then display the array after the for loop? How to push variables in to an array?

This is in TI-Nspire CX Cas btw.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
steedsnisps
  • 125
  • 1
  • 4

3 Answers3

3

To add an element x to an array A use augment(A, {x}).

For your specific case, I would use a string variable (call it string) to which I concatenate the next letter at each iteration of the for loop. So if the next letter to be added is in the variable letter, you would put the following line of code at the end of your for loop: string := string & letter.

PGmath
  • 381
  • 3
  • 16
1

here is also way:

Local array array[dim(array)+1] := value

SamSol
  • 2,885
  • 6
  • 37
  • 56
0

I would answer you by an example covering your scenario. Let's say we are aiming to have a array listing the elements of binaries when we construct an integer into the base2 (binary).

Define LibPub develope(a,b)=
Func
Local mi,m,q
mi:=mod(a,b)
q:=((a-mi)/(b))
Disp mi
While q≥b
  a:=q
  m:=mod(a,b)
  q:=((a-m)/(b))
  Disp m
EndWhile
EndFunc

The above small program develops an integer in decimal base into the binary base; however each binary is shown in a separate line as you mentioned: ex:

develope(222,2)
0
1
1
1
1
0
1

enter image description here

but this is not what you want, you want is in a single line. IMPORTANCE IS THAT YOU SHOULD LIKELIHOOD WANT EACH ELEMENT BE ACCESSIBLE AS A SEPARATE INTEGER, RIGHT? LIKE AS AN ELEMENT IN A ARRAY LIST, THAT'S WHAT YOU LOOKING FOR RIGHT?

There we Go:

    Define LibPub develope(n,b)=
Func
Local q,k,seti,set,valid
valid:=b
If valid>1 Then
  q:=n
  k:=0
  set:={}
While q≠0
    seti:=mod(q,b)
    q:=int(((q)/(b)))
    k:=k+1
    seti→set[k]
EndWhile
Else
  Disp "Erreur, La base doit être plus grand que 1."
EndIf
Return set
EndFunc

Basically, because we do not know how many elements are going to be added in the array list, the set:={} declares an array with an undefined dim (typically length) in order that dynamically be augmented.

The command seti→set[k] will add the value of the seti whatever it is, into the k position of the array list.

and the return set simply returns the array.

if you need to get access to a specific element, you know how to to that: elementNumber5:=set[5]

I wish it helps.

Saffa Seraj
  • 77
  • 1
  • 8