array task, I think is possible..can't figure it out

• (2 Pages)
• 1
• 2

25 Replies - 595 Views - Last Post: 06 January 2013 - 06:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=305204&amp;s=d971ce0f620a4ae8471e4ea28e3b23d1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 donfire

Reputation: -1
• Posts: 17
• Joined: 26-December 12

array task, I think is possible..can't figure it out

Posted 30 December 2012 - 07:50 AM

Ok so i post the code..which is working perfectly fine, then i go on with the question />/>
```#include <iostream>
using namespace std;

int ROW=44;
int LN [][5]={
/*	 0*/	{37,13,	20, 84,	90},
/* 1*/	{26,83,	90,	72,	79},
/* 2	*/{41,80,	39,	60,	40},
/* 3*/	{4,	46,	85,	61,	9 },
/* 4*/	{44,62,	74,	70,	49},
/* 5*/	{28,33,	72,	62,	88},
/* 6*/	{20,5,	6,	25,	71},
/* 7 */	{3,	9,	15,	84 ,34},
/*8*/{7,	52,	25,	83,	88},
/* 9*/{15,35, 67, 78, 6},
/*10*/	{53,38,	2,	10,	49},
/*11*/	{43,83,	17,	37,	15},
/*12*/	{90,36,	59,	3,	64},
/*13*/	{59,19,	28,	58,	76},
/*14*/	{51,81,	2,	8,	67},
/* 15/*/{64,72,	45,	86,	5},
/* 16*/{77,89,	38,	72,	45},
/*17*/{34,1,	74,	61,	89},
/*18*/{31,25,	75,	37,	84},
/*19*/	{23, 59, 57,	50,	77},
/*20*/	{79,90,	1,	23,	16},
/*21*/	{25,34,	22,	84,	7},
/*22*/	{70,46,	30,	37,	90},
/*23*/	{72,55,	75,	64,	71},
/*24*/	{90,8,	84,	36,	30},
/*25*/	{34,79,	81,	20,	12},
/*26*/	{82,56,	77,	49,	69},
/* 27*/	{42,30,	89,	84,	59},
/*28*/	{10,6,	68,	50,	87},
/*29*/	{28,7,	55,	62,	58},
/*30*/	{31,25, 86,	84,	87},
/*31*/	{36,66,	53,	3,	77},
/*32*/	{25,13,	72,	70,	47},
/*33*/{34, 77,80,	53,	33},
/*34*/	{86, 71, 23, 90, 8},
/*35*/{81, 45,	69,	38,	72},
/*36*/	{24,	61,	74,	41,	72},
/*37*/	{30, 27, 66, 43, 13},
/*38*/	{69, 20, 15, 64,59},
/*39*/	{14, 28, 59, 42, 67},
/*40*/	{2,	31,	88,	28,	55},
/*41*/{60,	73,	63,	19,	82},
/*42*/{67, 4,	42,	27,	29},
/*43*/	{16, 2,	51,	19,	74}
};
int r, c, k, Possiblenumbers1, Numberofpossiblenum=0;

int main ()
{

//1ST rolling, roll will be minus 2
for (r=0; r<ROW-2; r++)for (c=0;c<5;c++)  /*locking a particular row, in this case starting from row 0*/

{
if(LN[r][c]==LN [ROW-1][0]
||LN[r][c]==LN [ROW-1][1]
||LN[r][c]==LN [ROW-1][2]
||LN[r][c]==LN [ROW-1][3]
||LN[r][c]==LN [ROW-1][4]) /*if  at least a figure of row analysed equal to a figure of jumping board  row*/
{r+=1, Numberofpossiblenum+=5;
cout<<endl<<"Row number:"<<r<<endl;
for (c=0;c<5;c++)
cout<<LN[r][c]<<", ";

}
}
cout<<endl<<endl<<"Number of possible numbers are"<<Numberofpossiblenum<<endl;

int tally[k]
return 0;
}

```

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

