473,404 Members | 2,174 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,404 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 1597
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.