SI 204 Spring 2017 / HWs


This is the archived website of SI 204 from the Spring 2017 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Homework 34: Getting your feet wet with C++

Name: _____________________________________________ Alpha: ___________________

Describe help received: ________________________________________________________

  • Due before class on Wednesday, April 19
  • This homework contains code to be submitted electronically. Put your code in a folder called hw34 and submit using the 204sub command.
  • This is an electronic homework and you do not need to print out anything to turn in during class.

Assignment

  1. Required reading: Sections 1 (C++ basics), 2 (Bool), 3 (Functions), and 4 (allocation) from the Unit 10 notes.
  2. Write a C++ program called evens.cpp that reads in an array of numbers and then reports how many of those numbers are even. (Of course you could just check whether they're even as you read them in, but I really want you to actually make and use an array.)

    You must define and use the following two functions in your program:

    // prompts for the size, then allocates and reads in an array
    // of that size, returning a pointer to the array.
    // Of course you should use "new" to do the allocation!
    // Notice that size is passed by reference.
    int* readnums(int& size);
    
    // Returns true if x is an even number, otherwise false
    bool isEven(int x);

    Example runs:

    roche@ubuntu$ ./evens
    How many numbers? 3
    4 4 7
    2 even numbers
    roche@ubuntu$ ./evens
    How many numbers? 5
    6 5 4 3 2
    3 even numbers
    roche@ubuntu$ ./evens
    How many numbers? 1
    19
    0 even numbers