Practice Web Page - http://www.cs.tau.ac.il/~efif/courses/Software1_Spring_04
#include <stdio.h>
/*! getline() reads one line from standard input and copies it to line array
* (but no more than max chars).
* It does not place the terminating \n in line array.
* Returns line length, or 0 for empty line, or EOF for end-of-file.
*/
int getline(char line[], int max)
{
int nch = 0;
int c;
max = max - 1; /* leave room for '\0' */
while ((c = getchar()) != EOF) {
if (c == '\n')
break;
if (nch < max) {
line[nch] = c;
nch = nch + 1;
}
}
if (c == EOF && nch == 0)
return EOF;
line[nch] = '\0';
return nch;
}
|
#include <stdlib.h>
int rand() - returns a pseudo-random integer between
0 and RAND_MAX
void 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.
#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;
}
|
#include <stdio.h>
#include <math.h>
int main()
{
int i, j, sq, last;
printf("Enetr a number: ");
if (scanf("%d", &last) != 1) {
printf("ERROR: failed to read a number!\n");
return -1;
}
for (i = 2; i <= last; i++) {
sq = (int) sqrt(i);
for (j = 2 ; j <= sq; j++)
if (i % j == 0)
break;
if (j > sq)
printf("the number %d is prime\n", i);
}
return 0;
}
|