C++ FILE IO/FUNCTIONS PART ONE AND TWO OF THE ASSIGNMENT WERE ABOUT GETTING A US
ID: 3748846 • Letter: C
Question
C++ FILE IO/FUNCTIONS
PART ONE AND TWO OF THE ASSIGNMENT WERE ABOUT GETTING A USER INPUT FILE AND TRANSFORMING THAT DATA INTO A 5X6 2D ARRAY. THE VERY FIRST NUMBER (CAN BE 1-5) IN THE INPUT FILE IS NOT PART OF THE ARRAY BUT IS JUST THE STARTING POINT. ONLY NEED HELP WITH PARTS 3 AND 4.
3.3 Generate a Path of Least Resistance Given that the map is in a 2D grid, a walk can be defined as starting a given point on the left-most edge of the map and proceed to take a step into one of three possible points in the column to the right: top-right(),direct right), and bottom-right(). The formula for calculating the change in elevation of at a given prospective step: Ay- lyf-yo yf refers to the value of the final elevation (the elevation of a prospective step) yO refers to the value of the initial elevation (the current elevation) For the purposes of this assignment, you can assume that there not be a case where there will be a tie in the change of elevation between your three options. Henceforth, there will only be one single path you can take Case 1: Smallest change in elevation. Move to the block with the least change in elevation 101 100 107 102 Smallest change is 1, move to the top right() Additionally, your program must use at least this function: double difference(double array[5][6], int r, int c, intri, int c1)( implementation not shown/ This function must return the change in elevation between the current point and a prospective step Ex. If I am at point (row 2, col:1) and am thinking about going to (3,2), difference0 should return 5.0 109 100 107Explanation / Answer
//main.cpp
#include "BookStore.h"
using namespace std;
int main(){
const string s1 = "Bookdata";
Bookstore bookstore = Bookstore(s1);
bookstore.print();
return 0;
}
----------------------------------------------------------------------
//Book.cpp
#include <cstring>
#include <iostream>
#include <iomanip>
#include "Book.h"
using namespace std;
// default constructor
Book::Book() {
ISBN[0] = '';
title[0] = '';
setPrice(0);
setQuantity(0);
}
//Constructor(char array new ISBN, char array new Title, double variable new Price, int new Price
Book::Book(char *newISBN, char *newTitle, double newPrice, int newQuantity) {
strcpy(ISBN, newISBN);
strcpy(title,newTitle);
setPrice(newPrice);
setQuantity(newQuantity);
}
//Accessor method declaration returns ISBN data member
char* Book::getISBN() {
return ISBN;
}
//Accessor method declaration returns Title data member
char* Book::getTitle() {
return title;
}
//Accessor method declaration returns Price data member as type double
double Book::getPrice() {
return price;
}
//Accessor method declaration returns Quantity as type int
int Book::getQuantity() {
return quantity;
}
//Method declaration that takes a double argument and newPrice
void Book::setPrice(double newPrice) {
if (newPrice >= 0) {
price = newPrice;
} else {
price = 0;
}
}
//Method declaration that takes an int newQuantity
void Book::setQuantity(int newQuantity) {
if (newQuantity >= 0) {
quantity = newQuantity;
} else {
quantity = 0;
}
}
//Accessor method that takes an int which is the quantity of the book and returns the quantity available
int Book::fulfillOrder(int orders) {
int current_quantity = getQuantity();
if (orders < 0) {
return 0;
} else if (orders <= getQuantity()) {
setQuantity(current_quantity - orders);
return current_quantity;
} else {
int current_quantity = getQuantity();
setQuantity(0);
return current_quantity;
}
}
//Method prints the ISBN, title, price, and quantity members in custom format
void Book::print(){
left( cout << ISBN << setw(14) << endl );
left( cout << title << setw(44) << endl );
right( cout << price << setw(5) << endl );
right( cout << quantity << setw(6) << endl );
}
------------------------------------------------------------------
//Book.h
#ifndef ASSIGNMENT_2_BOOK_H
#define ASSIGNMENT_2_BOOK_H
class Book
{
private:
// Char array with room for 10 chars + null
char ISBN[11];
//Char array with room for 40 chars + null
char title[41];
//Double variable used for storing price value
double price;
//Integer used for storing stock quantity
int quantity;
public:
Book(); //Default Constructor
//Alt Constructor(char array new ISBN, char array new Title, double variable new Price, int new Price
Book(char *newISBN, char *newTitle, double newPrice, int newQuantity);
char * getISBN(); //Accessor method declaration returns ISBN data member
char * getTitle(); //Accessor method declaration returns Title data member
double getPrice(); //Accessor method declaration returns Price data member as type double
int getQuantity(); //Accessor method declaration returns Quantity as type int
void setPrice(double newPrice); //Method declaration that takes a double argument and newPrice
void setQuantity(int newQuantity); //Method declaration that takes an int and newQuantity
int fulfillOrder(int orders); //Accessor method that takes an int which is the quantity of the book and returns the quantity available
void print(); //Method prints the ISBN, title, price, and quantity members in custom format
};
#endif //ASSIGNMENT_2_BOOK_H
----------------------------------------------------------------------------
//BookStore.cpp
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "BookStore.h"
using namespace std;
// default constructor
Bookstore::Bookstore() {
booksSize = 0;
}
// alternate constructor
Bookstore::Bookstore(const std::string& str) {
ifstream inFile;
inFile.open(str);
if (!inFile.is_open()) {
perror("could not open file!");
} else {
inFile.read((char*) this, sizeof(Bookstore));
inFile.close();
}
}
void Bookstore::print(){
cout << "Book Inventory Listing ";
cout << "----------------------- ";
for (int i = 0; i < (int) sizeof(inventory); i++) {
inventory[i].print();
}
}
----------------------------------------------------------------------
//BookStore.h
#ifndef ASSIGNMENT_2_BOOKSTORE_H
#define ASSIGNMENT_2_BOOKSTORE_H
#include "Book.h"
#include <string>
class Bookstore {
private:
Book inventory[30];
int booksSize; // = sizeof(books);
public:
// default constructor
Bookstore();
// alternate constructor
Bookstore(const std::string&);
// print
void print();
};
#endif //ASSIGNMENT_2_BOOKSTORE_H
-------------------------------------------------------------------------------
//main.cpp
#include <iostream>
#include "Book.h"
using std::cout;
using std::endl;
int main()
{
char isbn1[11] = "1111111111";
char title1[41] = "Learn C++ Now";
char isbn2[11] = "2222222222";
char title2[41] = "Learn Java Later";
int numShipped;
// Test default constructor
Book book1;
// Test alternate constructor
Book book2(isbn1, title1, 39.99, 5);
// Test data validation
Book book3(isbn2, title2, -22.99, -6);
// Test print() method and whether constructors
// properly initialized objects
cout << "Printing book1 ";
book1.print();
cout << endl << endl;
cout << "Printing book2 ";
book2.print();
cout << endl << endl;
cout << "Printing book3 ";
book3.print();
cout << endl << endl;
// Test accessor methods
cout << book2.getISBN() << endl;
cout << book2.getTitle() << endl;
cout << book2.getPrice() << endl;
cout << book2.getQuantity() << endl;
// Test the fulfillOrder() method
numShipped = book2.fulfillOrder(-5);
cout << " Shipped " << numShipped << endl;
cout << "Quantity now " << book2.getQuantity() << endl;
numShipped = book2.fulfillOrder(3);
cout << "Shipped " << numShipped << endl;
cout << "Quantity now " << book2.getQuantity() << endl;
numShipped = book2.fulfillOrder(4);
cout << "Shipped " << numShipped << endl;
cout << "Quantity now " << book2.getQuantity() << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.