Switch statement with two arguments - c

I have a question related to switch statement with 2 arguments. I do not understand which value to look for. I've read somewhere that comma(,) is equal to AND,but that is not helping me much since when debugging it goes to case 1,case 1,and then default,so I really don't get it.
Any help would be appreaciated,Thanks!
#define ADD(x, y) x+y
#define MUL(x, y) x*y
void main() {
int arr[6] = { 012, -2, 7 - 011, 0x1F, 3 }, res = 0;
int n = sizeof(arr) / sizeof(int), i, j;
for (i = -1, j = n; i <= n / 2;) {
switch (--j, ++i) {
case 0: res += MUL(arr[i], arr[j]);
case 1: res += ADD(arr[i], arr[j]); break;
case 4: res += MUL(ADD(arr[i], arr[j]), ADD(arr[i + 1], res));
default: res++; break;
}
}
printf("%d", res);
} ```

Operator comma is not equal to AND. Operator comma evaluates operands from left to right, and returns the last result. Thus,
switch (--j, ++i) {
i = 0;
it goes to the case 0, you don`t have break statement so you go to the case 1

Related

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("%d\n", 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;
}
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.
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...

Program that moves the knight on the chessboard

This program changes the position of a chessboard from 0 to 1 when the knight moves from one position to another. If I try the case 0 the program works. Then after the case 0 if I try the case 1 the position changed to 1 is the one that is placed up-left from the case 0 one. Instead it should be the one placed up-up-right from the case 0 one. Why is the output like this?
#include <stdio.h>
int main(){
int board[8][8]={0};
int currentRow=4, currentColumn=4;
int cont=0, moveNumber=0, i, j;
while(moveNumber>=0 && moveNumber<=7){
printf("Enter a move: ");
scanf("%d", &moveNumber);
cont++;
switch(moveNumber){
case 0:
board[currentRow-1][currentColumn+2]=1;
break;
case 1:
board[currentRow-2][currentColumn+1]=1;
break;
case 2:
board[currentRow-2][currentColumn-1]=1;
break;
case 3:
board[currentRow-1][currentColumn-2]=1;
break;
case 4:
board[currentRow+1][currentColumn-2]=1;
break;
case 5:
board[currentRow+2][currentColumn-1]=1;
break;
case 6:
board[currentRow+2][currentColumn+1]=1;
break;
case 7:
board[currentRow+1][currentColumn+2]=1;
break;
}
for(i=0; i<8; i++){
for(j=0; j<8; j++){
printf("%d ", board[i][j]);
}
printf("\n");
}
printf("Total moves: %d\n",cont);
}
return 0;
}
The main issue is that you never update currentRow or currentColumn, so all moves happen from their initial values.
Some other notes: You should avoid "magic numbers" - 8 in this case. If you decide to change the size of the array, you have to search through all the code and find the eights and replace them. Use a define or const int.
You should always check the return value from scanf. What if the user types a, for example?
Array bounds checking is needed. What happens when a move takes the knight off the board - and past the bounds of the array?
#include <stdio.h>
// Avoid magic numbers
#define ROWS 8
#define COLS 8
int main(){
int board[ROWS][COLS] = {0};
int currentRow = 4, currentColumn = 4;
int cont = 0, moveNumber = 0, i, j;
// Loop forever
while (1) {
printf("Enter a move: ");
// Make sure user enters a number
while (1 != scanf("%d", &moveNumber)) {
// clear stdin
int c;
while((c = getchar()) != '\n' && c != EOF);
// See https://stackoverflow.com/a/6277391/669576
// for why fgets/sscanf is a better option than scanf
// Prompt user for new input
printf("Enter a valid integer:");
}
// Moved this here
if (moveNumber < 0 || moveNumber > 7) break;
cont++;
// Going to use some temp vars to calculate indices
int tempRow, tempCol;
// Calc new indices
switch (moveNumber) {
case 0:
tempRow = currentRow - 1;
tempCol = currentColumn + 2;
break;
case 1:
tempRow = currentRow - 2;
tempCol = currentColumn + 1;
break;
case 2:
tempRow = currentRow - 2;
tempCol = currentColumn - 1;
break;
case 3:
tempRow = currentRow - 1;
tempCol = currentColumn - 2;
break;
case 4:
tempRow = currentRow + 1;
tempCol = currentColumn - 2;
break;
case 5:
tempRow = currentRow + 2;
tempCol = currentColumn - 1;
break;
case 6:
tempRow = currentRow + 2;
tempCol = currentColumn + 1;
break;
case 7:
tempRow = currentRow + 1;
tempCol = currentColumn + 2;
break;
}
// Make sure we have valid indices
if (tempRow < 0 || tempCol < 0 || tempRow >= ROWS || tempCol >= COLS) {
printf("Illegal move\n");
}
else {
// Update the board
currentRow = tempRow;
currentColumn = tempCol;
board[currentRow][currentColumn] = 1;
// And print
for(i = 0; i < ROWS; i++){
for(j = 0; j < COLS; j++){
printf("%d ", board[i][j]);
}
printf("\n");
}
printf("Total moves: %d\n", cont);
}
}
return 0;
}
As #Jonhnny Mopp pointed out, you never update currentRow or currentColumn.
So after case 0 you set board[3][6] and after case 1 you set board[2][5].
So the return of case 1 is up-left of case 0.
I recommend you write a function TryToMoveKnight(int* x, int* y, int moveNumber) that attempts to move a knight in the desired direction.
You already have eight lines of very similar code, and the more work you need to do to move the piece, the worse it's going to get.
Things the function needs to do:
determine the target coordinates from the moveNumber and current
position
verify that the target coordinates are on the board
possibly check for a piece at that position
assign the new coordinates to your variables x and y whose addresses you passed in
This last step was missing from your original code, but checking that the target position is on the board is also going to be essential.
So where you currently have switch(moveNumber)... you would instead call TryToMoveKnight(&currentRow,&currentColumn,moveNumber);

