Loops and conditions, need direction please

working on some error correction scheme.

my code works when I try to find repeating 3's (2) times. But I need to
be able to find the patterns for any number of times. I am kind of lost on where to go from here. I know I am a little sloppy here, but maybe you can
give me some direction?

thanks a million!!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;

int analyzePatternDigitJeremyM(int arg);

int main () {
int a = 1333343333;
int b = -1333343333;

	
analyzePatternDigitJeremyM(a);

analyzePatternDigitJeremyM(b);

return 0;
}
int analyzePatternDigitJeremyM(int arg) {
	int tempInteger = arg;
	int modifyArg = arg;
	
	int count = 0;
	int digitPatternNumber;
	int digitPatternCount;
	int patternCount = 0;
	int tempPatternCount = 0;


cout << "please enter the digit to be found in a pattern" << endl;
cin >> digitPatternNumber;
cout << "please enter the amount of digits in the pattern to be found." << endl;
cin >> digitPatternCount;

do {									
	tempInteger = tempInteger / 10;
	count++;
} while (tempInteger != 0);
if(modifyArg < 0) {
	modifyArg = -modifyArg;
}
	
int* dynamicArray = new int[count];

for(int index = count -1 ; index >= 0; index--) {
		dynamicArray[index] = modifyArg % 10;
		modifyArg = modifyArg / 10;
}
for(int index = 0 ; count - 1 >= index; index++) {
	cout << dynamicArray[index];
}
cout << endl;

for(int index = count -1 ; index >= 0; index--) {
	if(dynamicArray[index] == digitPatternNumber) {
		tempPatternCount = digitPatternCount;
			if(dynamicArray[index + 1 - tempPatternCount] == digitPatternNumber) {
				tempPatternCount--;
			
				do {
					if(dynamicArray[index + 1 - tempPatternCount] == digitPatternNumber) {
						tempPatternCount--;
					} else if (dynamicArray[index + 1 - tempPatternCount] != digitPatternNumber) {
						break;
					}
				} while(tempPatternCount > 1);

				if(tempPatternCount == 0) {
					dynamicArray[index + 1- digitPatternCount] = 0;
					patternCount++;
					
				}
			}
	
	}

}
for(int index = 0 ; count - 1 >= index; index++) {
	cout << dynamicArray[index];
}
cout << "\n\nthe number of patterns found are: " << patternCount << endl;

return patternCount;
}
Hello,
could you explain a bit more the meaning of the digitPatternCount?
The statement dynamicArray[index + 1 - tempPatternCount] seems dangerous to me when digitPatternCount is > 1 (and index reaches 0).
digitPatternCount is the user input of the pattern length

sorry, I wasn't clearly stating my goal here, i think.

the goal to take the two given integers, seach out a digit pattern by way of user input.

i.e.
1333343333 , search out sets of 2,3,4, etc. and put a zero at the left most of the pattern.

so for 3 digit pattern of 2 would output 1030340303
3 digit pattern of 3 would output
1303343033
3 digit pattern of 4 would output
1033340333
thanks for that point, I understand now.

May me suggest you using string and the find method of the string.
The loop becomes shorter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <stdlib.h>

using namespace std;

int analyzePatternDigitJeremyM(int arg);

int main(void) {
    int a = 1333343333;
    int b = -1333343333;

    analyzePatternDigitJeremyM(a);
    analyzePatternDigitJeremyM(b);

    return 0;
}

int analyzePatternDigitJeremyM(int arg) {
    // transform the arg in order to process it more easily
    int positiveForm = abs(arg);
    stringstream theStream;
    theStream << positiveForm;
    string reverseStringForm = theStream.str();
    reverse(reverseStringForm.begin(),reverseStringForm.end());

    // inputs
    char digitPatternNumber = ' ';
    int digitPatternCount=0;
    cout << "please enter the digit to be found in a pattern" << endl;
    cin >> digitPatternNumber;
    cout << "please enter the amount of digits in the pattern to be found." << endl;
    cin >> digitPatternCount;
    string thePattern(digitPatternCount,digitPatternNumber);
    //inputs validation should be checked here: digitPatternCount > 0

    // search the first occurence of thePattern
    string::size_type index = reverseStringForm.find(thePattern);
    bool isTheLoopFinished = (index == string::npos);
    while (not isTheLoopFinished) {
        reverseStringForm.at(index+digitPatternCount-1) = '0';
        // search the next occurence of the Pattern
        index = reverseStringForm.find(thePattern,index+digitPatternCount);
        isTheLoopFinished = (index == string::npos);
    }
    //reverses the arg again, and outputs it
    reverse(reverseStringForm.begin(),reverseStringForm.end());
    cout << reverseStringForm << endl;
}


I hope this helps.
I'am not sure I've answered your question in fact.
Last edited on
Topic archived. No new replies allowed.