+1 (315) 557-6473 

Program To Solve Array Problems in C++ Language Assignment Solution.


Instructions

Objective
Write a C++ assignment program to solve array problems in C++ language. This assignment will challenge you to create a program that effectively handles arrays and their associated operations. By working on this assignment, you will have the opportunity to practice your C++ coding skills while tackling various array-related problems. Remember to apply the concepts you've learned in class, such as array indexing, loops, and conditional statements, to successfully complete the assignment and gain a deeper understanding of array manipulation in C++.

Requirements and Specifications

Program to solve array problems in C++
Program to solve array problems in C++ 1
Program to solve array problems in C++ 2
Program to solve array problems in C++ 3
Program to solve array problems in C++ 4

Source Code

MAIN

#include

#include "myArray.h"

using namespace std;

int main()

{

 // Creates list1 and list2

 myArray list1(5);

 myArray list2(5);

 // Zeroing (initializing) first array

 int i;

 cout << "list1 : ";

 for (i = 0; i < 5; i++)

  cout << list1[i] << " ";

  cout << endl;

 // Prompt to enter five numbers into list1

 cout << "Enter 5 integers: ";

 for (i = 0; i < 5; i++)

  cin >> list1[i];

  cout << endl;

 // show contents list1

 cout << "After filling list1: ";

 for (i = 0; i < 5; i++)

  cout << list1[i] << " ";

  cout << endl;

 // Transfer five elements from list1 to list2, Print list2

 list2 = list1;

 cout << "list2 : ";

 for (i = 0; i < 5; i++)

  cout << list2[i] << " ";

  cout << endl;

 // Write three elements replacing first 3 of list1

 cout << "Enter 3 elements: ";

 for (i = 0; i < 3; i++)

  cin >> list1[i];

  cout << endl;

 // Prints three elements of list1 just entered

 cout << "First three elements of list1: " << endl;

 for (i = 0; i < 3; i++)

  cout << list1[i] << " ";

  cout << endl;

 // Create list3 for first time

 // Induced Chaos by

 // cramming -2 to 6 into array list3

 myArray list3(-2, 6);

 // Print 8 array location numbers from -2 through 5

 // Should print zero eight times if memory is initialized

 cout << "list3: ";

 for (i = -2; i < 6; i++) cout << list3[i] << " ";

  cout << endl;

  // Assign some numbers to out-of-bounds locations

  list3[-2] = 7;

  //list3[-1] = 0; implied

  list3[0] = 54;

  //list3[1] = 0; implied

  list3[2] = list3[4] + list3[-2]; // 8 + 7 = 15

  //list3[3] = 0; implied

  list3[4] = 8;

  //list3[5] = 0; implied

 // Print results to screen

 cout << "list3: ";

 for (i = -2; i < 6; i++)

  cout << list3[i] << " ";

 cout << endl;

 // system("pause");

 return 0;

}

ARRAY

#include

class myArray

{

 private:

  int* values;

  int startIndex;

  int size;

  void reset() {

   for(int i = 0; i

    values[i] = 0;

   }

  }

 public:

  myArray() : startIndex(0), size(5) {

   values = new int[size];

   reset();

  }

  myArray(int _size) : startIndex(0), size(_size) {

   values = new int[size];

   reset();

  }

  myArray(int _first, int _last) : startIndex(_first), size(_last-_first) {

   values = new int[size];

   reset();

  }

  myArray(const myArray& other) {

   startIndex = other.startIndex;

   size = other.size;

   values = new int[size];

   for(int i = 0; i

    values[i] = other.values[i];

   }

  }

  ~myArray() {

   delete[] values;

  }

  int operator[](int i) const {

   if(i < startIndex || i >= startIndex + size) {

    std::cout << "Index " << i << " is out of bounds" << std::endl;

    exit(0);

   }

   return values[i - startIndex];

  }

  int& operator[](int i) {

   if(i < startIndex || i >= startIndex + size) {

    std::cout << "Index " << i << " is out of bounds" << std::endl;

    exit(0);

   }

   return values[i - startIndex];

  }

  myArray& operator=(const myArray& other)

    {

      if (this != &other) // not a self-assignment

      {

          delete[] values;

     startIndex = other.startIndex;

     size = other.size;

     values = new int[size];

     for(int i = 0; i

      values[i] = other.values[i];

     }

      }

      return *this;

  }

  bool operator==(const myArray& other) {

   if (size != other.size || startIndex != other.startIndex) {

    return false;

   }

   for(int i = 0; i

    if (values[i] != other.values[i]) {

     return false;

    }

   }

   return true;

  }

  bool operator!=(const myArray& other) {

   if (size != other.size || startIndex != other.startIndex) {

    return true;

   }

   for(int i = 0; i

    if (values[i] != other.values[i]) {

     return true;

    }

   }

   return false;

  }

};