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

C++ question - Classes - question 2 is part of question 1 ----------------------

ID: 3864090 • Letter: C

Question

C++ question - Classes - question 2 is part of question 1 -----------------------------------------------------

Exercise 1: Implement the Array class that is defined below and test it in the main program. The main program must test all the functions that are defined in the class. #ifndef array_h #define array_h class Array { // Class declaration public: Array (int =5); //Initialize the array with 0 values Array (const Array & ); // copy constructor ~Array(); //destructor bool setValue (int index, int value); // assign a value to an //array element bool getValue (int index, int &value); // get an array element Array & increment(); /* cascade function to increment each array element by 1 */ int getSize ( ); // return the size of the array. void print ( ) const; // print each element of the array Array& Add(const Array ar);/*casade function to add all elements of arr to all elements of the “this” array (element by element) and return a reference to the resulting array */ bool Equal(const Array *ar)const;// return true if all elements in both arrays are equal, false otherwise. Array& removeAt(int index);//cascade function to remove element at the index.( if index is within the valide range, you should allocate new memory with 1 element less, copy all elements but the element at index to new memory, delete the old one) Array& insertAt(int index, int value);// insert value at index location, if index is greater than the size, then insert the element at the end of the array, if index is < 0 then insert the element at the beginig of the array, otherwise, insert in the exact index ( you should allocate new memory, copy element etc…) private: int size; // size of created array int* arr; }; #endif Exercise 2: sumArray ( ) is a function that sums all the elements of a given array object. Implement sumArray ( ) using the three techniques that we have learned in the lecture, namely: • As a non-member function of the class Array int sumArray ( const Array &); • As a friend function of the class Array friend int sumArray ( const Array &); • As a public member function of the class Array int Array::sumArray (); C++ question - Classes - question 2 is part of question 1 -----------------------------------------------------

Exercise 1: Implement the Array class that is defined below and test it in the main program. The main program must test all the functions that are defined in the class. #ifndef array_h #define array_h class Array { // Class declaration public: Array (int =5); //Initialize the array with 0 values Array (const Array & ); // copy constructor ~Array(); //destructor bool setValue (int index, int value); // assign a value to an //array element bool getValue (int index, int &value); // get an array element Array & increment(); /* cascade function to increment each array element by 1 */ int getSize ( ); // return the size of the array. void print ( ) const; // print each element of the array Array& Add(const Array ar);/*casade function to add all elements of arr to all elements of the “this” array (element by element) and return a reference to the resulting array */ bool Equal(const Array *ar)const;// return true if all elements in both arrays are equal, false otherwise. Array& removeAt(int index);//cascade function to remove element at the index.( if index is within the valide range, you should allocate new memory with 1 element less, copy all elements but the element at index to new memory, delete the old one) Array& insertAt(int index, int value);// insert value at index location, if index is greater than the size, then insert the element at the end of the array, if index is < 0 then insert the element at the beginig of the array, otherwise, insert in the exact index ( you should allocate new memory, copy element etc…) private: int size; // size of created array int* arr; }; #endif Exercise 2: sumArray ( ) is a function that sums all the elements of a given array object. Implement sumArray ( ) using the three techniques that we have learned in the lecture, namely: • As a non-member function of the class Array int sumArray ( const Array &); • As a friend function of the class Array friend int sumArray ( const Array &); • As a public member function of the class Array int Array::sumArray (); -----------------------------------------------------