C - getting prime numbers using this algorithm

I am fighting some simple question.
I want to get prime numbers
I will use this algorithm
and... I finished code writing like this.
int k = 0, x = 1, n, prim, lim = 1;
int p[100000];
int xCount=0, limCount=0, kCount=0;
p[0] = 2;
scanf("%d", &n);
start = clock();
do
{
x += 2; xCount++;
if (sqrt(p[lim]) <= x)
{
lim++; limCount++;
}
k = 2; prim = true;
while (prim && k<lim)
{
if (x % p[k] == 0)
prim = false;
k++; kCount++;
}
if (prim == true)
{
p[lim] = x;
printf("prime number : %d\n", p[lim]);
}
} while (k<n);
I want to check how much repeat this code (x+=2; lim++; k++;)
so I used xCount, limCount, kCount variables.
when input(n) is 10, the results are x : 14, lim : 9, k : 43. wrong answer.
answer is (14,3,13).
Did I write code not well?
tell me correct point plz...
If you want to adapt an algorithm to your needs, it's always a good idea to implement it verbatim first, especially if you have pseudocode that is detailed enough to allow for such a verbatim translation into C-code (even more so with Fortran but I digress)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main (void){
// type index 1..n
int index;
// var
// x: integer
int x;
//i, k, lim: integer
int i, k, lim;
// prim: boolean
bool prim;
// p: array[index] of integer {p[i] = i'th prime number}
/*
We cannot do that directly, we need to know the value of "index" first
*/
int res;
res = scanf("%d", &index);
if(res != 1 || index < 1){
fprintf(stderr,"Only integral values >= 1, please. Thank you.\n");
return EXIT_FAILURE;
}
/*
The array from the pseudocode is a one-based array, take care
*/
int p[index + 1];
// initialize the whole array with distinguishable values in case of debugging
for(i = 0;i<index;i++){
p[i] = -i;
}
/*
Your variables
*/
int lim_count = 0, k_count = 0;
// begin
// p[1] = 2
p[1] = 2;
// write(2)
puts("2");
// x = 1
x = 1;
// lim = 1
lim = 1;
// for i:=2 to n do
for(i = 2;i < index; i++){
// repeat (until prim)
do {
// x = x + 2
x += 2;
// if(sqr(p[lim]) <= x) then
if(p[lim] * p[lim] <= x){
// lim = lim +1
lim++;
lim_count++;
}
// k = 2
k = 2;
// prim = true
prim = true;
// while (prim and (k < lim)) do
while (prim && (k < lim)){
// prim = "x is not divisible by p[k]"
if((x % p[k]) == 0){
prim = false;
}
// k = k + 1
k++;
k_count++;
}
// (repeat) until prim
} while(!prim);
// p[i] := x
p[i] = x;
// write(x)
printf("%d\n",x);
}
// end
printf("x = %d, lim_count = %d, k_count = %d \n",x,lim_count,k_count);
for(i = 0;i<index;i++){
printf("%d, ",p[i]);
}
putchar('\n');
return EXIT_SUCCESS;
}
It will print an index - 1 number of primes starting at 2.
You can easily change it now--for example: print only the primes up to index instead of index - 1 primes.
In your case the numbers for all six primes up to 13 gives
x = 13, lim_count = 2, k_count = 3
which is distinctly different from the result you want.
Your translation looks very sloppy.
for i:= 2 to n do begin
must translate to:
for (i=2; i<=n; i++)
repeat
....
until prim
must translate to:
do {
...
} while (!prim);
The while prim... loop is inside the repeat...until prim loop.
I leave it to you to apply this to your code and to check that all constructs have been properly translated. it doesn't look too difficult to do that correctly.
Note: it looks like the algorithm uses 1-based arrays whereas C uses 0-based arrays.