oh, so the problem is that i want to figure out a way that these numbers are extracted into the array tally: I cant tell from the beginning how long tally could be. all i know is that for every row that comes out, there gonna be five numbers, i cant also tell how many rows could come out. All are decided at runtime, if thats the term />/>
so at the end i should be able to do something like for(c=0;c<k+1;c++) cout<<tally;
can it be done? and if so can some good samaritan explain it to me? am only a beginner. Thanks />/>

This post has been edited by jimblumberg: 30 December 2012 - 07:53 AM
Reason for edit:: Fixed Code tags.

Is This A Good Question/Topic? 0

Replies To: array task, I think is possible..can't figure it out

#2 jimblumberg

Reputation: 4565
• Posts: 14,417
• Joined: 25-December 09

Re: array task, I think is possible..can't figure it out

Posted 30 December 2012 - 07:58 AM

Since Variable Length Arrays (VLA) are not allowed you will need to use dynamic memory to allocate your array, or better yet use a std::vector instead of the arrays.

The following code (a VLA) is not allowed since k is not a compile time constant.
```int tally[k]
```

Jim

This post has been edited by jimblumberg: 30 December 2012 - 07:59 AM

#3 donfire

Reputation: -1
• Posts: 17
• Joined: 26-December 12

Re: array task, I think is possible..can't figure it out

Posted 30 December 2012 - 02:54 PM

jimblumberg, on 30 December 2012 - 07:58 AM, said:

Since Variable Length Arrays (VLA) are not allowed you will need to use dynamic memory to allocate your array, or better yet use a std::vector instead of the arrays.

The following code (a VLA) is not allowed since k is not a compile time constant.
```int tally[k]
```

Jim

Ok jim i've been reading about dynamic memory all day and i only posted on here cos i couldnt figure a way to do the task. i want a code example...am sure all guys who post here know how to research on the net..but sometimes just reading notes is not enough...i want an example code..so i see how is done..be it with dynamic memory or vectors...thanks...

#4 jimblumberg

Reputation: 4565
• Posts: 14,417
• Joined: 25-December 09

Re: array task, I think is possible..can't figure it out

Posted 30 December 2012 - 02:59 PM

Did you read the links I provided? They both have examples of how you use these concepts. What exactly don't you understand?

Jim

#5 donfire

Reputation: -1
• Posts: 17
• Joined: 26-December 12

Re: array task, I think is possible..can't figure it out

Posted 01 January 2013 - 06:56 AM

```voin inserimento(Nodo * &testa, int numero)

{
whatever is here

}

```

i mean what is testa? a pointer of type nodo? can a declaration be done like that??? notice the ampersand sign infront of testa? that is making me sooo confused but the main code is working fine..soo am the ignorant one here...pls dont tell me to go and read on pointers been doing that for sometime now...some one pls just explain... Nodo * testa; is fine..declaring a pointer called testa of type Nodo..but Nodo * &testa, in the parameter of the function inserimento...is making me go nuts...help pls...

This post has been edited by jimblumberg: 01 January 2013 - 05:42 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

#6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11138
• Posts: 41,840
• Joined: 27-December 08

Re: array task, I think is possible..can't figure it out

Posted 01 January 2013 - 05:13 PM

Moved to C/C++. Please do not post help threads in the Challenges subforum.

#7 sepp2k

• D.I.C Lover

Reputation: 2226
• Posts: 3,429
• Joined: 21-June 11

Re: array task, I think is possible..can't figure it out

Posted 01 January 2013 - 05:18 PM

It's defining a reference to a pointer. So if you assign testa to point to something else, this will also affect the pointer you passed in (while using a regular pointer, the change would only be visible within the inserimento method).

#8 donfire

Reputation: -1
• Posts: 17
• Joined: 26-December 12

Re: array task, I think is possible..can't figure it out

