I got not one but two good answers to my last question, which was how to make a function that would both create an array and fill it. I was surprised to find that the first one of them was so simple - I had been making it harder than it needed to be. But if both do the job, when is one better than the other? Anyone want to tackle that?

void  fillArray();
int main()
{  fillArray();
   return 0;  }  //End main fn
//*************************************************
//Function definition 
void fillArray()//No args or return values
{  const int SIZE = 3;
   int num[SIZE];//Create the array and size
   cout  << "Please enter three integers: ";
   for (int i = 0; i < SIZE; i ++)
      cin   >> num [i];

   cout  << "Here are your three integers: ";
   for (int i = 0; i < SIZE; i ++)
      cout   << num [i]  << " ";  } //End fillArray
//================================
//Another way to do the same thing; which is better, and in what situations?
#include <iostream>
using namespace std;

void  fillArray(int[], const int);
void  displayArray(int[], const int);

int main()
{  const int SIZE = 3;
   int num[SIZE];

   fillArray(num, SIZE);
   displayArray(num, SIZE);

   return 0; }
//****************** Fn definitions
void fillArray(int arr[], const int sz)
{    cout  << "Please enter three integers: ";
     for (int i=0; i < sz; i++)
        cin>>arr[i];   }  // End fillArray fn

void displayArray(int arr[], const int sz)
{   cout  << "Here are your three integers: ";
    for (int i=0; i < sz; i++)
      cout  << arr[i]   << " ";  } //End displayArray

Recommended Answers

All 2 Replies

1) use Code tags.

2) The second one. Its more reusable.

Thanks - Sorry about forgetting the code tags; still new at this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.