How to find closest number in C?

So, I was working out some exercises from a recommended beginner's book: C Programming: A Modern Approach (2nd Edition) While working out one of the questions I found myself unable to proceed, as I couldn't return the number closest to the user's input.
Note: The question was asked before loops, functions and arrays were covered, thus I am assuming that these should not be required.
I managed to extract and simplify the problem to the following. Lets say I have the following numbers, and I want to return the number closest to the user's input:
10 20 30 40 50 60 70 80
printf("Enter a number to find closest value: ");
scanf("%d", &num);
For example:
User's input: 28
Closest number: 30
Next, I decided to find the difference between num and each of the numbers by subtracting from each.
difference1 = num - 10;
difference2 = num - 20;
and so on. (I am not using loops as these have not yet been covered in the book)
I taking into consideration, negative differences (25 - 40 = -15). If the difference is less than 0, I am multiplying the difference by -1 to get all the differences to a positive integer. By doing so I will be able to compare the differences successfully:
if (difference1 < 0) {
difference1 = difference1 * -1;
}
Next I am checking for the minimum difference in order to identify which was the closest number by doing:
if (difference1 < difference2) {
min1 = difference1;
}
else {
min1 = difference2;
}
if (difference3 < difference4) {
min2 = difference3;
}
else {
min2 = difference4;
}
if (difference5 < difference6) {
min3 = difference5;
}
else {
min3 = difference6;
}
if (difference7 < difference8) {
min4 = difference7;
}
else {
min4 = difference8;
}
if (min1 < min2) {
min5 = min1;
}
else {
min5 = min2;
}
if (min3 < min4) {
min6 = min3;
}
else {
min6 = min4;
}
if (min5 < min6) {
min = min5;
}
else {
min = min6;
}
I know this is a very long method, however I was unable to shorten the code without the use of a for-loop.
printf("Difference between the two numbers is %d\n", min);
Since min contains the difference between the user's input and the closest number I am unable to find a way to print the number closest to the user's input.
How can I trace the minimum difference to the original number which this was subtracted from ? Any suggestions would be appreciated, and please excuse the basic nature of this question.
I can help you to make the code shorter.
Here are my approach
int num = 71;
int diff,ans;
int div10 = (num / 10);
int lower = div10 * 10;
int upper = (div10 + 1) * 10;
if(lower<10) lower=10;
if(lower>80) lower=80;
if(upper<10) upper=10;
if(upper>80) upper=80;
int diff1 = lower - num;
int diff2 = upper - num;
if(diff1 < 0) diff1 *= -1;
if(diff2 < 0) diff2 *= -1;
if(diff1 < diff2) {
ans = lower;
diff = diff1;
}
else {
ans = upper;
diff = diff2;
}
printf("ans=%d, diff=%d\n",ans,diff);
Well what I would do:
int main(){
int closest;
int temp;
int a0 = 10, a1 = 20, a2 = 30; //and more..
int user_input = 28;
closest = abs(user_input - a0);
temp = abs(user_input - a1);
if (temp <= closest)
closest = temp;
//and so on
return 0;
}
Where abs is absolute value. Numbers should be in some data structure, because in the mean time you should ask if you still have something to check.
If you're dealing with 10s this should be quite simple:
num += 5;
closest_10 = num/10;
closest_10 *= 10;
Then you can have simple look up if you have an array of numbers. You can expand this to other multiples using a similar algorithm (say for 8s just replace 5 with 4 and 10 with).
As all numbers are fixed, the best approach (at least the most efficient once compiled) is to use the following snippet of code:
int closest(int number)
{
switch(number) {
case 0: case 1: case 2: case 3: case 4:
return 0;
case 5: case 6: case 7: case 8: case 9:
case 10: case 11: case 12: case 13: case 14:
return 10;
...
case 95: case 96: case 97: case 98: case 99: case 100:
return 100;
} /* switch */
} /* closest */
Even more efficient if you can inline the code instead of writing a function.
You need to better define: Lets say I have the following numbers.
How many numbers is the big question. Your example has them all as evenly divisible by 10. Your numbers are 10, 20, 30, and so on, and you mention a total of 8 numbers. Is it only 8 numbers maximum? Can they be any number? Will they all be integers? If its all integers and all divisible by 10, then you can use the modulo or modulus operator instead of subtraction.
The mod operator % only works on integers.
int a, b, c;
a = 3;
b = 25
c = a % 10; /* c will equal 3, 0 / 10 = 0 with remainder 3 */
c = b % 10; /* c will equal 5, 25 / 10 = 2 but remainder is 5 */
/* check if a number is even, just mod by 2 if zero then even */
if ( ( b % 2 ) == 0 )
printf(" value in b is even number\n");
else
printf(" value in b is odd number\n");

