473,671 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a functions that returns an array

Environemtnt: Visual C++ on Windows Xp

I am new somewhat new to C++ and would like to some advise and
correction to my code below. I am simply trying to write function that
generates an array and returns it to the caller for output and further
use. I have read bits sugessting that thios can be done using pointers,
structs and or vectors but no workin solution yet. Thanks in
advance...

int generateInteger s(int searchList[])
/*This function generates a thousand integers held in an array*/
{
//searchList[1000];
for(int i=0; i<1000;++i)
{
searchList[i]=(i+1)*3;
}
return searchList;
}
int main()
{
int searchList[1000];
generateInteger s(searchList);

for(int i=0; i<1000;++i) {

cout << searchList << endl;
}

}

Jul 23 '05 #1
1 1307
co********@gmai l.com wrote:
Environemtnt: Visual C++ on Windows Xp

I am new somewhat new to C++ and would like to some advise and
correction to my code below. I am simply trying to write function that
generates an array and returns it to the caller for output and further
use. I have read bits sugessting that thios can be done using pointers,
structs and or vectors but no workin solution yet. Thanks in
advance...

int generateInteger s(int searchList[])
This defines a function that returns a SINGLE "int" by value. You're
passing in a pointer to an array of int (no size given).

Why are you returning an int ? The code below does initialize the array
except you're trying to return a pointer where you otherwise declared to
return an int by value.
/*This function generates a thousand integers held in an array*/
{
//searchList[1000];
for(int i=0; i<1000;++i)
{
searchList[i]=(i+1)*3;
}
return searchList;
}


This code would do what you're looking for.

void generateInteger s(int * searchList, unsigned length)
{
for(int i=0; i<length;++i)
{
searchList[i]=(i+1)*3;
}
}

The function above is more appropriate - it simply sets the values into
the array passed in.

// simply for determining the number of elements in an array....
template <typename w_Type, int w_N>
inline int CountElements( w_Type (&i_array)[ w_N ] )
{
return w_N;

}

int main()
{
int searchList[1000];
generateInteger s(searchList, CountElements( searchList ) );

for(int i=0; i<CountElements ( searchList );++i) {

cout << searchList[i] << endl;
}

}

However, if you REALLY want to return a value (lots of memory copies
that degrade performance and not a good thing to do), you can do this:

#include <vector>
std::vector<int > generateInteger s(int count)
{
std::vector<int > searchList(coun t);

for(int i=0; i<count;++i)
{
searchList[i]=(i+1)*3;
}
return searchList;
}
int main()
{
int count = 1000;
std::vector<int > searchList = generateInteger s(count);

for(int i=0; i<searchList.si ze(); ++i)
{
std::cout << searchList[i] << std::endl;
}
}

The example above does more like what you described. It returns a vector
and probably makes 2 copies before you're done with the process. You
really want to avoid that if you can.

Finally, you can use a vector passed in by reference to eliminate the
copies (similar to what you did in your original example).

#include <vector>
void generateInteger s(std::vector<i nt> & searchList)
{
for(int i=0; i<searchList.si ze();++i)
{
searchList[i]=(i+1)*3;
}
}
int main()
{
std::vector<int > searchList(1000 );
generateInteger s(searchList);

for(int i=0; i<searchList.si ze(); ++i)
{
std::cout << searchList[i] << std::endl;
}
}
Jul 23 '05 #2

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

Similar topics

8
2345
by: [RaZoR] | last post by:
hello, main script creates IE window, put an array there and then calls a child script. chils script makes an array element equal to some object and returns control to main script. then main script doesn't see that object and its functions. why? here is the code I implemented: //in main script
13
2192
by: agentxx04 | last post by:
Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include<stdio.h> int main() { int item; int a, b, t, mode; int median_index;
15
2899
by: Jaraba | last post by:
I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am particulary having problems inserting records using a function. I am attaching the code for your review. Please, help. =================================--Function 1--====================================
4
2811
by: roxorsoxor2345 | last post by:
I have this custom string class that I have created. #include <cstdlib> #include <iostream> using namespace std; class Cstring { private:
1
3787
by: Someone21 | last post by:
Hi all, I am currently trying to retrieve a string from a textfile, separate that and put it in an array. I stumbled on the problem that *token is a pointer, and not the string. However when i printf("%s", token) it prints out the word that it points to. Could anyone explain how to fix this problem? Words.txt: dog|cat|hamster
0
8392
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8912
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
8819
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
8597
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
8669
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1807
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.