this program could be a lot simpler and I'm struggling grasping your concepts |
Ok, no panic :-)
Write a little piece of code and test it immediately: don’t write a lot of code without testing it. You’ve decided to use arrays: that’s good. To start, declare your array and immediately write the procedure to display its content on screen, even if in an output that doesn’t comply with your assignment.
This way, as you write, you can verify your results.
I’m afraid my English is terrible, but I hope this can push you in the right direction:
1) In this program, the user will enter two digit positive and negative integers (i.e. -99 to +99)
2) You may assume the user will never type in a number that has magnitude (absolute value) of 101 or more.
3) a) When the user types 100 or -100 the program should exit... |
That means:
- you need a loop where the user is asked to input numbers
- that loop will stop when the user types 100 or -100
- the user will NOT type 101 or more (or -101 or less)
So: you only need to check for 100 or -100 to appear: the user will do anything right.
(of course, if you create a loop that runs as long as the user types values greater than -100 and lower than 100 that’s also fine)
3) b) ...and print out the statistic of how many times each integer between [-9 and +9] occurred as well as how many numbers outside the range of [-9 to +9] occurred. |
That means:
- As soon as the loops end, you need to have all your values ready to display.
So: everything must happen inside the loop (except for the display on screen step).
4) your job is to only count the number of occurrences of each integer between [-9 and +9], inclusive. |
That means:
- inside your loop, you need to check if any values are greater than -10 and lower than +10.
- If yes, you need to increment a specific variable which stores that piece of information (the number ‘x’ has been inserted again: let’s increment the variable for ‘x’).
Hints: in your code, you already have an array. An array is no more than a bunch of variables which lie one next to the other, in order. The values you are selecting are in the same situation: they are contiguous; there’re no ‘interruptions’ between -9 and 9.
Let’s assume the user types -9. You want to increment the value for "how many time -9 has been typed in?". You have an array with a ‘row’ of variables (you decide how many you need) which can store any piece of information you need to save.
So...
Now, your teacher has set a trap for you ;-)
You apparently need to manage only values from -9 and 9, but that’s false.
When the program finishes, you also need to state how many ‘not-in-range’ numbers have been typed in. So, you don’t need room only for 19 values from -9 to 9, but also one further variable which stores the frequency of all other numbers.