Below mentioned program is a basic form of switch case. I'm trying to print the macros variable in the out put. For more details refer the output. Its not printing the macros variable.
Switch case
#include<stdio.h>
#define a 4
#define d 5
int main()
{
int x = 10;
switch (x)
{
case a: printf("Number is 40");
break;
case d: printf("Number is 50");
break;
default: printf("Default case");
break;
}
}
Expect:
Number is 40
Actual:
Default case
Kindly refer the Point 1 & 2.
#include<stdio.h>
#define a 4
#define d 5
int main()
{
int x = 4; //(Point1)Here I'm declared the x value as 4 which is equal to the above defined one of the macros. Hence the output has printed the expected value as said by #ed heal.
switch (x)
{
case a: printf("Number is 40"); //(Point 2)User can be expect anything as dummy output.
break;
case d: printf("Number is 50");
break;
default: printf("Default case");
break;
}
}
You're setting x to 10. You're switching on x, that is 10, and neither a nor b evaluate to 10, so the default case statement is used.
Related
I have written this piece of code which is supposed to simulate throwing dice many many times and counting that how many times each face is up. I have attached the output down there and as you can see it looks kind of strange. For example face 5 comes up exactly 10 times, faces 2, 3, 4 are about the same and face 6 comes zero in two rounds. The only face which acts about normal is 1.
Can anyone explain this to me? Is this normal? Am I doing something wrong or is it something related to my system?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
unsigned long int freq1, freq2, freq3, freq4, freq5, freq6;
unsigned long int L00p = 1;
unsigned short int DF;
while (L00p <= 6e7){
srand (time(NULL));
DF = 1 + (rand ()%6);
switch (DF)
{
case 1:
++freq1;
break;
case 2:
++freq2;
break;
case 3:
++freq3;
break;
case 4:
++freq4;
break;
case 5:
++freq5;
break;
case 6:
++freq6;
break;
default:
break;}
++L00p;
}
printf ("%s%25s\n", "Dice's Face", "Face Frequency");
printf ("1%25lu\n", freq1);
printf ("2%25lu\n", freq2);
printf ("3%25lu\n", freq3);
printf ("4%25lu\n", freq4);
printf ("5%25lu\n", freq5);
printf ("6%25lu\n", freq6);
return 0;
}
and here is the program's output after four times running it:
You don't initialize the frequency counters, so they'll likely contain garbage from the stack. (So yes, you were getting randomness, but not the randomness you want.)
You don't want to call srand() in the loop, but only once before it. Calling srand() with the same number (and time(NULL) will quite inevitably return the same second in a tight loop) will reset the rand() generator to return the same sequence of numbers, and since you only ever call rand() once before calling srand() again, you'll get a whole bunch of the same number.
The following version works fine, but you'd have a better time with an array.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
unsigned long int freq1 = 0, freq2 = 0, freq3 = 0, freq4 = 0, freq5 = 0,
freq6 = 0;
srand(time(NULL));
for (int loop = 0; loop < 1000; loop++) {
int DF = 1 + (rand() % 6);
switch (DF) {
case 1:
++freq1;
break;
case 2:
++freq2;
break;
case 3:
++freq3;
break;
case 4:
++freq4;
break;
case 5:
++freq5;
break;
case 6:
++freq6;
break;
default:
break;
}
}
printf("%s%25s\n", "Dice's Face", "Face Frequency");
printf("1%25lu\n", freq1);
printf("2%25lu\n", freq2);
printf("3%25lu\n", freq3);
printf("4%25lu\n", freq4);
printf("5%25lu\n", freq5);
printf("6%25lu\n", freq6);
return 0;
}
Example output:
Dice's Face Face Frequency
1 177
2 160
3 166
4 169
5 155
6 173
For reference, a version using an array of 6 ints:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
unsigned long int freqs[6] = {0};
srand(time(NULL));
for (int loop = 0; loop < 1000; loop++) {
freqs[rand() % 6] ++;
}
printf("%s%25s\n", "Dice's Face", "Face Frequency");
for(int face = 0; face < 6; face++) {
printf("%d%25lu\n", face + 1, freqs[face]);
}
return 0;
}
Here is an annotated adaptation of your code for educational purposes. You've learned about "loops", so here is an application for a do/while() loop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Global variables are frowned upon because as the code grows more complex
// it is difficult or impossible to see where a value may be changed (inappropriately)
// For a tiny program like this that is unlikely to grow
// this proves the global variables are, by default, initialised to zero.
unsigned long int freq1, freq2, freq3, freq4, freq5, freq6;
int main() {
unsigned long int L00p = 0; // "local" var initialised. Good!
srand( time( NULL ) ); // called once at start of program.
do {
switch( ( rand() % 6 ) ) { // braces consistent with your main()
case 1: ++freq1; break; // get used to base-0 counting
case 2: ++freq2; break;
case 3: ++freq3; break;
case 4: ++freq4; break;
case 5: ++freq5; break;
case 0: ++freq6; break; // Ha-ha!! modulo!!!
default: printf( "A miracle has happened!!\n" );
break;
} // DO NOT hide that closing brace as you did. Prominent!
} while( ++L00p < 6e3 ); // increment counter after each loop done
// Swapped your output columns
// Using one format specifier for header and one for counts
// Notice how easy to modify only one instance?
char *tFmt = "%9s : %s Loops = %d\n";
char *oFmt = "%9lu : %d\n";
printf( tFmt, "Frequency", "Face", L00p );
// and... why not???
for( L00p = 0; L00p < 6; L00p++ ) {
int n; // not init'd because used immediately
switch( L00p ) {
case 1: n = freq1; break;
case 2: n = freq2; break;
case 3: n = freq3; break;
case 4: n = freq4; break;
case 5: n = freq5; break;
case 0: n = freq6; break;
}
printf( oFmt, n, L00p + 1 );
}
return 0;
}
Output
Frequency : Face Loops = 6000
958 : 1
1038 : 2
1018 : 3
1031 : 4
956 : 5
999 : 6
Again, for a simple, small piece of code like this, being able to see the entire switch block and compare values at a glance, concatenating statements can AID in writing bug-free code.
In this code, why I am getting i = "some garbage value" as output? I see that i is being declared but value = 10, not assigned. Why ?
main()
{
int a =1;
switch (a)
{
int b = 10;
case 1: printf ("b = %d \n", b);
break;
}
b is not being initialized. The assignment is outside of any case in the switch, so it picks whatever was in the stack at that point.
If you want a variable inside a case statement, the right way to do it is:
switch(a)
{
case 1:
{
int b=10; //start a new block scope
printf("b=%d",b);
}
break;
}
Despite having set srand() only once as pointed by similar Q/A about rand()
I think the following rand does not return a value for my customized random function.
Anyway my purpose was to generate a few random numbers and right after their appearance to count +1 at an array (to calculate their frequency later on)
One represents (5x)+1 at freq[] array
I have read documentation about rand()/srand but I cant figure out the error.
Thanks for your patience!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 1000
int RandomInteger(int low , int high);
main()
{
int freq[9]={0},i,j,number,div;
srand((int)time(NULL));
for(i=0;i<N;i++)
number=RandomInteger(1,9);
switch(number){
case 1:
freq[0]+=1;
break;
case 2:
freq[1]+=1;
break;
case 3:
freq[2]+=1;
break;
case 4:
freq[3]+=1;
break;
case 5:
freq[4]+=1;
break;
case 6:
freq[5]+=1;
break;
case 7:
freq[6]+=1;
break;
case 8:
freq[7]+=1;
break;
case 9:
freq[8]+=1;
break;
default:
;
}
for(i=0;i<9;i++){
div=freq[i]/5;
printf("%d|",div);
for(j=0;j<div;j++)
printf("*");
printf("\n");
}
}
int RandomInteger(int low , int high)
{
int k;
double d;
d=(double)rand()/((double)RAND_MAX+1);
k=(int)(d*(high-low+1));
return low+k;
}
You problem is this:
for(i=0;i<N;i++)
number=RandomInteger(1,9);
It should have
for(i=0;i<N;i++) {
number=RandomInteger(1,9);
..
..
}
Otherwise it runs it N times (same line), but never goes to the switch until it finishes the loop
In your code high is set to 1 and low is set to 9 this will result in a negative k. The return will be outside of the range of your cases and will fall to default.
On compiling and running the code below, we get the output as stated. Please explain the output. case 2 is nested in case 0 so the program shouldn't print anything at all.
#include <stdio.h>
int main() {
int i=5;
switch ( 2 ) {
case 0:
for ( i=0; i<10; i++ ) {
case 1:
printf("A i=%d\n",i);
case 2:
printf("B i*i=%d\n",i*i);
};
case 3:
printf("done");
break;
}
return 0;
}
/* OUTPUT
B i*i=25
A i=6
B i*i=36
A i=7
B i*i=49
A i=8
B i*i=64
A i=9
B i*i=81
done
*/
The switch statement is just a jump into the middle of a for loop (at case label 2). Then the code executes the for loop. Pretty much equivalent to:
#include <stdio.h>
int main() {
int i=5;
goto label_2;
for ( i=0; i<10; i++ ) {
printf("A i=%d\n",i);
label_2:
printf("B i*i=%d\n",i*i);
};
label_3:
printf("done");
return 0;
}
That's all there is to it.
for ( i=0; i<10; i++ ) {
case 1:
printf("A i=%d\n",i);
case 2:
printf("B i*i=%d\n",i*i);
};
So case 2 is inside the for loop, hence the repetition in the outputs. With a case you need to put in a break or it executes every case after the one it switches to.
switch(2)
case 1: //blah
case 2: //blah
case 3: //blah
For this example above the code in cases 2 and 3 are ran whereas normally you write:
switch(2)
case 1: //blah
break;
case 2: //blah
break;
case 3: //blah
break;
In which case only case 2 is ran
The following isn't the it's just the part causing problem:
int s,p;
scanf("%d",s);
switch(s)
{
case 1:
{
p=10;
break;
}
case 2:
{
p=15;
break;
}
}
printf("%d",p);
The problem is that p prints a random and very large number, What is causing it?
So i used some of your advice and know i have the following code:
int s,p=0;
scanf("%d",&s);
switch(s)
{
case 1:
{
p=10;
break;
}
case 2:
{
p=15;
break;
}
default:
{
printf("Number invalid");
return 0;
}
}
printf("%d",p);
Now i it's always going to default even though i only enter 1 or 2
Ok now it worked Thank You All!
You have two problems: (i) p is uninitialised and (ii) you're passing s to scanf where the address of s is required.
Change:
int s,p;
scanf("%d",s);
to:
int s, p = 0;
scanf("%d", &s);
"int p" declaration assigns p to an arbitrary value: whatever happened to be in memory. Now, if s doesn't equal 1 or 2, that value never changes, and that's what you see. What you can do is
add default: clause to your switch() and assign p to something meaningful there
declare p as "int p = 0;"
What number are you scanning in? You probably you don't have a switch case for that number, which means that p is uninitialized (and hence random). For example if you enter 3, there is no case statement for 3 and p will contain a random value. I would suggest that you detect invalid input with an default case where you assign a value of 0 to p.
default:
p = 0;
Corrected code:
int s,p;
scanf("%d", &s);
switch(s)
{
case 1:
{
p=10;
break;
}
case 2:
{
p=15;
break;
}
default:
{
p=0;
break;
}
}
printf("%d",p);
Edit: Fixed scanf problem per Paul R
You can use default and then try to figure out what was scanned in