Homework is not optional

Read the rules about submission of homework and assignments.

I want to help you.  I want everyone to pass.  If you ask me 100 questions every day, I will work hard and answer them.  Your questions need to be specific, not just completely general.

Hand in by email

I have found that it is easiest for me to send comments on your homework if you email it to me with the word "homework" (not "Home Work") somewhere in the subject.  So please do that bringing another copy to class unless I have acknowledged receiving your homework, since email does not guarantee delivery.  Deadline is 1.30pm (start of class).

Final week's homework, due Tuesday 4 April 2006

  1. Write a program that does the following:
    1. statically defines an array of ten pointers to integer
      • you could statically define an array of ten pointers to integer like this:
        int *pnums[ 10 ];
        
      • Then:
        pnums[ 0 ] is of type pointer to integer
        *pnums[ 0 ] is of type integer
    2. dynamically allocates an integer for each of the ten pointers
      • Ask yourself: how many times will I need to call new?
      • Ask yourself: would you call new as:
        new int;
        
        or as:
        new int[ 10 ];
        
        ???
    3. checks that memory allocation succeeds
    4. puts a value into the storage pointed to by each element
    5. prints each value
    6. frees up the memory that was dynamically allocated
      • Ask yourself: how many times will I need to call delete?
      • Ask yourself: would you call delete as:
        delete
        
        or as:
        delete []
        
        ???
  2. Compare this problem with the program new-1.cpp given in the lecture notes, which dynamically allocates an array of ten integers.
  3. A solution will be available at the start of the next class.

Homework, due Tuesday 28 March 2006

Write a C++ program that:

  1. reads up to 20 floating point numbers from standard input into an array first
  2. then, after the numbers are stored in the array
    1. Calculates the maximum value of the numbers in the array using a function that accesses no global variables
    2. Calculates the minimum value of the numbers in the array using a second function that accesses no global variables
    3. Calculates the average value of the numbers in the array using a third function that accesses no global variables
    4. prints these three values
  3. Note that your program should stop and give a correct answer even if the input contains something that is not part of a number, or if input has reached end of file.
  4. The output is the same as for the homework from last week; in other words, it should behave the same as the homework from the previous two weeks (that were due 21 March, 14 March).
  5. A solution will be available at the start of the next class.

Homework, due Tuesday 21 March 2006

Write a C++ program that:

  1. reads up to 20 floating point numbers from standard input into an array first
  2. then, after the numbers are stored in the array
    1. Calculates the maximum value of the numbers in the array
    2. Calculates the minimum value of the numbers in the array
    3. Calculates the average value of the numbers in the array
    4. prints these three values
  3. Note that your program should stop and give a correct answer even if the input contains something that is not part of a number, or if input has reached end of file.
  4. The output is the same as for the homework from last week; in other words, it should behave the same as the homework from last week.
  5. A solution will be available at the start of the next class.

Homework, due Tuesday 14 March 2006

Write a C++ program that:

  1. Reads up to 20 floating point numbers from standard input
  2. prints a count of the numbers
  3. prints the maximum value
  4. prints the minimum value of these numbers
  5. prints the average value of these numbers
    1. The average value of n numbers is the sum of these n numbers divided by n.
  6. Do not use an array.
  7. Here is a sample run:
  8. $ ./max-min-mean
    2345 2343245.3465 345654 -345635464565.4564375 43

    5 numbers: max = 2.34325e+06 min = -3.45635e+11 average = -6.91266e+10
  9. Note that I pressed the "end of file" keystroke (Control-Z on Windows) after entering those five numbers
  10. A solution

Homework, due Tuesday 7 March 2006

