473,809 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1630
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.c omwrote 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(vect or<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(vec tor<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
10300
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. Returning a pointer to the first element might do but I want to do what I've said. Fraser.
1
3331
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 for my first element but my subsequent printf's will return garbage. Can someone let me know what I am doing wrong. Thanks in advance. -jlewis
36
2289
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 code ... */
3
2703
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 return an array as a property because it will return a copy of the array instead a reference to it. How can I force the property to return a reference to the array? Is it only a feature of arrays? I hope normal class objects (including collections)...
5
9068
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
3266
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
19
1950
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 array. It seems easy, but I keep running into the same problem. The function appears to execute properly, but when I print out the array values, only the first one is correct. The other are all wrong. A snippet of code is below. Please take a...
3
330
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
2222
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
1908
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 a declarator......". The example used is char *pc where the declarator " *pc" tells us that " *pc is a character". Can one thus broadly say that a declarator is in fact equivalent to the base type?
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.