Count Number Of Ones In Array Of Integers In 8085
I don't know if I'm just being a total fool, most likely I am, it's been a long day, but this isn't working as I want it to, and, well, I don't see why.
- Count Number Of Ones In Array Of Integers In 8085 Math
- Count Number Of Ones In Array Of Integers In 8085 2
It should be able to have 11 numbers entered, a new number on each line, add them to the array, then total them, yet it's just not working. It's not stopping to exit the loop, even though I am incrementing i.
Any ideas?
}
A program which is used to count the number of numbers in an array using a 8085 microprocessor is known as a assembly language program. Reduce the upper bound of the array by one element, thus. Basically you set up a second data structure which has an integer value and an integer count as part of its members. If the integer range is smaller than the number of integers to be read then the second structure could be an array and you would u.
Bart8 Answers
You have 10 elements in the array, numbered 0 - 9. You are overflowing the buffer, so all bets are off. This is undefined behaviour.
You can't add eleven entries to a ten-element array.
Hot LicksHot LicksMy guess is buffer over-run since the for-loop reads in 11 numbers and the 11th number gets stored outside the array, probably overwriting i.
Try changing the 11 to a 10 in the for loop.
Jaco Van NiekerkJaco Van NiekerkYou're storing eleven numbers into an array of size 10. Thus you're storing the last element out of bounds, which invokes undefined behavior.
The reason that this undefined behavior manifests itself as an infinite loop in your case is probably that i
is stored after array
in memory on your system and when you write a number into array[10]
(which is out of bounds, as I said), you're overwriting i
. So if you entered a number smaller than 11 this will cause the loop to continue and ask for input once more.
If an array is a[10], then every array starts from its index number 0, so here it will have 10 elements; given that their positions will start from 0 to 9, counting gives 10 elements.
You can try this:
Count Number Of Ones In Array Of Integers In 8085 Math
Nick CoxYou have problems with your array declaration. You are defining an array of size 10 array[10]
and saying the program to calculate the sum of 11 elements which is resulting in memory overflows.
To correct the program just increase size of the array as array[11]
. Also if you wish you can check the recursive approach to find sum of array elements.