Counting numbers greater and less than input using 4 variables

87 Views Asked by At

I got a request for help with exercise (dedicated for primary school students) to prepare a program that counts numbers greater and less than input with limitation to using 4 int variables.

Write a program that reads one integer N. It will then read N integers and print out how many of these numbers were greater than N and how many were less than N.

I used kotlin for my attempts just for more readable example, but language doesn't matter.

val sc = Scanner(System.`in`)

println("N: ")
var A = sc.nextInt()
var B = 0
var C = 0
var D = 0

while (D < A){
    println("--> ")
    B = sc.nextInt()
    if (B > A) {
        C++
    }
    D++
}

println(C)

while (C > 0){
    C--
    A--
}

println(A)

Finally code must be describable by pseudocode like this.

1. load into A
2. if D < A jump to next otherwise jump to 8
3. Load to B
4. if B > A jump to next otherwise jump to 6
5. Increase C by 1
6. increase D by 1
7. jump to 2
8. write out box C
9. move to a new line
10. if C > 0 jump to next otherwise jump to 13
11. decrease C by 1
12. decrease A by 1
13. write out box A

I did some attempts to solve problem but I have no idea how to handle numbers that are equal that should not be counted.
I started to have some doubts that it is possible with restriction that not allowing more variables.

3

There are 3 best solutions below

9
no comment On BEST ANSWER

Usually you'd use five variables. So I combine the two counts in one variable.

N = int(input())
count = 0
i = 0
while i < N:
    i += 1
    number = int(input())
    if number > N:
        count += N+1
    if number < N:
        count += 1
print(count // (N+1), 'were greater than', N)
print(count % (N+1), 'were less than', N)

Attempt This Online!

Without division/modulo:

N = int(input())
count = 0
i = 0
while i < N:
    i += 1
    number = int(input())
    if number > N:
        count += N+1
    if number < N:
        count += 1
i = 0
while count > N:
    count -= N+1
    i += 1
print(i, 'were greater than', N)
print(count, 'were less than', N)

Attempt This Online!

3
Cary Swoveland On

It seems straightforward. Am I missing something?

Variables are N, cp, cm and e.

N = <read value>.to_i
cp = 0
cm = 0
N.times do
  e = <read value>.to_i
  if e > N
    cp = cp + 1
  else if e < N
    cm = cm + 1
  end
end

puts "#{cp} values greater than #{n}"
puts "#{cm} values lest than #{n}"

to_i is a method that converts a string to an integer.

0
radzmiv On

Thanks to @nocomment solution, resulting pseudocode looks like this. It was necessary to implement divide and modulo without predefined function.

1. load into A
2. if D < A jump to next otherwise jump to 10
3. Load to B
4. if B > A jump to next otherwise jump to 6
5. increase C by 1
6. If B < A jump to next otherwise jump to 8
7. increase C by A
8 increase D by 1
9. jump to 2
#modulo counting
10. Set B to C
11. if B >= A jump to next otherwise jump to 14
12. decrease B by A
13. jump to 11
14. write box B
#div counting
15. move to a new line
16. set B to 0
17. if C >= A jump to next otherwise jump to 21
18. decrease C by A
19. increase B by 1
20. jump to 17
21. write box B