Michael Wendel - 3 months ago 19

C Question

I'm trying to write a simple C program that generates 1 of 3 cases:

- An integer with no adjacent digits the same.
- An integer with a single pair of same adjacent digits.
- An integer with a single trio of same adjacent digits.

After the integer value does not meet the requirements of any one of the cases, the integer will be incremented by 1 and then checked again.

Conditions: The user with enter an option between 1-3, the starting integer value is a non-negative integer, and the generated value is larger than the starting integer value entered by the user.

NOTE: I may not use a method involving an array or string. Recursion is allowed however my professor discourages its use.

My current issue is determining how to compare the digits in the entered integer value without creating a new variable for each one. Here's what I have so far:

`int getOption() //retrieves the option entered by the user`

{

int option;

do

{

printf("\nEnter desired option: ");

scanf("%d", &option);

if (option <= 0 || option > 3)

{

printf("\nError! Invalid option selected!!\n");

}

}while(option <= 0 || option > 3);

return(option);

}

int getStart() //retrieves the starting integer value entered by the user

{

int startValue;

do

{

printf("\nEnter starting integer: ");

scanf("%d", &startValue);

if (startValue <= 0)

{

printf("\nError! Non-negative values only!!\n");

}

}while(startValue <= 0);

return(startValue);

}

int checkSame(int startValue); //checks if the user-given integer has any pairs of same adjecent digits.

{

int counter;

int sameAdjacent = 0;

while (sameAdjacent < 1)

{

while(startValue > 0)

{

startValue = startValue % 10;

counter++;

}

for(startValue; startValue > 0; startValue / 10)

{

//?????

}

}

}

Here's some examples of what a proper execution looks like:

`1. No adjacent digits the same.`

2. A single pair of adjacent digits the same.

3. A single trio of adjacent digits the same.

Enter desired option: 1

Enter starting integer: 1222

Next larger value with no two adjacent digits the same is 1230.

1. No adjacent digits the same.

2. A single pair of adjacent digits the same.

3. A single trio of adjacent digits the same.

Enter desired option: 2

Enter starting integer: 133300

Next larger value with only a single pair of digits the same is 133401.

1. No adjacent digits the same.

2. A single pair of adjacent digits the same.

3. A single trio of adjacent digits the same.

Enter desired option: 3

Enter starting integer: 123456

Next larger value with only a single trio of digits the same is 123555.

Any help is appreciated. Thank you!

Answer

Since this is an assignment that you're presumably meant to solve on your own, let me just give a hint to get you on the right track:

- If
`x`

is an integer variable, then:`x % 10`

gives you the last base-10 digit of`x`

, and`x = x / 10`

chops off the last digit of`x`

.

Also note that you can save the last digit of `x`

in another variable before you chop it off. In fact, you can easily save the two (or more!) most recently chopped-off digits this way, just by having one variable for each of them.

If that's not a sufficient hint, let me know where you're still stuck and I'll try to give a bit more help.

OK, since you say you're still stuck, let me show you how I'd solve the problem:

```
int pairs = 0, triples = 0;
int digit1 = -1, digit2 = -1, digit3 = -1; // no actual digit can be -1
while (x > 0) {
// keep track of the last three digits chopped off
digit3 = digit2;
digit2 = digit1;
digit1 = x % 10;
// chop off the last digit of x
x /= 10;
// check whether we've just chopped off a pair or a triple
if (digit1 == digit2) {
pairs++;
if (digit2 == digit3) {
triples++;
}
}
// uncomment these lines for debugging output:
// fprintf(stderr, "x = %d, digit(1-3) = %d, %d, %d; %d pairs and %d triples seen\n",
// x, digit1, digit2, digit3, pairs, triples);
}
```

As an exercise, you may want to run this code for various values of `x`

, and either step through the loop in a debugger (recommended!) or uncomment the `fprintf()`

call to see how the variables change on each iteration.

Once you've done that, I believe you should be able to easily fill in the rest of the assignment.