Question

Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

Who could write the array.cpp file ?  

//main.cpp

#include "array.hpp"
#include <iostream>

int main() {
int n;

std::cin >> n;

array a(n);

for (int i = 0; i < n; i++) {
std::cin >> a.data()[i];
}

std::cout << "array size:" << a.max_size() << std::endl;
std::cout << "array front:" << a.front() << std::endl;
std::cout << "array back:" << a.back() << std::endl;

int* data = a.data();
std::cout << "array elements using data:" << std::endl;
for (int i = 0; i < n; i++) {
std::cout << data[i] << " ";
}
std::cout << std::endl;

std::cout << "array elements using at:" << std::endl;
for (int i = 0; i < n; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;

std::cout << "array fill:" << std::endl;
a.fill(9);
for (int i = 0; i < n; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;

for (int i = 0; i < n; i++) {
std::cin >> a.at(i);
}

std::cout << "array assign:" << std::endl;
for (int i = 0; i < n; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;

std::cout << "sort the array:" << std::endl;
a.sort(0, a.max_size());
for (int i = 0; i < n; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;

int m;
std::cin >> m;

a.resize(n + m);

for (int i = n; i < n + m; i++) {
std::cin >> a.at(i);
}

std::cout << "resize and then input:(max size:" << a.max_size() << ")"
<< std::endl;
for (int i = 0; i < n + m; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;

std::cout << "resize and then sort:" << std::endl;

a.sort(n + m / 2, m + n);

for (int i = n + m / 2; i < n + m; i++) {
std::cout << a.at(i) << " ";
}
std::cout << std::endl;

return 0;
}

//array.hpp

#ifndef ARRAY_H_
#define ARRAY_H_

class array {
typedef int* pointer;
typedef unsigned int size_t;
typedef int data_type;

pointer _data;
size_t _size;

public:
// constructor
array(size_t size);
// destructor
~array();
// Capacity
// Return maximum size
size_t max_size(void);

// Element access
// Access element
// int& operator[](const int &i) {return data[i];}
// Access element
data_type& at(const data_type& i);
// Access first element
data_type& front();
// Access last element
data_type& back();
// Get pointer to data
pointer data();

// Modifiers
// Fill array with the same value
void fill(const data_type& value);
// Resize the array (newSize > oldSize)
void resize(int newSize);
// Sort the array in the section [from, to)
void sort(int from, int to);
};

#endif // ARRAY_H_

//array.cpp  

?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp

#include "array.hpp"
#include <iostream>

int main() {
int n;

std::cin >> n;

array a(n);

for (int i = 0; i < n; i++) {
    std::cin >> a.data()[i];
}

std::cout << "array size:" << a.max_size() << std::endl;
std::cout << "array front:" << a.front() << std::endl;
std::cout << "array back:" << a.back() << std::endl;

int* data = a.data();
std::cout << "array elements using data:" << std::endl;
for (int i = 0; i < n; i++) {
    std::cout << data[i] << " ";
}
std::cout << std::endl;

std::cout << "array elements using at:" << std::endl;
for (int i = 0; i < n; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

std::cout << "array fill:" << std::endl;
a.fill(9);
for (int i = 0; i < n; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

for (int i = 0; i < n; i++) {
    std::cin >> a.at(i);
}

std::cout << "array assign:" << std::endl;
for (int i = 0; i < n; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

std::cout << "sort the array:" << std::endl;
a.sort(0, a.max_size());
for (int i = 0; i < n; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

int m;
std::cin >> m;

a.resize(n + m);

for (int i = n; i < n + m; i++) {
    std::cin >> a.at(i);
}

std::cout << "resize and then input:(max size:" << a.max_size() << ")"
            << std::endl;
for (int i = 0; i < n + m; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

std::cout << "resize and then sort:" << std::endl;

a.sort(n + m / 2, m + n);

for (int i = n + m / 2; i < n + m; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

return 0;
}


array.cpp

#include "array.hpp"
#include <iostream>

int main() {
int n;

std::cin >> n;

array a(n);

for (int i = 0; i < n; i++) {
    std::cin >> a.data()[i];
}

std::cout << "array size:" << a.max_size() << std::endl;
std::cout << "array front:" << a.front() << std::endl;
std::cout << "array back:" << a.back() << std::endl;

int* data = a.data();
std::cout << "array elements using data:" << std::endl;
for (int i = 0; i < n; i++) {
    std::cout << data[i] << " ";
}
std::cout << std::endl;

std::cout << "array elements using at:" << std::endl;
for (int i = 0; i < n; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

std::cout << "array fill:" << std::endl;
a.fill(9);
for (int i = 0; i < n; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

for (int i = 0; i < n; i++) {
    std::cin >> a.at(i);
}

std::cout << "array assign:" << std::endl;
for (int i = 0; i < n; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

std::cout << "sort the array:" << std::endl;
a.sort(0, a.max_size());
for (int i = 0; i < n; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

int m;
std::cin >> m;

a.resize(n + m);

for (int i = n; i < n + m; i++) {
    std::cin >> a.at(i);
}

std::cout << "resize and then input:(max size:" << a.max_size() << ")"
            << std::endl;
for (int i = 0; i < n + m; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

std::cout << "resize and then sort:" << std::endl;

a.sort(n + m / 2, m + n);

for (int i = n + m / 2; i < n + m; i++) {
    std::cout << a.at(i) << " ";
}
std::cout << std::endl;

return 0;
}


array.hpp

#ifndef ARRAY_H_
#define ARRAY_H_

class array {
typedef int* pointer;
typedef unsigned int size_t;
typedef int data_type;

pointer _data;
size_t _size;

public:
// constructor
array(size_t size);
// destructor
~array();
// Capacity
// Return maximum size
size_t max_size(void);

// Element access
// Access element
// int& operator[](const int &i) {return data[i];}
// Access element
data_type& at(const data_type& i);
// Access first element
data_type& front();
// Access last element
data_type& back();
// Get pointer to data
pointer data();

// Modifiers
// Fill array with the same value
void fill(const data_type& value);
// Resize the array (newSize > oldSize)
void resize(int newSize);
// Sort the array in the section [from, to)
void sort(int from, int to);
};

#endif // ARRAY_H_

Add a comment
Know the answer?
Add Answer to:
Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void...

    #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){    int number;    int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl;    displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl;    return 0;       } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT