Create an array and allow the user to enter values in sequential fashion as well as allow random read/write. I would want to do this using classes and objects so that the code is more dynamic and re-usable.
class array{
private:
int size,user_array[100];
public:
array() {
for(int i=0;i<2;i++)
user_array[i]=0;
}
/* array(int size_n) { //should i include this function to dynamically allocate the array??
size=size_n;
user_array=new int[size];
}*/
array(int size, int input){
for(int i=2;i<size;i++)
user_array[i]=input;
}
/* ~array()
{delete [] user_array;
user_array=0;}*/
int read(int i)//allow user to read any element of his/her choice
{ if(i<0||i>size)
cout<<"There is no array element with that subscript"<<endl;
return user_array[i];
}
// int get_element(int value)
// { return value;}
array write(int i,int value)//allow user to write/modify any element
{user_array[i]=value;}
int &operator[](int sub)
{ if(sub<0||sub>size) //overload []
cout<<"Terminating program, wrong choice"<<endl;
return user_array[sub];
}
// friend ostream &operator << (ostream &os,array &a)//overload <<
// { cout<<a.get_element(value);
// return os;
// }
};
int main() {
array obj1,obj2;
int *ptr,j,k,n,enter;
cout<<" Number of elements?"<<endl;
cin>>n;
ptr = newint[n];
int new_array[n];
cout<<"Enter the values for this sparse array of "<<n<<" elements"<<endl;
for(int i=0; i<n; i++)
cin>>new_array[i];
cout<<"Here is the array that you entered"<<endl;
for(int i=0;i<n;i++)
cout<<new_array[i];
cout<<"\nWhat element do you want to view?" <<endl;
cin>>j;
cout<<obj1.read(j);
// cout<<"If you want to change an ellment, enter the subscript and the value, else program terminates?" <<endl; //this write function does not work as expected.
// cin>>k>>enter;
// cout<<obj2.write(k);
cout<<endl;
return 0;
}
Issues:I'm not able todefine a function(write_element) that allows the user to enter a value for any element in an array.
What can I do here? also, I'm unable to overload << operator to work with the class.
any other issues that you can see?
The easiest way to learn how to develop a matrix or array C++ class is to get some already well done matrix or array class or library, and see how they are coded and orginaized.
I would go to sourceforge and download me some C++ linear algebra library and learn from them.
One point that you should take into consideration when downloading a C++ library/class is the documentation, so first have a look at the documentation and then go forward....
#include<iostream>
usingnamespace std;
class array{
private: int size,*storage;
public:
array() { //default constructor to initialize first two elements of array to zero
for(int i=0;i<2;i++)
storage[i]=0;
}
array(int size) //allows user to input the array elelments
{
storage = newint [size];
this -> size = size;
}
~array()
{
delete [] storage;
}
int read_element(int index)
{
return storage[index];
}
void write_element(int index, int value)
{
storage[index] = value;
}
int & array::operator[] (int index)
{
if(index < 0 || index >size)
cout<<"Error in accessing array subscript\n";
elsereturn storage[index];
}
ostream & operator <<(ostream &os,array &a)
{
int i;
os << "[ ";
for(i=0; i<a.size; i++)
os << a[i] << " ";
os << "]" << endl;
return os;
}
}};
int main()
{
int i,size;
array obj1(size),obj2; //2 objects
cout<<"Size of array?? ";
cin>>size;
cout<<"Enter elelments of array" \n ";
for(i=0;i<size;i++)
cin>>obj1[i];
cout<<"Here is what you entered :"<<endl
//for(i=0;i<3;i++)
cout<<obj2[i];
for(i=0;i<size;i++)
cout<<obj1[i];
return 0;
}
I have modified my code as above.
My question is : how do I call the default constructor so that I can initialize the first two elements of my array to zero? I created one object here that calls the default constructor(obj2). is this correct?
when you create a new object of any class and provide no parameter, you create the object by calling the default constructor.
In your main you created the obj2 by calling the default constructor , it is ok, however your default constructor itself is not alright, it has some errors and bugs.