Name: ________________________________ Section: _________________

Honor Code Acknowledgment: ___________________


Random Quiz # 7

CPS 6, Fall 1995

Due: October 30 (boo)


Problem Array Max (2 pts)

Write the function MaxValue that returns the largest value stored in the first num locations of the array a.

    int MaxValue(const Vector<int> & a, int num)
    // precondition: a contains num values
    // postcondition: returns the largest of the values stored in a













Problem 2: Acch, ptooey (3 pts)

The function atoi in <stdlib.h> converts a string to an integer. However, the value of atoi("dog") is 0. Suppose that you don't have access to <stdlib.h> so that you must write your own version of atoi. Do this using the header below. The function doesn't have to work with negative numbers, but you get extra credit if it does. The intOk parameter is set to true if the string passed is a valid integer (all digits) and is set to false if the string is NOT a valid integer.

Thus atoi("123",x,y) should result in x getting the value 123 and y getting the value true; whereas atoi("12a3",z,w) should result in w getting the value false (the value of z in this case can be anything).

    void atoi(string s,int & value, bool & intOK)
    // postcondition: value = integer represented by string s
    //                intOk = true if s represents a valid integer
    //                otherwise intOk = false