I didn\'t realize that we needed to take the average of each array and print it
ID: 3771284 • Letter: I
Question
I didn't realize that we needed to take the average of each array and print it to the screen. Could someone help insert the average for each array?
#ifndef MYARRAY_H_INCLUDED
#define MYARRAY_H_INCLUDED
#include
using namespace std;
class myArray
{
public:
myArray(int);
myArray(int, int);
~myArray();
int &operator[](int);
int operator[](int) const;
private:
int size;
int startIndex;
int *ptr;
};
#endif
#include
#include "myArray.h"
using namespace std;
myArray :: myArray(int si, int s)
{
startIndex = -si;
size = s;
ptr = new int[size + startIndex];
for(int i = 0; i < (s + startIndex); i++)
ptr[i] = 0;
}
myArray :: myArray (int s)
{
startIndex = 0;
size = s;
ptr = new int[size];
for(int i = 0; i < s; i++)
ptr[i] = 0;
}
myArray :: ~myArray()
{
delete [] ptr;
}
int &myArray :: operator[] (int subscript)
{
if(subscript < -startIndex || subscript >= size)
{
cerr << "Error: Subscript " << subscript << " out of range." << endl;
}
return ptr[subscript + startIndex];
}
int myArray :: operator[] (int subscript) const
{
if(subscript < -startIndex || subscript >= size)
{
cerr << "Error: Subscript " << subscript << " out of range." << endl;
}
return ptr[subscript + startIndex];
}
#include
#include "myArray.h"
using namespace std;
int main()
{
cout << "A program to check the functionality of the class myArray.";
cout << endl;
cout << endl;
myArray a1(5);
myArray a2(2, 13);
myArray a3(-5, 9);
for(int i = 0; i < 5; i++)
a1[i] = (i * 3 + 65);
for(int i = 2; i < 13; i++)
a2[i] = i * 3;
for(int i = -5; i < 9; i++)
a3[i] = i * 2;
cout << "The elements in the first object are:";
cout << endl;
for (int i = 0; i < 5; i++)
cout << endl << "myList1[" << i << "] = " << a1[i];
cout << endl;
cout << endl;
cout << "The elements in the second object are:";
cout << endl;
for(int i = 2; i < 13; i++)
cout << endl <<"myList2[" << i << "] = " << a2[i];
cout << endl;
cout << endl;
cout << "The elements in the thrid object are:";
cout << endl;
for(int i = -5; i < 9; i++)
cout << endl << "myList3 [" << i << "] = " << a3[i];
cout << endl;
return 0;
}
Explanation / Answer
#ifndef MYARRAY_H_INCLUDED
#define MYARRAY_H_INCLUDED
#include
using namespace std;
class myArray
{
public:
myArray(int);
myArray(int, int);
~myArray();
int &operator[](int);
int operator[](int) const;
private:
int size;
int startIndex;
int *ptr;
};
#endif
#include
#include "myArray.h"
using namespace std;
myArray :: myArray(int si, int s)
{
startIndex = -si;
size = s;
ptr = new int[size + startIndex];
for(int i = 0; i < (s + startIndex); i++)
ptr[i] = 0;
}
myArray :: myArray (int s)
{
startIndex = 0;
size = s;
ptr = new int[size];
for(int i = 0; i < s; i++)
ptr[i] = 0;
}
myArray :: ~myArray()
{
delete [] ptr;
}
int &myArray :: operator[] (int subscript)
{
if(subscript < -startIndex || subscript >= size)
{
cerr << "Error: Subscript " << subscript << " out of range." << endl;
}
return ptr[subscript + startIndex];
}
int myArray :: operator[] (int subscript) const
{
if(subscript < -startIndex || subscript >= size)
{
cerr << "Error: Subscript " << subscript << " out of range." << endl;
}
return ptr[subscript + startIndex];
}
#include
#include "myArray.h"
using namespace std;
int main()
{
cout << "A program to check the functionality of the class myArray.";
cout << endl;
cout << endl;
myArray a1(5);
myArray a2(2, 13);
myArray a3(-5, 9);
for(int i = 0; i < 5; i++)
a1[i] = (i * 3 + 65);
for(int i = 2; i < 13; i++)
a2[i] = i * 3;
for(int i = -5; i < 9; i++)
a3[i] = i * 2;
cout << "The elements in the first object are:";
cout << endl;
for (int i = 0; i < 5; i++)
cout << endl << "myList1[" << i << "] = " << a1[i];
cout << endl;
cout << endl;
cout << "The elements in the second object are:";
cout << endl;
for(int i = 2; i < 13; i++)
cout << endl <<"myList2[" << i << "] = " << a2[i];
cout << endl;
cout << endl;
cout << "The elements in the thrid object are:";
cout << endl;
for(int i = -5; i < 9; i++)
cout << endl << "myList3 [" << i << "] = " << a3[i];
cout << endl;
//changes
float average;
average = 0;
cout << " The average of elements in the first object is: ";
for (int i = 0; i < 5; i++)
average += a1[i];
cout<<average/5;
average = 0;
cout << " The average of elements in the second object is: ";
for(int i = 2; i < 13; i++)
average += a2[i];
cout<<average/11;
average = 0;
cout << " The average of elements in the third object is: ";
for(int i = -5; i < 9; i++)
average += a3[i];
cout<<average/14<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.