When calling a function is it possible to use the same input used in the main function for the one I am calling?

Hello everyone,
This is my first time using this forum, I dont know how Im supposed to ask questions, or how does this work. Anyways, I kinda need an explanation about my code. First of all, is it possible to call a function, and within that function read the same values as the ones inputted for the main function? I am trying to use the same values of age, for the function Im trying to call, but I dont know how to make it. Can you guys check the end of the code, I wrote there what I am trying to say here.


#include <iostream>
#include <cmath>
using namespace std;

void mode (int arr[], int size)
{
int mode [500];
int frequency, maxFrequency = {0};
int a = 1;


for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (arr[i] == arr[j])
frequency++;
}

if (frequency > maxFrequency)
{
maxFrequency = frequency;
mode [a - 1] = arr[i];
}

else if (frequency == maxFrequency && maxFrequency > 1 && arr[i] != mode [a - 1])
{
a++;
mode[a - 1] = arr[i];
}

else
{

}
frequency = 0;

}

if (maxFrequency == 1)
{
cout << "Mode is undefined. " << endl;
}

else
{
cout << "The Mode of this set of data is: " ;

for (int i = 0; i < a; i++)
{
cout << mode[i] << " ";
}

cout << "with a frequency of: " << maxFrequency << endl;
}
}


int main(int argc, const char * argv[])
{

int x, y;
double mean, sDeviation, squareSum;
int infant, young, middleAged, old, reallyOld = {0};
int sum = 0;
int age[500];


cout << "Enter a list of ages (-1 to terminate): " << endl;


for (x = 0; x <= 500; x++)
{
cin >> age[x];

if (age[x] == -1)
break;

}


for (y = 0; y < x ; y++)
{
if (age[y] >= 0 && age[y] < 18)
infant++;
if (age[y] >= 18 && age[y] < 29)
young++;
if (age[y] >= 29 && age[y] < 50)
middleAged++;
if (age[y] >= 50 && age[y] < 69)
old++;
if (age[y] >= 69)
reallyOld++;
}

cout << "Results:" << endl << endl;
cout << "0 - 18: " << infant << " people." << endl;
cout << "18 - 29: " << young << " people." << endl;
cout << "29 - 50: " << middleAged << " people." << endl;
cout << "50 - 69: " << old << " people." << endl;
cout << "69 and older: " << reallyOld << " people." << endl;


for (y = 0; y < x; y++)
sum = sum + age[y];

mean = (double)sum / x;

for (y = 0; y < x; y++)

squareSum = squareSum + pow(age[y] - mean, 2);

sDeviation = sqrt((squareSum) / (x - 1));

cout << "The Mean of this set of data is: " << mean << endl;
cout << "The Standard Deviation of this set of data is: " << sDeviation << endl;


// Here, let's say I input already 6 integers, for example, 24, 25, 26, 24, 21, 24, with these values I can calculate the mean and sdeviation, but how can I call the function mode to read these same values? This is what I am having trouble with. Hope someone can help me. Thank you in advance.


return 0;

}
closed account (j3Rz8vqX)
Use the code tags please.
[code]//Insert code here!!![/code]
> When calling a function is it possible to use the same input used in the main function for the one I am calling?

Yes. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

long long sum( const int array[], int sz )
{
    long long s = 0 ;
    for( int i = 0 ; i < sz ; ++i ) s += array[i] ;
    return s ;
}

int main()
{
    const int MAX_SIZE = 100 ;
    int actual_size = 0 ;
    int a[MAX_SIZE] ;

    std::cout << "enter a sequence of integers (end with a non-ws non-digit character):\n" ;
    while( std::cin >> a[actual_size] && actual_size < MAX_SIZE ) ++actual_size ;

    std::cout << "sum: " << sum( a, actual_size ) << '\n' ;
}
Topic archived. No new replies allowed.