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.
 
C

CBFalconer

Dinçay Akçören said:
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];

Now we have established this to be C++. C++ is not C. It has its
own newsgroup, called (surprise) comp.lang.c++. Go there. Make
code complete, with includes, etc. and compilable.
 
K

Keith Thompson

Dinçay Akçören said:
Following function
[snip]
temp = new int[(a-1)*b]; [snip]
cout << "\last version of array" << d << " " << tmp << "\n";

Your code is C++, not C. Either convert it to C and re-post, or post
to comp.lang.c++. (Most of it may well be compatible with C, but
there are subtle differences between the two languages; most of us
here aren't C++ experts.)

[...]
system("pause");
[...]

This is non portable. My system, for example, doesn't have a "pause"
command.

[snip]
 

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

Forum statistics

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

Latest Threads

Top