Entire array as argument example

Hi cplusplus.com forum. Could someone help me build this code into anything simple so that it works please.


#include <iostream>
using namespace std;

int main()
{
void fillUp(int a[], int size);
// Precondition: size is the declared size of the array a.
// The user will type in size integers.
// Postcondition: The array a is filled with size integers
// from the keyboard.

void fillUp(int a[], int size)
{
cout << "Enter " << size << " numbers:\n";
for (int i = 0; i < size; i++)
cin >> a[i];
cout << "The last array index used is " << (size - 1) << endl;
}
}

// Try to get this to work.
I would separate the code that provides entering of the array size and the code that provides filling of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int get_size()
{
   std::cout << "Enter array size: ";
   int n;

   if ( ! ( std::cin >> n ) ) n = 0;

   return ( n );
}

int * create_array( int n )
{
   int *a = new intw[n];

   std::cout << "Enter " << n << " integer numbers: ";
   for ( int int i = 0; i < n; i++ ) std::cin >> a[i];

   return ( a );
}


and in the main they used the following way

1
2
3
int *a = create_array( get_size() );

delete [] a;


However as your array is declared in main then your code will look


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

void fillUp( int a[], int size );
 
int main()
{
   const int N = 10;
   int a[N];

   fillUp( a, N );

   return 0;
}

 // Precondition: size is the declared size of the array a.
 // The user will type in size integers.
 // Postcondition: The array a is filled with size integers
 // from the keyboard.
 
void fillUp( int a[], int size )
{
   cout << "Enter " << size << " numbers:\n";
   for (int i = 0; i < size; i++)   cin >> a[i];

   cout << "The last array index used is " << (size - 1) << endl;
}
Last edited on
You cannot declare functions inside other functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    void fillUp(int a[], int size); // Wrong!
    void fillUp(int a[], int size)  // Wrong!
    {
        cout << "Enter " << size << " numbers:\n";
        for (int i = 0; i < size; i++)
        cin >> a[i];
        cout << "The last array index used is " << (size - 1) << endl;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
void fillUp(int a[], int size); // Right!
void fillUp(int a[], int size)  // Right!
{
    cout << "Enter " << size << " numbers:\n";
    for (int i = 0; i < size; i++)
    cin >> a[i];
    cout << "The last array index used is " << (size - 1) << endl;
}
int main()
{
    //...
}
@EssGeEich

You cannot declare functions inside other functions:



It is a wrong statement. Any function may be declared in another function. A function may not be defined in other function.

So the code

1
2
3
int main()
{
    void fillUp(int a[], int size);

is valid
It is a wrong statement. Any function may be declared in another function. A function may not be defined in other function.

Didn't know about that, gonna trust you on the word, but he still defined the function into main...
Topic archived. No new replies allowed.