Exercise 1: Implement the Array class that is defined below and test it in the main program. The main program must test all the functions that are defined in the class. #ifndef array_h #define array_h class Array { // Class declaration public: Array (int =5); //Initialize the array with 0 values Array (const Array & ); // copy constructor ~Array(); //destructor bool setValue (int index, int value); // assign a value to an //array element bool getValue (int index, int &value); // get an array element Array & increment(); /* cascade function to increment each array element by 1 */ int getSize ( ); // return the size of the array. void print ( ) const; // print each element of the array Array& Add(const Array ar);/*casade function to add all elements of arr to all elements of the “this” array (element by element) and return a reference to the resulting array */ bool Equal(const Array *ar)const;// return true if all elements in both arrays are equal, false otherwise. Array& removeAt(int index);//cascade function to remove element at the index.( if index is within the valide range, you should allocate new memory with 1 element less, copy all elements but the element at index to new memory, delete the old one) Array& insertAt(int index, int value);// insert value at index location, if index is greater than the size, then insert the element at the end of the array, if index is < 0 then insert the element at the beginig of the array, otherwise, insert in the exact index ( you should allocate new memory, copy element etc…) private: int size; // size of created array int* arr; }; #endif Exercise 2: sumArray ( ) is a function that sums all the elements of a given array object. Implement sumArray ( ) using the three techniques that we have learned in the lecture, namely: • As a non-member function of the class Array int sumArray ( const Array &); • As a friend function of the class Array friend int sumArray ( const Array &); • As a public member function of the class Array int Array::sumArray ();
Exercise 1: Implement the Array class that is defined below and test it in the main program. The main program must test all the functions that are defined in the class. #ifndef array_h #define array_h class Array { // Class declaration public: Array (int =5); //Initialize the array with 0 values Array (const Array & ); // copy constructor ~Array(); //destructor bool setValue (int index, int value); // assign a value to an //array element bool getValue (int index, int &value); // get an array element Array & increment(); /* cascade function to increment each array element by 1 */ int getSize ( ); // return the size of the array. void print ( ) const; // print each element of the array Array& Add(const Array ar);/*casade function to add all elements of arr to all elements of the “this” array (element by element) and return a reference to the resulting array */ bool Equal(const Array *ar)const;// return true if all elements in both arrays are equal, false otherwise. Array& removeAt(int index);//cascade function to remove element at the index.( if index is within the valide range, you should allocate new memory with 1 element less, copy all elements but the element at index to new memory, delete the old one) Array& insertAt(int index, int value);// insert value at index location, if index is greater than the size, then insert the element at the end of the array, if index is < 0 then insert the element at the beginig of the array, otherwise, insert in the exact index ( you should allocate new memory, copy element etc…) private: int size; // size of created array int* arr; }; #endif Exercise 2: sumArray ( ) is a function that sums all the elements of a given array object. Implement sumArray ( ) using the three techniques that we have learned in the lecture, namely: • As a non-member function of the class Array int sumArray ( const Array &); • As a friend function of the class Array friend int sumArray ( const Array &); • As a public member function of the class Array int Array::sumArray (); Exercise 1: Implement the Array class that is defined below and test it in the main program. The main program must test all the functions that are defined in the class. #ifndef array_h #define array_h class Array { // Class declaration public: Array (int =5); //Initialize the array with 0 values Array (const Array & ); // copy constructor ~Array(); //destructor bool setValue (int index, int value); // assign a value to an //array element bool getValue (int index, int &value); // get an array element Array & increment(); /* cascade function to increment each array element by 1 */ int getSize ( ); // return the size of the array. void print ( ) const; // print each element of the array Array& Add(const Array ar);/*casade function to add all elements of arr to all elements of the “this” array (element by element) and return a reference to the resulting array */ bool Equal(const Array *ar)const;// return true if all elements in both arrays are equal, false otherwise. Array& removeAt(int index);//cascade function to remove element at the index.( if index is within the valide range, you should allocate new memory with 1 element less, copy all elements but the element at index to new memory, delete the old one) Array& insertAt(int index, int value);// insert value at index location, if index is greater than the size, then insert the element at the end of the array, if index is < 0 then insert the element at the beginig of the array, otherwise, insert in the exact index ( you should allocate new memory, copy element etc…) private: int size; // size of created array int* arr; }; #endif Exercise 2: sumArray ( ) is a function that sums all the elements of a given array object. Implement sumArray ( ) using the three techniques that we have learned in the lecture, namely: • As a non-member function of the class Array int sumArray ( const Array &); • As a friend function of the class Array friend int sumArray ( const Array &); • As a public member function of the class Array int Array::sumArray ();

Explanation / Answer

Answer: What i got is that you want the answer of question 2 which is te part of 1 if you have anyclearification or problem ask in comments.I am giving the answer of of part 2 below:

1) As the non member of class Array

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

  
class Array {
int a;
public:
void set1(int i) { a = i; }
// public: int sumarray(); //function to do sum of objects of array
int get1() { return a; }
};
int sumarray(Array obs[],int n){
int sum,i;
for(i=0; i < n; i++)
sum=sum+ obs[i].get1();
return sum;
}
int main()
{ int n;
cout<<"Enter the size of array"<<endl;
cin>>n;
Array obs[n];

int i;

for(i=0; i <n ; i++)
obs[i].set1(i);
cout<<"sum of objects of array is= "<<sumarray(obs,n);
  

return 0;
}

output:

Input (stdin)

Your Output

2)

As the public member function of class Array:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

  
class Array {
int a;
public:
void set1(int i) { a = i; }
public: int sumarray(); //function to do sum of objects of array
int get1() { return a; }
};
int sumarray(Array obs[],int n){
int sum,i;
for(i=0; i < n; i++)
sum=sum+ obs[i].get1();
return sum;
}
int main()
{ int n;
cout<<"Enter the size of array"<<endl;
cin>>n;
Array obs[n];

int i;

for(i=0; i <n ; i++)
obs[i].set1(i);
cout<<"sum of objects of array is= "<<sumarray(obs,n);
  

return 0;
}

output:

Input (stdin)

Your Output

3)

As the firne function of class Array:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

  
class Array {
int a;
public:
void set1(int i) { a = i; }
friend int sumarray(); //function to do sum of objects of array
int get1() { return a; }
};
int sumarray(Array obs[],int n){
int sum,i;
for(i=0; i < n; i++)
sum=sum+ obs[i].get1();
return sum;
}
int main()
{ int n;
cout<<"Enter the size of array"<<endl;
cin>>n;
Array obs[n];

int i;

for(i=0; i <n ; i++)
obs[i].set1(i);
cout<<"sum of objects of array is= "<<sumarray(obs,n);
  

return 0;
}

output:

Compilation Successful

Input (stdin)

Your Output

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote