30 Days of Code - Day 7: Arrays

Objective

Today, we’re learning about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video!

Task

Given an array, A, of N integers, print A's elements in reverse order as a single line of space-separated numbers.

Input Format

The first line contains an integer, N (the size of our array).

The second line contains N space-separated integers describing array A's elements.

Constraints:

  • 1 <= n <= 1000
  • 1 <= A[i] <= 10000, where A[i] is the ith integer in the array.

Output Format

Print the elements of array A in reverse order as a single line of space-separated numbers.

Sample

input00.txt
4
1 4 3 2
output00.txt
2 3 4 1

Solution

main.go
package main

import "fmt"

func main() {
  var N int
  fmt.Scan(&N)

  A := make([]int, N)

  for i := 0; i < N; i++ {
    fmt.Scan(&A[i])
  }

  for ; N > 1; N-- {
    fmt.Printf("%v ", A[N-1])
  }

  fmt.Printf("%v", A[0])
}
Privacy policy

This site uses tracking cookies when:

  • Comments are loaded. If you don’t want them, just don’t click «Show comments».

If you use only Open Source products, sorry about using cookies, I will replace Disqus as my comments platform in the future.

If you use private source products, worrying about privacy and using this products is like worrying about global warming and not recycling.. So just don’t.. 😒