c++ , questions about some problem

+1 Dusan Radic · May 16, 2015
Implement a class order which will represent a sequence of integers of arbitrary length, and contains a class
information on the length of the line. Ensure that the index of the first member may be different from zero if the user
jade. To provide access to members of the Order of registration and data exchange. Write the main program in which
to enter the corresponding row and the output to give a sum of members of this order as the order that is compiled in the square
elements of the order that the user entered.

#include<iostream>
#include<math.h>
using namespace std;
class Array
{
private:
int *p; //pointer
int num; //array length
const int i1; //  Initial index (can be different from 0)
public:
Array(int first_index=1,int number=0);
Array(Array & ); 
~Array();  
int first(){return i1;}    
int length(){return num;}  
int last(){return i1+num-1;}  
int read(int index){return p[index-i1];}  
void change(int, int);
};
Array::Array(int first_index, int number):i1(first_index), num(number)
{
p=new int[num];
}
Array::Array(Array &a): i1(a.i1), num(a.num)
{
p=new int[num];
for(int i=0;i<num;i++) p=a.p;
}
Array::~Array(){delete[] p; p=0;}
void Array::change(int index, int value)
{
p[index-i1]=value;
}
int sum(Array &a)
{
int s=0;
for(int i=a.first();i<=a.last();i++) s+=a.read(i);
return s;
}
void square(Array &a)
{
for(int i=a.first();i<=a.last();i++)
{
int x;
x=a.read(i);
a.change(i,pow(x,2));
}
}
int main()
{
int i, n,x;
cout<<"How much members do you want to add"<<'\n';
cin>>n;
cout<<"from witch index you would like to start"<<'\n';
cin>>i;
cout<<"Add members of array"<<endl;
Array a1(i,n);
for(int j=i;j<n+i;j++)
{
cin>>x;
a1.change(j,x);
}
Array a2(a1);
square(a2);
cout<<"Sum of array members is: "<<sum(a1)<<endl;
cout<<"Array of square members  is: "<<endl;
for(int j=a2.first(); j<=a2.last();j++) cout<<a2.read(j)<<" ";
}


I have a questions:
1. Why is p=new int[num] , (can be something other than num) is it because its the alocation memory for array ?
2. When was void main() it was a error, but when i changed to int is worked just fine, why ?
3.In the line of code ---  void Array::change(int index,int value){ p[index-i1]=value;},  what means this p[index-i1] ?

Sorry for my bad english and for me being so bored with this questions  ...


Post a Reply

Replies

Oldest  Newest  Rating
0 Sengngy Kouch · May 17, 2015
@Dusan: 


Hi there!

I am not sure if I answer your question right. However, let me try my best.


1. First of all, I think you mean "is p".  and I am not sure what your question is, but let me try my best.

 if you creating a new pointer to an array, it will need a size. NUM is the size of the array.
int num = 20;
int *p = new int[num]; // now "P" has a size of 20.


Remember that you are creating an integer type of pointer. 
//Pointer of type double array.

double *prt = new double [num];

//or

double *prter = new double[20];


2. You should always use int main() for c++.  The reason why people use void main() is because they get used to java. and void main might only work with Microsoft's C++ compiler. (Microsoft visual studio , etc).
3. This is the first time that I see this kind of thing.

 P[index - i1] = value ;

Does this even compile? I think Krootushas might be right.
After all, I hope this help.
0 sumeet prasad · May 18, 2015
just to help u ..
every time u ask the operating system the memory you should give it back by using the function
delete[ ]
0 Dusan Radic · May 18, 2015
Thanks, you all helped me a lot :)
  • 1

C++

107,004 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator