question for returning an array

  • Thread starter =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?=
  • Start date
?

=?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?=

Following function

void mdelr(int *ar1, int a, int b, int d )
{
int i,j,tmp;
int *temp;


for (i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
if (i >= d)
ar1[i*b+j] = ar1[(i+1)*b+j];
if (i == b-1)
ar1[i*b+j] = 0;
}
}
tmp = ar1[0];
temp = new int[(a-1)*b];


for(i=0; i<b*a-b; i++)
temp = ar1;


delete ar1;
ar1 = temp;


cout << "\last version of array" << d << " " << tmp << "\n";
for (i=0; i<a-1; i++)
{
for(j=0; j<b; j++)
{
cout << ar1[i*b +j] << " ";
}
cout << "\n";
}



}


and following main block

int main(void)
{
int *ar1,*ar2;
int i,j,k;
int temp, sayi,ROW,COL;
int *pt;


ROW=COL=5;


ar1 = new int[ROW*COL];


cout << "row number to be deleted";
cin >> sayi;


pt = new int[sayi];


for(i=0; i<sayi; i++)
{
cout << "Enter col num" << i;
cin >> temp;
temp--;
pt = temp-i;
}


for(i=0; i<sayi; i++)
cout << pt << " ";


cout << "\n";


system("pause");
/*assign values*/
for (i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
ar1[i*COL +j] = (i+i+j+1);
}
}


cout << "original matrix\n";
for (i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
cout << ar1[i*ROW +j] << " ";
}
cout << "\n";
}


for(k=0; k<sayi; k++)
{
cout << "step " << k << "\n";
mdelr(ar1,ROW-k,COL,pt[k]);


cout << "\nresult in main" << k << "\n";
for (i=0; i<ROW-k-1; i++)
{
for(j=0; j<COL; j++)
{
cout << ar1[i*(COL) +j] << " ";
}
cout << "\n";
}
}


delete ar1;
delete pt;
system("pause");
return 0;



}


gives the following output

row number to be deleted 2
Enter col num02
Enter col num14
1 2
Devam etmek için bir tusa bas n . . .
original matris
1 2 3 4 5
3 4 5 6 7
5 6 7 8 9
7 8 9 10 11
9 10 11 12 13
step 0


last version of array1 1
1 2 3 4 5
5 6 7 8 9
7 8 9 10 11
9 10 11 12 13


result in main 0
0 2 3 4 5
5 6 7 8 9
7 8 9 10 11
9 10 11 12 13
adim 1


last version of array2 0
0 2 3 4 5
5 6 7 8 9
9 10 11 12 13


result in main 1
4007120 2 3 4 5
5 6 7 8 9
9 10 11 12 13
Devam etmek için bir tusa bas n . . .


My question is why the first element of the array becomes stupid
despite all of my effords? I believe returning to main changes the
first element but I cannot prevent this.
 
?

=?ISO-8859-9?Q?Erik_Wikstr=F6m?=

Following function

void mdelr(int *ar1, int a, int b, int d )
{
int i,j,tmp;
int *temp;


for (i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
if (i >= d)
ar1[i*b+j] = ar1[(i+1)*b+j];
if (i == b-1)
ar1[i*b+j] = 0;
}
}
tmp = ar1[0];
temp = new int[(a-1)*b];


for(i=0; i<b*a-b; i++)
temp = ar1;


delete ar1;
ar1 = temp;


This will not change what ar1 points to in main() since you passed a
copy of the pointer. You need to pass the pointer by reference:

void mdelr(int *&ar1, int a, int b, int d )
 
A

Alf P. Steinbach

* Dinçay Akçören:
Following function

void mdelr(int *ar1, int a, int b, int d )
{
int i,j,tmp;
int *temp;


for (i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
if (i >= d)
ar1[i*b+j] = ar1[(i+1)*b+j];
if (i == b-1)
ar1[i*b+j] = 0;
}
}
tmp = ar1[0];
temp = new int[(a-1)*b];


for(i=0; i<b*a-b; i++)
temp = ar1;


delete ar1;
ar1 = temp;


cout << "\last version of array" << d << " " << tmp << "\n";
for (i=0; i<a-1; i++)
{
for(j=0; j<b; j++)
{
cout << ar1[i*b +j] << " ";
}
cout << "\n";
}



}


and following main block

int main(void)
{
int *ar1,*ar2;
int i,j,k;
int temp, sayi,ROW,COL;
int *pt;


ROW=COL=5;


ar1 = new int[ROW*COL];


cout << "row number to be deleted";
cin >> sayi;


pt = new int[sayi];


for(i=0; i<sayi; i++)
{
cout << "Enter col num" << i;
cin >> temp;
temp--;
pt = temp-i;
}


for(i=0; i<sayi; i++)
cout << pt << " ";


cout << "\n";


system("pause");
/*assign values*/
for (i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
ar1[i*COL +j] = (i+i+j+1);
}
}


cout << "original matrix\n";
for (i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
cout << ar1[i*ROW +j] << " ";
}
cout << "\n";
}


for(k=0; k<sayi; k++)
{
cout << "step " << k << "\n";
mdelr(ar1,ROW-k,COL,pt[k]);


cout << "\nresult in main" << k << "\n";
for (i=0; i<ROW-k-1; i++)
{
for(j=0; j<COL; j++)
{
cout << ar1[i*(COL) +j] << " ";
}
cout << "\n";
}
}


delete ar1;
delete pt;
system("pause");
return 0;



}


gives the following output

row number to be deleted 2
Enter col num02
Enter col num14
1 2
Devam etmek için bir tusa bas n . . .
original matris
1 2 3 4 5
3 4 5 6 7
5 6 7 8 9
7 8 9 10 11
9 10 11 12 13
step 0


last version of array1 1
1 2 3 4 5
5 6 7 8 9
7 8 9 10 11
9 10 11 12 13


result in main 0
0 2 3 4 5
5 6 7 8 9
7 8 9 10 11
9 10 11 12 13
adim 1


last version of array2 0
0 2 3 4 5
5 6 7 8 9
9 10 11 12 13


result in main 1
4007120 2 3 4 5
5 6 7 8 9
9 10 11 12 13
Devam etmek için bir tusa bas n . . .


My question is why the first element of the array becomes stupid
despite all of my effords?


One reason is of course that the code you have shown is not the code
that produced the output. In the code it says "matrix", in the output
it says "matris", and we don't know what other changes there are from
the original code and/or output. It's a Good Idea(TM) to present the
code that actually produces the output, or, from another point of view,
present the output that is actually from the given code.

Another reason is that in the called function you delete the array.

That would in itself make the array rather "stupid", as you write,
because accessing the array after that is Undefined Behavior, but on top
of doing that, the code (which as mentioned isn't the code producing the
output shown above) uses "delete" instead of correct "delete[]", which
is also Undefined Behavior, so, in short, you have Undefined Behavior.

I believe returning to main changes the
first element but I cannot prevent this.

Use std::vector instead of raw arrays. Use Boost matrix classes or
other matrix classes instead of inventing your own.

Use std::copy instead of element copying loops.

Don't use raw pointers, or rather, simply don't use pointers.


Cheers, & hth.,

- Alf
 
B

BobR

Dinçay Akçören said:
Following function

Are you comeing from a 'C' background?
void mdelr(int *ar1, int a, int b, int d ){
int i,j,tmp;
int *temp;

Why are those there, and un-initialized?
You don't use 'i' or 'j' outside the loops, so, let's rewrite that to be
more C++ like...

int* mdelr( int *ar1, int a, int b, int d ){
for( int i(0); i < a; ++i ){
for( int j(0); j < b; ++j ){
if( i >= d ) ar1[i*b+j] = ar1[(i+1)*b+j];
if( i == b-1 ) ar1[i*b+j] = 0;
} // for(j)
} // for(i)
int *temp( new int[(a-1)*b] );
for( int i(0); i < b*a-b; ++i )
temp = ar1;
using std::cout;
cout << "\nlast version of array" << d << " " << ar1[0] << "\n";
delete [] ar1;
ar1 = temp;
delete [] temp;

for( int i(0); i < a-1; ++i){
for( int j(0); j < b; ++j){
cout << ar1[i*b +j] << " ";
}
cout << "\n";
}
return ar1;
} // mdelr(int*,int,int,int)
and following main block

// > int main(void)
int main(){ // C++
using std::cout;
// > int *ar1,*ar2;
// > int i,j,k;
// > int temp, sayi,ROW,COL;
// > int *pt;

Why are those there, and un-initialized?

// > ROW=COL=5;
Reserve all-caps for macros.

size_t Row(5), Col(5);

// > ar1 = new int[ROW*COL];
int *ar1( new int[ Row * Col ] );
cout << "row number to be deleted"; int sayi(0);
cin >> sayi;

// > pt = new int[sayi];
int *pt( new int[ sayi ] );

int temp(0);
// > for(i=0; i<sayi; i++){
for( int i(0); i < sayi; ++i){
cout << "Enter col num" << i;
cin >> temp;

How do you know input succeeded?
temp--;
pt = temp-i;
}


You fix the rest...

// in for(k)
// > mdelr(ar1,ROW-k,COL,pt[k]);

ar1 = mdelr( ar1, Row-k, Col, pt[k] );

// > delete ar1;
// > delete pt;
delete [] ar1;
delete [] pt;
system("pause");
return 0;
} // main()
My question is why the first element of the array becomes stupid
despite all of my effords? I believe returning to main changes the
first element but I cannot prevent this.

What Erik and Alf said.

To get you going, I'll give you a short example using std::vector.
// ------------
#include <iostream>
#include <vector>

void FillVector( std::vector< int > &vec){ // non-const here
vec.push_back( 1 );
vec.push_back( 2 );
vec.push_back( 3 );
// vec.at(0) = 42; // change 1 to 42 in first element.
return;
} // FillVector(vector<int>&)

void PrintVector( std::vector< int > const &vec, // const here
std::eek:stream &out ){
for( std::size_t i(0); i < vec.size(); ++i ){
out<<vec.at(i)<<std::endl;
// or: out<<vec[ i ]<<std::endl;
}
// I won't show the std::copy method here.
// Look it up, or review this NG for it.
return;
} // PrintVector(vector<int>const&,ostream&)

int main(){
std::vector< int > MyVec;
FillVector( MyVec );
PrintVector( MyVec, std::cout );
return 0;
} // main()
// ------------

You can initialize the vector:

// std::vector< int > MyVec( 25 );
std::vector< int > MyVec( 25, 7 );

.....will give you a vector with 25 elements, all set to '7'.
Read-up on std::vector for much more.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top