Posted 05 January 2013 - 10:36 AM

hi guys..this short program is almost done..but the last part been beating me up since 3 days now..so am going to post the whole code here then we go straight to the problem
```#include <iostream>
#include <vector>
using namespace std;
int const RESET = 0;
int ROW=44;
int LN [][5]={
/* 0*/ {37,13, 20, 84, 90},
/* 1*/ {26,83, 90, 72, 79},
/* 2 */{41,80, 39, 60, 40},
/* 3*/ {4, 46, 85, 61, 9 },
/* 4*/ {44,62, 74, 70, 49},
/* 5*/ {28,33, 72, 62, 88},
/* 6*/ {20,5, 6, 25, 71},
/* 7 */ {3, 9, 15, 84 ,34},
/*8*/{7, 52, 25, 83, 88},
/* 9*/{15,35, 67, 78, 6},
/*10*/ {53,38, 2, 10, 49},
/*11*/ {43,83, 17, 37, 15},
/*12*/ {90,36, 59, 3, 64},
/*13*/ {59,19, 28, 58, 76},
/*14*/ {51,81, 2, 8, 67},
/* 15/*/{64,72, 45, 86, 5},
/* 16*/{77,89, 38, 72, 45},
/*17*/{34,1, 74, 61, 89},
/*18*/{31,25, 75, 37, 84},
/*19*/ {23, 59, 57, 50, 77},
/*20*/ {79,90, 1, 23, 16},
/*21*/ {25,34, 22, 84, 7},
/*22*/ {70,46, 30, 37, 90},
/*23*/ {72,55, 75, 64, 71},
/*24*/ {90,8, 84, 36, 30},
/*25*/ {34,79, 81, 20, 12},
/*26*/ {82,56, 77, 49, 69},
/* 27*/ {42,30, 89, 84, 59},
/*28*/ {10,6, 68, 50, 87},
/*29*/ {28,7, 55, 62, 58},
/*30*/ {31,25, 86, 84, 87},
/*31*/ {36,66, 53, 3, 77},
/*32*/ {25,13, 72, 70, 47},
/*33*/{34, 77,80, 53, 33},
/*34*/ {86, 71, 23, 90, 8},
/*35*/{81, 45, 69, 38, 72},
/*36*/ {24, 61, 74, 41, 72},
/*37*/ {30, 27, 66, 43, 13},
/*38*/ {69, 20, 15, 64,59},
/*39*/ {14, 28, 59, 42, 67},
/*40*/ {2, 31, 88, 28, 55},
/*41*/{60, 73, 63, 19, 82},
/*42*/{67, 4, 42, 27, 29},
/*43*/ {16, 2, 51, 19, 74}
};
int r, c, k, Possiblenumbers1, Numberofpossiblenum=0;

int main ()
{
vector<int> Tally;

//1ST rolling, roll will be minus 2
for (r=0; r<ROW-2; r++)for (c=0;c<5;c++) /*locking a particular row, in this case starting from row 0*/

{
if(LN[r][c]==LN [ROW-1][0]
||LN[r][c]==LN [ROW-1][1]
||LN[r][c]==LN [ROW-1][2]
||LN[r][c]==LN [ROW-1][3]
||LN[r][c]==LN [ROW-1][4]) /*if at least a figure of row analysed equal to a figure of jumping board row*/
{r+=1; Numberofpossiblenum+=5; // rolling one, would be r=+2 if rolling 2, or 3 if rolling 3 and so on...
cout<<endl<<"Row number:"<<r<<endl;
for (c=0;c<5;c++)
{Tally.push_back(LN[r][c]);
cout<<LN[r][c]<<", ";
}
}
}
cout<<endl<<endl<<"Number of possible numbers are: "<<Numberofpossiblenum<<endl<<endl;
cout<<"...and they are:\n";
for(int i=0;i<Tally.size();i++)
cout<<Tally[i]<<", ";

int i,n,j,counter;

//from here downwards am making a mistake....i want help from here on
cout<<endl<<endl<<"We will now find how many times numbers are repeated."<<endl<<endl;

for (i=0;i<Numberofpossiblenum;i++)
{
for (j=0;j<Numberofpossiblenum;j++)
{
if(Tally[i] == Tally[j])
{
counter+=1;
// Tally.erase(Tally.begin()+j-1);
}
}
cout<<endl<<"Value "<<Tally[i]<<" repeated "<<counter<<" times!"<<endl;
}

return 0;
}

```

Ok so this is the whole code! the problem is at the end from where i have cout<<we will now find out how many times numbers are repeated. from there on i've been trying to write in a way so that the numbers in vector Tally are checked to see how many times they repeat...but i've havent been able to do anything good...guys help me please...thank you soo much in advance. what i have there is not working well...giving me some strange things /> help...thanks

This post has been edited by Skydiver: 05 January 2013 - 06:48 PM
Reason for edit:: Fixed code tags.

#9 #define

• Duke of Err

Reputation: 1520
• Posts: 5,315
• Joined: 19-February 09

Re: array task, I think is possible..can't figure it out

Posted 05 January 2013 - 11:00 AM

This seems unusual you are changing the value of r, and also increasing Numberofpossiblenum by 5.

```{r+=1; Numberofpossiblenum+=5; // rolling one, would be r=+2 if rolling 2, or 3 if rolling 3 and so on...

```

Can you describe what you are wanting to do in the for loop.

#10 donfire

Reputation: -1
• Posts: 17
• Joined: 26-December 12

Re: array task, I think is possible..can't figure it out

Posted 05 January 2013 - 11:04 AM

#define, on 05 January 2013 - 11:00 AM, said:

This seems unusual you are changing the value of r, and also increasing Numberofpossiblenum by 5.

```{r+=1; Numberofpossiblenum+=5; // rolling one, would be r=+2 if rolling 2, or 3 if rolling 3 and so on...

```

Can you describe what you are wanting to do in the for loop.

Dont worry about this side..is working fineeee!!! its doing exactly what i want it to do!!! my problem is in the end of the program...where i try to fine how many times the numbers repeat in vector Tally...thats where i want help ...thanks

#11 #define

• Duke of Err

Reputation: 1520
• Posts: 5,315
• Joined: 19-February 09

Re: array task, I think is possible..can't figure it out

Posted 05 January 2013 - 12:10 PM

You could sort your vector beforehand.

sort

Another option could be a map.

#12 donfire

Reputation: -1
• Posts: 17
• Joined: 26-December 12

Re: array task, I think is possible..can't figure it out

Posted 05 January 2013 - 12:14 PM

#define, on 05 January 2013 - 12:10 PM, said:

You could sort your vector beforehand.

sort

Another option could be a map.

sorry but what do you mean by sorting out my vector before hand?...

#13 #define

• Duke of Err

Reputation: 1520
• Posts: 5,315
• Joined: 19-February 09

Re: array task, I think is possible..can't figure it out

Posted 05 January 2013 - 12:28 PM

If you sort the vector, the values in it will be in order.

eg {1,2,2,2,3,4,4,8,9,9}

then you can count tyhe values that are equal.

#14 donfire

Reputation: -1
• Posts: 17
• Joined: 26-December 12

Re: array task, I think is possible..can't figure it out

Posted 05 January 2013 - 12:35 PM

you mean after sorting i count manually? wow...ok this is just a short code..but the main program should be giving like 300 numbers plus...mmm count manually all the time..is not a good idea thanks anyway...

#15 Skydiver

• Code herder

Reputation: 3966
• Posts: 12,620
• Joined: 05-May 12

Re: array task, I think is possible..can't figure it out

Posted 05 January 2013 - 06:40 PM

If you don't want to count manually, you could write code to count the repeated numbers.