Challenge "no arrays" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Challenge "no arrays"

Write a program in any language which reads 100 numbers from keyboard and then writes these numbers in reverse order. You are not allowed to use arrays, lists and other similar containers. As another challenge you are encouraged to use the least number of variables as possible. I did this challenge with just 3 variables.

5th Sep 2017, 11:41 AM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
10 Answers
+ 4
By this challenge, I meant not using advanced containers and advanced tools of programming. So please do it by elemetary tools
5th Sep 2017, 12:23 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 4
But somehow you have to use the idea of a stack...
5th Sep 2017, 12:26 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 4
I tried it in code blocks maybe I have missed something
5th Sep 2017, 12:58 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 4
@kurwius I tried again and it works fine
5th Sep 2017, 1:04 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 3
This is a nice academic exercise, but totally useless in practice as the recursion eats much more memory than what's saved by not using an array. I haven't actually compiled this, forgive me if there are any syntax errors I've missed. void f(int cnt) { int d; scanf("%d", &d) ; if (cnt <100) f(cnt+1); printf("%d\n", d); } void main(int argc, char *argv[]) { f(1); }
5th Sep 2017, 12:46 PM
Udi Finkelstein
Udi Finkelstein - avatar
+ 3
@kurwius I tried your answer. It stops after reading 3 numbers
5th Sep 2017, 12:49 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 3
This is an answer in java: import java.util.Scanner; public class HundredNumbers { static Scanner scan=new Scanner(System.in); public static void main(String[] args) { String s=""; int beginIndex=0, endIndex=0; for(int i=0;i<5;i++){ s=s+"#"+scan.nextLine(); } beginIndex=endIndex=s.length(); for(int i=0; i<5; i++){ do { beginIndex--; }while(s.charAt(beginIndex-1)!='#'); System.out.println(Integer.parseInt(s.substring(beginIndex, endIndex))); beginIndex=endIndex=beginIndex-1; } } }
5th Sep 2017, 4:15 PM
Vahid
Vahid - avatar
+ 3
@Matthew Li interesting! but you are using lists
6th Sep 2017, 10:18 AM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 2
python print(input('Enter 100 numbers:')[::-1])
6th Sep 2017, 9:38 AM
Matthew Li
Matthew Li - avatar
+ 1
Can i use a stack?just offer 100 numbers in and poll them out. But one question is to implentation stack, I have to use Deque interface and LinkedList or ArrayDeque.
5th Sep 2017, 12:21 PM
Ran
Ran - avatar