VIZA 652 Mid-Term Exam

Open Book, Open Notes

Name ______________________                      October 15, 1997 90 Minutes

 
1) (30 points) Write a C program that computes and prints out all of the prime numbers between two specified positive integer values (including the specified values). The two specified integers are to be supplied as command line arguments to the program. If there are no prime numbers between the two specified values, print out a message stating that no primes were found. Include appropriate input error checking in your program.

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 */

#include <GL/gl.h> /* definitions for GL graphics routines */
#include <GL/glu.h> /* definitions for GLU routines */
#include <glut.h>

#define WIDTH 600 /* window dimensions */

#define HEIGHT 600

/* Clear the screen and draw a blue diamond (a square rotated 45 degrees) that

is 200 units wide and 200 units high and located in the center of the window */

void drawDiamond()

{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(0,0,1);
/* note vertices are in counterclockwise order */
glVertex2i(WIDTH/2, HEIGHT/2 + 100);
glVertex2i(WIDTH/2 -100, HEIGHT/2);
glVertex2i(WIDTH/2, HEIGHT/2 - 100);
glVertex2i(WIDTH/2 +100, HEIGHT/2);
glEnd();
}

/* Main program to draw the diamond and wait for quit */

void main(int argc, char* argv[])

{
glutInit(&argc, argv);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("A Blue Diamond");
glutDisplayFunc(drawDiamond);
gluOrtho2D(0, WIDTH, 0, HEIGHT);
glClearColor(1, 1, 1, 0);
glutMainLoop();
}

 

3) (10 points)
 

(a) How would you modify the program above to draw the blue diamond on a yellow background?

    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

    #define HEIGHT 300
 
also if the vertices were supplied as absolute coordinates, they must be changed to keep the diamond centered in the window.
 
(c) How would you modify the program above to put the phrase "Color Gradient" in the window title bar?
 
Change the glutCreateWindow() to:

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);

glVertex2i(WIDTH/2, HEIGHT/2 + 100);
glColor3f(1,0,0);
glVertex2i(WIDTH/2 -100, HEIGHT/2);
glColor3f(0,0,1);
glVertex2i(WIDTH/2, HEIGHT/2 - 100);
glColor3f(1,0,0);
glVertex2i(WIDTH/2 +100, HEIGHT/2);
 
(e) If the " #include <GL/glu.h>" statement were deleted from the program above what would happen?
 
The "include" file for the glu utility routines would not be loaded by the C preprocessor.  The various definitions and other code in this include file would not be found by the compiler.  This would cause compile errors, runtime errors, or logical errors.

 

4) (10 points) Write the UNIX commands to do the following:
 

a) Create a subdirectory called "src" in the directory "/n/gromit/examples"

mkdir /u/gromit/examples/src

or

cd /u/gromit/examples

mkdir src

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"

b) change the names of all files in "/n/gromit/viza652/images" by adding the prefix "new_" to each file name
c) output the number of files copied and the number of files renamed
 

# midterm problem 5 shell script

set ccount = 0

set num = 14

while($num <= 35)

   cp ~/frame.$num.img /n/gromit/viza652/images
   @num = $num + 1
   @ccount = $ccount + 1
end

cd /n/gromit/viza652/images

set filelist = `ls -C1`

set fcount = 1

while($fcount <= $#filelist)

   set type = `file $filelist[$fcount]`
   if($type[2] != directory) then
      mv $filelist[$fcount] new_$filelist[$fcount]
   endif
   @fcount = $fcount + 1
end

@fcount = $fcount - 1

echo $ccount "files copied"

echo $fcount "files renamed"