Programming Assignments for SCMP 118, Fall 2014
Make sure you save your programs in your folder in P drive before the deadline and make sure it compiles in Dev-C++.
Having it elsewhere (e.g. in your H drive, or personal computer) does not help. Do NOT modify after the deadline
Lab 11: Inheritance (not to be graded, assigned as practice, can turn it as bonus, due 11:59 pm Sunday, Dec 14)
Lab 10
Part I: Due 11:59 pm, Tue Dec 9
Programming Projects 5 and 7 on pages 820-1.(20+20 pts)
For project 5, the second function should take a string argument (not a C-string). You will need to convert this string argument to a C-string using the c_str member function. Then, you will need to convert a C-string to a string object by using a constructor (whose argument is a C-string).
For Project 7, all the functions you use in sorting (including finding index_of_smallest, if you use it) must be recursive; you cannot use any loops. If you do this by modifying the sort function from Chapter 7, it would be easier to use 3 arguments for the sort function (instead of 2).
Note: Writing a reasonable test program and providing a good user interface is part of the assignment.
Part II: Due 11:59 pm, Wed Dec 10
Do Programming Project 8 on page 821 (10 pts)
Bonus: Due 11:59 pm Sun Dec 14. Do project 8 with a graphical illustration of all moves of the disks during the execution of the algorithm ( if you can do this with 3 disks, that will be good enough). As an additional bonus problem, write an iterative solution to Towers of Hanoi problem (as opposed to recursion).
Note: If you do any of the bonus problems, you must create a folder titled "Bonus" and save all of your programs there.
Lab 9: Due 11:59 pm, Fri Nov 21 (50 pts)
For this lab you need to create a project composed of several files. If you are using a Mac to do this project, you need to make sure what you submit works in Dev-C++.
The file that contains the main function must have the content of the file "Lab9Main.cpp" EXACTLY (except that you should replace the namespace "aydin" with your lastname, and possibly change the names of the header files), located in the class folder (P:\Class\Math\Aydin\118\FilesForLabs). Copy that file to your folder.
Write other necessary files, create a namespace (having your last name) and combine them into a project (you can use either method we learned) so that you can run the main program and it produces a reasonable output.
Here are the other files you need to write:
An interface (header) file called complex.h for the complex number class (which contains a definition of the complex number class, but no implementations of the member functions or constructors)
An implementation file, complex.cpp, which contains implementations of the members of the complex number class.
An interface (header) file called Functions.h with prototypes of the following functions:
factorial function // int fact(int);
exponential function // double exp(double)
natural logarithmic function// double log(double)
trig functions sine and cosine // double sin(double) and double cos(double)
An implementation file for the functions, Functions.cpp which contains definitions of the functions declared in the interface file. Use the formulas given in the class to define these functions. Also pay attention to the following: The factorial function is only defined for non-negative integers and the formula given for log function is only valid if the argument is positive but less than 2. If an argument different from these is given to these 2 functions, then an appropriate error message should be output.
All of these functions and ComplexNumber class must be a part of the namespace having your last name.
Lab 8: Due 11:59 pm, Fri Nov 14 (50 pts)
In this lab we are going to practice operator overloading with complex numbers.
We will also be using some of the earlier concepts such as file input-output, formatting and sorting.
First, copy the file "inputdata.txt" from the class folder (P:\Class\Math\Aydin\118\FilesForLabs) into your own folder (and DO NOT modify this file).
All the output should be sent BOTH to the screen and to an output file which should be called "outputdata.txt".
The file "inputdata.txt" contains 9 pairs of real (double) numbers.
Overload the binary operators +, *, >>, <<, and > as well as the unary operator - for the complex number class. For >, compare complex numbers on the basis of their moduli, i.e., if z1 and z2 are two complex numbers then z1 > z2 if and only if modulus of z1 > modulus of z2. The modulus of a complex number z = a+bi is sqrt(a*a+b*b)
Read these numbers from inputdata.txt and create a complex number corresponding to each pair. Store these complex numbers in an array (of complex numbers) of size 10.
Ask for one more complex number from the user to store in the last entry of the array using the overloaded operator >>.
Print out (and send to the file as well) each one of these complex numbers in a+bi form (use overloaded << operator to do that).
If the imaginary part is negative, there should be negative sign only. For example if a complex number has real part 1 and imaginary part -2 then it should be printed out as 1-2i rather than 1+-2i. If the imaginary part is + or -1, do not write 1. For instance instead of 2-1i, write 2-i. Also, if either (but noth both) part is 0, then do not write it out.
Print out the negatives of each one these complex numbers.
Find and print out the sum and product of all these 10 complex numbers. Do necessary formatting so that the real and imaginary parts of sum and product have 2 places after the decimal point. Also print out the modulus of the sum and the modulus of the product.
Finally, sort these complex numbers in descending order according to their moduli (moduli is the plural of modulus) and print them in sorted order.
Lab 7, Due 11:59 pm, Fri Nov 7
A) For this part, first copy the file "gradefile.txt" which is available in our class folder (full path: P:\\Class\\Math\\Aydin\\118\\FilesForLabs) into your own folder. DO NOT modify this file! (even after copying it into your folder). (20 pts)
Then write a program which does the following
1. Open gradefile.txt (from your own folder) and copy its content EXACTLY to another file in your folder. Call this output file gradeout.txt. Make sure it is an exact copy (including spacing).(Hint: use the member functions get and eof ). Close the file gradeout.txt after this operation.
2. As you see, this file contains a number of test scores. You will read the data from the gradefile.txt again. This time use another ifstream object to read the file a second time. Read every score as an integer (as opposed to regarding them as characters) and store in an integer array. (Assume that there are at most 100 scores). Find the number of scores in this file, maximum score, minimum score and average score. Now, open the file gradeout.txt, this time with the option to append. Output all of these values BOTH to the screen and to the file gradeout.txt. The average must be a double and should put exactly 2 places after the decimal point. (So you need to do necessary formatting). APPEND these data to the file gradeout.txt.
For part 1. using the member function get to read would be more convenient. For part 2, the extractor operator >> may be more useful. (See the relevant examples in the textbook).
3. Also output all the scores in descending order, and the median score both to the screen and to the file gradeout.txt
B) Do programming project 17 on page 370. The file babynames2012.txt is located at (20 pts)
P:\\Class\Math\\Aydin\\118\\SourceCodeFromTextbook\\Chapter06
C) Do Programming Project 18 on page 371-2 of the textbook. (Note: Firefox browser in our system should work to open svg files) (10 pts)
Bonus Project (You can work on this until the last day of classes): Do Programming Project 19 on page 373. You will earn bonus points by creating a good picture of your professor. You can directly write the svg file (you do not need to write the svg file through a C++ program). If you do this, please create folder titled "Bonus". It is due the last day of classes.
Lab 6: Strings and Vectors. Both parts due 11:59 pm, Fri Oct 31 (extended by 24 hours)
Part 1: (20+20 pts) Do programming projects 1 and 2 on pages 491-492. Write one program to do both exercises. For these problems, you may find it to be helpful to use library functions such as
isalpha( char ) (described on page 356, and 1006) or isalnum(char) [this one returns true if and only if the argument is a letter or a digit]. To count the number of occurences of each letter, it would be useful to remember the conversions between characters and integers. For example, what is int('c')-int('a') ?
Part 2: (20 pts) Write a program that asks user to enter students (name and score; grade should be determined on the basis of score). Student will be a class as in Lab 5. Each time ask the user whether s/he wants to add another student (do not ask how many students the user wants to enter). Store the students in a vector (so you will continuously grow the vector). After the user finishes entering students, the program should report how many students have been entered and what the average score is. It should also sort the students (and print out the info) in descending order of scores. Note: Unlike arrays, a vector argument is not automatically call-by-reference. Pay attention to this when sorting the vector of students.
Lab 5
Part I: Due 11:59 pm, Monday October 20 (20 pts). (You should be able to do much of this lab in class on Thursday Oct 16)
Write a program that reads student names followed by their test scores. The program should output each student's name, followed by the test score and relevant grade. It should also find and print the highest test score and the name(s) of students having the highest test score. Each student data should be stored in a struct variable of the type StudentType, which has 3 components: name (string), score (int, between 0-100), and grade (char). Assume that the class has no more than 20 students. Ask the user how many students is s/he going to enter. Use an array to store all students (so you need an array of students). Let the user repeat the computations as long as s/he wishes.
The grades are assigned as follows:
90-100: A
80-89: B
70-79: C
60-69: D
below 60: F
Finally (after finishing the above tasks), sort the array of students according to the alphabetical ordering of the names, and print out the names (only names) in the sorted order.
Note: You may encounter the "mixing cin >> variable and getline" problem again. See pages 472-3 for the description and possible solutions to the problem. An easy fix is to use cin.ignore( ) after every cin>> statement in the program.
Part II: Due 11:59 pm, Fri Oct 24
a) Implement Part I above using classes this time. But you do NOT need to sort the data this time. (20 pts)
Requirements:
All the data members of Student class (name, score, grade) must be private.
Define appropriate member functions to set and access private data.
Define appropriate constructors.
You should not use any functions other than member functions of Student class and constructors.
Do not forget to define the default constructor (Remember, once you define a constructor with at least one parameter, the compiler's default constructor is no longer available. See the programming tip on page 572)
b) Write a definition of a class named Point that can be used to store and manipulate the location of a point in the 2 dimensional plane. You will need to declare and implement the following member functions: (20 + 5 pts)
0) appropriate constructors
i) a member function set that sets the private data after an object of this class is created
ii) a member function to move the point by any amount along the vertical and horizontal directions specified by the first and second arguments
iii) two accessor functions to retrieve the current coordinates of the point
iv)*** a member function (with one parameter) to rotate the point by a specified amount of degrees (given by the argument) counter-clockwise around the origin *** Writing this function will be bonus. You will need some trigonometry, and some mathematical functions from the cmath library. Using polar coordinates will help. Try this after finishing other parts.
Embed your class in a test program that requests data for several points from the user, create the points, then uses the member functions. Writing a good/reasonable main program that tests essential features of this class is part of the assignment.
Lab 4: Due 11:59 pm, Wed Oct 8
Part I: (20 pts) Make necessary changes to to the sorting program 7.12 (pages 415-20 in the textbook) so that it sorts an array of names (strings) in dictionary order. Make sure that the following are satisfied:
1. Use a global constant variable =20 for an upper bound on the size of the array to be sorted
2. Use a function to print the array in sorted order (also use a function to fill in the array)
3. Give the user option of entering (and sorting) as many sets of names as s/he wants (but no more than 20).
4. Do not assume that the strings will be single words (so don't use cin >> to get the input)
5. Do not worry about changing the case of the letters (upper case vs lower case). You may assume that all names will be given in a standard form such as: Lionel Messi
Note: You may encounter the "mixing cin >> variable and getline" problem. See pages 472-3 for the description and possible solutions to the problem.
Part II: (20 pts) Call a square matrix a "magic square" if the sum of each row, the sum of each column, and the sum of the (main) diagonal are all equal to the same number. As an easy example, if all the entries of the matrix are equal, then it is a magic square. Write a program which determines whether a given 4 by 4 matrix is a magic square. Assume that the entries are integers. Be sure to use a 2 dimensional array to store the input. It would be nice if you let the user repeat the computation before quitting the program.
Lab 3: Due 11:59 pm, Tue Sep 30
Do the programming project described here
Lab 2: Due 11:59 pm, Fri Sep 19
i) Do programming project 6 on page 175 (Note: If a is 0, it is not a quadratic equation) (20 pts)
ii) Do programming project 16 on page 179. (10 pts)
iii) Write a program that takes a positive integer n from the user and computes the following quantity with 4 digit precision (15 pts). Give the user the option of repeating the computations as long as s/he wants.
Hint: Use a nested, double for loop, and use two running totals. To help you test correctness of your program, here are some values: for n = 3, sum is 4.9073; for n =5 it is 10.1283; for n = 6 you should get 13.0839. You need to access the square root function from the cmath library.
iv) For practice, not to be graded: Do programming project 13 on page 178 (Note that there is a "Video Note" solution to this program here ). This is a very interesting problem. I highly recommend that you try it.
Lab 1: Due 11:59 pm, Fri Sep 12
***************************************************************************************************************
Notes:
1) Starting with this lab, you must follow the guidelines on Program Style described in section 2.5 of the textbook. In particular, follow the style given in Display 2.16 (page 95) of the textbook. You will lose points if you don't. You must follow these guidelines in all future labs (including Lab1).
ii) For Project 7, all decimal point numbers must be printed with 2 digits after the decimal point. (e.g. 2.50 instead of 2.5). See pages 55-57 of the textbook for this.
**************************************************************************************************************
Part I) (20 pts) Do Programming Project 7 (harder version) at the end of Chapter 2 (page 106)
Part II) (20 pts) Write a C++ program which takes a 4-digit number (integer) as input from the user and prints the number in reverse order. For example, if the number entered is 5678 then the output should be 8765. The program should repeat this until the user decides to quit.
You need to pay attention to the following
1. Provide appropriate comments in your code
2. Use meaningful identifiers
3. Don't take the input as a sequence of characters. Take it as a single integer. Also, construct the output as a single integer, as opposed to printing it digit by digit.
4. Your program should give a warning message if the number (value) is not a 4-digit number. For example, 0345 is not considered to be a 4-digit number.
5. Your program should not be case-sensitive for the user choice i.e. both Q and q are should be acceptable quit.
6. You may assume that when asked to enter a 4-digit number, the user enters a positive integer (as opposed to other kind of data, such as a character)
7. When the last digit (units) of the input is 0, the reverse is not a 4-digit number. So it is Ok to report the reverse as a 3-digit number in that case.
Here is a typical dialogue for this program:
Enter a 4-digit number
134
This is not a 4-digit number: Enter a 4-digit number
1234
The reverse number is 4321
Enter Q to quit, enter any letter to continue
C
Enter a 4-digit number
9001
The reverse number is 1009
Enter Q to quit, enter any letter to continue
Q
Thanks for using this program, bye !...
(Hint: If the number entered is n, what does n %10 give you? How about n/10? )
Part III) (for practice, not to be submitted) Project 14 on page 108.
Lab 0: Due Thu, Sep 4, 11:59 pm
0) Do the programming projects 6 and 7 on page 37. Use the program in Display 1.8 (source code available). Do this for yourself, do NOT turn in.
i) Do the programming project 5 on page 36. Use the program in Display 1.8 as a model. Submit your program (save it in your class folder in P drive) by midnight Thu, Sep 4. Do NOT modify the program after that time. (20 pts)
ii) Do the programming project 8 on page 38. Use the program in Display 1.8 as a model. Submit your program (save it in your class folder in P drive) by midnight Thu, Sep 4. Do NOT modify the program after that time. (20 pts)