Printing to output: integer as sum of powers of 2

I had an exam, and I've been struggling ever since.
You have an array of integers(ex. 13, 6, 21, 4), and I need to make an output that looks like:
13 = 2^3 + 2^2 + 2^0
6 = 2^2 + 2^1
21 = 2^4 + 2^2 + 2^0
4 = 2^2
here's what i've got so far.
#include <stdio.h>
#define MAX 100
int main() {
int niz[MAX], nizb, n, i, ones, k;
while(1) {
printf("Array length: ");
scanf("%d", &n);
if (n<=0 || n>MAX) break;
printf("Array elements: ");
for(i=0;i<n;i++){
scanf("%d", &niz[i]);
if (niz[i] <=0) {
printf("Error! Wrong value. Enter new one: ");
scanf("%d", &niz[i]);
}
}
for(i=0;i<n;i++) {
nizb = niz[i];
ones = 0;
for(k=0; k < 16; k++) {
//What should i do here?
}
}
}
}
I'm stuck here. I dont know how many bits should i use, and how does C sees those bits of integer. I'm using var 'k' to add to a string that is in format '2^3 + 2^2 ...', where k is the value of 'for' iteration. I have made an assumption that length of the integer is 16, but im really not sure since we do this on a sheet of paper.
I want to say BIG THANKS TO EVERYONE!!!
You can calculate how many bits to use by using the sizeof operator and CHAR_BIT:
int bitsPerInt = sizeof(int) * CHAR_BIT;
CHAR_BIT is definied in limits.h.
After you have that limit, you can use the bitwise & operator to extract each bit:
for (k = bitsPerInt - 1; k >= 0; k--)
{
if (nizb & (1U << k))
// output
else
// don't
}
I'll leave the details up to you.
Aside: It looks like you're trying to use niz as an array, but you haven't declared it as one. Does this code even compile? Also, the return value of main should be int.
Not sure what this has to do with twos-complement (which is a particular way of representing negative numbers). What you are trying to do is express an integer as a sum of powers of 2, apparently. Here's the way I'd do it, which isn't necessarily better or worse than the other answers...
void powersum(int n)
{ int powers[sizeof(int) << 3];
int i;
char *sep = "";
printf("%d = ", n);
powers[0] = 0;
for (i = 0; n; n >>= 1, ++i)
powers[i] = n & 1;
while (--i >= 0)
{ if (powers[i])
{ printf("%s2^%d", sep, i);
sep = " + ";
}
}
printf("\n");
}
EDIT: Here's another version that doesn't use the stack-allocated array, but as a tradeoff has to go around the loop more (once for each bit, as opposed to only looping until the highest 1-bit is found):
void powersum2(int n)
{ int i = (sizeof(int) << 3) - 2;
int m = 1 << i;
char *sep = "";
printf("%d = ", n);
while (m)
{ if (n & m)
{ printf("%s2^%d", sep, i);
sep = " + ";
}
m >>= 1;
--i;
}
printf("\n");
}
This is complete conjecture, since I'm not really good with math, but I think I'd go about it like this:
int potency = 0, base = 1;
while(base < NumberInQuestion) {
base *= 2;
++potency;
}
After the loop finishes, you'll know the highest potency which still fits into 'Number'.
Number -= base/2; //Removes the base you just calculated from the number.
printf("2^%d", potency);
Rinse and repeat, until Number falls to 0, which should be at 2^0 at latest.
For your use-case, the code may look somewhat like this:
for(i=0; i < n; ++i) {
int Number = niz[i];
while(Number > 0) {
int potency = 0, base = 1;
do { //Executes at least once, making a number of '1' possible.
base *= 2;
++potency;
} while(base < Number);
Number -= base/2; //Reverts the last step you made, making the 'base' smaller than 'Number'.
printf("2^%d", potency);
}
}
There's a possible alternative, which can give you a more complete picture of things and will save you iterations. For this we use a two-step process.
for(i=0; i < n; ++i) {
int Number = niz[i];
int potency = 0, base = 1;
do { //Executes at least once, making a number of '1' possible.
base *= 2;
++potency;
} while(base < Number);
base /= 2; //Reverses the last iteration.
//At this point, we know the maximum potency, which still fits into the number.
//In regards of base 2, we know the Most Significant Bit.
while(base > 0) {
Number -= base; //Removes the MSD (Most significant digit)
printf("2^%d", potency); //Prints your '1'.
while(base > Number) { //Executes at least once.
base /= 2; //Goes back one potency. (Ends at '0' latest.)
--potency; //For each potency (except for first), it's a '0'.
}
}
}
quotient = niz[i];
int k=0,c[MAX];
while(quotient!=0){
binaryNumber[i++]= quotient % 2; //this will convert your numbers to binary form
quotient = quotient / 2; //and store in reverse in array
}
for(j = 0 ;j<i;j++)
{
if(binaryNumber[j]==1) */e.g binary of 4 is stored in array as 001 ie 1 atpos2*/
{ c[k]=j;
k++;}
}
while(k--)
printf("2^%d +",c[k]);
If you can tolerate a GCC-dependency, a hack on #twalberg's solution get's really nice and small ;)
void powersum(int n)
{
char *sep = "";
printf("%d = ", n);
while (n) {
int pos = 31 - __builtin_clz(n);
printf("%s2^%d", sep, pos);
sep = " + ";
n ^= 1 << pos;
}
printf("\n");
}

Resources