#define how to use with is code ?
-
int main() { printf("Enter 0 or 1 :"); scanf("%d",&i_choice); if ( i_choice ==0 ) for( ;i_count_1 < i_number;) {------- ------ } if (choice == 1) for( ;i <= i_number;) {------- ------ } Here I raed a user input ( 0 or 1) So as per i/p I've to change the condition in my for loop. if choice ==1 the condition must be (i <= i_number) if choice == 0 the condition must be (i_count_1 < i_number) I don't know how to use #define with this program. anybody can help me.... Krish
-
int main() { printf("Enter 0 or 1 :"); scanf("%d",&i_choice); if ( i_choice ==0 ) for( ;i_count_1 < i_number;) {------- ------ } if (choice == 1) for( ;i <= i_number;) {------- ------ } Here I raed a user input ( 0 or 1) So as per i/p I've to change the condition in my for loop. if choice ==1 the condition must be (i <= i_number) if choice == 0 the condition must be (i_count_1 < i_number) I don't know how to use #define with this program. anybody can help me.... Krish
You don't need
#define
in your code sample, but you need aswitch(i_choice)
.Maxwell Chen
-
You don't need
#define
in your code sample, but you need aswitch(i_choice)
.Maxwell Chen
here I wanna reduce the code size.... swith(i_choice) { case 0 : for (.. ; condition 1 ; ..) { } break; case 1 : for (.. ; condition 2 ; ..) { } break; } So the code will come twise ; except the conditions in for loop all other statements are same for case 1 and 0. That's why I tried for a #define Macro. ???? with regards Krish
-
here I wanna reduce the code size.... swith(i_choice) { case 0 : for (.. ; condition 1 ; ..) { } break; case 1 : for (.. ; condition 2 ; ..) { } break; } So the code will come twise ; except the conditions in for loop all other statements are same for case 1 and 0. That's why I tried for a #define Macro. ???? with regards Krish
krish_kumar wrote:
case 0 : for (.. ; condition 1 ; ..)
Then could you show the complete code (I mean those
...
portions)? I will figure out a solution for you.Maxwell Chen
-
here I wanna reduce the code size.... swith(i_choice) { case 0 : for (.. ; condition 1 ; ..) { } break; case 1 : for (.. ; condition 2 ; ..) { } break; } So the code will come twise ; except the conditions in for loop all other statements are same for case 1 and 0. That's why I tried for a #define Macro. ???? with regards Krish
krish_kumar wrote:
That's why I tried for a #define Macro. ????
Which would be totally wrong. The
#define
directive is handled by the preprocessor before the compile phase. You don't know the value ofi_choice
until the code is running. See the problem?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
krish_kumar wrote:
That's why I tried for a #define Macro. ????
Which would be totally wrong. The
#define
directive is handled by the preprocessor before the compile phase. You don't know the value ofi_choice
until the code is running. See the problem?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
Okkk... I realize I can't use a #define here.... But my code is repeating twice ...I wanna avoid that.... Except condition the other statements are same for both... Here is my code printf("\n U wanna find prime nos up to N(press 1) or N prime nos (press 0 "); scanf("%d",&i_choice); switch(i_choice) { case 0 : for (i = 1, i_count = 0; i_count_1 < i_number; i++ ) { for (j = 1,i_count = 0; j <= i ; j++) { if ((i % j) == 0) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } break; case 1 : for (i = 1, i_count = 0 ; i <= i_number; i++ ) { for (j = 1, i_count = 0; j <= i ; j++) { if( (i % j)==0 ) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } } }
-
Okkk... I realize I can't use a #define here.... But my code is repeating twice ...I wanna avoid that.... Except condition the other statements are same for both... Here is my code printf("\n U wanna find prime nos up to N(press 1) or N prime nos (press 0 "); scanf("%d",&i_choice); switch(i_choice) { case 0 : for (i = 1, i_count = 0; i_count_1 < i_number; i++ ) { for (j = 1,i_count = 0; j <= i ; j++) { if ((i % j) == 0) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } break; case 1 : for (i = 1, i_count = 0 ; i <= i_number; i++ ) { for (j = 1, i_count = 0; j <= i ; j++) { if( (i % j)==0 ) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } } }
I would suggest you to re-design your code architecture. See the sample code below.
#include <iostream>
int GetNextPrime(int iStartNum)
{
int i, j;
int i_count;
for(i = iStartNum; ; i++) {
for(j = 1, i_count = 0; j <= i ; j++) {
if( (i % j) == 0 )
++i_count;
}
if(i_count <= 2) {
return i;
}
}
return 0;
}void main()
{
int i_choice;
int i_count;
int i_count_1 = 0;
int i_number = 10;
int i, j;
int iValNow;
int count;printf("Find prime nos up to %d (press 1), or %d prime nos (press 0) ", i\_number, i\_number); scanf("%d", &i\_choice); // --------------------------------------------------------- printf("\\n My version: \\n"); switch(i\_choice) { case 0 : // N count. for(i = 0, iValNow = i, count = 0; count < i\_number; i++, count++) { iValNow = GetNextPrime(iValNow +1); printf("\\t %d \\n", iValNow); } break; case 1 : // up to N. for(iValNow = 0; ; ) { iValNow = GetNextPrime(iValNow +1); if(iValNow > i\_number) { break; } printf("\\t %d \\n", iValNow); } break; } // --------------------------------------------------------- printf("\\n Your version: \\n"); switch(i\_choice) { case 0 : // N count. for(i = 1, i\_count = 0; i\_count\_1 < i\_number; i++ ) { for(j = 1,i\_count = 0; j <= i ; j++) { if( (i % j) == 0 ) ++i\_count; } if(i\_count <= 2) { printf("\\t %d \\n",i); i\_count\_1++; } } break; case 1 : // up to N. for(i = 1, i\_count = 0 ; i <= i\_number; i++ ) { for(j = 1, i\_count = 0; j <= i ; j++) { if( (i % j) == 0 ) ++i\_count; } if(i\_count <= 2) { printf("\\t %d \\n",i); i\_count\_1++; } } }
}
Maxwell Chen
-
Okkk... I realize I can't use a #define here.... But my code is repeating twice ...I wanna avoid that.... Except condition the other statements are same for both... Here is my code printf("\n U wanna find prime nos up to N(press 1) or N prime nos (press 0 "); scanf("%d",&i_choice); switch(i_choice) { case 0 : for (i = 1, i_count = 0; i_count_1 < i_number; i++ ) { for (j = 1,i_count = 0; j <= i ; j++) { if ((i % j) == 0) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } break; case 1 : for (i = 1, i_count = 0 ; i <= i_number; i++ ) { for (j = 1, i_count = 0; j <= i ; j++) { if( (i % j)==0 ) ++i_count; } if (i_count <= 2) { printf("\t %d \n",i); i_count_1++; } } } }
krish_kumar wrote:
But my code is repeating twice ...I wanna avoid that....
No problem. Just put the common code in a function.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons