Test I, CSCI 230, Fall 2020


  1. Algorithms
    1. [2 points] What is the purpose of writing an algorithm in the design phase?

      Writing an algorithm allows the programmer to :
      • Plan how they will implement the program.
      • Decide on the data structures and formal algorithms needed.
      • Look for patterns and suggest possible support functions.
      • Better understand the specifications and what is required for the program.

      You don't need to provide all of these and other answers may be acceptable.

    2. [3 points] When writing an algorithm in the design phase, why is it inappropriate to write the algorithm in c++ code?
      Writing c++ code forces the program to focus on the syntax of the language. There is little or no advantage to using a text editor to create code.

      Using pseudo code allows the user to focus on the problem.

      I really don't buy the "it makes it easier for another person to read your design" argument.

  2. Integer Data Types
    1. [3 points] List the different integer data types in c++?
      • bool
      • char
      • short
      • int
      • long
      • long long

      You do not need to include long long.

      This is presented in various locations. Page 92 of the book has the first presentation of this material.

    2. [2 points] What is the relationship between the integer data types in c++?
      sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long)

      See page 463.

    3. [1 point] What is the difference between a signed and an unsigned integer type?
      Using the unsigned reserved word shifts the range for an $n$ bit integer from $-2^{n-1} ... 2^{n-1}-1$ to $ 0 ... 2^n-1$.

      This allows the representation of positive numbers with a much larger magnitude when the corresponding unsigned type.

      By default all integer types are signed.

      I think you really should discuss the difference in range as well.

  3. Strings
    1. [1 point] What is the domain of a string?
      A string is a collection of 0 or more characters.

    2. [3 points] List and describe three operations on a c++ style string.
    There are many operations on a string. They include
    • Finding the length of a string (string.size()). This operation returns the number of characters in the string.
    • Locating a substring within a string (string.find()). This operation takes a string and a substring and returns the location of the substring within the string.
    • Extracting a substring from a string (string.substr()). This operation takes a string, a starting position and a length and returns the substring starting at the position of the given length.

    There are many other potential answers to this question.

  4. [5 points] Write a function which takes a string as a parameter and returns the string as all upper case characters. The function prototype is provided.
    
    string ToUpper(string phrase){
       size_t i;
    
       for(i = 0; i < phrase.size(); i++) {
           phrase[i] = toupper(phrase[i]);
       }
    
       return phrase;
    }
    

  5. [2 points] What does the c++ typedef statement do?
    The typedef statement provides a nickname for a type.

  6. Enumerator Types
    1. [2 points] What is the purpose of enumerator types?
      The enumerator type allows the programmer to create a new data type that has a finite number of values. This allows the programmer to use data of this type in a more "natural" way. Ie the user does not need to map this type to strings or integers.

    2. [4 points] Why are enumerator types superior to strings when either is applicable? State and justify at least two arguments.
      1. Due to variations such as different letter cases or spelling problems, multiple strings can represent the same value. This is not true with an enumeratior type.
      2. Strings can take up much more memory than an enumerator type.
      3. The compiler can check for some simple logical errors when using enumerator types. This includes missing cases in a switch statement.

    3. [2 points] Provide code to declare an enumerator type that represents the days of the week.
      enum WeekDayT {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY};
      

      Enumerators should all be upper case

    4. [2 points] Describe a common operation that can not be performed directly on an enumerator type.
      The following can not be performed on enumerator types
      • Direct I/O
      • The increment operator (++, --)
      • Direct assignment from integer

  7. Structures
    1. [2 points] Describe two properties of the datatype struct.
      • The struct data type can hold heterogeneous data
      • The individual data values (called fields) can be accessed by name.

    2. [2 points] What is the purpose of the following code. How much memory is allocated due to this code?
      struct MyDataT {
          string word;
          int count;
      }; 
      This provides a blueprint telling the compiler how to construct an item of type MyDataT. No memory is allocated due to this code, it is only a plan for how memory will be allocated.

    3. [2 points] Using the structure defined in the previous step, provide code which will
      • declare an instance of the structure
      • set the word field of the structure to be "hello"
      • set the count field of the structure to be 1
      MyDataT avar;
      avar.word = "hello";
      avar.count = 1;
      

    4. [2 points] Name two different types operations which can not be performed on structures.
    • Direct I/O
    • Math Operations.
    • Direct Comparison

  8. [5 points] The file numbers.txt contains 10 integers. Write a function which finds the maximum and minimum of the values in this file. The function should take the file name, a string and two integer reference parameters max and min. The prototype for the function is provided.
    void FindExtreme(string fileName, int & max, int & min){
    
        ifstream inFile;
        size_t i;
        int data;
    
        inFile.open("numbers.txt");
    
        cin  >> data;
    
        max = data;
        min = data;
        for(i = 1; i < 10; i++) {
            cin >> data;
            if (max < data) {
               max = data;
            } 
            if (min > data) {
               min = data;
            }
        }
    
        inFile.close();
    }
             

  9. [5 points] Assume afile.txt exists and contains only integer data. The programmer wishes to count the number of integers in the file. Describe all errors contained in the following code segment. You may assume that all code before and after this code is correct and that all required files have been included.

Submission Instructions