Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program for 12.9 does not work in my cpp this is what i have . this program

ID: 3831765 • Letter: T

Question

The program for 12.9 does not work in my cpp this is what i have . this program is in c++

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <math.h>
#include<string>
#include<cctype>
#include <stdexcept>
#include "Stack.h"
//#include "ImprovedStack.h"
using namespace std;


template <typename T>
T Max(T arr[], int size){
T max = arr[0]; // initializing with first element
for(int i=1; i<size; i++){
if(arr[i] > max)
max = arr[i];
}

return max;
}

void Prog12_1()
{
   int arr[] = {43,12,56,78,12,45};

cout << "Max int array: " << Max(arr, 6) << endl;

string arr1[] = {"sandwhiches", "phone", "numbers", "computer","mark"};

cout << "Max string array: " << Max(arr1, 5) << endl;

double arr2[] = {45.34, 12.34, 34.43, 56.6, 10.6, 78};

cout << "Max double array: " << Max(arr2, 6) << endl;

}


template <typename T>
int binarySearch( T list[], const T key, int listSize)
{
int low = 0;
int high = listSize - 1;

while(high >= low)
{
int mid = (low + high) / 2;
if(key < list[mid])
high = mid - 1;
else if(key == list[mid])
return mid;
else
low = mid + 1;
}
return low - 1;
}
void Prog12_3()
{
   int list[] = {2, 4, 7, 10, 11, 45};

   cout << " the values are " << binarySearch(list,45,6) << endl;

   string list1[] = {" mark ", " apples", "phones", " Physics", "books", "calculus"};
   cout << " The string is " << binarySearch(list1,string("phones"),5) << endl;

   double list2[] = {2.0,5.0,8.5,9.1,3.0,4.7};
   cout << " The double numbers are " << binarySearch(list2,8.5,6) << endl;
}

template <typename T>
//The function swapargs() accepts ‘&a’ and ‘&b’ as input parameters and performs the swap
//operation.
// Definition of the generic function
void swapargs(T &a, T &b)
{
// Declare the generic variable
T temp;
// Perform the swap operation
temp = a;
a = b;
b = temp;
}

void Prog12_5()
{
   //Declare and assign the input elements for int, double and string.
// Declare and initialize the integer variables
int i1=10, i2=20;
// Declare and initialize the double variables
double d1=10.1, d2=23.3;
// Declare and initialize the string variables
string s1="Alpha", s2="Beta";
//Print the values before swapping.
cout<< "Before swapping"<<endl;
// Print the original values
cout << "Original integer values i1, i2: " << i1 << ' ' << i2 << endl;
cout << "Original double values d1, d2: " << d1 << ' ' << d2 << endl;
cout << "Original string values s1, s2: " << s1 << ' ' << s2 << endl;
cout<<endl;
//Call the function swap() to perform the swapping of two values.
// Function call to swap integer values
swapargs(i1, i2);
// Function call to swap double values
swapargs(d1, d2);
// Function call to swap string values
swapargs(s1,s2);
//Print the values after swapping.
cout<< "After swapping"<<endl;
// Print the result
cout << "Swapped integer values i1, i2: " << i1 << ' ' << i2 << endl;
cout << "Swapped double values d1, d2: " << d1 << ' ' << d2 << endl;
cout << "Swapped string values s1, s2: " << s1 << ' ' << s2 << endl;

}

void Prog12_7()
{

   Stack<int> s;

s.push(1);
s.push(2);

if(s.contains(100))
{
cout << " Found in Stack.. ";
}
else
{
cout << " Not Found in Stack.. ";
}

cout << "Done" << endl;

cout << " ";

}


template <class T>
class Stack
{
private:
vector<T> elements;

public:
void push(T const &);
void pop();
T peek();
bool empty();
int getSize();
};

template <class T>
void Stack<T>::push(T const &elem) {
elements.push_back(elem);
}

template <class T>
void Stack<T>::pop() {
if (elements.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
} else {
elements.pop_back();
}
}

template <class T>
T Stack<T>::peek() {
if (empty()) {
throw out_of_range("Stack<>::top(): empty stack");
}
return elements.back();
}

template <class T>
bool Stack<T>::empty() {
return elements.empty();
}

template <class T>
int Stack<T>::getSize() {
return elements.size();
}


void Prog12_9()
{
   try{
   Stack<int> intStack; // stack of ints
Stack<string> stringStack; // stack of strings

//manipulate integer stack
intStack.push(7);
cout << intStack.peek() << endl;

// manipulate string stack
stringStack.push("hello");
stringStack.push("Pravesh");
stringStack.push("Kuamar");
cout << stringStack.peek() << std::endl;
stringStack.pop();
stringStack.pop();
}
catch (exception const &ex) {
cerr << "Exception: " << ex.what() << endl;
}
}


/*void Prog12_13()
{
   string expression;
cout << "Enter an expression: ";
getline(cin, expression);

cout << expression << " = "
<< evaluateExpression(expression) << endl;

  
} */

double deltaA(vector<vector<double> > a)
{
double result = a[0][0] * a[1][1] * a[2][2] +
a[2][0] * a[0][1] * a[1][2] + a[0][2] * a[1][0] * a[2][1] -
a[0][2] * a[1][1] * a[2][0] - a[0][0] * a[1][2] * a[2][1] -
a[2][2] * a[1][0] * a[0][1];
return result;
}

void Prog12_24()
{

   vector<vector<double> > a(3);
a[0] = vector<double>(3);
a[1] = vector<double>(3);
a[2] = vector<double>(3);

cout << "Enter a11, a12, a13, a21, a22, a23, a31, a32, a33: ";
cin >> a[0][0] >> a[0][1] >> a[0][2] >>
a[1][0] >> a[1][1] >> a[1][2] >>
a[2][0] >> a[2][1] >> a[2][2];

cout << "Enter b1, b2, b3: ";
double b1, b2, b3;
cin >> b1 >> b2 >> b3;

double delta = deltaA(a);
if (delta == 0)
cout << "No solution" << endl;
else
{
double x = ((a[1][1] * a[2][2] - a[1][2] * a[2][1]) * b1 +
(a[0][2] * a[2][1] - a[0][1] * a[2][2]) * b2 +
(a[0][1] * a[1][2] - a[0][2] * a[1][1]) * b3) / delta;
double y = ((a[1][2] * a[2][0] - a[1][0] * a[2][2]) * b1 +
(a[0][0] * a[2][2] - a[0][2] * a[2][0]) * b2 +
(a[0][2] * a[1][0] - a[0][0] * a[1][2]) * b3) / delta;
double z = ((a[1][0] * a[2][1] - a[1][1] * a[2][0]) * b1 +
(a[0][1] * a[2][0] - a[0][0] * a[2][1]) * b2 +
(a[0][0] * a[1][1] - a[0][1] * a[1][0]) * b3) / delta;

cout << "The solution is " << x << " " << y << " " << z << endl;
}

}

const int N = 4;

int sumRow(vector<int> row);
int sumColumn(vector< vector<int> > matrix, int column);

int sumRow(vector<int> row)
{
int sum = 0;
for (unsigned i = 0; i < row.size(); i++)
sum += row[i];
return sum;
}

int sumColumn(vector< vector<int> > matrix, int column)
{
int sum = 0;
for (unsigned i = 0; i < matrix.size(); i++)
sum += matrix[i][column];
return sum;
}


