Assigning a variable count: need some help

L

Lorn

I'm stil getting used to c++ and I'm having a really tough time solving
this problem. I have an integer variable whose purpose is to count the
occurence of repeating data in a set of data until the data value in
the set changes. Example data:

{setA = 2, setB = 2, setC = 2, setD = 5}

So here setCnt would be = 3... counting up the number of times the
number 2 repeated after the first set. Values are always >= to the last
set.

I think I should create an array and loop through the values with a for
loop, but I'm having a hard time coming up with the right structure...
maybe someone here could give me some pointers. Here is my current
code:

int setValues[] = {setA, setB, setC, setD};

int setCnt;

for(int i = 0; i < 5; i++)
{
if(setValues == setValues[i-1])
{
setCnt = i;
}
else
setCnt = i - 1;
}

Anyway, this doesn't work. I've been racking my brain trying to get a
handle on for loops... but I'm a bit frustrated. Any input would be
really appreciated.

Lorn
 
T

Theron NightStar

Lorn said:
I'm stil getting used to c++ and I'm having a really tough time solving
this problem. I have an integer variable whose purpose is to count the
occurence of repeating data in a set of data until the data value in
the set changes. Example data:

{setA = 2, setB = 2, setC = 2, setD = 5}

So here setCnt would be = 3... counting up the number of times the
number 2 repeated after the first set. Values are always >= to the last
set.

I think I should create an array and loop through the values with a for
loop, but I'm having a hard time coming up with the right structure...
maybe someone here could give me some pointers. Here is my current
code:

int setValues[] = {setA, setB, setC, setD};

int setCnt;

for(int i = 0; i < 5; i++)
{
if(setValues == setValues[i-1])
{
setCnt = i;
}
else
setCnt = i - 1;
}

Anyway, this doesn't work. I've been racking my brain trying to get a
handle on for loops... but I'm a bit frustrated. Any input would be
really appreciated.

Lorn


I am still very new to this as well, so pardon any answers that are already obvious or have been tried.

I had a similar problem and couldn't figure out why. If it is simply not working or you are getting a seg fault, make sure that 'i' is not outside of the bounds of the array. Remember to take into consideration that 'i' will extend one more higher and/or lower than you might expect. Try i < 3 in your for loop. It looks like you should only have access to setValues[0] thru setValues[3]. If you try to use setValues when i == 4 you will get a segfault Hope that helps....
 
J

Jonathan Mcdougall

I'm stil getting used to c++ and I'm having a really tough time solving
this problem. I have an integer variable whose purpose is to count the
occurence of repeating data in a set of data until the data value in
the set changes. Example data:

{setA = 2, setB = 2, setC = 2, setD = 5}

So here setCnt would be = 3... counting up the number of times the
number 2 repeated after the first set. Values are always >= to the last
set.

I think I should create an array and loop through the values with a for
loop, but I'm having a hard time coming up with the right structure...
maybe someone here could give me some pointers. Here is my current
code:

int setValues[] = {setA, setB, setC, setD};

int setCnt;

Always initialize your variables:

int setCnt = 0;
for(int i = 0; i < 5; i++)

You only have 4 values in setValues (from 0 to 3):

for (int i=0; i<4; ++i)
{
if(setValues == setValues[i-1])


What happens when i==0 here?

if (setValues[0] == setValues[-1])

is that really what you want?
{
setCnt = i;
}
else
setCnt = i - 1;

Always try first to execute your program on a piece of paper. You'll
see what happens and will be able to debug it.


Jonathan
 
G

Greg

Lorn said:
I'm stil getting used to c++ and I'm having a really tough time solving
this problem. I have an integer variable whose purpose is to count the
occurence of repeating data in a set of data until the data value in
the set changes. Example data:

{setA = 2, setB = 2, setC = 2, setD = 5}

So here setCnt would be = 3... counting up the number of times the
number 2 repeated after the first set. Values are always >= to the last
set.

I think I should create an array and loop through the values with a for
loop, but I'm having a hard time coming up with the right structure...
maybe someone here could give me some pointers. Here is my current
code:

int setValues[] = {setA, setB, setC, setD};

int setCnt;

for(int i = 0; i < 5; i++)
{
if(setValues == setValues[i-1])
{
setCnt = i;
}
else
setCnt = i - 1;
}

Anyway, this doesn't work. I've been racking my brain trying to get a
handle on for loops... but I'm a bit frustrated. Any input would be
really appreciated.

Lorn




Since this is a C++ newsgroup, the program implemented should have more
of a C++ flavor:



#include <algorithm>

int setValues[] = {setA, setB, setC, setD };

const kSetValuesLimit = sizeof(setValues)/sizeof(setValues[0]);
int setCnt = 0;

int * theIter = std::adjacent_find( &setValues[ 0 ],
&setValues[ kSetValuesLimit ],
std::not_equal_to<int>() );


setCnt = theIter - &setValues[0];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top