When im doing an if and then an else, and i want the else to be a "do nothing". How do i do that and can i have an example?
thanks
When im doing an if and then an else, and i want the else to be a "do nothing". How do i do that and can i have an example?
thanks
what im trying to do is get the program to print the numbers in ascending order. How do i get the program to go to the swap part from main? Im not really understanding how to do things separatly from the main. Heres my really bad code:
Code:void swap(int x, int y){ int z = x; x = y; y = z; } int main(){ int a = 45; int b = 2; if (a < b){ printf("%d %d\n", a, b); } if (a > b){ swap(a, b); printf("%d %d\n", a, b); } }
you need to pass pointers instead.
Code:void swap(int *x, int *y){ int z = *x; *x = *y; *y = z; } int main(){ int a = 45; int b = 2; if (a < b){ printf("%d %d\n", a, b); } if (a > b){ swap(&a, &b); printf("%d %d\n", a, b); } }
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
You could just try this...
Code:void swap(int *x, int *y) { int z = *x; *x = *y; *y = z; } int main() { int a = 45; int b = 2; if (a > b) { swap(&a, &b); } printf("%d %d\n", a, b); }
This works ok. How do i make it so that you can input different arguments instead of having them set when i run the program.. like i want to be able to enter values when i execute it if you know what i mean?
main can be declared in two ways...
int main( void )
or
int main( int argc, char *argv[] )
If you use the second declaration, argc relates to the number of parameters passed at runtime, and argv is an array of strings representing the parameters themselves
e.g. from the command prompt I could type
myprogram 45 2
argc = 3
argv[0] = "myprogram"
argv[1] = "45"
argv[2] = "2"
The parameters are expressed as strings, so you would have to use atoi to convert them to integers (and probably something else to check that they are actually numbers).
Alternatively you could prompt the user from within the program to choose two numbers...
http://faq.cprogramming.com/cgi-bin/...&id=1043284392Originally posted by sworc66
This works ok. How do i make it so that you can input different arguments instead of having them set when i run the program.. like i want to be able to enter values when i execute it if you know what i mean?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
May be i might not got the point correctly. Question is
why not use scanf and compare, if condition is true swap??like i want to be able to enter values when i execute it
Saravanan.T.S.
Beginner.
THanks everyone, its been great help. In regards to main() i just need a simple explination as to how i write functions outside of the main and then refer to them in the main. At the moment i really dont understand exactly how to get away from writing everything in the main().
There are loads of examples on here. Maybe start with the tutorial, and then ask a specific question when you're stuck.Originally posted by sworc66
THanks everyone, its been great help. In regards to main() i just need a simple explination as to how i write functions outside of the main and then refer to them in the main. At the moment i really dont understand exactly how to get away from writing everything in the main().
Code:#include <stdio.h> void printme(int i) { printf ("value: %d\n", i); } int main(void) { printme(10); return(0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I can only seem to find c++ tutes on this site... anyways... here is the start of what ive done. Can you show me using my code how to refer to functions outside of main() because i want to add more. Just need another example. Thanks.
Code:#include <stdio.h> int main(){ } void one(){ int a; scanf("%d", &a); switch(a){ case 0: printf("zero \n"); break; case 1: printf("one \n"); break; case 2: printf("two \n"); break; case 3: printf("three \n"); break; case 4: printf("four \n"); break; case 5: printf("five \n"); break; case 6: printf("six \n"); break; case 7: printf("seven \n"); break; case 8: printf("eight \n"); break; case 9: printf("nine \n"); break; case 10: printf("ten \n"); break; case 11: printf("eleven \n"); break; case 12: printf("twelve \n"); break; case 13: printf("thirteen \n"); break; case 14: printf("fourteen \n"); break; case 15: printf("fifteen \n"); break; case 16: printf("sixteen \n"); break; case 17: printf("seventeen \n"); break; case 18: printf("eighteen \n"); break; case 19: printf("ninteen \n"); break; default: printf("error \n"); break; } }
Probably the best swap function.Code:void inline swap(int *x, int *y) { *x = *x ^ *y; *y = *y ^ *x; *x = *x ^ *y; }
>Just need another example.Code:#include <stdio.h> void foo(size_t x); /* prototype if function definition is after function call */ int main(void) { size_t i; for ( i = 0; i < 7; ++i ) { foo(i); /* function call */ } return 0; } void foo(size_t x) /* function definition */ { const char *number[] = { "zero", "one", "two", "three", "you get the idea" }; puts( x < sizeof(number)/sizeof(*number) ? number[x] : "huh?" ); } /* my output zero one two three you get the idea huh? huh? */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
i get this error:
bash-2.02$ gcc -o ass ass.c
ass.c:47: warning: type mismatch with previous implicit declaration
ass.c:43: warning: previous implicit declaration of `one'
ass.c:47: warning: `one' was previously implicitly declared to return `int'
Also what i want to be able to do next with the two() function is for the user to be able to type in a number in word format ie thirteen and the prog outputs the number in integer format. What i want to know how to do is do scanf in the main() and then depending on whether a number or a word was entered in to go to the correct function.
Code:#include <stdio.h> int main(void){ one(); return 0; } void one(){ int a; scanf("%d", &a); switch(a){ case 0: printf("zero \n"); break; case 1: printf("one \n"); break; case 2: printf("two \n"); break; case 3: printf("three \n"); break; case 4: printf("four \n"); break; case 5: printf("five \n"); break; case 6: printf("six \n"); break; case 7: printf("seven \n"); break; case 8: printf("eight \n"); break; case 9: printf("nine \n"); break; case 10: printf("ten \n"); break; case 11: printf("eleven \n"); break; case 12: printf("twelve \n"); break; case 13: printf("thirteen \n"); break; case 14: printf("fourteen \n"); break; case 15: printf("fifteen \n"); break; case 16: printf("sixteen \n"); break; case 17: printf("seventeen \n"); break; case 18: printf("eighteen \n"); break; case 19: printf("ninteen \n"); break; default: printf("error \n"); break; } } void two(){ }
If you used prototypes for your functions you wouldn't have this problem.bash-2.02$ gcc -o ass ass.c
ass.c:47: warning: type mismatch with previous implicit declaration
ass.c:43: warning: previous implicit declaration of `one'
ass.c:47: warning: `one' was previously implicitly declared to return `int'
>then depending on whether a number or a word was entered in to go to the correct function.
I personally wouldn't trust the user to enter a string accurate enough to do this conversion, but you could do something like this:
Code:#include <stdio.h> #include <stdlib.h> int main(void) { char buff[BUFSIZ]; int num; printf("Enter a number or the word equivalent (10 or ten): "); fflush(stdout); if (fgets(buff, sizeof buff, stdin) != NULL) { char *pos; num = (int)strtol(buff, &pos, 0); if (pos == buff) puts("You entered a string"); else puts("You entered a number"); } return 0; }
My best code is written with the delete key.