Question

In: Computer Science

This is c++. The goal is to test each method function by writing a simple program...

This is c++. The goal is to test each method function by writing a simple program displaying how they work in int main .
#ifndef ARRAY_FUNCTIONS_H
#define ARRAY_FUNCTIONS_H
#include <iostream>
#include <iomanip>
 
using namespace std;
 
template <typename T>
class Array_functions
{
 
public:
    int size;
    int cap;
    int* ptr;
 
    //default ctor
    Array_functions();
 
    // ctor with one arg
    Array_functions(int size);
 
    //allactes a dynamic array
    T* allocate(int n);
 
    //resizes array onto another
   T* resize_arr(T *dest,int old_cap, int new_cap);
 
    //deletes array
    void delete_arr(T& a);
 
    //prints array
    T* copy_arr(T*src, int size);
 
};
 
 
template <typename T>
Array_functions<T>::Array_functions(){
    const bool debug = true;
    size = 0;
    cap = 0;
    ptr = nullptr;
    cout<< "debug Array_functions(): size : "<< size<<" cap : "<< cap;
}
 
template <typename T>
Array_functions<T>::Array_functions(int size){
    size = 50;
    ptr = allocate(cap + 1);
    cout<< "debug Array_functions(): size : "<< size <<" ptr : ";
}
 
template <typename T>
T* Array_functions<T>::allocate(int capacity){
    const bool debug = true;
    if(debug)
        cout<< " debug: allocate(int n) "<<endl;
    return new T[capacity];
}
 
template <typename T>
T* Array_functions<T>::resize_arr(T *dest,int size, int new_cap){
    dest = allocate(new_cap);
    copy_arr(dest, size);
    return *dest;
}
 
template <typename T>
void Array_functions<T>::delete_arr(T& arr){
    delete []arr;
    arr = nullptr;
}
 
template <typename T>
//Print array pointer
T* Array_functions<T>::copy_arr(T* src,
int size){
    T* new_arr;
    for(int i = 0; i < size; i++){
        cout<< "Constents of list :"<<endl;
        cout<<  setw(4)<< *src <<endl;
        new_arr = src;
        new_arr++;
        src++;                       //list array increments
        }
    delete_arr(src);
    new_arr -=size;
    cout<< endl;
    return *new_arr;
}
#endif // ARRAY_FUNCTIONS_H
#include <iostream>
#include "array_functions"
using namespace std;
int main(){


return 0;
}

Solutions

Expert Solution

OUTPUT:

CODE:

#ifndef ARRAY_FUNCTIONS_H
#define ARRAY_FUNCTIONS_H
#include <iostream>
#include <iomanip>


using namespace std;


template <typename T>
class Array_functions
{


public:
int size;
int cap;
int* ptr;


//default ctor
Array_functions();


// ctor with one arg
Array_functions(int size);


//allactes a dynamic array
T* allocate(int n);


//resizes array onto another
T* resize_arr(T* dest, int old_cap, int new_cap);


//deletes array
void delete_arr(T& a);


//prints array
T* copy_arr(T* src, int size);


};


template <typename T>
Array_functions<T>::Array_functions() {
const bool debug = true;
size = 0;
cap = 0;
ptr = nullptr;
cout << "debug Array_functions(): size : " << size << " cap : " << cap;
}


template <typename T>
Array_functions<T>::Array_functions(int size) {
size = 50;
ptr = allocate(cap + 1);
cout << "debug Array_functions(): size : " << size << " ptr : ";
}


template <typename T>
T* Array_functions<T>::allocate(int capacity) {
const bool debug = true;
if (debug)
cout << " debug: allocate(int n) " << endl;
return new T[capacity];
}


template <typename T>
T* Array_functions<T>::resize_arr(T* dest, int size, int new_cap) {
dest = allocate(new_cap);
copy_arr(dest, size);
return dest;
}


template <typename T>
void Array_functions<T>::delete_arr(T& arr) {
delete[]arr;
arr = nullptr;
}


template <typename T>
//Print array pointer
T* Array_functions<T>::copy_arr(T* src,
int size) {
T* new_arr = NULL;
for (int i = 0; i < size; i++) {
cout << "Constents of list :" << endl;
cout << setw(4) << *src << endl;
new_arr = src;
new_arr++;
src++; //list array increments
}
new_arr -= size;
cout << endl;
return new_arr;
}
#endif // ARRAY_FUNCTIONS_H
#include <iostream>
using namespace std;
int main() {

Array_functions<int> arr;
Array_functions<int> arr1(5);

int samplearr[] = { 2,3,4,1,5,6 };
arr.copy_arr(samplearr, 6);
arr1.allocate(10);

arr.resize_arr(samplearr, 6, 10);
  
return 0;
}


Related Solutions

The goal of this project is to practice (Write a C Program) with a function that...
The goal of this project is to practice (Write a C Program) with a function that one of its parameter is a function.The prototype of this function is: void func ( float (*f)(float*, int), float* a, int length); This means the function: func has three parameters: float (*f)(float*, int): This parameter itself is a function: f that has two parameters and returns a floating-point number. In the body of the function: func we call the function: f with its arguments...
You will be writing a C program to test the data sharing ability of a thread...
You will be writing a C program to test the data sharing ability of a thread and process. Your C program will do the following: 1. Your parent program will have three variables: int x,y,z; which to be initialized as 10, 20, and 0, respectively. 2. parent creating child: parent will create a child by fork() and the child will perform z = x+y (i.e., add x and y and store the results in z). parent will wait for child...
Write a C program A simple method for encrypting data is that of ROT 13. The...
Write a C program A simple method for encrypting data is that of ROT 13. The method takes each latin letter of plain text and moves it to the next letter 13 times in latin alphabet (wrapping around if necessary). For those of you who are unaware the latin alphabet is the following a b c d e f g h i j k l m n o p q r s t u v w x y z This...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
You have been instructed to use C++ to develop, test, document, and submit a simple program...
You have been instructed to use C++ to develop, test, document, and submit a simple program with the following specifications. The program maintains a short data base of DVDs for rent with their name, daily rental charge, genre, and a a short description . The program will welcomes the user, user will enter the name of the movie from a displayed list of all movies that are available, the user will enter the number next to the movie's name, number...
write a report about travel agency in c++ not a program writing just report writing
write a report about travel agency in c++ not a program writing just report writing
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT PLEASE. Problem: Driving Function driving(): Write a function driving(), that updates the odometer and fuel gauge of a car. The function will take in a reference to the variables storing the odometer and fuel gauge readings, along with a double representing the miles per gallon (mpg) the car gets and the number of miles the driver intends to go. The function should update the...
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The program will now be broken up into methods and store all the inventory in an ArrayList object. The program will be able to run a report for all inventory details as well as a report for items that are low in...
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The Food and Book items should inherit all the properties of the Product item in the previous assignment. Foods cannot be added to the inventory without an expiration date. Implement a toString method for Product, Food, and Book. Grading details: Correct usage...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT