Note1: argument1 < argument2
Note2: a prime number is evenly divisible only by itself and 1
/* program to print out all of the prime numbers
between
and including two specified integer values
*/
/* command line: primes start stop */
#include <stdio.h>
void main(int argc, char *argv[])
{
int start, stop, num, i, isprime;
if(argc != 3) /* incorrect command
line */
{
printf("Expected command line
is: primes start stop\n");
printf("where start < stop
and both are positive integers\n");
exit(0);
};
start = atoi(argv[1]); /* convert to integers
*/
stop = atoi(argv[2]);
if(start > stop || start < 1) /* check
input values in range */
{
printf("start or stop out
of range\n");
printf("Expect start <
stop and both positive intergers\n");
exit(0);
};
num = 0; /* initialize the prime count */
if(start==1) /* check for start = 1 */
{printf("%d is a prime\n",start);
num++;
if (stop>start) start++;
};
if(start==2) /* check for start = 2 */
{printf("%d is a prime\n", start);
num++;
};
if(start%2 == 0) start++; /* increment
to next odd integer */
while(start <= stop)
{
i = 3; isprime
= 0;
while(i
< start)
{
if(start%i == 0) isprime = 1; /* not prime */
i+=2;
};
if(isprime==0)
{
printf("%d is a prime number\n", start);
num++;
};
start+=2;
};
if(num==0) printf("no prime numbers
found\n");
}
2) (25 points) Complete the following program by writing the needed drawDiamond function.
/* OpenGL Program to Draw a Diamond */
#include <stdio.h> /* definitions for standard I/O routines */
#define WIDTH 600 /* window dimensions */
/* Clear the screen and draw a blue diamond (a square rotated 45 degrees) that
void drawDiamond()
/* Main program to draw the diamond and wait for quit */
void main(int argc, char* argv[])
3) (10 points)
Change glClearColor(1,1,1,0) to glClearColor(1,1,0,0)
(b) How would you modify the program above to use a window 400 units wide and 300 units high?
Change the definitions to:
#define WIDTH 400
glutCreateWindow("Color Gradient");
(d) How would you modify your drawDiamond function to draw a diamond which is blue at the top and bottom vertices but is red at the middle vertices?
Modify the function as shown below by adding additional glColor3f() function calls.
glColor3f(0,0,1);
4) (10 points) Write the UNIX commands to do the following:
mkdir /u/gromit/examples/src
or
cd /u/gromit/examples
b) Remove all files whose names end with ".o" in the current directory
rm *.o
c) List all files in the subdirectory "progs" that have names which include the string "exp" and that end with ".c"
ls progs/*exp*.c
d) In the current directory, find all occurrences of the word "smile" in all files whose names contain the string "txt".
grep "smile" *txt*
or
awk '/smile/' *txt*
e) Move the subdirectory "astro" in your current directory up one level in the directory structure.
mv astro ..
5) (25 points) Write a shell script to do the following for the sequence of files starting with the file "frame.14.img" and ending with the file "frame.35.img" in your home directory:
a) copy all the files in this sequence into the directory "/n/gromit/viza652/images"
# midterm problem 5 shell script
set ccount = 0
while($num <= 35)
cd /n/gromit/viza652/images
set filelist = `ls -C1`
while($fcount <= $#filelist)
@fcount = $fcount - 1
echo $ccount "files copied"
echo $fcount "files renamed"