Name ______(5)

SSN______(5)

ISQS 6337 Fall 98 Exam 1

Please follow all class style guidelines. Hungarian notation, indention, neatness of code, neatness of writing (IF I CAN"T READ IT THEN IT IS WRONG!!!!!) simple comments, and correct data type will all count. You do not need to worry about the program description at the top. I make every effort to give partial credit, if you cannot complete the entire problem do as much as possible.

  1. Identify and correct the errors in each of the following code segments (There may be more than one error in each piece of code) (3 points for each error). To correct simply mark out the error and beside it write the correct statement. You do not need to rewrite the entire code segment.
  1. if (age >= 65);

cout < "WOW!" < endl;

else

cout "Life is GOOD!" < endl;

  1. While (x <= 100) {while (x <= 100) {

Total += x;

++y;
}

  1. // to get "John R. Durrett" from the keyboard

cout > sName;cin.getline(sName, 99);

  1. char sName[99];char sName[99] = "John R Durrett"

sName = "John R Durrett";

  1. if (x > 10){

x++;

y = x + z;

} else

x--;

  1. const float fPI; fpi = 3.14159; const float fPI=3.14159;
  1. // to return only a char array from a function

char [99]getNameFunction(); void getNameFunction(char []);

  1. // the following should print whether the value of i is odd or even

switch( i % 2 ) {

case 0: cout < "Even< endl; break;

case 1: cout < "Odd" < endl;

}

i. if (x*3 > 100)

cout < "large number;

3.. Please create a new datatype to store coordinates in a three axis system. Use integers x, y, and z for the points in space (6)

class coord {

public:

int x;

int y;

int z;

};

  1. What does the following code print if called by: FunctionOne(25); (10)

void FunctionOne(int iA) {0 1 1

int j = 0, k = 1;2

clrscr();3

cout < j < "\t" < k < "\t";5 8

for (int i = 2; i < iA; i++) {13

i = j + k; j = k; k = i;21

cout < i < "\t";34

if (i%5) cout < "\n";

}

}

  1. Translate the following loop into a while and a do while loop (5 points each):

for (int x=20; x > 0; x--) {

if (x%3 == 0) cout < x < endl;

}

int x = 20;

while(x > 0) {

if (x%3 == 0) cout < x < endl;

x--;

}

int x = 20;

do {

if (x <= 0) break;

if (x%3) cout < x < endl;

} while(1);
Write a program according to the following specifications: (30 points)

  1. The main function of the program must clear the screen, prompt for and receive input from the keyboard of an integer variable equal to or less than 50,000.
  2. The main function must then call another function called PRIMES which will display a list of all the prime numbers equal to or less than the number input from the keyboard. When finished the function PRIMES will return the maximum prime number found to main.
  3. After completion of the PRIMES function, the main function will display the sentence: "Maximum prime number found: xxxxx" (xxxxx represents the maximum prime number returned from the function PRIMES) on a new display line.
  4. You may not use any built-in Math or Bit manipulation functions.
  5. Hint: A prime number is any number evenly divisible only by 1 and itself.
  6. Hint: Arrays are NOT required
  7. Hint: you will need a nested loop structure in PRIMES
  8. Hint: you might find the remainder operator (%) and the break statement useful.

// a possible solution

#include <pak.h>

long PRIMES(long l); // or unsigned int for bonus

void main() {

long iMaxPrime;

clrscr();

cout < "Max Prime: ";

cin > iMaxPrime;

// check value loop for bonus

// one line as below (bonus?) or define variable and disp it

cout < "\n\nMaximum prime number found: " < PRIMES(iMaxPrime) < "\n";

PAK();

}

long PRIMES(long L) {

long lMax = 0;

int iFlag;

for (long i = 0; i <= L; i++) {

iFlag = 1; // is a prime

for (long j=i-1; j>1; j--) {

if (i%j == 0) {

iFlag = 0;

break;

}

}

if (iFlag) {

cout < i < "\t";

lMax = i;

}

}

return lMax;

}