# How to augment an \`Int64\` 1-D array or vector

**URL:** <https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729>\
**Category:** New to Julia\
**Tags:** array, vector, push\
**Created:** [December 17, 2023, 7:57am UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729 "2023-12-17T07:57:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chandra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chandra/32/207807_2.png) [@chandra](https://discourse.julialang.org/u/chandra)\
**Post date:** [December 17, 2023, 7:57am UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729/1 "2023-12-17T07:57:16Z")

</div>

```julia
function continued_fraction(num, denom)
    a = []
    quo, rem = divrem(num, denom)
    a0 = quo
    push!(a, a0)
    num = denom
    denom = rem
    i = 1
    while (rem != 0)
        quo, rem = divrem(num, denom)
        push!(a, quo)
        num = denom
        denom = rem
        i += 1
    end
    return(a)
end

a = continued_fraction(1001, 64)
println(a)

```

I am experimenting writing continued fraction functions; at present restricted to integers, as shown above.

I got the above to work, but I want the array or vector `a` to be an integer so that I do not get the `Any` in front of the output.

But I have found that `push!` does not work with an `Int64` array.

Kindly help me improve the above code.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 17, 2023, 8:03am UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729/2 "2023-12-17T08:03:18Z")

</div>

`[]` is equivalant to `Any[]` and creates an empty vector that can hold values of any type. `Int[]` creates an empty vector that can hold values of type `Int`.

It is a bit unclear what you tried that didn’t work, but your code runs if you change `[]` to `Int[]` at least.

---

<div class="post-metadata">

**Author:** ![chandra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chandra/32/207807_2.png) [@chandra](https://discourse.julialang.org/u/chandra)\
**Post date:** [December 17, 2023, 8:07am UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729/3 "2023-12-17T08:07:54Z")

</div>

What is the difference between using `Int` and `Int64`?

I tried `a = Array{Int64, 1}` and got into strife with `push!` which was why I raised the query.

On another note, is there a neat way to change the first comma in the output to a semi-colon?

Thank you.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 17, 2023, 8:11am UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729/4 "2023-12-17T08:11:43Z")

</div>

> [@chandra](#):
>
> What is the difference between using `Int` and `Int64`?

`Int` is just an alias for your native integer type, on most systems this is equivalent to `Int64` (64-bit integer).

> [@chandra](#):
>
> I tried `a = Array{Int64, 1}` and got into strife with `push!` which was why I raised the query.

That is a _type_ not a value with that type. You could use `Array{Int, 1}()`, or the equivalent `Vector{Int}()`, but using `Int[]` is the most common method I think.

> [@chandra](#):
>
> On another note, is there a neat way to change the first comma in the output to a semi-colon?

I don’t know what this means – there are no commas printed in my output:

```julia
julia> a = continued_fraction(1001, 64)
8-element Vector{Int64}:
 15
  1
  1
  1
  3
  1
  1
  2

```

---

<div class="post-metadata">

**Author:** ![chandra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chandra/32/207807_2.png) [@chandra](https://discourse.julialang.org/u/chandra)\
**Post date:** [December 17, 2023, 8:44am UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729/7 "2023-12-17T08:44:02Z")

</div>

> [@fredrikekre](#):
>
> I don’t know what this means – there are no commas printed in my output:

The output from my above code, using a file rather than the REPL, gives me a _row_ vector:  
`[15, 1, 1, 1, 3, 1, 1, 2]`

If I can change the first comma to a semi-colon, keeping the other commas intact, the output will be in one of the standard ways to express continued fractions.

What I am seeking is `[15; 1, 1, 1, 3, 1, 1, 2]`

---

<div class="post-metadata">

**Author:** ![chandra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chandra/32/207807_2.png) [@chandra](https://discourse.julialang.org/u/chandra)\
**Post date:** [December 17, 2023, 9:49am UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729/8 "2023-12-17T09:49:06Z")

</div>

`println("[$(a[1]); " * join(a[2:end], ", ") * "]")` seems to do the trick.

If there are better ways, please advise.

---

<div class="post-metadata">

**Author:** ![gvdr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gvdr/32/6387_2.png) [@gvdr](https://discourse.julialang.org/u/gvdr)\
**Post date:** [December 17, 2023, 9:54am UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729/9 "2023-12-17T09:54:30Z")

</div>

Are you trying to show that array like that? Or to also enable the user to define a continues fraction through that syntax

In any case, defining your own struct and extending a method for showing it seems a robust solution.

---

<div class="post-metadata">

**Author:** ![chandra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chandra/32/207807_2.png) [@chandra](https://discourse.julialang.org/u/chandra)\
**Post date:** [December 17, 2023, 10:22am UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729/10 "2023-12-17T10:22:10Z")

</div>

> [@gvdr](#):
>
> Are you trying to show that array like that? Or to also enable the user to define a continues fraction through that syntax

Initially, I am going from a fraction to a continued fraction expansion.

I am not thinking now of the reverse process of going from the continued fraction to the fraction.

I have to read up on structs (like C?) before I take up your hint of one data structure for the forward and reverse directions. It strikes me as a clean approach.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 17, 2023, 3:32pm UTC](https://discourse.julialang.org/t/how-to-augment-an-int64-1-d-array-or-vector/107729/11 "2023-12-17T15:32:55Z")

</div>

> [@chandra](#):
>
> gives me a _row_ vector:  
> `[15, 1, 1, 1, 3, 1, 1, 2]`

This is not actually a row vector, but a flat, 1D vector, which behaves like a _column_ vector. It just happens to _print_ horizontally in some contexts.

> [@chandra](#):
>
> What I am seeking is `[15; 1, 1, 1, 3, 1, 1, 2]`

This isn’t valid Julia syntax. You can either write

```julia
[15, 1, 1, 1, 3, 1, 1, 2]

```

or

```julia
[15; 1; 1; 1; 3; 1; 1; 2]

```

they are equal, and represent regular (column-like) vectors.

For a row-vector, transpose this, or write

```julia
[15 1 1 1 3 1 1 2] 

```

for a row-like 1xN matrix.