void Prog12_28()
{

   vector< vector<int> > matrix;

for (int i = 0; i < N; i++)
{
matrix.push_back(vector<int>());
for (int j = 0; j < N; j++)
{
   matrix[i].push_back(rand() % 2);
cout << matrix[i][j] << " ";
}

cout << endl;
}

// Check rows
int rowSum = sumRow(matrix[0]);
vector<int> rowList(1);
for (unsigned i = 1; i < N; i++)
{
   if (rowSum < sumRow(matrix[i]))
   {
rowSum = sumRow(matrix[i]);
rowList.clear();
   rowList.push_back(i);
   }
   else if (rowSum == sumRow(matrix[i]))
rowList.push_back(i);
}

cout << "The largest row index: ";
for (unsigned i = 0; i < rowList.size() - 1; i++)
cout << rowList[i] << ", ";
cout << rowList[rowList.size() - 1] << endl;

// Check columns
int columnSum = sumColumn(matrix, 0);
vector<int> columnList(1);
for (unsigned i = 1; i < N; i++)
{
   if (rowSum < sumColumn(matrix, i))
   {
columnSum = sumColumn(matrix, i);
columnList.clear();
   columnList.push_back(i);
   }
   else if (columnSum == sumColumn(matrix, i))
columnList.push_back(i);
}

cout << "The largest column index: ";
for (unsigned i = 0; i < columnList.size() - 1; i++)
cout << columnList[i] << ", ";
cout << columnList[columnList.size() - 1] << endl;


}


enum {x, y};
typedef struct triangle {
double v1[2];
double v2[2];
double v3[2];
} triangle;
double area(triangle a);

double side(double *p1, double *p2);
/* calculate triangle area with Heron's formula */
double area(triangle a)
{
double s1, s2, s3, S, area;

s1 = side(a.v1, a.v2);
s2 = side(a.v2, a.v3);
s3 = side(a.v3, a.v1);
S = (s1 + s2 + s3) / 2;
area = sqrt(S*(S - s1)*(S - s2)*(S - s3));

return area;
}


/* calculate length of side */
double side(double *p1, double *p2)
{
double s1, s2, s3;

s1 = (p1[x] - p2[x]);
s2 = (p1[y] - p2[y]);
s3 = (s1 * s1) + (s2 * s2);

return sqrt(s3);
}

void Prog12_33()
{
   const int MAX_VERT =50;
   int n, idx;
int triangles;
int index;
int xycount;
double xy;
double triangle_area;
double polygon_area;
double perim;
double polygon_vertices[MAX_VERT] = {0.0};
triangle a;
  

  
polygon_area = 0;
  
cout<<"Enter the number of the points: "<<endl;
cin>>xycount;
cout<<"Enter the coordinates of the points: "<<endl;
for(int i=0; i<2*xycount; i++)
{
cin>>polygon_vertices[i];
}

/* Read x-y coordinates of the vertices
of the polygon from a file. */

idx = 0;
/* triangles in polygon = vertices - 2 */
triangles = xycount - 2;
//putchar('');

for(index = 2, idx = 0; idx < triangles; index += 2, ++idx)
{
/* Load vertices of a triangle into struct.
1st vertex of the polygon will be the 1st
vertex of each triangle. index holds the
starting index of each consecutive set of
triangle vertices after the 1st. */
a.v1[x] = polygon_vertices[0];
a.v1[y] = polygon_vertices[1];
a.v2[x] = polygon_vertices[index+0];
a.v2[y] = polygon_vertices[index+1];
a.v3[x] = polygon_vertices[index+2];
a.v3[y] = polygon_vertices[index+3];

/* calculate the area of the triangle */
triangle_area = area(a);

/* add triangle area to polygon area */
polygon_area += triangle_area;
}
cout<<"area of polygon"<<" "<<polygon_area;

/* calculate the perimeter of the polygon */
  

}

bool contains(vector<int> v, int answer)
{
for (unsigned i = 0; i < v.size(); i++)
if (v[i] == answer)
return true;

return false;
}


