Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,007 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,386 people online right now. Registration is fast and FREE... Join Now!




Passing arrays through functions

 
Reply to this topicStart new topic

Passing arrays through functions

stezz
9 Dec, 2006 - 01:53 PM
Post #1

New D.I.C Head
*

Joined: 9 Dec, 2006
Posts: 4


My Contributions
Hi guys,

I have a question about passing arrays to functions. The program is simple, it asks the user for numbers which it stores in an array (maximum of 20). I've written a function that processes the array to calculate the minimum, maximum and average of the numbers stored in it.

As there are three different values I need the function to return, can I do it in one function, or do I need three seperate functions. Two of the three values are intergers and the third is a float.

Hope somebody can help.

Regards,

Stezz
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Passing Arrays Through Functions
9 Dec, 2006 - 02:02 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You can do it in one function, but you would need to declare a structure to hold the three values. And then you could pass the structure back to your main method.

Are you familiar with Structures?

User is offlineProfile CardPM
+Quote Post

stezz
RE: Passing Arrays Through Functions
9 Dec, 2006 - 02:50 PM
Post #3

New D.I.C Head
*

Joined: 9 Dec, 2006
Posts: 4


My Contributions
Nope! This program is for my programming class. We haven't done structures yet, so im assuming I'll need thre seperate functions.

Thanks anyways.
User is offlineProfile CardPM
+Quote Post

TuffGuy
RE: Passing Arrays Through Functions
9 Dec, 2006 - 03:23 PM
Post #4

New D.I.C Head
*

Joined: 31 Oct, 2006
Posts: 6


My Contributions
QUOTE(stezz @ 9 Dec, 2006 - 03:50 PM) *

Nope! This program is for my programming class. We haven't done structures yet, so im assuming I'll need thre seperate functions.

Thanks anyways.


An array itself can not be returned from a method, yet you can, call the method from within the method that makes the array.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Passing Arrays Through Functions
9 Dec, 2006 - 03:37 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
It would be better to keep them in three separate functions then, it is good practice for you. smile.gif
User is offlineProfile CardPM
+Quote Post

ericode
RE: Passing Arrays Through Functions
9 Dec, 2006 - 09:06 PM
Post #6

D.I.C Head
Group Icon

Joined: 9 Dec, 2006
Posts: 89


Dream Kudos: 75
My Contributions
QUOTE(stezz @ 9 Dec, 2006 - 02:53 PM) *

Hi guys,

I have a question about passing arrays to functions. The program is simple, it asks the user for numbers which it stores in an array (maximum of 20). I've written a function that processes the array to calculate the minimum, maximum and average of the numbers stored in it.

As there are three different values I need the function to return, can I do it in one function, or do I need three seperate functions. Two of the three values are intergers and the third is a float.

Hope somebody can help.

Regards,

Stezz




Indeed, it would probably be best to make separate functions for each (average, maximum, minimum), but...there is another way that doesn't involve structures. You could pass pointers to the integers and float representing the minimum and maximum and average. Then the function could modify those values through the reference parameters.

CODE


int arr [20];

int maximum;
int* maxPtr = &maximum;
int minimum;
int* minPtr = &minimum;
float average;
float* avPtr = &average;

/*fill the array*/

/*now you'd call the function that had
   a prototype like so:
  void doAll(int[] array, int*min, int*max, float*av)
*/

doAll(arr, minPtr,maxPtr,avPtr);

/*print out the three values which have been set by doAll*/


Then in the doAll function, you would just refer to the pointers as so:

CODE

void doAll(int[] array, int*min, int*max, float*av)
{
  /*sort array*/
  *min = array[0];
  *max = array[19];
  *float = /*the average you calculated*/
}



User is offlineProfile CardPM
+Quote Post

skaoth
RE: Passing Arrays Through Functions
23 Apr, 2008 - 01:13 AM
Post #7

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 356



Thanked: 12 times
Dream Kudos: 100
My Contributions
oops.. to many tabs blink.gif

This post has been edited by skaoth: 23 Apr, 2008 - 01:14 AM
User is offlineProfile CardPM
+Quote Post

hereicome
RE: Passing Arrays Through Functions
18 Dec, 2008 - 02:03 AM
Post #8

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 16

QUOTE(stezz @ 9 Dec, 2006 - 01:53 PM) *

Hi guys,

I have a question about passing arrays to functions. The program is simple, it asks the user for numbers which it stores in an array (maximum of 20). I've written a function that processes the array to calculate the minimum, maximum and average of the numbers stored in it.

As there are three different values I need the function to return, can I do it in one function, or do I need three seperate functions. Two of the three values are intergers and the third is a float.

Hope somebody can help.

Regards,

Stezz


Well, don't judge me because I am pretty new to C++, but below is my version. If you have any comments on what I was doing wrong and how you would do it. Thanks!

CODE


#include <time.h>
#include <iostream>
#include <time.h>
#include <iomanip>

using namespace std;

void fill(int *palex, int n);
void print(int *palex, int n);
int max(int *palex, int n);
int min(int *palex, int n);
float average(int *palex, int n);
void main(void)
{
    int n;
    cout<<"Enter a number of elements: ";
    cin>>n;
    cout<<endl;
    int *palex = new int[n];
    fill(palex, n);
    cout<<"Below are elements of this array: ";
    cout<<endl<<endl;
    print(palex, n);
    cout<<endl<<endl;
    cout<<"Maximum value of an array: "<<endl<<endl<<max(palex, n);
    cout<<endl<<endl;
    cout<<"Minimum value of an array: "<<endl<<endl<<min(palex, n);
    cout<<endl<<endl;
    cout<<"An average value of an array: "<<endl<<endl<<average(palex, n);
    cout<<endl<<endl;
    return;
}

void fill(int *palex, int n)
{
    srand(time(NULL));
    for (int i = 0; i < n; i++)
    {
        *(palex + i) = rand()%100;
    }
}

void print(int *palex, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout<<*(palex + i)<<", ";
    }    
}

int max(int *palex, int n)
{
    int max = *(palex + 0);
    for (int i = 0; i < n; i++)
    {
        if ((*(palex + i)) >= max)
        {
            max = (*(palex + i));
        }
        else
        {
            (*(palex + i)) = (*(palex + i));
        }
    }
    return max;
}

int min(int *palex, int n)
{
    
    int min = (*(palex + 0));
    for (int i = 0; i < n; i++)
    {
        if ((*(palex + i)) < min)
        {
            min = (*(palex + i));
        }
        else
        {
            (*(palex + i)) = (*(palex + i));
        }
    }
    return min;
}

float average(int *palex, int n)
{
    float average;
    int total = 0;
    for (int i = 0; i < n; i++)
    {
        total += *(palex + i);
    }
    average = (float)total/n;
    return average;
}




User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 08:43PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month