473,322 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

question for returning an array

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[i] = ar1[i];
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[i] = temp-i;
}
for(i=0; i<sayi; i++)
cout << pt[i] << " ";
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.

Sep 2 '07 #1
2 1596
On 2007-09-02 10:50, Dinçay Akçören wrote:
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[i] = ar1[i];
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 )

--
Erik Wikström
Sep 2 '07 #2

Dinçay Akçören <di****@gmail.comwrote in message...
>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[i] = ar1[i];
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[i] = 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::ostream &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.

--
Bob R
POVrookie
Sep 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
36
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
5
by: Naveen Mukkelli | last post by:
Hi, I've got a property that returns byte. For example, private byte bytes; public byte ReturnsByteArray { get
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
19
by: Andrew Gentile | last post by:
Hello, I have been working on a program where I need to have a function return an array. I found out that C doesn't do this, so now I am trying to get the function to return a pointer to an...
3
by: =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?= | last post by:
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++)
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
10
by: mdh | last post by:
Sorry in advance if this is really dumb, but I am trying to get my head around exactly what the declarator is. In the FAQ, 1.21, part of it says "C declarations ....come in 2 parts, a base type and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.