void Prog12_34()
{

   // 1. Generate two random single-digit integers
srand(time(0));
int number1 = rand() % 10;
int number2 = rand() % 10;

// 2. If number1 < number2, swap number1 with number2
if (number1 < number2)
{
int temp = number1;
number1 = number2;
number2 = temp;
}

// 3. Prompt the student to answer "What is number1 - number2"
cout << "What is " << number1 << " - " << number2 << "? ";
int answer;
cin >> answer;

vector<int> v;
v.push_back(answer);

// 4. Repeatedly ask the user the question until it is correct
while (number1 - number2 != answer)
   {
cout << "Wrong answer. Try again. What is "
<< number1 << " - " << number2 << "? ";
cin >> answer;

if (contains(v, answer))
cout << "You already entered " << answer << endl;
else
v.push_back(answer);
}

cout << "You got it!" << endl;

}

int main()

{

while (true)

{

system("cls");

cout << " Main Menu - Chapter 12" << endl;

cout << "==============================" << endl;

cout << " 1: Programming Exercise 12.1" << endl;

cout << " 3: Programming Exercise 12.3" << endl;

cout << " 5: Programming Exercise 12.5" << endl;

cout << " 7: Programming Exercise 12.7" << endl;

cout << " 9: Programming Exercise 12.9" << endl;

cout << " 13: Programming Exercise 12.13" << endl;

cout << " 24: Programming Exercise 12.24" << endl;

cout << " 28: Programming Exercise 12.28" << endl;

cout << " 33: Programming Exercise 12.33" << endl;

cout << " 34: Programming Exercise 12.34" << endl;

cout << "other: Exit" << endl;

cout << "==============================" << endl;

cout << "Enter an exercise: ";

char exercise[2];

cin >> exercise;

cout << endl;

switch (atoi(exercise))

{

case 1: Prog12_1(); break;

case 3: Prog12_3(); break;

case 5: Prog12_5(); break;

case 7: Prog12_7(); break;

case 9: Prog12_9(); break;

//case 13: Prog12_13(); break;

case 24: Prog12_24(); break;

case 28: Prog12_28(); break;

case 33: Prog12_33(); break;

case 34: Prog12_34(); break;

default: exit(0);

}

cout << endl;

system("pause");

cin.clear();

}

return 0;

}

In my main file i have this

#ifndef STACK_H
#define STACK_H

template<typename T>
class Stack
{
public:
Stack();
bool empty();
T peek();
T push(T value);
T pop();
bool const contains(T element);
int getSize();
  

private:
T elements[100];
int size;
  
};

template <typename T>
Stack<T>::Stack()
{
size = 0;
}

template <typename T>
bool Stack<T>::empty()
{
return (size == 0);
}

template <typename T>
T Stack<T>::peek()
{
return elements[size-1];
}

template <typename T>
T Stack<T>::push(T value)
{
return elements[size++] = value;
}

template <typename T>
T Stack<T>::pop()
{
return elements[--size];
}

template <typename T>
int Stack<T>::getSize()
{
return size;
}

template <typename T>
bool const Stack<T>::contains(T element)
{
for(int i=0; i<size; i++)
if(element == elements[i])
return true;

return false;
}

#endif

2.9 (Implement a stack class using a vector) In Listing 12.4, GenericStack is implemented using arrays. Implement it using a vector.

Explanation / Answer

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <math.h>
#include<string>
#include<cctype>
#include <stdexcept>
#include "Stack.h"
//#include "ImprovedStack.h"
using namespace std;


template <typename T>
T Max(T arr[], int size){
T max = arr[0]; // initializing with first element
for(int i=1; i<size; i++){
if(arr[i] > max)
max = arr[i];
}

return max;
}

void Prog12_1()
{
   int arr[] = {43,12,56,78,12,45};

cout << "Max int array: " << Max(arr, 6) << endl;

string arr1[] = {"sandwhiches", "phone", "numbers", "computer","mark"};

cout << "Max string array: " << Max(arr1, 5) << endl;

double arr2[] = {45.34, 12.34, 34.43, 56.6, 10.6, 78};

cout << "Max double array: " << Max(arr2, 6) << endl;

}


