473,322 Members | 1,778 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.

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 generateIntegers(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];
generateIntegers(searchList);

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

cout << searchList << endl;
}

}

Jul 23 '05 #1
1 1290
co********@gmail.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 generateIntegers(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 generateIntegers(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];
generateIntegers(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> generateIntegers(int count)
{
std::vector<int> searchList(count);

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

for(int i=0; i<searchList.size(); ++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 generateIntegers(std::vector<int> & searchList)
{
for(int i=0; i<searchList.size();++i)
{
searchList[i]=(i+1)*3;
}
}
int main()
{
std::vector<int> searchList(1000);
generateIntegers(searchList);

for(int i=0; i<searchList.size(); ++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
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...
13
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...
15
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...
4
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
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...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.