array task

Ok so i post the code..which is working perfectly fine, then i go on with the question :)/>/>
01 #include <iostream>
02 using namespace std;
03
04 int ROW=44;
05 int LN [][5]={
06 /* 0*/ {37,13, 20, 84, 90},
07 /* 1*/ {26,83, 90, 72, 79},
08 /* 2 */{41,80, 39, 60, 40},
09 /* 3*/ {4, 46, 85, 61, 9 },
10 /* 4*/ {44,62, 74, 70, 49},
11 /* 5*/ {28,33, 72, 62, 88},
12 /* 6*/ {20,5, 6, 25, 71},
13 /* 7 */ {3, 9, 15, 84 ,34},
14 /*8*/{7, 52, 25, 83, 88},
15 /* 9*/{15,35, 67, 78, 6},
16 /*10*/ {53,38, 2, 10, 49},
17 /*11*/ {43,83, 17, 37, 15},
18 /*12*/ {90,36, 59, 3, 64},
19 /*13*/ {59,19, 28, 58, 76},
20 /*14*/ {51,81, 2, 8, 67},
21 /* 15/*/{64,72, 45, 86, 5},
22 /* 16*/{77,89, 38, 72, 45},
23 /*17*/{34,1, 74, 61, 89},
24 /*18*/{31,25, 75, 37, 84},
25 /*19*/ {23, 59, 57, 50, 77},
26 /*20*/ {79,90, 1, 23, 16},
27 /*21*/ {25,34, 22, 84, 7},
28 /*22*/ {70,46, 30, 37, 90},
29 /*23*/ {72,55, 75, 64, 71},
30 /*24*/ {90,8, 84, 36, 30},
31 /*25*/ {34,79, 81, 20, 12},
32 /*26*/ {82,56, 77, 49, 69},
33 /* 27*/ {42,30, 89, 84, 59},
34 /*28*/ {10,6, 68, 50, 87},
35 /*29*/ {28,7, 55, 62, 58},
36 /*30*/ {31,25, 86, 84, 87},
37 /*31*/ {36,66, 53, 3, 77},
38 /*32*/ {25,13, 72, 70, 47},
39 /*33*/{34, 77,80, 53, 33},
40 /*34*/ {86, 71, 23, 90, 8},
41 /*35*/{81, 45, 69, 38, 72},
42 /*36*/ {24, 61, 74, 41, 72},
43 /*37*/ {30, 27, 66, 43, 13},
44 /*38*/ {69, 20, 15, 64,59},
45 /*39*/ {14, 28, 59, 42, 67},
46 /*40*/ {2, 31, 88, 28, 55},
47 /*41*/{60, 73, 63, 19, 82},
48 /*42*/{67, 4, 42, 27, 29},
49 /*43*/ {16, 2, 51, 19, 74}
50 };
51 int r, c, k, Possiblenumbers1, Numberofpossiblenum=0;
52
53 int main ()
54 {
55
56
57 //1ST rolling, roll will be minus 2
58 for (r=0; r<ROW-2; r++)for (c=0;c<5;c++) /*locking a particular row, in this case starting from row 0*/
59
60 {
61 if(LN[r][c]==LN [ROW-1][0]
62 ||LN[r][c]==LN [ROW-1][1]
63 ||LN[r][c]==LN [ROW-1][2]
64 ||LN[r][c]==LN [ROW-1][3]
65 ||LN[r][c]==LN [ROW-1][4]) /*if at least a figure of row analysed equal to a figure of jumping board row*/
66 {r+=1, Numberofpossiblenum+=5;
67 cout<<endl<<"Row number:"<<r<<endl;
68 for (c=0;c<5;c++)
69 cout<<LN[r][c]<<", ";
70
71 }
72 }
73 cout<<endl<<endl<<"Number of possible numbers are"<<Numberofpossiblenum<<endl;
74
75 int tally[k]
76 return 0;
77 }


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 = new int[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 = new int[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;
  }
}
Last edited on
ok thanks...quite helpful..somebody was talking about vectors...can someone figure away to use std::vector to do the trick? thanks again
You can implement vectors in your code as follows:

Declare vector<int> tally; in int main(), then, from line 68:
1
2
3
4
5
for (c = 0; c < 5; c++)
{
  cout << LN[R][C]; //etc...
  tally.push_back(LN[R][C]); //add element to the vector
}


You can easily access the elements with: tally[k] where k is the element you want to access.
thank you sooo much SHINKANSEN...you're a genius!!!
Topic archived. No new replies allowed.