template <typename T>
int binarySearch( T list[], const T key, int listSize)
{
int low = 0;
int high = listSize - 1;

while(high >= low)
{
int mid = (low + high) / 2;
if(key < list[mid])
high = mid - 1;
else if(key == list[mid])
return mid;
else
low = mid + 1;
}
return low - 1;
}
void Prog12_3()
{
   int list[] = {2, 4, 7, 10, 11, 45};

   cout << " the values are " << binarySearch(list,45,6) << endl;

   string list1[] = {" mark ", " apples", "phones", " Physics", "books", "calculus"};
   cout << " The string is " << binarySearch(list1,string("phones"),5) << endl;

   double list2[] = {2.0,5.0,8.5,9.1,3.0,4.7};
   cout << " The double numbers are " << binarySearch(list2,8.5,6) << endl;
}

template <typename T>
//The function swapargs() accepts ‘&a’ and ‘&b’ as input parameters and performs the swap
//operation.
// Definition of the generic function
void swapargs(T &a, T &b)
{
// Declare the generic variable
T temp;
// Perform the swap operation
temp = a;
a = b;
b = temp;
}

void Prog12_5()
{
   //Declare and assign the input elements for int, double and string.
// Declare and initialize the integer variables
int i1=10, i2=20;
// Declare and initialize the double variables
double d1=10.1, d2=23.3;
// Declare and initialize the string variables
string s1="Alpha", s2="Beta";
//Print the values before swapping.
cout<< "Before swapping"<<endl;
// Print the original values
cout << "Original integer values i1, i2: " << i1 << ' ' << i2 << endl;
cout << "Original double values d1, d2: " << d1 << ' ' << d2 << endl;
cout << "Original string values s1, s2: " << s1 << ' ' << s2 << endl;
cout<<endl;
//Call the function swap() to perform the swapping of two values.
// Function call to swap integer values
swapargs(i1, i2);
// Function call to swap double values
swapargs(d1, d2);
// Function call to swap string values
swapargs(s1,s2);
//Print the values after swapping.
cout<< "After swapping"<<endl;
// Print the result
cout << "Swapped integer values i1, i2: " << i1 << ' ' << i2 << endl;
cout << "Swapped double values d1, d2: " << d1 << ' ' << d2 << endl;
cout << "Swapped string values s1, s2: " << s1 << ' ' << s2 << endl;

}

void Prog12_7()
{

   Stack<int> s;

s.push(1);
s.push(2);

if(s.contains(100))
{
cout << " Found in Stack.. ";
}
else
{
cout << " Not Found in Stack.. ";
}

cout << "Done" << endl;

cout << " ";

}


template <class T>
class Stack
{
private:
vector<T> elements;

public:
void push(T const &);
void pop();
T peek();
bool empty();
int getSize();
};

template <class T>
void Stack<T>::push(T const &elem) {
elements.push_back(elem);
}

template <class T>
void Stack<T>::pop() {
if (elements.empty()) {
throw out_of_range("Stack<>::pop(): empty stack");
} else {
elements.pop_back();
}
}

template <class T>
T Stack<T>::peek() {
if (empty()) {
throw out_of_range("Stack<>::top(): empty stack");
}
return elements.back();
}

template <class T>
bool Stack<T>::empty() {
return elements.empty();
}

template <class T>
int Stack<T>::getSize() {
return elements.size();
}


void Prog12_9()
{
   try{
   Stack<int> intStack; // stack of ints
Stack<string> stringStack; // stack of strings

//manipulate integer stack
intStack.push(7);
cout << intStack.peek() << endl;

// manipulate string stack
stringStack.push("hello");
stringStack.push("Pravesh");
stringStack.push("Kuamar");
cout << stringStack.peek() << std::endl;
stringStack.pop();
stringStack.pop();
}
catch (exception const &ex) {
cerr << "Exception: " << ex.what() << endl;
}
}


/*void Prog12_13()
{
   string expression;
cout << "Enter an expression: ";
getline(cin, expression);

cout << expression << " = "
<< evaluateExpression(expression) << endl;

  
} */