Write a C++ program that:

  1. reads all characters from standard input;
  2. writes all the characters to standard output
  3. counts all the characters that are:
    1. letters
    2. numbers
    3. white space
    4. punctuation
    5. any characters that are not letters, numbers, white space or punctuation if there are any
  4. Your program should also print a count of the total number of characters
  5. You may wish to use the library functions isalpha(), isdigit(), isspace(), ispunct(), declared in ctype.h
  6. Here is a sample run:
  7. $ ./classifychars < to-binary-simple-sign.cpp
    #include <iostream>
    #include <stdlib.h>
    #include <limits.h>
    
    int main( void )
    {
            long long num;
            std::cout << "Enter a positive or negative number: ";
            if ( ! ( std::cin >> num ) ) {
                    std::cerr << "Unable to read a number; giving up\n";
                    exit( 1 );
            }
            std::cout << num << " == " << std::hex << std::showbase << num << '\n';
            std::cout << std::dec << num << " (dec) = ";
            for ( unsigned i = 0; i < sizeof( long long ) * CHAR_BIT; ++i ) {
                    if ( num < 0 )
                            std::cout << 1;
                    else
                            std::cout << 0;
                    num <<= 1;
            }
            std::cout << " (bin) \n";
            return 0;
    }
    whitespace:  322
    digits:      7
    letters:     280
    punctuation: 131
    total chars: 740
    
  8. A solution using ctype.h, and a solution not using ctype.h will be available at the start of the next class

Homework, due Tuesday 28 February 2006

Write a C++ program that:

  1. Uses the iostream library for input and output;
  2. Reads a single positive or negative decimal value that can fit in a long long integer;
  3. Writes to standard output the binary value of the decimal number that was read in.
  4. Note that a long long constant is shown with two 'L's, i.e., you write a constant long long 1 as 1LL
  5. For extra marks:
    • Show fewer than 64 bits if appropriate, i.e.,
      • if the signed value could fit in a character, show only CHAR_BIT bits, or
      • if the signed value could fit in a short, show only sizeof( short ) * CHAR_BIT bits, or
      • if the signed value could fit in an int, show only sizeof( int ) * CHAR_BIT bits, or
      • if the signed value could fit in a long, show only sizeof( long ) * CHAR_BIT bits, or
      • if the signed value could fit in a long long, show only sizeof( long long ) * CHAR_BIT bits.
    • Separate the binary number into groups of four bits, starting at the right hand side.
    • Here are some examples of running a program (that earns extra marks) to show what I mean (things in bold are what I typed):
      $ ./to-binary
      Enter a positive or negative number: 9
      9 (dec) = 0000 1001 (bin)
      9 == 0x9
      $ ./to-binary
      Enter a positive or negative number: 256
      256 (dec) = 0000 0001 0000 0000 (bin)
      256 == 0x100
      $ ./to-binary
      Enter a positive or negative number: 65536
      65536 (dec) = 0000 0000 0000 0001 0000 0000 0000 0000 (bin)
      65536 == 0x10000
      $ ./to-binary
      Enter a positive or negative number: 4294967296
      4294967296 (dec) = 0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000 (bin)
      4294967296 == 0x100000000
      $ ./to-binary
      Enter a positive or negative number: 111111111111111
      111111111111111 (dec) = 0000 0000 0000 0000 0110 0101 0000 1110 0001 0010 0100 1110 1111 0001 1100 0111 (bin)
      111111111111111 == 0x650e124ef1c7
  6. Two solutions with extra marks (second) will be available at the start of the next class. Two simpler solutions will also be available.

Homework, due Tuesday 21 February 2006

Write a C++ program that:

  1. Uses the iostream library for input and output
  2. converts from Celsius to Fahrenheit or Fahrenheit to Celcius
  3. Can read an unlimited number of temperatures from standard input of both Fahrenheit and Celsius, and convert each to the other temperature scale, and print the result to standard output
  4. You may assume that the input is in the form of floating point number followed immediately by any of the letters C, c, F, f
  5. The output should have the same format
  6. Print an error message to standard error and terminate the program if the input temperature format does not follow this format.
  7. You might want to look here for some ideas
  8. Make your program as simple and elegant as you can.
  9. Submit your program before the beginning of the next class by email.
  10. Here is a sample run (things in bold are what I typed):
    $ ./temperature
    62.3c
    144.14F
    60 C
    140F
    27 C
    80.6F
    140 F
    60C
    q

  11. My solution will be available at the next class. Here is another using an if statement. Here is another using an if statement and with no functions.
If you find it too hard, send me an email asking questions.

homework, due Tuesday 14 February 2006

Download and install the Dev-C++ compiler.

I have written a detailed explanation about how to add C:\Dev-Cpp\bin to your PATH