modified: learnc/a.exe
new file: learnc/learnc12.c new file: learnc/learnc13.c
This commit is contained in:
parent
66997adad5
commit
f3602b1fb8
BIN
learnc/a.exe
BIN
learnc/a.exe
Binary file not shown.
41
learnc/learnc12.c
Executable file
41
learnc/learnc12.c
Executable file
@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
int i, j;
|
||||
/* TODO: define the 2D pointer variable here */
|
||||
int **pnumbers;
|
||||
|
||||
/* TODO: complete the following line to allocate memory for holding three rows */
|
||||
pnumbers = (int **) malloc(3 *sizeof(int *));
|
||||
|
||||
/* TODO: allocate memory for storing the individual elements in a row */
|
||||
pnumbers[0] = (int *) malloc(1 * sizeof(int));
|
||||
pnumbers[1] = (int *) malloc(2 * sizeof(int));
|
||||
pnumbers[2] = (int *) malloc(3 * sizeof(int));
|
||||
|
||||
|
||||
pnumbers[0][0] = 1;
|
||||
pnumbers[1][0] = 1;
|
||||
pnumbers[1][1] = 1;
|
||||
pnumbers[2][0] = 1;
|
||||
pnumbers[2][1] = 2;
|
||||
pnumbers[2][2] = 1;
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
for (j = 0; j <= i; j++) {
|
||||
printf("%d", pnumbers[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
/* TODO: free memory allocated for each row */
|
||||
free(pnumbers[i]);
|
||||
}
|
||||
|
||||
/* TODO: free the top-level pointer */
|
||||
free(pnumbers);
|
||||
|
||||
return 0;
|
||||
}
|
25
learnc/learnc13.c
Executable file
25
learnc/learnc13.c
Executable file
@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int factorial(int x);
|
||||
|
||||
int main() {
|
||||
/* testing code */
|
||||
printf("0! = %i\n", factorial(0));
|
||||
printf("1! = %i\n", factorial(1));
|
||||
printf("3! = %i\n", factorial(3));
|
||||
printf("5! = %i\n", factorial(5));
|
||||
}
|
||||
|
||||
/* define your function here (don't forget to declare it) */
|
||||
int factorial(int x) {
|
||||
|
||||
if (x == 0) {
|
||||
|
||||
return 1;
|
||||
} else if (x >= 1) {
|
||||
|
||||
return x*factorial(x-1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user