Exemption exam for ECS 102 - C/C++

The goal of this exam is to give recognition for programming you already know, so you can fulfill a requirement and move on to learning something new. If you already know C++ or C, don't plan on taking ECS102 to learn Java. You already know some fundamentals of programming, and you can learn the syntax of Java in a weekend if you need it.

This is a closed book exam. You are writing code without running it. Please answer the questions in this Word document, immediately below the question. Use as much space as you need.If you do not remember the exact syntax of a statement, make your best guess and continue. Send solutions to . Use "ecs102 exemption exam" as the Subject to make sure I see it and process it promptly.

Your name:______Your SU ID#:______

Campus email:______

Major:______

ECS102 section______

Have you taken the Computer Science AP exam? If so, what did you get? ______

1. Write a function,

void convert( char conv_type, double * metlength, double * britlength)

that converts a length from metric to British measure or from British to metric measure. You may not assume any global variables. There should be a parameter conv_type where the user can tell the function which kind of conversion to do (British to metric or metric to British). There should be pointers as parameters metlength and britlength that can be used for both communication from the calling program and for communication back to the calling program, that is as input and output parameters.

  • British lengths are in inches.
  • Metric lengths are in centimeters.
  • You may use the values of 2.54 cm is 1 inch and
  • 0.39 inches is 1 cm.
  • If conv_type is 'B' or 'b' then the conversion is from British to metric.
  • If conv_type is 'M' or 'm' then the conversion is from metric to British.
  • If conv_type is any other character, then Invalid Conversion is printed and no values are changed.
  • When the function is done, the values accessed by metlength and britlength should be equivalent but in their respective systems.

For example, if we have

ml = 0;

bl = 10;

Then after the call convert('B', &ml, &bl)

we should have

ml is 25.4

bl is 10

because 10 inches is equal to 25.4 cm.

2. a) I have an array with ten thousand numbers, in ascendingorder. I want to find the index (subscript) of a particular number, if it is in the array. Is it better for me to use a binary search or a linear search? Why?

b) I have an array with ten thousand numbers that is unordered. I want to find the index (subscript) of a particular number if it is in the array. Is it better for me to use a binary search or a linear search? Why?

3. Consider the following code:

int mystery( int n )

{

if ( n = = 1)

return 1;

else

return (2 * mystery(n-1) + n);

}

Fill in values in the table below:

n / mystery(n)
1
2
3

4. Assume you have access to the function (method)

int myrand( );

that returns a random integer in the range 0 to 100,000 with uniform probability. (That is, every whole number is equally likely to come up.) You do not have to write this function, and you should not use a built in random function. Use myrand for this problem.

You are given the code segment:

char cards[15];

int i;

for (i=0; i<15; i++)

cards[i]='Z';

Write a code segment (several lines of code) to follow this code that will change the values of two distinct,randomlychosen cards to 'A'. Make sure that the two cards are not the same card! Declare any variables that you use.

5. Write a function fill(int array[][6], int listSize, int stepSize);

array is a 2-dimensional array. The fuction should fill the array with values, starting with the row number, and increasing by stepsize across each row. listSize is the number of rows. So for example, the call

fill( myArray, 3, 5)

would fill the array

myArray:

0 5 10 15 20 25

1 6 11 16 21 26

2 7 12 17 22 27

6. Write a code segment that reads an integer and prints “odd” or “even” to show that the number is odd or even.

7. Write a code segment to read a string from the keyboard and print the number of occurrences of the letter E (upper case and lower case.)

The pointer char* lastE shouldpoint to the last E (upper or lower case) of the string. If there are no E's in the string, lastE should be a null pointer.

8. Consider the struct

struct Vegetable {

double pricePerPound;

double pounds;

char[20] name };

Write a code segment that

  • declares 2 Vegetables, veg1 and veg2,
  • asks a user for the name, unit price, and weight of 2 vegetables and stores the answers in veg1 and veg2,
  • prints the name of the vegetable with the greatest total cost (pricePerPound times pounds). If they come out the same, print both names.

9. Write a function

int FindLargest(double d[])

that will return the index of the largest number in the array d.