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
- Write a program that does the following:
-
- 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
- you could statically define an array of ten
pointers to integer like this:
- 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 ];
???
- checks that memory allocation succeeds
- puts a value into the storage pointed to by each element
- prints each value
- 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 []
???
- Compare this problem with the program new-1.cpp given in the lecture notes, which dynamically allocates an array of ten integers.
- A solution will be available at the start of the next class.
Homework, due Tuesday 28 March 2006
Write a C++ program that:
- reads up to 20 floating point numbers from standard input into an array first
-
then, after the numbers are stored in the array
- Calculates the maximum value of the numbers in the array using a function that accesses no global variables
- Calculates the minimum value of the numbers in the array using a second function that accesses no global variables
- Calculates the average value of the numbers in the array using a third function that accesses no global variables
- prints these three values
- 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.
- 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).
- A solution will be available at the start of the next class.
Homework, due Tuesday 21 March 2006
Write a C++ program that:
- reads up to 20 floating point numbers from standard input into an array first
-
then, after the numbers are stored in the array
- Calculates the maximum value of the numbers in the array
- Calculates the minimum value of the numbers in the array
- Calculates the average value of the numbers in the array
- prints these three values
- 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.
- 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.
- A solution will be available at the start of the next class.
Homework, due Tuesday 14 March 2006
Write a C++ program that:
- Reads up to 20 floating point numbers from standard input
- prints a count of the numbers
- prints the maximum value
- prints the minimum value of these numbers
- prints the average value of these numbers
- The average value of n numbers is the sum of these n numbers divided by n.
- Do not use an array.
- Here is a sample run:
- $ ./max-min-mean
2345 2343245.3465 345654 -345635464565.4564375 43
5 numbers: max = 2.34325e+06 min = -3.45635e+11 average = -6.91266e+10 - Note that I pressed the "end of file" keystroke (Control-Z on Windows) after entering those five numbers
- A solution
Homework, due Tuesday 7 March 2006
Write a C++ program that:
- reads all characters from standard input;
- writes all the characters to standard output
- counts all the characters that are:
-
- letters
- numbers
- white space
- punctuation
- any characters that are not letters, numbers, white space or punctuation if there are any
- Your program should also print a count of the total number of characters
- You may wish to use the library functions isalpha(), isdigit(), isspace(), ispunct(), declared in ctype.h
- Here is a sample run:
-
$ ./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
- 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:
- Uses the iostream library for input and output;
- Reads a single positive or negative decimal value that can fit in a long long integer;
- Writes to standard output the binary value of the decimal number that was read in.
- Note that a long long constant is shown with two 'L's, i.e., you write a constant long long 1 as 1LL
- 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
- Show fewer than 64 bits if appropriate, i.e.,
- 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:
- Uses the iostream library for input and output
- converts from Celsius to Fahrenheit or Fahrenheit to Celcius
- 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
- 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
- The output should have the same format
- Print an error message to standard error and terminate the program if the input temperature format does not follow this format.
- You might want to look here for some ideas
- Make your program as simple and elegant as you can.
- Submit your program before the beginning of the next class by email.
- 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 - 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.
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