C++ Assignment Help | Online Programming Tutor | Help with Homework
We offer solutions for your C++ programming project help and will ensure that you receive a solution that satisfies you. Our experts C++ programming tutors are highly educated and provide best help with C++ programming assignment. We also offer online C++ programming project help to students so that they can get help with C++ programming homework anytime.
If you require help with your C++ assignment or are looking for a C++ online tutor to help you complete your C++ programming project then we can provide that for you.
Help with C++ programming homework
ICS/CSE 45C: Assignment 4
Like the last assignment, pay attention to what your files and functions are supposed to be named. More instructions can be found at the end of this document. This assignment is due August 13, 2015 at 11:55pm. Upload your zip/tar file to Canvas. Be sure that your zip/tar contains all of the files you think it does. You will not be able to resubmit your work due to missing files.
Problem 1:
You will be creating a generic list, much like std::list from the STL. This list represents a collection of unordered elements. To practice using exceptions, this list implementation will throw three kinds of exceptions – ElementNotFoundException, EmptyListException, and FullListException.
Implement your list in files named List.h and List.cpp. Your list class implementation should support the following functions:
- get(int i) – returns the element at the index i. Throws ElementNotFoundException if the index is invalid. Throws EmptyListException if the list is empty.
- isEmpty() – returns true if the list is empty, false otherwise.
- first() – returns the element at index 0. Throws EmptyListException if the list is empty.
- last() – returns the element at index size – 1. Throws EmptyListException if the list is empty.
- insert(T item) – inserts the item at the end of the list. Throws FullListException if the list is full
- remove(int i) – removes the element at index i. “Removing” doesn’t mean erasing the contents from memory in this case. You will need to shift all elements inserted after the ith element in order to prevent any “holes” in the list. Throws ElementNotFoundException if the index is invalid.
A few constraints on your implementation:
- You may not use any STL data structures
- Implement this class using templates such that this list can store any type (but only one type)
- All return types will be actual objects and not references to objects (so no pointer return types).
- You may assume that your list will not exceed 10 elements.
- You must use an array to represent the actual list. Since we know there will be at most 10 elements, you can statically allocate this array.
- You will want to keep track of the number of elements in the list using a class variable named size.
You will have to define the exception classes. You may put them in the List.h file, but they should not be nested inside of the List class. In a file named list_tests.cpp, write a main method that tests your list or write a series of Gtest functions. You may define any helper functions you want to help test your list. You should use try-catch blocks to test that exceptions are thrown when they ought to be thrown. Note that this class throws an enormous amount of exceptions. Hopefully, this proves to you that you should use exceptions sparingly
Problem 2:
In an ancient land, the beautiful princess Eve had many suitors. She decided on the following procedure to determine which suitor she would marry. First, all of the suitors would be lined up one after the other and assigned numbers. The first suitor would be number 1, the second number 2, and so on up to the last suitor, number n. Starting at the first suitor she would then count three suitors down the line (because of the three letters in her name) and the third suitor would be eliminated from winning her hand and removed from the line. Eve would then continue, counting three more suitors, and eliminating every third
suitor. When she reached the end of the line she would continue counting from the beginning. For example, if there were six suitors then the elimination process would proceed as follows:
123456 initial list of suitors, start counting from 1 12456 suitor 3 eliminated, continue counting from 4 1245 suitor 6 eliminated, continue counting from 1 125 suitor 4 eliminated, continue counting from 5 15 suitor 2 eliminated, continue counting from 5 1 suitor 5 eliminated, 1 is the lucky winner
In a file named suitors.cpp
Write a function that uses your list from Problem 1 to determine which position you should stand in to marry the princess if there are n suitors (where n ≤ 10). Your function should take the number of suitors as a parameter and return the position of the list that you should stand in. The function should show each step while computing who will win. You may find it useful to write a helper function that prints out the contents of your list. The function signature should be
int findWinningPosition(int numSuitors)
For example, if you were to call findWinningPosition(6);, then your program should output
123456 12456 1245 125 15 1
Write another function that takes a list of names. You should use your list from Problem 1. The function should return the name of the person who gets to marry the princess. The function signature should be
std::string pickSuitor(List<std::string> suitors)
In a main method or a Gtest file, test your functions with some different inputs. Name this test file suitors_tests.cpp.
Problem 3:
For this problem, you will be handling very simple picture files. The format that we will be using is the Plain PGM format. A PGM file is very simple. A PGM file has the following format:
P2 widthOfPicture heightOfPicture MaxGrayValue [ width x height array of pixel values, where each value is separated with spaces ]
In this format, P2 designates the file as a plain PGM file. The maximum gray value defines the value that will be used as white. Pixels with the value 0 are black. Pixels with values between 0 and the max value are colored gray. Pixel values are given in ASCII decimal numbers and should be between 0 and 255.
For example, consider this (enlarged) picture :
The PGM file looks like this:
P2 24 7 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Your assignment is to write an Image class and an ImageScrambler class. The Image class will provide utilities for reading, writing, and getting fourths of the image. The ImageScrambler class will provide functions to move around fourths of the picture. The interface for each of these classes is provided below.
Scrambling an Image
We will divide up the picture into 4 quadrants (or fourths). The top left is the 1st quadrant, the top right is the 2nd quadrant, the bottom left is the 3rd quadrant, and the bottom right is the 4th quadrant. The ImageScrambler class should allow you to move the quadrants clockwise and counter clockwise. For example, suppose you start with the picture below.
The quadrants of this picture are shown here:
Moving the quadrants once clockwise should result in the following picture:
Image Interface
class Image{ private: int width; int height; int maxGrayValue; int** picture; public: // Constructors // Default Constructor Image(); // Initialize an image by reading in the picture // contained in filename Image(std::string filename); // initialize an image with dimensions width and height Image(int width, int height); // copy Constructor Image(const Image& other); // Destructor ~Image(); // Accessors/Mutators int getHeight() const {return height;} int getWidth() const {return width;} int getMaxGrayValue() const {return maxGrayValue;} // Other Functions // Assignment Operator Overload Image& operator=(const Image& other); // Reads in the image file contained in filename // Sets this picture to the image contained in the file // returns true if read successfully, false otherwise bool readImage(std::string filename); // Writes the current picture to file // filename: name of the file to write to // returns true if successful // returns false if unsuccessful or if this Image doesn't have // a picture to write to file bool writeImage(std::string filename); // returns the specified quadrant (1,2,3 or 4) of the picture as an image // whichQuadrant: 1 - top left fourth, 2 - top right fourth, // 3 - bottom left fourth, 4 - bottom right fourth Image getQuadrant(int whichQuadrant); // sets the quadrant to the new values contained in newQuadrant void setQuadrant(int whichQuadrant, const Image* newQuadrant); };
ImageScrambler Interface
class ImageScrambler{ private: Image myImage; public: // Constructor // Default Constructor ImageScrambler(); // initialize with the given image ImageScrambler(Image startingImage); // Destructor // doesn't need one // Accessors/Mutators void changeImage(Image newImage); // Other Functions // move quadrants of the image clockwise // will permanently modify myImage void moveQuadrantsClockwise(); // move the quadrants of the image counter clockwise // will permanently modify myImage void moveQuadrantsCounterClockwise(); // write the image to file void writeImage(std::string outfileName); };
Additional Notes
Some notes to keep in mind when implementing your classes:
- You may define any additional private helper functions or private member variables you need.
- When reading picture files, you may assume the image file is properly formatted. Keep in mind, however, that the more error checking you do, the easier it will be to hunt down bugs. Hence, it might be a good idea to check for things like out of range numbers and negative numbers, especially in the width, height, and max gray value fields. With these checks in place, you can read in files you’ve written to make sure that you’ve written the files correctly.
- You can make your life easier by making sure that the width and height are even numbers. You can add a row and/or column of zeros to the picture to make this happen.
- To see the picture, use the command display filename.pgm in the terminal. This will open ImageMagick and show you the picture, provided it is formatted correctly.
- You should write tests for your code. You do not need to turn in these tests. Be aware, however, that we will run our own tests against your code.
What to Turn In
Use your classes to read in 3 different pictures and scramble them. Scramble one by rotating once clockwise. Scramble another by rotating once counterclockwise. Scramble the third any way you’d like.
Here is a website where you can find some PGM images.
Submit Image.h, Image.cpp, ImageScrambler.h, ImageScrambler.cpp, the 3 original pictures, and the 3 scrambled pictures. You do not need to turn in the main method you write to scramble your pictures.
File Check list:
The files and folders you should turn in are listed below. Files listed indented under a folder should be contained in that folder.
Folder: Part1 File: List.h File: List.cpp File: list_tests.cpp File: suitors.h File: suitors.cpp File: suitors_tests.cpp File: makefile Folder: Part2 File: Image.h File: Image.cpp File: ImageScrambler.h File: ImageScrambler.cpp File: makefile File: picture1.pgm File: picture2.pgm File: picture3.pgm File: picture1_scrambled.pgm File: picture2_scrambled.pgm File: picture3_scrambled.pgm
Zip or tar up these folders. Do not zip/tar up these folders and then zip/tar those files. Just create a compressed file directly from these folders. Be sure to include your UCI NetID in the name of the zip/tar file. Finally, double check your submission. Make sure that the file you finally upload contains all of the files you think it does. There will be no re-grades due to forgotten files or corrupted files.
Folder: Part1
List.h
#ifndef _LIST_H #include <iostream> // define the exceptions for the list class class ElementNotFoundException : public std::exception { public: const char *what() const throw() { return "element is not found in the list"; } }; class EmptyListException : public std::exception { public: const char *what() const throw() { return "the list is empty"; } }; class FullListException : public std::exception { public: const char *what() const throw() { return "the list is full"; } }; // the template list class const int CAPACITY = 10; template <typename T> class List { private: T elements[CAPACITY]; // assume at most 10 elements int count; // the number of elements in the list public: // constructor and destructor List(); ~List(); T get(int i) const; int size() const; // the number of elements in the list bool isEmpty() const; T first() const; T last() const; void insert(T item); void remove(int i); }; #endif
List.cpp
#include "List.h" // implementation of the List class // template <typename T> List<T>::List() : count(0) { } template <typename T> List<T>::~List() { } template <typename T> T List<T>::get(int i) const { if (count == 0) { throw EmptyListException(); } if (i < 0 || i >= count) { throw ElementNotFoundException(); } return elements[i]; } template <typename T> int List<T>::size() const { return count; } template <typename T> bool List<T>::isEmpty() const { return size() == 0; } template <typename T> T List<T>::first() const { return get(0); } template <typename T> T List<T>::last() const { return get(count - 1); } template <typename T> void List<T>::insert(T item) { if (count == CAPACITY) { // already full throw FullListException(); } // put in the last position in the list elements[count++] = item; } template <typename T> void List<T>::remove(int i) { if (i < 0 || i >= count) { throw ElementNotFoundException(); } // shift the elements after i-th while (i < count - 1) { elements[i] = elements[i + 1]; i ++; } count--; }
list_tests.cpp
#include "List.cpp" // have to include cpp for template class #include <string> #include <iostream> using namespace std; int main() { // test the List class with different types // int List<int> l1; for (int i = 0; i < 10; i++) { l1.insert(i); } cout << "isempty ? " << (l1.isEmpty() ? "true" : "false") << endl; cout << "first = " << l1.first() << endl; cout << "last = " << l1.last() << endl; // now display the list elements using get method for (int i = 0; i < l1.size(); i++) { cout << "index = " << i << " element = " << l1.get(i) << endl; } // test exceptions try { l1.get(5); // ok l1.get(11); // not okay } catch(ElementNotFoundException e) { cout << e.what() << endl; } try { l1.insert(4); // not okay, already full } catch(FullListException e) { cout << e.what() << endl; } try { l1.remove(11); // not okay, no such element in the list } catch(ElementNotFoundException e) { cout << e.what() << endl; } // now remove all elements while (!l1.isEmpty()) { l1.remove(0); } try { l1.get(0); } catch(EmptyListException e) { cout << e.what() << endl; } // string List<string> l2; l2.insert("List"); l2.insert("class"); cout << l2.get(0) << " " << l2.get(1) << endl; return 0; }
suitors.h
#include "List.cpp" int findWinningPosition(int numSuitors); std::string pickSuitor(List<std::string> suitors);
suitors.cpp
#include "suitors.h" #include <iostream> #include <string> using namespace std; // a helper function to print the elements in the list template <typename T> void print(List<T> lst) { for (int i = 0; i < lst.size(); i++) { cout << lst.get(i); } cout << endl; } int findWinningPosition(int numSuitors) { // construct the initialize list List<int> lst; for (int i = 1; i <= numSuitors; i++) { lst.insert(i); } print (lst); // now start at index 0, each time count three steps int index = 0; while (lst.size() > 1) { index = (index + 2) % lst.size(); // eliminate it lst.remove(index); index = index % lst.size(); // adjust the index print (lst); } // the only one left is the winning position return lst.get(0); } std::string pickSuitor(List<std::string> suitors) { int numSuitors = suitors.size(); int index = findWinningPosition(numSuitors); return suitors.get(index - 1); }
suitors_tests.cpp
#include "suitors.h" #include <iostream> #include <string> using namespace std; // test the function in suitors int main() { int suitors = 6; List<string> names; names.insert("Ben"); names.insert("Jack"); names.insert("Robin"); names.insert("Alex"); names.insert("Simon"); names.insert("Mike"); // pick the index int position = findWinningPosition(suitors); cout << "Winning position is " << position << endl; string name = pickSuitor(names); cout << "Winner's name is " << name << endl; return 0; }
makefile
all: list_tests suitors suitors: suitors.cpp List.h List.cpp suitors_tests.cpp g++ suitors_tests.cpp suitors.cpp -W -o suitors_tests list_tests: list_tests.cpp List.h List.cpp g++ list_tests.cpp -o list_tests clean: rm -f list_tests suitors_tests
Folder: Part2
Image.h
#ifndef _IMAGE_H #define _IMAGE_H #include <string> #include <iostream> class Image { private: int width; int height; int maxGrayValue; int** picture; void reset(); // a helper function to release memory of the image public: // Constructors // Default Constructor Image(); // Initialize an image by reading in the picture // contained in filename Image(std::string filename); // initialize an image with dimensions width and height Image(int width, int height); // copy Constructor Image(const Image& other); // Destructor ~Image(); // Accessors/Mutators int getHeight() const {return height;} int getWidth() const {return width;} int getMaxGrayValue() const {return maxGrayValue;} // Other Functions // Assignment Operator Overload Image& operator=(const Image& other); // Reads in the image file contained in filename // Sets this picture to the image contained in the file // returns true if read successfully, false otherwise bool readImage(std::string filename); // Writes the current picture to file // filename: name of the file to write to // returns true if successful // returns false if unsuccessful or if this Image doesn't have // a picture to write to file bool writeImage(std::string filename); // returns the specified quadrant (1,2,3 or 4) of the picture as an image // whichQuadrant: 1 - top left fourth, 2 - top right fourth, // 3 - bottom left fourth, 4 - bottom right fourth // this assumes the height and width of the image are even numbers. Image getQuadrant(int whichQuadrant); // sets the quadrant to the new values contained in newQuadrant // this assumes that the height and width of the image are double of the given quadrant void setQuadrant(int whichQuadrant, const Image* newQuadrant); }; #endif
Image.cpp
#include "Image.h" #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; // Constructors // Default Constructor Image::Image() : width(0), height(0), maxGrayValue(0), picture(NULL) { } // Initialize an image by reading in the picture // contained in filename Image::Image(std::string filename) : width(0), height(0), maxGrayValue(0), picture(NULL) { readImage(filename); } // initialize an image with dimensions width and height Image::Image(int width, int height) : width(width), height(height), maxGrayValue(0), picture(NULL) { // allocate the memory for the width*height matrix picture = new int*[height]; for (int i = 0; i < height; i++) { picture[i] = new int[width]; } } // copy Constructor Image::Image(const Image& other) : width(0), height(0), maxGrayValue(0), picture(NULL) { *this = other; } void Image::reset() { // release the memory of picture for (int i = 0; i < height; i++) { delete[] picture[i]; } delete[] picture; // reset the parameter width = 0; height = 0; maxGrayValue = 0; picture = NULL; } // Destructor Image::~Image() { reset(); } // Other Functions // Assignment Operator Overload Image& Image::operator=(const Image& other) { reset(); width = other.width; height = other.height; maxGrayValue = other.maxGrayValue; // allocate memory for picture and copy them from the other image picture = new int*[height]; for (int i = 0; i < height; i++) { picture[i] = new int[width]; for (int j = 0; j < width; j++) { picture[i][j] = other.picture[i][j]; } } return *this; } // Reads in the image file contained in filename // Sets this picture to the image contained in the file // returns true if read successfully, false otherwise bool Image::readImage(std::string filename) { string line; int width = 0, height = 0, maxGrayValue = 0; int **picture = NULL; ifstream fin(filename.c_str()); if (fin.fail()) { // cannot open the file to read return false; } // read the content of this image from the file // skip comments bool flag = true; while ((flag = getline(fin, line))) { // skip P2 if (line.length() > 0 && line[0] == '#') continue; else break; } // now read the next line as the width and height while ((flag = getline(fin, line))) { if (line.length() > 0 && line[0] == '#') continue; else { istringstream iss(line); if (!(iss >> width >> height)) { // fail to read the width and height fin.close(); return false; } break; } } // make sure the width and height are positive if (width <= 0 || height <= 0) { fin.close(); return false; } // now read the maxGrayValue while ((flag = getline(fin, line))) { if (line.length() > 0 && line[0] == '#') continue; else { istringstream iss(line); if (!(iss >> maxGrayValue)) { // fail to read the value fin.close(); return false; } break; } } if (!flag) return false; // now allocate memory for the picture picture = new int*[height]; for (int i = 0; i < height; i++) { picture[i] = new int[width]; // load the line for this row of picture for (int j = 0; j < width; j++) { // read the value for (i, j) if (!(fin >> picture[i][j])) { // cannot read the value for that cell // release the memory of the arrays and return false for (j = i; j >= 0; j--) { delete[] picture[j]; } delete[] picture; fin.close(); return false; } } } fin.close(); // succeed reading the file, assign to the current image object reset(); this->width = width; this->height = height; this->maxGrayValue = maxGrayValue; this->picture = picture; return true; } // Writes the current picture to file // filename: name of the file to write to // returns true if successful // returns false if unsuccessful or if this Image doesn't have // a picture to write to file bool Image::writeImage(std::string filename) { // make sure the Image has a picture to write into file if (height <= 0 || width <= 0) { return false; } // open the file to write into ofstream fout(filename.c_str()); if (fout.fail()) { // cannot open the file return false; } // write into the file fout << "P2" << endl << width << " " << height << endl << maxGrayValue << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { fout << picture[i][j] << " "; } fout << endl; } fout.close(); return true; } // returns the specified quadrant (1,2,3 or 4) of the picture as an image // whichQuadrant: 1 - top left fourth, 2 - top right fourth, // 3 - bottom left fourth, 4 - bottom right fourth Image Image::getQuadrant(int whichQuadrant) { // make sure the parameter is valid if (whichQuadrant < 1 || whichQuadrant > 4) { return Image(); // an empty image } // make sure the width and height of this image are even numbers if (width % 2 != 0 || height % 2 != 0) { cerr << "the image does not have a even width or height." << endl; return Image(); } int offset_x = 0, offset_y = 0; switch (whichQuadrant) { case 1: break; case 2: offset_x += width / 2; break; case 3: offset_y += height / 2; break; case 4: offset_x += width / 2; offset_y += height / 2; break; } // otherwise, copy the quadrant into a new image Image r(width / 2, height / 2); r.maxGrayValue = maxGrayValue; // copy the picture for (int i = 0; i < height / 2; i++) { for (int j = 0; j < width / 2; j++) { r.picture[i][j] = picture[i + offset_y][j + offset_x]; } } return r; } // sets the quadrant to the new values contained in newQuadrant void Image::setQuadrant(int whichQuadrant, const Image* newQuadrant) { if (newQuadrant == NULL) return; if (whichQuadrant < 1 || whichQuadrant > 4) { return; } // make sure the width and height of this image are even numbers if (newQuadrant->width * 2 != width || newQuadrant->height * 2 != height) { cerr << "the image does not have double width or height of the quadrant." << endl; return; } // otherwise, copy the quadrant into the current image int offset_x = 0, offset_y = 0; switch (whichQuadrant) { case 1: break; case 2: offset_x += width / 2; break; case 3: offset_y += height / 2; break; case 4: offset_x += width / 2; offset_y += height / 2; break; } // copy the picture for (int i = 0; i < height / 2; i++) { for (int j = 0; j < width / 2; j++) { picture[i + offset_y][j + offset_x] = newQuadrant->picture[i][j]; } } }
ImageScrambler.h
#ifndef _IMAGE_SCRAMBLER #define _IMAGE_SCRAMBLER #include "Image.h" class ImageScrambler { private: Image myImage; public: // Constructor // Default Constructor ImageScrambler(); // initialize with the given image ImageScrambler(Image startingImage); // Destructor // doesn't need one // Accessors/Mutators void changeImage(Image newImage); // Other Functions // move quadrants of the image clockwise // will permanently modify myImage void moveQuadrantsClockwise(); // move the quadrants of the image counter clockwise // will permanently modify myImage void moveQuadrantsCounterClockwise(); // write the image to file void writeImage(std::string outfileName); }; #endif
ImageScrambler.cpp
#include "ImageScrambler.h" // Constructor // Default Constructor ImageScrambler::ImageScrambler() { } // initialize with the given image ImageScrambler::ImageScrambler(Image startingImage) : myImage(startingImage) { } // Destructor // doesn't need one // Accessors/Mutators void ImageScrambler::changeImage(Image newImage) { myImage = newImage; } // Other Functions // move quadrants of the image clockwise // will permanently modify myImage void ImageScrambler::moveQuadrantsClockwise() { // get the four quadrand Image q1 = myImage.getQuadrant(1); Image q2 = myImage.getQuadrant(2); Image q3 = myImage.getQuadrant(3); Image q4 = myImage.getQuadrant(4); // update the four quadrand // 3 1 // 4 2 myImage.setQuadrant(1, &q3); myImage.setQuadrant(2, &q1); myImage.setQuadrant(3, &q4); myImage.setQuadrant(4, &q2); } // move the quadrants of the image counter clockwise // will permanently modify myImage void ImageScrambler::moveQuadrantsCounterClockwise() { // get the four quadrand Image q1 = myImage.getQuadrant(1); Image q2 = myImage.getQuadrant(2); Image q3 = myImage.getQuadrant(3); Image q4 = myImage.getQuadrant(4); // update the four quadrand // 2 4 // 1 3 myImage.setQuadrant(1, &q2); myImage.setQuadrant(2, &q4); myImage.setQuadrant(3, &q1); myImage.setQuadrant(4, &q3); } // write the image to file void ImageScrambler::writeImage(std::string outfileName) { myImage.writeImage(outfileName); }
makefile
all: build build: g++ Image.cpp ImageScrambler.cpp scrambler.cpp -Wall -o scrambler clean: rm -f scrambler
We use the simple concepts of object oriented programming to provide C++ programming homework help. Our experts are available 24*7 to offer C++ programming assignment help You may contact us to avail C++ programming homework help that you need. Students can also upload their assignments on our C++ programming website to get our C++ programming assignment help.