double deltaA(vector<vector<double> > a)
{
double result = a[0][0] * a[1][1] * a[2][2] +
a[2][0] * a[0][1] * a[1][2] + a[0][2] * a[1][0] * a[2][1] -
a[0][2] * a[1][1] * a[2][0] - a[0][0] * a[1][2] * a[2][1] -
a[2][2] * a[1][0] * a[0][1];
return result;
}

void Prog12_24()
{

   vector<vector<double> > a(3);
a[0] = vector<double>(3);
a[1] = vector<double>(3);
a[2] = vector<double>(3);

cout << "Enter a11, a12, a13, a21, a22, a23, a31, a32, a33: ";
cin >> a[0][0] >> a[0][1] >> a[0][2] >>
a[1][0] >> a[1][1] >> a[1][2] >>
a[2][0] >> a[2][1] >> a[2][2];

cout << "Enter b1, b2, b3: ";
double b1, b2, b3;
cin >> b1 >> b2 >> b3;

double delta = deltaA(a);
if (delta == 0)
cout << "No solution" << endl;
else
{
double x = ((a[1][1] * a[2][2] - a[1][2] * a[2][1]) * b1 +
(a[0][2] * a[2][1] - a[0][1] * a[2][2]) * b2 +
(a[0][1] * a[1][2] - a[0][2] * a[1][1]) * b3) / delta;
double y = ((a[1][2] * a[2][0] - a[1][0] * a[2][2]) * b1 +
(a[0][0] * a[2][2] - a[0][2] * a[2][0]) * b2 +
(a[0][2] * a[1][0] - a[0][0] * a[1][2]) * b3) / delta;
double z = ((a[1][0] * a[2][1] - a[1][1] * a[2][0]) * b1 +
(a[0][1] * a[2][0] - a[0][0] * a[2][1]) * b2 +
(a[0][0] * a[1][1] - a[0][1] * a[1][0]) * b3) / delta;

cout << "The solution is " << x << " " << y << " " << z << endl;
}

}

const int N = 4;

int sumRow(vector<int> row);
int sumColumn(vector< vector<int> > matrix, int column);

int sumRow(vector<int> row)
{
int sum = 0;
for (unsigned i = 0; i < row.size(); i++)
sum += row[i];
return sum;
}

int sumColumn(vector< vector<int> > matrix, int column)
{
int sum = 0;
for (unsigned i = 0; i < matrix.size(); i++)
sum += matrix[i][column];
return sum;
}


void Prog12_28()
{

   vector< vector<int> > matrix;

for (int i = 0; i < N; i++)
{
matrix.push_back(vector<int>());
for (int j = 0; j < N; j++)
{
   matrix[i].push_back(rand() % 2);
cout << matrix[i][j] << " ";
}

cout << endl;
}

// Check rows
int rowSum = sumRow(matrix[0]);
vector<int> rowList(1);
for (unsigned i = 1; i < N; i++)
{
   if (rowSum < sumRow(matrix[i]))
   {
rowSum = sumRow(matrix[i]);
rowList.clear();
   rowList.push_back(i);
   }
   else if (rowSum == sumRow(matrix[i]))
rowList.push_back(i);
}

cout << "The largest row index: ";
for (unsigned i = 0; i < rowList.size() - 1; i++)
cout << rowList[i] << ", ";
cout << rowList[rowList.size() - 1] << endl;

// Check columns
int columnSum = sumColumn(matrix, 0);
vector<int> columnList(1);
for (unsigned i = 1; i < N; i++)
{
   if (rowSum < sumColumn(matrix, i))
   {
columnSum = sumColumn(matrix, i);
columnList.clear();
   columnList.push_back(i);
   }
   else if (columnSum == sumColumn(matrix, i))
columnList.push_back(i);
}

cout << "The largest column index: ";
for (unsigned i = 0; i < columnList.size() - 1; i++)
cout << columnList[i] << ", ";
cout << columnList[columnList.size() - 1] << endl;


}