Practice Web Page - http://www.cs.tau.ac.il/~efif/courses/Programming_Fall_04
#include <stdio.h> int comp_floors(int exprs, int balls) { if (balls == 0) return 0; if (exprs == 0) return 0; return comp_floors(exprs - 1, balls) + comp_floors(exprs - 1, balls - 1) + 1; } int main() { int floors, exprs, balls; printf("Enter number of floors followed by number of balls: "); if (scanf("%d %d", &floors, &balls) != 2) { printf("ERROR: cannot read input!\n"); return -1; } for (exprs = 0; 1; exprs++) { if (comp_floors(exprs, balls) >= floors) break; } printf("Required %d exprs\n", exprs); return 0; } |
int a[10]; float temperatures[10]; |
a[0] = 10; a[1] = 20; a[2] = a[0] + a[1]; |
a = 0; /* WRONG */ |
int b[10]; b = a;/* WRONG */ |
int b[10]; for(i = 0; i < 10; i++) b[i] = a[i]; |
int array [8] = {2, 4, 6, 8, 10, 12, 14, 16}; |
int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16}; int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16}; |
#include <stdio.h> int main() { int i; char a[10]; for (i = 0; i < 10; i++) scanf("%c",&a[i]); for (i = 9;i >= 0; i--) printf("%c",a[i]); printf("\n"); return 0; } |
int rand()
- returns a pseudo-random integer between
0 and RAND_MAX
srand(unsigned int seed)
- sets seed
as the seed for a new sequence of pseudo-random integers to be returned by
rand()
. These sequences are repeatable by calling srand() with
the same seed value.
/* * The behavior of rolling a pair of dice. * * The total roll can be anywhere from 2 to. We want to count how often each * roll comes up. We use an array to keep track of the counts: a[2] will count * how many times we've rolled 2, etc. * * We simulate the roll of a die by calling C's random number generation * function, rand(). Each time rand() is called, it returns a different, * pseudo-random integer. The values that rand() returns typically span a large * range. We use C's modulus (or ``remainder'') operator % to produce random * numbers in the range we want. The expression rand() % 6 produces random * numbers in the range 0 to 5, and rand() % 6 + 1 produces random numbers in * the range 1 to 6. */ #include <stdio.h> #include <stdlib.h> main () { int i; int d1, d2; int a[13]; /* uses [2..12] */ /* Initialize: */ for (i = 2; i <= 12; i++) a[i] = 0; /* Compute: */ for (i = 0; i < 1000; i++) { d1 = rand() % 6 + 1; d2 = rand() % 6 + 1; a[d1 + d2] = a[d1 + d2] + 1; } /* Print out: */ for (i = 2; i <= 12; i++) printf ("%2d: %3d\n", i, a[i]); return 0; } |
int * ptr; |
int i = 1; int *ptr = &i; |
int i = 1; int *ptr = &i; |
int i = 1; int *ptr = &i; *ptr = 2; printf ("%d\n", i); |
int i = 1; int *ptr = &i; *ptr = 2; printf ("%d\n", i); |
int i = 1; int *ptr = &i; *ptr = 2; printf ("%d\n", i); |
int i = 42; int j = 17; int *ptr = &i; *ptr = 1; ptr = &j; *ptr = 2; |
int i = 42; int j = 17; int *ptr = &i; *ptr = 1; ptr = &j; *ptr = 2; |
int i = 42; int j = 17; int *ptr = &i; *ptr = 1; ptr = &j; *ptr = 2; |
int i = 42; int j = 17; int *ptr = &i; *ptr = 1; ptr = &j; *ptr = 2; |
int i = 42; int j = 17; int *ptr = &i; *ptr = 1; ptr = &j; *ptr = 2; |
int i = 42; int j = 17; int *ptr = &i; *ptr = 1; ptr = &j; *ptr = 2; |
int i = 42; int j = 17; int *ptr = &i; *ptr = 1; ptr = &j; *ptr = 2; |
&j = &i |
int i; int *ptr = &i; |
double d; int *ptr = &d; |
int * ptr; |
#include |
#include |
#include |
#include |
#include |
#include |
#include |
#include |
#include |
#include |
void swap (int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; } |
void swap (int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; } |
/* * power: calculate m to the nth power */ long *power (int m, int n) { int i; int product = 1; for (i = 1; i <= n; ++i) product *= m; return &product; } |