adjacency #11138

15 views
Skip to first unread message

Steven Zeswitz

unread,
Nov 11, 2012, 9:16:20 PM11/11/12
to bc...@googlegroups.com
The first adjacency problem is driving me nuts, I think I've been looking at it for too long:

my code:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream inFile1("data1");
ofstream outFile2("data1-CONS");
int x, xPrev, consCount=0;
// bool firstTime = true;
inFile1>>xPrev;
outFile2<<xPrev<<" <<== This is Xprev"<<endl;
inFile1>>x;
outFile2<<x<<" <<== This is X in first read in"<<endl<<endl;
while (!inFile1.fail()){
if (xPrev>=0 || x>=0){
if (x==xPrev) 
consCount++;
} else 
outFile2<<consCount<<"<<== # of CONS DUPS"<<endl;
xPrev=x;
inFile1>>x;
}
outFile2<<"THERE ARE "<<consCount<<" consecutive duplicates"<<endl;
cout<<"hold on to ya butts"<<endl;
return 0;
}

INFILE::
data1:
4 4 4 3 2 2 4 3 2 4 5 6 7 8 -9
2 3 4 4 4 0

One: the code is not exiting once it hits a negative number , I'm a little confused as to where to put that condition, 

Two: the number of consecutive duplicates should be 2, but i'm getting 3 (this is before I added the extra numbers after the -9... with the numbers after -9, I get 5 consecutive duplicates)

any advice? Thanks 
SZ

Manor Perets

unread,
Nov 11, 2012, 10:43:14 PM11/11/12
to bc...@googlegroups.com
Steven,
Your code is not exiting the loop because you have a condition that xprev >=0 OR x>=0. Since one of those is true the condition is true, xprev is >0 but x<0. And then you assign xprev the value that's less than 0 BUT x gets a new value of greater than 0. So again, one of the values is greater than 0 so the condition is true. If you'd have two consecutive values less than zero, only then, will your condition fail.
BTW, you're missing some quotes on lines 13, 15, 22, 27.

I have not addressed your second problem.

Best wishes,
Manor Perets
Reply all
Reply to author
Forward
0 new messages