473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Assigning a variable count: need some help

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[i] == 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

Jul 23 '05 #1
3 1633
Lorn wrote:
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[i] == 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[i] when i == 4 you will get a segfault Hope that helps....
Jul 23 '05 #2
> 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[i] == 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

Jul 23 '05 #3
Lorn wrote:
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[i] == 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];

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dariusz | last post by:
I need some help with trying to assign a string to a variable name, whilst the variable name itself is read out of an array. The code below is part of a routine in a database which checks if the...
10
by: Matthew Sims | last post by:
Python Newbie here. This is my first time learning object-oriented programming and trying to break out of the usual Korn/Perl/PHP style of programming. Having some difficulty understand some items....
0
by: vanGogh | last post by:
I have generated classes based on an Xml schema file (xsd) using the XSD.exe tool. My goal is to: - write an application that can parse XML documents - fill objects (based on the generated...
11
by: JohnR | last post by:
I'm trying to find a way to create a variable of a given type at runtime where I won't know the type until it actually executes. For example, dim x as object = "hi" x is declared as an object...
1
by: Michael | last post by:
I am using Visual Studio 2005 (ASP 2.0) to build a webpage that uses a Microsoft Access Database. The data in the database simply holds the information on a link to a document that I am storing on...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
24
by: Tony Girgenti | last post by:
Hello. Developing a Windows Form program in VS.NET VB, .NET Framework 1.1.4322 on a windows XP Pro, SP2. Before printing a document, i want to set the font to a font that is only available...
4
by: Mark77 | last post by:
Hello I am trying to assign the result of a SQL statement (Count of recordset) to a integer variable. I am getting a type mismatch on the assignment. The SQL statement is good. What am I missing....
25
by: ramprat | last post by:
Hi All, I'm essentially trying to extract a value from the pre_05_growth_factor column of my traffic table and assign it to a variable to be used later. Does anyone know why the code below...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.