so here is the code and is working fine. it prints on the screen something like this ;
row number 5;
34, 45, 56, 67, 56,
row number 10;
76, 98, 12, 78, 77,
row number 11:
23, 56, 87, 99, 100
assuming tally is a dynamic array with dynamic memory...how can i get the program to extract the results of row number 5, row number 10, and row number 11...all into tally to be a single array of size 15? is that possible?
please dont tell me to go and read on dynamic memory been doing that all yesterday and the whole today...i dont know how to apply it in this case...if is possible can you pls show how with example codes? lets say is not possible with dynamic memory, then should be possible with another method, can you still please show me how with an example code? thanks you..taking in account that all the results of row 5,row 10 and row 11 are all decided during runtime(if thats the term) i mean when i click run...thank you soo much in advance
First, you need a pointer of the type that constitutes your array. This is int in your case:
int *kp;
Once you know how many elements you want to write into your dynamic memory array, you allocate the memory like so:
kp = newint[k];
Unfortunately, C++ doesn't offer a way to construct each array element in turn, so you have to assign the values yourself:
1 2 3 4
for (int i = 0; i < k; i ++)
{
kp[i] = value;//your value here. This is how you read and write to your dynamic memory array.
}
When you're done, delete like so:
delete[] kp; //use of delete[] operator instead of delete ensures that the destructor of every element is called
EDIT:
In your case though, you actually have to run your algorithm just to determine the ultimate size of your array. To tackle this issue, define a class as follows:
1 2 3 4 5 6
struct lnklst
{
int nums[5];
lnklst *next;
};
lnklst *start, *last; //start of list, and last element created
Whenever your algorithm wants to add the next 5 numbers, do this like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if (Numberofpossiblenum == 0)
{
last = start = new lnklst();
}
else
{
last->next = new lnklst(); //tell last element about our new element
last = last->next; //last pointer has to be most recently created element
}
last->next = 0;
for (int i = 0; i < 5; i++)
{
last->nums[i] = value; //your value here
}
Using a linked list in this way means you allocate the memory you need, as you need it. Then, when your algorithm is complete, you can compile into a dynamic memory array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
kp = newint[Numberofpossiblenum];
int i2 = 0;
lnklst *pref = start;
while(true)
{
for (int i = 0; i < 5; i ++)
{
kp[i2] = pref->nums[i]
i2 ++;
}
if (pref->next)
{
pref = pref->next; //go to next element of list, if it has one
}
else
{
break; //otherwise done.
}
}
PPS: Remember that as with all allocations on the heap, you must delete all of the linked list elements. You do this by walking the last, as above:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//using our pref pointer from before:
pref = start;
while (true)
{
if(pref->next) //another element?
{
start = pref->next;
delete pref;
pref = start;
}
else
{
delete pref; //Last element, delete and quit the loop
break;
}
}