home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 December
/
simtel1292_SIMTEL_1292_Walnut_Creek.iso
/
msdos
/
c
/
cnews013.arc
/
PRIME2.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-12-20
|
4KB
|
142 lines
/* Diana Sysinger
C Programming
Extra Credit */
/* a few comments by jack velte */
/* Division by subtraction with error checking */
/* xxx you sure like extra \n's */
#include <stdio.h>
#ifdef notdef
#include "myname.c" /* xxx what is this? you shouldn't include */
/* other C files, but rather link them together */
/* after all the compiles. */
#endif
/* avoid global variables -- here i leave this one. */
int count;
/* xxx declare this integer so it return an int */
int getlim(void); /* gets upper limit from user, must be > 2 */
void prime(const int); /* finds the prime numbers between 2 and the limit */
/* calls prtit to do the output */
void prtit(const int); /* formats the output, 10 numbers per line */
void main(void);
void
main(void) {
int limit; /* xxx these should be local variables */
/* NOT global variables. save global */
/* variables when you HAVE to use them. */
limit = getlim();
prtit(1); /* uh, my version didn't print 1 as a */
/* prime.... */
prime(limit);
/* xxx redirect the output if you want it to go to the printer */
/* for example prime > prn or prime > output.fil */
printf("\nThere are %d primes from 2 to %d.\n", count, limit);
}
int
getlim(void) {
/* extern int limit; /* xxx what is this??? this is wrong! */
int limit; /* xxx just use a local variable */
#ifdef notdef
printf("\n\nPlease enter the upper Limit for the prime search.\n");
printf("The Limit should be 2 or larger.\n");
scanf("%d", &limit);
if (limit < 2) {
/* xxx \n go at the end of most lines. */
printf("\n\nOops! Limit must be 2 or larger!");
printf("\nTry again.\n");
getch();
getlim(); /* xxx ouch, recursion! */
}
else
fprintf(stdprn,"\nHere come the primes!\n\n");
#else
limit = 0;
while (limit < 2) {
/* xxx (v removed `the' so it would fit...) */
printf("Please enter upper Limit for the prime search.\n");
printf("The Limit should be 2 or larger.\n");
scanf("%d", &limit);
if (limit >= 2)
/* leave this loop */
break;
/* ok, didn't leave, print error message */
printf("Oops! Limit must be 2 or larger!\n");
printf("Try again.\n");
getch();
}
printf("Here come the primes!\n");
return (limit);
#endif
}
/* pass limit as a constant (isn't modified by the function) */
/* parameter */
void
prime(const int limit) {
int div = 2;
int num;
/*
* i don't really follow this code, but it seems to work. one
* comment, though. multiply and divide are *very* expensive
* operations relative to most instructions. if you can factor
* out the / % and * functions, the algorithm will run much
* faster. for this case, it doesn't matter, the program
* runs quite fast, but in general this is a good thing to
* watch for.
*/
/* do until limit reached */
for (num = 2; num <= limit; num++) {
for (div = 2; num >= div * div; div++)
/* compare to square of divisor */
if (num % div * div == 0)
/* if num/square = 0 then num */
/* not a prime number - do loop */
break;
/* if num/div = zero then check */
for (; num % div != 0; div++)
/* xxx mark null loop conditions with this */
/* comment... */
/* void */; /* for prime condition */
if (div == num)
/* if div = num then num is a */
/* prime so print it out */
prtit(num);
}
}
void prtit(const int num) {
/*
* `count' should be a local not an 'extern'. extern means it was
* declared in another module. static means it starts with 0 and
* does NOT start over when the module is called the second time.
* a static variable (in a function) retains it's value between
* function calls.
* .... but now i declared count to be global, so there should be
* no declaration here.
*/
printf("%5d", num); /* print the numbers 10 to a line. */
if (++count % 10 == 0)
printf("\n");
}
/*
* good luck -- your code was good, with only a few rough 'style' edges.
*/