C language switch-case: avoiding a case after used once
I'm trying a challenge where I need to get a random number, and print the sum of the digits inside the number without duplicates:
for example, 123 will print 6 ( 1 + 2 + 3 ), and 32111 will do the same ( because we don't add duplicates to our sum, the sum of this number is similar to the sum of 123. )
In my solution I thought about using switch case for each number, and use a flag that its value is one, than in each case I add 1 to the flag, and when the flag is 2 I add the number to the sum, but I don't know how to avoid a case after it happend, which if I see it correctly will avoid me using multiple flags for each number (because if we could avoid a case after it happend, i just could set the flag back to one after the switch and do all the process again )
can you help me out? thanks a lot!
#include <stdio.h>
#define TEN 10
#define NINE 9
#define EIGHT 8
#define SEVEN 7
#define SIX 6
#define FIVE 5
#define FOUR 4
#define THREE 3
#define TWO 2
#define ONE 1
int main(void)
{
int answer = 0, i = 0, remain = 0, sum = 0, flag = 1;
printf("Enter a number: ");
scanf("%d", &answer);
while(answer >= ONE)
{
remain = answer % TEN;
answer /= TEN;
printf("%dn", remain);
switch (remain)
{
case ONE:
{
flag++;
if (flag == TWO)
{
sum = sum + ONE;
}
break;
}
case TWO:
{
flag++;
if (flag == TWO)
{
sum = sum + TWO;
}
break;
}
case THREE:
{
flag++;
if (flag == TWO)
{
sum = sum + THREE;
}
break;
}
case FOUR:
{
flag++;
if (flag == TWO)
{
sum = sum + FOUR;
}
break;
}
case FIVE:
{
flag++;
if (flag == TWO)
{
sum = sum + FIVE;
}
break;
}
case SIX:
{
flag++;
if (flag == TWO)
{
sum = sum + SIX;
}
break;
}
case SEVEN:
{
flag++;
if (flag == TWO)
{
sum = sum + SEVEN;
}
break;
}
case EIGHT:
{
flag++;
if (flag == TWO)
{
sum = sum + EIGHT;
}
break;
}
case NINE:
{
flag++;
if (flag == TWO)
{
sum = sum + NINE;
}
break;
}
default:
{
}
}
}
printf("The sum of the number is: %d", sum);
return 0;
}
c switch-statement
add a comment |
I'm trying a challenge where I need to get a random number, and print the sum of the digits inside the number without duplicates:
for example, 123 will print 6 ( 1 + 2 + 3 ), and 32111 will do the same ( because we don't add duplicates to our sum, the sum of this number is similar to the sum of 123. )
In my solution I thought about using switch case for each number, and use a flag that its value is one, than in each case I add 1 to the flag, and when the flag is 2 I add the number to the sum, but I don't know how to avoid a case after it happend, which if I see it correctly will avoid me using multiple flags for each number (because if we could avoid a case after it happend, i just could set the flag back to one after the switch and do all the process again )
can you help me out? thanks a lot!
#include <stdio.h>
#define TEN 10
#define NINE 9
#define EIGHT 8
#define SEVEN 7
#define SIX 6
#define FIVE 5
#define FOUR 4
#define THREE 3
#define TWO 2
#define ONE 1
int main(void)
{
int answer = 0, i = 0, remain = 0, sum = 0, flag = 1;
printf("Enter a number: ");
scanf("%d", &answer);
while(answer >= ONE)
{
remain = answer % TEN;
answer /= TEN;
printf("%dn", remain);
switch (remain)
{
case ONE:
{
flag++;
if (flag == TWO)
{
sum = sum + ONE;
}
break;
}
case TWO:
{
flag++;
if (flag == TWO)
{
sum = sum + TWO;
}
break;
}
case THREE:
{
flag++;
if (flag == TWO)
{
sum = sum + THREE;
}
break;
}
case FOUR:
{
flag++;
if (flag == TWO)
{
sum = sum + FOUR;
}
break;
}
case FIVE:
{
flag++;
if (flag == TWO)
{
sum = sum + FIVE;
}
break;
}
case SIX:
{
flag++;
if (flag == TWO)
{
sum = sum + SIX;
}
break;
}
case SEVEN:
{
flag++;
if (flag == TWO)
{
sum = sum + SEVEN;
}
break;
}
case EIGHT:
{
flag++;
if (flag == TWO)
{
sum = sum + EIGHT;
}
break;
}
case NINE:
{
flag++;
if (flag == TWO)
{
sum = sum + NINE;
}
break;
}
default:
{
}
}
}
printf("The sum of the number is: %d", sum);
return 0;
}
c switch-statement
2
Probably create an array of 10 integers, all initialized to zero. When you use a specific digit, set the element of the array to one; before using a digit, check that the element of the array is zero.
– Jonathan Leffler
Nov 22 '18 at 19:46
Do you have to use aswitch? With a bit mask, you can replace it with:unsigned int totmask = 0; unsigned int curmask; /* where switch goes: */ curmask = 1u << remain; if ((totmask & curmask) == 0) { sum += remain; totmask |= curmask; }
– Craig Estey
Nov 22 '18 at 20:25
add a comment |
I'm trying a challenge where I need to get a random number, and print the sum of the digits inside the number without duplicates:
for example, 123 will print 6 ( 1 + 2 + 3 ), and 32111 will do the same ( because we don't add duplicates to our sum, the sum of this number is similar to the sum of 123. )
In my solution I thought about using switch case for each number, and use a flag that its value is one, than in each case I add 1 to the flag, and when the flag is 2 I add the number to the sum, but I don't know how to avoid a case after it happend, which if I see it correctly will avoid me using multiple flags for each number (because if we could avoid a case after it happend, i just could set the flag back to one after the switch and do all the process again )
can you help me out? thanks a lot!
#include <stdio.h>
#define TEN 10
#define NINE 9
#define EIGHT 8
#define SEVEN 7
#define SIX 6
#define FIVE 5
#define FOUR 4
#define THREE 3
#define TWO 2
#define ONE 1
int main(void)
{
int answer = 0, i = 0, remain = 0, sum = 0, flag = 1;
printf("Enter a number: ");
scanf("%d", &answer);
while(answer >= ONE)
{
remain = answer % TEN;
answer /= TEN;
printf("%dn", remain);
switch (remain)
{
case ONE:
{
flag++;
if (flag == TWO)
{
sum = sum + ONE;
}
break;
}
case TWO:
{
flag++;
if (flag == TWO)
{
sum = sum + TWO;
}
break;
}
case THREE:
{
flag++;
if (flag == TWO)
{
sum = sum + THREE;
}
break;
}
case FOUR:
{
flag++;
if (flag == TWO)
{
sum = sum + FOUR;
}
break;
}
case FIVE:
{
flag++;
if (flag == TWO)
{
sum = sum + FIVE;
}
break;
}
case SIX:
{
flag++;
if (flag == TWO)
{
sum = sum + SIX;
}
break;
}
case SEVEN:
{
flag++;
if (flag == TWO)
{
sum = sum + SEVEN;
}
break;
}
case EIGHT:
{
flag++;
if (flag == TWO)
{
sum = sum + EIGHT;
}
break;
}
case NINE:
{
flag++;
if (flag == TWO)
{
sum = sum + NINE;
}
break;
}
default:
{
}
}
}
printf("The sum of the number is: %d", sum);
return 0;
}
c switch-statement
I'm trying a challenge where I need to get a random number, and print the sum of the digits inside the number without duplicates:
for example, 123 will print 6 ( 1 + 2 + 3 ), and 32111 will do the same ( because we don't add duplicates to our sum, the sum of this number is similar to the sum of 123. )
In my solution I thought about using switch case for each number, and use a flag that its value is one, than in each case I add 1 to the flag, and when the flag is 2 I add the number to the sum, but I don't know how to avoid a case after it happend, which if I see it correctly will avoid me using multiple flags for each number (because if we could avoid a case after it happend, i just could set the flag back to one after the switch and do all the process again )
can you help me out? thanks a lot!
#include <stdio.h>
#define TEN 10
#define NINE 9
#define EIGHT 8
#define SEVEN 7
#define SIX 6
#define FIVE 5
#define FOUR 4
#define THREE 3
#define TWO 2
#define ONE 1
int main(void)
{
int answer = 0, i = 0, remain = 0, sum = 0, flag = 1;
printf("Enter a number: ");
scanf("%d", &answer);
while(answer >= ONE)
{
remain = answer % TEN;
answer /= TEN;
printf("%dn", remain);
switch (remain)
{
case ONE:
{
flag++;
if (flag == TWO)
{
sum = sum + ONE;
}
break;
}
case TWO:
{
flag++;
if (flag == TWO)
{
sum = sum + TWO;
}
break;
}
case THREE:
{
flag++;
if (flag == TWO)
{
sum = sum + THREE;
}
break;
}
case FOUR:
{
flag++;
if (flag == TWO)
{
sum = sum + FOUR;
}
break;
}
case FIVE:
{
flag++;
if (flag == TWO)
{
sum = sum + FIVE;
}
break;
}
case SIX:
{
flag++;
if (flag == TWO)
{
sum = sum + SIX;
}
break;
}
case SEVEN:
{
flag++;
if (flag == TWO)
{
sum = sum + SEVEN;
}
break;
}
case EIGHT:
{
flag++;
if (flag == TWO)
{
sum = sum + EIGHT;
}
break;
}
case NINE:
{
flag++;
if (flag == TWO)
{
sum = sum + NINE;
}
break;
}
default:
{
}
}
}
printf("The sum of the number is: %d", sum);
return 0;
}
c switch-statement
c switch-statement
asked Nov 22 '18 at 19:40
xTheMoonLigherx 007xTheMoonLigherx 007
224
224
2
Probably create an array of 10 integers, all initialized to zero. When you use a specific digit, set the element of the array to one; before using a digit, check that the element of the array is zero.
– Jonathan Leffler
Nov 22 '18 at 19:46
Do you have to use aswitch? With a bit mask, you can replace it with:unsigned int totmask = 0; unsigned int curmask; /* where switch goes: */ curmask = 1u << remain; if ((totmask & curmask) == 0) { sum += remain; totmask |= curmask; }
– Craig Estey
Nov 22 '18 at 20:25
add a comment |
2
Probably create an array of 10 integers, all initialized to zero. When you use a specific digit, set the element of the array to one; before using a digit, check that the element of the array is zero.
– Jonathan Leffler
Nov 22 '18 at 19:46
Do you have to use aswitch? With a bit mask, you can replace it with:unsigned int totmask = 0; unsigned int curmask; /* where switch goes: */ curmask = 1u << remain; if ((totmask & curmask) == 0) { sum += remain; totmask |= curmask; }
– Craig Estey
Nov 22 '18 at 20:25
2
2
Probably create an array of 10 integers, all initialized to zero. When you use a specific digit, set the element of the array to one; before using a digit, check that the element of the array is zero.
– Jonathan Leffler
Nov 22 '18 at 19:46
Probably create an array of 10 integers, all initialized to zero. When you use a specific digit, set the element of the array to one; before using a digit, check that the element of the array is zero.
– Jonathan Leffler
Nov 22 '18 at 19:46
Do you have to use a
switch? With a bit mask, you can replace it with: unsigned int totmask = 0; unsigned int curmask; /* where switch goes: */ curmask = 1u << remain; if ((totmask & curmask) == 0) { sum += remain; totmask |= curmask; }– Craig Estey
Nov 22 '18 at 20:25
Do you have to use a
switch? With a bit mask, you can replace it with: unsigned int totmask = 0; unsigned int curmask; /* where switch goes: */ curmask = 1u << remain; if ((totmask & curmask) == 0) { sum += remain; totmask |= curmask; }– Craig Estey
Nov 22 '18 at 20:25
add a comment |
2 Answers
2
active
oldest
votes
Try using a bitmask representing each case. The main idea is to keep track for each number (from 0 to 9) using only one integer. Some bit of this single integer can be used as to find whether this number was seen before or not. If the bit is 0 then the corresponding number is seen for the first time (and we now set the bit to 1), and if we see that the bit is already 1, then we don't add it into our final sum.
int mask = 0;
switch (remain) {
case 1: // 001
if ((mask & 1) == 0) { // 1 = 1 << 0
sum += 1;
mask |= 1;
}
break;
...
case 3: // 100
if ((mask & 4) == 0) { // 4 = 1 << 2
sum += 3;
mask |= 4;
}
break;
...
case n:
if ((mask & k) == 0) { // k = 1 << (n-1)
sum += n;
mask |= k;
}
break;
...
Now you can see a pattern in the last case I used, we can simplify this switch-case into a single if statement.
int mask = 0;
int sum = 0;
while (answer) {
remain = answer % 10;
answer /= 10;
int offset = remain;
int flag = 1 << offset;
if ((mask & flag) == 0) {
sum += remain;
mask |= flag;
}
}
// sum contains the required answer
Each bit represents one of the cases, since you have only 10 cases this is the most efficient way to track the flags as you have more than 10 bits for an integer and it will be wastage to have a separate boolean flag for 0 to 9.
1
Thank you! i forgot that bitwise and exists... thanks!
– xTheMoonLigherx 007
Nov 22 '18 at 19:57
2
int offset = remain - 1;– shifting negative positions is undefined: If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
– Swordfish
Nov 22 '18 at 20:06
You are totally right of course, I forgot about that evil 0.
– Minn
Nov 22 '18 at 20:58
@Minn Its still there in a comment, the evil -1 ;)
– Swordfish
Nov 23 '18 at 2:45
add a comment |
A case-term is a compile time constant, so you cannot "disable" it at runtime at the c language level. You would have to introduce a separate flag for each digit then.
I'd say - and have a look at you code - the switch-case approach is not the best as you duplicate a lot of similar code. A much easier way would be to have an array of 10 ints, each standing for a particular digit, and once a digit is encountered, set the respective array element to 1. At the end sum up in a loop.
If you have troubles getting this approach running, don't hesitate to ask again...
I like the array approach, but in this challenge I'm not allowed to use arrays, do you have any other suggestion to do the challenge without using tons of variables? thanks for helping :D
– xTheMoonLigherx 007
Nov 22 '18 at 19:51
1
@xTheMoonLigherx007 You could use bits of anunsigned.
– Swordfish
Nov 22 '18 at 19:52
1
Use a bitmask then.
– Havenard
Nov 22 '18 at 19:52
I'll check the bitmask approach, also, what do you mean by using bits of an unsigned?
– xTheMoonLigherx 007
Nov 22 '18 at 19:54
bitmasks are usually used together with unsigned integrals; manipulating the most significant bit of a signed int may lead to undefined behaviour. In your case, as you need just 10 bits, it should not make a difference.
– Stephan Lechner
Nov 22 '18 at 19:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437210%2fc-language-switch-case-avoiding-a-case-after-used-once%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try using a bitmask representing each case. The main idea is to keep track for each number (from 0 to 9) using only one integer. Some bit of this single integer can be used as to find whether this number was seen before or not. If the bit is 0 then the corresponding number is seen for the first time (and we now set the bit to 1), and if we see that the bit is already 1, then we don't add it into our final sum.
int mask = 0;
switch (remain) {
case 1: // 001
if ((mask & 1) == 0) { // 1 = 1 << 0
sum += 1;
mask |= 1;
}
break;
...
case 3: // 100
if ((mask & 4) == 0) { // 4 = 1 << 2
sum += 3;
mask |= 4;
}
break;
...
case n:
if ((mask & k) == 0) { // k = 1 << (n-1)
sum += n;
mask |= k;
}
break;
...
Now you can see a pattern in the last case I used, we can simplify this switch-case into a single if statement.
int mask = 0;
int sum = 0;
while (answer) {
remain = answer % 10;
answer /= 10;
int offset = remain;
int flag = 1 << offset;
if ((mask & flag) == 0) {
sum += remain;
mask |= flag;
}
}
// sum contains the required answer
Each bit represents one of the cases, since you have only 10 cases this is the most efficient way to track the flags as you have more than 10 bits for an integer and it will be wastage to have a separate boolean flag for 0 to 9.
1
Thank you! i forgot that bitwise and exists... thanks!
– xTheMoonLigherx 007
Nov 22 '18 at 19:57
2
int offset = remain - 1;– shifting negative positions is undefined: If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
– Swordfish
Nov 22 '18 at 20:06
You are totally right of course, I forgot about that evil 0.
– Minn
Nov 22 '18 at 20:58
@Minn Its still there in a comment, the evil -1 ;)
– Swordfish
Nov 23 '18 at 2:45
add a comment |
Try using a bitmask representing each case. The main idea is to keep track for each number (from 0 to 9) using only one integer. Some bit of this single integer can be used as to find whether this number was seen before or not. If the bit is 0 then the corresponding number is seen for the first time (and we now set the bit to 1), and if we see that the bit is already 1, then we don't add it into our final sum.
int mask = 0;
switch (remain) {
case 1: // 001
if ((mask & 1) == 0) { // 1 = 1 << 0
sum += 1;
mask |= 1;
}
break;
...
case 3: // 100
if ((mask & 4) == 0) { // 4 = 1 << 2
sum += 3;
mask |= 4;
}
break;
...
case n:
if ((mask & k) == 0) { // k = 1 << (n-1)
sum += n;
mask |= k;
}
break;
...
Now you can see a pattern in the last case I used, we can simplify this switch-case into a single if statement.
int mask = 0;
int sum = 0;
while (answer) {
remain = answer % 10;
answer /= 10;
int offset = remain;
int flag = 1 << offset;
if ((mask & flag) == 0) {
sum += remain;
mask |= flag;
}
}
// sum contains the required answer
Each bit represents one of the cases, since you have only 10 cases this is the most efficient way to track the flags as you have more than 10 bits for an integer and it will be wastage to have a separate boolean flag for 0 to 9.
1
Thank you! i forgot that bitwise and exists... thanks!
– xTheMoonLigherx 007
Nov 22 '18 at 19:57
2
int offset = remain - 1;– shifting negative positions is undefined: If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
– Swordfish
Nov 22 '18 at 20:06
You are totally right of course, I forgot about that evil 0.
– Minn
Nov 22 '18 at 20:58
@Minn Its still there in a comment, the evil -1 ;)
– Swordfish
Nov 23 '18 at 2:45
add a comment |
Try using a bitmask representing each case. The main idea is to keep track for each number (from 0 to 9) using only one integer. Some bit of this single integer can be used as to find whether this number was seen before or not. If the bit is 0 then the corresponding number is seen for the first time (and we now set the bit to 1), and if we see that the bit is already 1, then we don't add it into our final sum.
int mask = 0;
switch (remain) {
case 1: // 001
if ((mask & 1) == 0) { // 1 = 1 << 0
sum += 1;
mask |= 1;
}
break;
...
case 3: // 100
if ((mask & 4) == 0) { // 4 = 1 << 2
sum += 3;
mask |= 4;
}
break;
...
case n:
if ((mask & k) == 0) { // k = 1 << (n-1)
sum += n;
mask |= k;
}
break;
...
Now you can see a pattern in the last case I used, we can simplify this switch-case into a single if statement.
int mask = 0;
int sum = 0;
while (answer) {
remain = answer % 10;
answer /= 10;
int offset = remain;
int flag = 1 << offset;
if ((mask & flag) == 0) {
sum += remain;
mask |= flag;
}
}
// sum contains the required answer
Each bit represents one of the cases, since you have only 10 cases this is the most efficient way to track the flags as you have more than 10 bits for an integer and it will be wastage to have a separate boolean flag for 0 to 9.
Try using a bitmask representing each case. The main idea is to keep track for each number (from 0 to 9) using only one integer. Some bit of this single integer can be used as to find whether this number was seen before or not. If the bit is 0 then the corresponding number is seen for the first time (and we now set the bit to 1), and if we see that the bit is already 1, then we don't add it into our final sum.
int mask = 0;
switch (remain) {
case 1: // 001
if ((mask & 1) == 0) { // 1 = 1 << 0
sum += 1;
mask |= 1;
}
break;
...
case 3: // 100
if ((mask & 4) == 0) { // 4 = 1 << 2
sum += 3;
mask |= 4;
}
break;
...
case n:
if ((mask & k) == 0) { // k = 1 << (n-1)
sum += n;
mask |= k;
}
break;
...
Now you can see a pattern in the last case I used, we can simplify this switch-case into a single if statement.
int mask = 0;
int sum = 0;
while (answer) {
remain = answer % 10;
answer /= 10;
int offset = remain;
int flag = 1 << offset;
if ((mask & flag) == 0) {
sum += remain;
mask |= flag;
}
}
// sum contains the required answer
Each bit represents one of the cases, since you have only 10 cases this is the most efficient way to track the flags as you have more than 10 bits for an integer and it will be wastage to have a separate boolean flag for 0 to 9.
edited Nov 22 '18 at 20:55
answered Nov 22 '18 at 19:55
MinnMinn
748215
748215
1
Thank you! i forgot that bitwise and exists... thanks!
– xTheMoonLigherx 007
Nov 22 '18 at 19:57
2
int offset = remain - 1;– shifting negative positions is undefined: If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
– Swordfish
Nov 22 '18 at 20:06
You are totally right of course, I forgot about that evil 0.
– Minn
Nov 22 '18 at 20:58
@Minn Its still there in a comment, the evil -1 ;)
– Swordfish
Nov 23 '18 at 2:45
add a comment |
1
Thank you! i forgot that bitwise and exists... thanks!
– xTheMoonLigherx 007
Nov 22 '18 at 19:57
2
int offset = remain - 1;– shifting negative positions is undefined: If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
– Swordfish
Nov 22 '18 at 20:06
You are totally right of course, I forgot about that evil 0.
– Minn
Nov 22 '18 at 20:58
@Minn Its still there in a comment, the evil -1 ;)
– Swordfish
Nov 23 '18 at 2:45
1
1
Thank you! i forgot that bitwise and exists... thanks!
– xTheMoonLigherx 007
Nov 22 '18 at 19:57
Thank you! i forgot that bitwise and exists... thanks!
– xTheMoonLigherx 007
Nov 22 '18 at 19:57
2
2
int offset = remain - 1; – shifting negative positions is undefined: If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.– Swordfish
Nov 22 '18 at 20:06
int offset = remain - 1; – shifting negative positions is undefined: If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.– Swordfish
Nov 22 '18 at 20:06
You are totally right of course, I forgot about that evil 0.
– Minn
Nov 22 '18 at 20:58
You are totally right of course, I forgot about that evil 0.
– Minn
Nov 22 '18 at 20:58
@Minn Its still there in a comment, the evil -1 ;)
– Swordfish
Nov 23 '18 at 2:45
@Minn Its still there in a comment, the evil -1 ;)
– Swordfish
Nov 23 '18 at 2:45
add a comment |
A case-term is a compile time constant, so you cannot "disable" it at runtime at the c language level. You would have to introduce a separate flag for each digit then.
I'd say - and have a look at you code - the switch-case approach is not the best as you duplicate a lot of similar code. A much easier way would be to have an array of 10 ints, each standing for a particular digit, and once a digit is encountered, set the respective array element to 1. At the end sum up in a loop.
If you have troubles getting this approach running, don't hesitate to ask again...
I like the array approach, but in this challenge I'm not allowed to use arrays, do you have any other suggestion to do the challenge without using tons of variables? thanks for helping :D
– xTheMoonLigherx 007
Nov 22 '18 at 19:51
1
@xTheMoonLigherx007 You could use bits of anunsigned.
– Swordfish
Nov 22 '18 at 19:52
1
Use a bitmask then.
– Havenard
Nov 22 '18 at 19:52
I'll check the bitmask approach, also, what do you mean by using bits of an unsigned?
– xTheMoonLigherx 007
Nov 22 '18 at 19:54
bitmasks are usually used together with unsigned integrals; manipulating the most significant bit of a signed int may lead to undefined behaviour. In your case, as you need just 10 bits, it should not make a difference.
– Stephan Lechner
Nov 22 '18 at 19:59
add a comment |
A case-term is a compile time constant, so you cannot "disable" it at runtime at the c language level. You would have to introduce a separate flag for each digit then.
I'd say - and have a look at you code - the switch-case approach is not the best as you duplicate a lot of similar code. A much easier way would be to have an array of 10 ints, each standing for a particular digit, and once a digit is encountered, set the respective array element to 1. At the end sum up in a loop.
If you have troubles getting this approach running, don't hesitate to ask again...
I like the array approach, but in this challenge I'm not allowed to use arrays, do you have any other suggestion to do the challenge without using tons of variables? thanks for helping :D
– xTheMoonLigherx 007
Nov 22 '18 at 19:51
1
@xTheMoonLigherx007 You could use bits of anunsigned.
– Swordfish
Nov 22 '18 at 19:52
1
Use a bitmask then.
– Havenard
Nov 22 '18 at 19:52
I'll check the bitmask approach, also, what do you mean by using bits of an unsigned?
– xTheMoonLigherx 007
Nov 22 '18 at 19:54
bitmasks are usually used together with unsigned integrals; manipulating the most significant bit of a signed int may lead to undefined behaviour. In your case, as you need just 10 bits, it should not make a difference.
– Stephan Lechner
Nov 22 '18 at 19:59
add a comment |
A case-term is a compile time constant, so you cannot "disable" it at runtime at the c language level. You would have to introduce a separate flag for each digit then.
I'd say - and have a look at you code - the switch-case approach is not the best as you duplicate a lot of similar code. A much easier way would be to have an array of 10 ints, each standing for a particular digit, and once a digit is encountered, set the respective array element to 1. At the end sum up in a loop.
If you have troubles getting this approach running, don't hesitate to ask again...
A case-term is a compile time constant, so you cannot "disable" it at runtime at the c language level. You would have to introduce a separate flag for each digit then.
I'd say - and have a look at you code - the switch-case approach is not the best as you duplicate a lot of similar code. A much easier way would be to have an array of 10 ints, each standing for a particular digit, and once a digit is encountered, set the respective array element to 1. At the end sum up in a loop.
If you have troubles getting this approach running, don't hesitate to ask again...
answered Nov 22 '18 at 19:47
Stephan LechnerStephan Lechner
28.6k32143
28.6k32143
I like the array approach, but in this challenge I'm not allowed to use arrays, do you have any other suggestion to do the challenge without using tons of variables? thanks for helping :D
– xTheMoonLigherx 007
Nov 22 '18 at 19:51
1
@xTheMoonLigherx007 You could use bits of anunsigned.
– Swordfish
Nov 22 '18 at 19:52
1
Use a bitmask then.
– Havenard
Nov 22 '18 at 19:52
I'll check the bitmask approach, also, what do you mean by using bits of an unsigned?
– xTheMoonLigherx 007
Nov 22 '18 at 19:54
bitmasks are usually used together with unsigned integrals; manipulating the most significant bit of a signed int may lead to undefined behaviour. In your case, as you need just 10 bits, it should not make a difference.
– Stephan Lechner
Nov 22 '18 at 19:59
add a comment |
I like the array approach, but in this challenge I'm not allowed to use arrays, do you have any other suggestion to do the challenge without using tons of variables? thanks for helping :D
– xTheMoonLigherx 007
Nov 22 '18 at 19:51
1
@xTheMoonLigherx007 You could use bits of anunsigned.
– Swordfish
Nov 22 '18 at 19:52
1
Use a bitmask then.
– Havenard
Nov 22 '18 at 19:52
I'll check the bitmask approach, also, what do you mean by using bits of an unsigned?
– xTheMoonLigherx 007
Nov 22 '18 at 19:54
bitmasks are usually used together with unsigned integrals; manipulating the most significant bit of a signed int may lead to undefined behaviour. In your case, as you need just 10 bits, it should not make a difference.
– Stephan Lechner
Nov 22 '18 at 19:59
I like the array approach, but in this challenge I'm not allowed to use arrays, do you have any other suggestion to do the challenge without using tons of variables? thanks for helping :D
– xTheMoonLigherx 007
Nov 22 '18 at 19:51
I like the array approach, but in this challenge I'm not allowed to use arrays, do you have any other suggestion to do the challenge without using tons of variables? thanks for helping :D
– xTheMoonLigherx 007
Nov 22 '18 at 19:51
1
1
@xTheMoonLigherx007 You could use bits of an
unsigned.– Swordfish
Nov 22 '18 at 19:52
@xTheMoonLigherx007 You could use bits of an
unsigned.– Swordfish
Nov 22 '18 at 19:52
1
1
Use a bitmask then.
– Havenard
Nov 22 '18 at 19:52
Use a bitmask then.
– Havenard
Nov 22 '18 at 19:52
I'll check the bitmask approach, also, what do you mean by using bits of an unsigned?
– xTheMoonLigherx 007
Nov 22 '18 at 19:54
I'll check the bitmask approach, also, what do you mean by using bits of an unsigned?
– xTheMoonLigherx 007
Nov 22 '18 at 19:54
bitmasks are usually used together with unsigned integrals; manipulating the most significant bit of a signed int may lead to undefined behaviour. In your case, as you need just 10 bits, it should not make a difference.
– Stephan Lechner
Nov 22 '18 at 19:59
bitmasks are usually used together with unsigned integrals; manipulating the most significant bit of a signed int may lead to undefined behaviour. In your case, as you need just 10 bits, it should not make a difference.
– Stephan Lechner
Nov 22 '18 at 19:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437210%2fc-language-switch-case-avoiding-a-case-after-used-once%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Probably create an array of 10 integers, all initialized to zero. When you use a specific digit, set the element of the array to one; before using a digit, check that the element of the array is zero.
– Jonathan Leffler
Nov 22 '18 at 19:46
Do you have to use a
switch? With a bit mask, you can replace it with:unsigned int totmask = 0; unsigned int curmask; /* where switch goes: */ curmask = 1u << remain; if ((totmask & curmask) == 0) { sum += remain; totmask |= curmask; }– Craig Estey
Nov 22 '18 at 20:25