#include <stdio.h> void printStars(int n) { int i ; for (i=0; i<n; ++i) putchar('*') ; }
#include <stdio.h> #include <stdlib.h> void printStars(int) ; int main(void) { int n ; printf("How many stars to print?\n") ; scanf("%d", &n) ; printStars(n) ; return(EXIT_SUCCESS) ; }
#include <stdio.h> void printChars(char c, int n) { int i ; for (i=0; i<n; ++i) putchar(c) ; }
void printChars(char c, int n) ; void printStars(int n) { printChars('*', n) ; }
void printChars(char c, int n) ; void printRect(char c, int width, int heigth) { int i ; for (i=0; i<heigth; ++i) printChars(c, width) ; }
int factorial(int n) { int f, i ; f = 1 ; for (i=1; i<=n; ++i) f = f*i ; return f ; }
int permutations(int m, int n) { int p, i ; p = 1 ; for (i=1; i<=m; ++i) { p = p * n ; --n ; } return p ; }
int combinations(int m, int n) { int c, i ; c = 1 ; for (i=1; i<=m; ++i) { c = c * n / i ; --n ; } return c ; }
int permutations(int m, int n) { return factorial(n)/factorial(n-m) ; }
int combinations(int m, int n) { return factorial(n)/(factorial(m)*factorial(n-m)) ; }
int factorial(int n) { if (n==0) return 1 ; else return n*factorial(n-1) ; }
int permutations(int m, int n) { if (m==0) return 1 ; else return n*permutations(m-1,n-1) ; }
int combinations(int m, int n) { if (m==0) return 1 ; else /* See Combination under Wikipedia */ return n*combinations(m-1,n-1)/m ; }
Suppose you wanted to write a function that received three values and returned the largest and smallest?
You can't.
Pointers are C values that contain addresses.
int *pInteger ; double *pDouble ; char *pChar ; int ***pConfusing ;
& |
unary operator that returns the address of a lvalue |
* |
unary operator that returns value stored at an address or unary operator that deferences a pointer |
int i, j ; int *p, *q ; p = &i ; q = &j ; j = 5 ; *p = *q + 1 ; p = q ; *p = *q + 1 ;
void MinMax(int x, int y, int z, int *lo, int *hi) { int t ; if (x > y) { t = x ; x = y ; y = t ; } if (y > z) { t = y ; y = z ; z = t ; } if (x > y) { t = x ; x = y ; y = t ; } *lo = x ; *hi = z ; }
Put your prototypes declarations in an header file whose name ends with the .h extension.
int combinations(int m, int n) ;
Put your function in a file and include the prototype file to make sure both agree.
#include "combinations.h" int combinations(int m, int n) { if (m==0) return 1 ; else return n*combinations(m-1,n-1)/m ; }
Any program that uses your function should also include the header file.
#include <stdio.h> #include <stdlib.h> #include "combinations.h" int main(void) { printf("There are %d different poker hands\n", combinations(5,52)) ; return(EXIT_SUCCESS) ; }
If you are not using an IDE, such as Netbeans, you'll have to compile the programs separately.
gcc -c -ansi -pendantic -Wall numhands.c gcc -c -ansi -pendantic -Wall combinations.c gcc -o numhands numhands.o combinations.o
Try out the following to see how arguments are stored on the stack.
#include <stdio.h> #include "combinations.h" int combinations(int m, int n) { printf("m=%d, n=%d, &m=%08x, &n=%08x\n", m, n, (int) &m, (int) &n) ; if (m==0) return 1 ; else return n*combinations(m-1,n-1)/m ; }