Extracting values from large array to put in smaller array

Good day everyone,

I've been throwing this around in my head for a while, but it just does not make sense so here goes. It's for a school project so the syntax is somewhat imposed. Sorry if my code is not necessarily up to 2015 super-productive standards :)

What I want to do is fill up a multidimensional array with values that are put in by the user. That all works fine. Then I want there to be an operation to extract the values from that array and "put" them in another small and empty array to avoid having to display the whole table with plenty of zeros. It's a forecast calculator.

I was easily able to extract the amount of values that are not zeros using a for loop, but I can't figure out how to extract the actual numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Variables
int LargeArray[4][7] ;
int SmallArray[][];
int NbDays;
int NbWk;
int QtyDay;
int NotZero;

for (NbWk = 0; NbWk < 4; NbWk++)
{
for (NbDays = 0; NbDays < 7; NbDays++)
{
fflush(stdin);
cin.clear();
cin >> QtyDay;
LargeArray[NbWk][NbDays] = QtyDay;
}
}


I get my large array filled up as such. Then I can easily know how many of these values are not zeros with this

1
2
3
4
5
6
7
8
9
10
for (int NbWk = 0; NbWk < 4; NbWk++)
	{
		for (int NbDays = 0; NbDays < 7; NbDays++)
		{
			if (LargeArray[NbWk][NbDays] != 0)
			{
				NotZero++;
			}
		}
	}


Now I want to fill up my SmallArray[][] and only display, without the zeros, my results. I cant for the life of me figure it out given that if I leave the brackets empty when I declare the variable up top there doesnt seem to be a way to fill them up later... Many thanks for any help!
Just replace int smallArray [] with
std::vector<int> smallArray
. Vector do not need a user( you ) to declare their size upfront, you can however insert as many elements as you want by calling push_back on it. Here's what I'm saying:
1
2
3
4
5
6
7
8
9
10
const int number_of_days = nbDays * nbWeeks;
int bigArray[number_of_days] {};
for( int x = 0; x != number_of_days; ++x ){
    // Fill in the values.
}
std::vector<int> smallArray {};
for( int x = 0; x != number_of_days; ++x ){
    if( bigArray[x] != 0 ) smallArray.push_back( bigArray[x] );
}
std::cout << "There are " << smallArray.size() << "days that are not zeros" << std::endl;


I intentionally changed the two-dimensional array into one because using the 2D array here isn't necessary, it can be rolled into 1D.
Thanks. That makes a ton of sense, but I have to leave the array 2D since its a requirement, school, you know... That code is logical though, I appreciate it.

Just to be sure, aside from pointers, or vectorizing the array, there is no way of declaring an empty array up front, lets say

int ShortArray[][]

And then putting in a not-set amount of values in there? Lets say that when I run the program the first time I need ShortArray to be [2][4] and on the second pass I need [1][6], etc, etc...?

I can't use pointers either, sadly... and to be honest I'm not comfortable doing so yet.
Just to be sure, aside from pointers, or vectorizing the array, there is no way of declaring an empty array up front, lets say
No. You will have to overallocate your array (declare it containing more elements than you will need) and hold two numbers: one for physical array size (which you should never go over) and second for logical size (number of values currently stored)
Topic archived. No new replies allowed.