passing arrays

OK so just learned about arrays in class. My new assignment needs to pass arrays that i have the user enter to a function. How can i get a function to accept 2 arrays at a time? such as one array being string and another being a double?
** ignore the missing code in if/else statement i am preparing for more functions there.**

as you can see im trying to get an array to pass to a function and in that function have it state the name and time of skiers entered.
Also in fastestTime function im having same problem needing the name and time posted.

using namespace std;

void menu();
void skierList();
double fastestTime(double skierTime[]);
const int ARRAYSIZE = 5;
int choice;

int main()
{
string skierName[ARRAYSIZE];
double skierTime[ARRAYSIZE];

int skierNum = 1;
cout << "Please enter the names of Skier's in order as the compete." << endl;
for (int i = 0; i < ARRAYSIZE; i++)
{
cout << "Skier: " << skierNum << " "; cin >> skierName[i];
skierNum++;
}
cout << "Please enter the Skier's run time." << endl;
for (int i = 0; i < ARRAYSIZE; i++)
{
cout << skierName[i] << " run time is : "; cin >> skierTime[i];
}
std::cout << "\n";
menu();
std::cout << "\n";
while (choice >= 1 && choice <= 4)
{
if (choice == 1)
{
double fastestSkier = fastestTime(skierTime);
cout << "The fastes skier is "<< "with a time of " << fastestSkier << endl;
std::cout << "\n";
menu();
std::cout << "\n";

}
else if (choice == 2)
{

}
else if (choice == 3)
{

}
else if (choice == 4)
{
skierList(skierName,skierTime);
}
}


system("pause");
return 0;
}
// Main menu
void menu()
{
cout << "*******************************************************" << endl;
cout << "ENTER 1 TO DETERMINE FASTER SKIER." << endl;
cout << "ENTER 2 TO CALCULATE THE AVERAGE TIME OF ALL SKIER'S." << endl;
cout << "ENTER 3 TO FIND THE TIME OF A SPECIFIC SKIER." << endl;
cout << "ENTER 4 TO DISPLAY A LIST OF SKIER'S THAT COMPETED." << endl;
cout << "TO EXIT ENTER ANY OTHER #." << endl;
cout << "*******************************************************" << endl;
cin >> choice;
}

// Find Fastest Time
double fastestTime(double skierTime[])
{
double fastest = skierTime[0];
if (skierTime[1] < fastest)
fastest = skierTime[1];
if (skierTime[2] < fastest)
fastest = skierTime[2];
if (skierTime[3] < fastest)
fastest = skierTime[3];
if (skierTime[4] < fastest)
fastest = skierTime[4];
return fastest;
}
void skierList(string skierName[], double skierTime[])
{
for (int i = 0; i < ARRAYSIZE; i++)
{
cout << "Skier name: " << skierName[i] << " with a time of " << skierTime[i] << endl;
}
}
Please put your code in [code] tags.

To pass an array to a function, you need two pieces of information: address of the first element, and number of elements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void print( int a[], int size )
{
  if (size == 0) return;
  for (int n = 0; n < (size-1); n++)
    std::cout << a[n] << ", ";
  std::cout << a[size-1] << "\n";
}

int main()
{
  int xs[] = { 2, 3, 5, 7, 11, 13, 17 };
  print( xs, 7 );
}

You need both those pieces of information for each array you wish to pass to a function. If the arrays have the same size, you can use that to your advantage:

1
2
3
4
5
void pairwise_sum( int a[], int b[], int size )
{
  for (int n = 0; n < size; n++)
    a[n] += b[n];
}


However, if I may make a suggestion: related information should be in a struct:

1
2
3
4
5
6
struct Skier
{
  string name;
  double time;
  Skier( const string& name, double time = 0.0 ): name(name), time(time) { }
};

Now you can make an array of skiers:

1
2
3
4
5
6
7
8
Skier skiers[ MAX_NUM_SKIERS ];
int num_skiers = 0;

skiers[ num_skiers++ ] = Skier( "Speedy Joe",     12.7 );
skiers[ num_skiers++ ] = Skier( "Quick Nick",     11.9 );
skiers[ num_skiers++ ] = Skier( "Miriam Mercury", 12.2 );

some_function( skiers, num_skiers );

Hope this helps.
thanks for the help i see where i was going wrong.

on the subject of STRUCTS i can only use what we have learned in class and we haven't got to structs. the instructor only allows what we have learned so he see's that we understand how each piece works.
closed account (48T7M4Gy)
Of course, passing the size with the array is not the only way. A termination element is another way of passing an array to a function where an element or range of elements are to be processed.
So passing the array is solved. But, how do i pass 2 arrays to same function with one being string and the other is a double?
Exactly the same as you would have any other function with two parameters.
void someFunction(string skierNames[], double skierTimes[])
For the record, calling an array of names skiername, makes it sound like one name, as if you were mixing up a char array and a string. I now realise that an array of strings is right, but that was confusing.
void someFunction(string skierNames[], double skierTimes[], int size);
Sorry, I thought they were using a global const int so they didn't have to pass the size (not saying it's a good idea). Although if you do want to do it that way wouldn't a constexpr be better?
Probably.

constexpr means that the const value is never volatile -- meaning the compiler can actually make it disappear if it wants, and do all kinds of nifty compile-time stuff with it (like use its value).

The problem is that there may be fewer skiers than there is room to store them. And size refers to the (mutable) number of skiers, and not the actual size of the arrays.

1
2
3
4
5
6
7
8
9
constexpr MaxNumSkiers = 100;
string skierNames[ MaxNumSkiers ];
int    skierTimes[ MaxNumSkiers ];
int    numSkiers = 0;

skierNames[ numSkiers   ] = "Henrietta";
skierTimes[ numSkiers++ ] = 12.8;

someFunction( skierNames, skierTimes, numSkiers );

Hope this helps.
Topic archived. No new replies allowed.