help with C++ code
The aim of this is to practice writing template classes. You must use SafeArray template class and implement a SafeMatrix template class that will allow you to work with two dimensional arrays of any type. The boundaries are checked. You must be able to create instances of SafeMatrix template class like,
SafeMatrix<int> m(2,3);
The above statement will create a 2 by 3 dimensional array of integers. You must be able to access and set values using bracket operators. For example m[0][1] = 5; will set element at row 0 column 1 to an integer value 5. You must be able to return dimensions of the array as follows: m.length() will return the number of rows in your 2D array. m[0].length() will return number of columns in row number 0. Provide all necessary methods to your template class. Write a Driver program that tests your template class with at least two different types.
CODE TO COPY:
#include<iostream>
#define SIZE 500
using namespace std;
//safearray class
template<typename t>
class SafeArray
{
private:
t array[SIZE];
int cols;
public:
SafeArray(int cols)
{//parameterized constructors
this->cols=cols;
//if size is smaller, throw an error
if(SIZE<cols)
{
cout<<"Array Size Overfow!!"<<endl;
return;
}
//initialize the values to 0
for(int i=0; i<cols; i++)
array[i]=0;
}
//operator overload []
t& operator[](int y)
{
return array[y];
}
//return cols in the safearray
int length()
{
return cols;
}
//stream operator overload
friend ostream& operator<<(ostream &out, t& valRef)
{
t val=valRef;
out<<val;
return out;
}
};
//safematrix class
template <typename t>
class SafeMatrix{
private:
SafeArray<t> *matrix[SIZE];
int rows;
int cols;
public:
//parameterized constructor
SafeMatrix(int rows, int cols)
{
this->rows=rows;//set rows
this->cols=cols; //set columns
if(rows>SIZE)//if size is smaller than rows
{
cout<<"Array Index Overflow!!"<<endl;//throw an error
return;//return
}
//initialize the safearrays
for(int i=0; i<rows; i++)
matrix[i]=new SafeArray<t>(cols);
}
//return length of the matrix
int length()
{
return rows;
}
//operator overload[]
SafeArray<t>& operator[](int x)
{
return *matrix[x];//return pointer to SafeArray
}
};
int main()
{
SafeMatrix<int> sm(3,3);//create a safe matrix of size 3 by 3
sm[0][1]=5;//set vale at index 0,1 as 5
cout<<"sm.length() -> "<<sm.length()<<endl;//output length of matrix
cout<<"sm[0].length() --> "<<sm[0].length()<<endl; //output length of first row of matrix
cout<<"Value of sm[0][1] -> "<<sm[0][1]<<endl;//output value at 0,1
cin.get();//hold the colsole screen
return 0;
}
PROGRAM SCREENSHOTS:
OUTPUT:
In case of any doubt, please let me know in the comments section. I'll get it resolved :) :) Don't forget to give a THUMBS UP!! :)
help with C++ code The aim of this is to practice writing template classes. You must...
Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...
//please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...
Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...
Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...
NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a template class to represent a matrix. Because it’s a template, your matrix class will be able to work with different types of underlying data. Start by creating a new file matrix.hpp. Inside this file, start to write your matrix template class. Here’s a start for the class definition: template <class T> class Matrix { private: int _rows; int _cols; T** data; public: Matrix(int rows,...
C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...
You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account in a bank. The class must have the following instance variables: a String called accountID a String called accountName a two-dimensional integer array called deposits (each row represents deposits made in a week). At this point it should not be given any initial value. Each...
COMP1410 - Lab Exercises #3 (Due at the end of the lab period or beginning of the next) Start Date: Oct. 1st, 2019 Objectives: Practice dealing with 2D arrays. Create a two dimensional array (e.g. int A2D[M] [N] ;) of size MXN to store integer values. Use #define M 4 and N 3 to start. (Using symbolic constants instead of hard coding the array sizes improves scalability). Create an interactive menu within main() for this program (call it Lab3.c) with...
Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...
Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...