How to compute the size of an array with leading zeros

So basically i have an array of size 20, and i am wanting to store a number inside the array
 
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |


Like this, the size of this would have to be 8.

But.... if i have an array like this

1
2

| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

In this case the size of the array would have to be 20, because 100,000,000,000,000,000,000 is a number of size 20....

How can i write a loop that can help me keep track of the size ???

i had something like this in mind

1
2
3
4
5
6
7
8
void HugeInt::computeSize()
{
	for (int i = 29; number != 0 && i >= 0; i--){
		
		size++;
	}
	
}

But the above doesnt work because it gives me 30, it's counting the whole thing... im very confused, please help
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>
#include <iterator>

template<size_t sz>
int fndsz(const int (&ar)[sz])
{
	return sz - std::distance(std::begin(ar), std::find_if(std::begin(ar), std::end(ar), [](auto i) { return i != 0; }));
}

int main()
{
	const int no1[20] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8};
	const int no2[20] = {1};

	std::cout << fndsz(no1) << "\n" << fndsz(no2) << '\n';
}


which displays:


8
20

Last edited on
fndsz()
¿they stole the vowels on your keyboard?
terrible name for a find_first_not_zero function
I'm a lazy typist - short for Find Size. Also when I learnt to program many, many, many years ago, variables were limited to 6 chars. I don't think I've shaken the habit..........

:)
@rnina, if it’s a genuine big-number class (which your notation does suggest) why store it inside a fixed-size array? Why not store it in a string or a vector so its size matches the length.
Last edited on
Topic archived. No new replies allowed.