Passing an array into function as parameter

Hey everyone,

I've literally spent hours working on this assignment and I just can't seem to get the last part right. Here is what the assignment reads:

Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. By removing, I mean that you should make it appear as if the elements hadn't been there. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.

Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10
The array contains: 1 2 3 4 5 6 7 8 9 10

Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9
The array contains: 1 3 6 7 8 9

Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1
The array contains: 1

The bolded area is where I'm having trouble. Can someone tell me how I can go about doing this, passing an array into the function as a parameter? Thanks so much for any help.

Here is my code:



#include "stdafx.h"
#include <iostream>
using namespace std;


int main ()

{
      const int MAX = 10;  
	  int a[MAX] = {0}; 
      int i; 
      int j; 
      int k = 0; 
      int duplicate; 
      int value; 

      cout << "Please enter your 10 numbers:" << endl;

     
      for ( i = 0; i <= MAX - 1; i++ )

	  {

         duplicate = 0;

         cin >> value;

      
		 if ( value >= 0 && value <= 100 )

		 {
            for ( j = 0; j < k; j++ )

		    {

               
               if ( value == a[ j ] )

			   {
                  duplicate = 1;
				  
                  break;
               }

          }

           
            if ( !duplicate )

		    {
		    	 a[ k++ ] = value;

            } 

		 } 

		
      } 

      cout << "The array contains:" << endl;

    
      for ( i = 0; a[i] && i < MAX != 0; i++ )

	  {

         cout << a[i] << " ";

      }

      cout << "\n";

	  system("pause");

return 0; 

}

The general syntax for a function that takes an array say int is:
void fnFunc(int arr[]);

Is your problem with this?

Aceix.
Hey Aceix,

I know about functions and arrays, its just that I don't know how to pass the array into the function as a parameter. Like, I don't know the format of how to do that for my specific code above. If you can provide me something that would fit into my code that would be much appreciated.

Thanks.
Aceix has shown you the "format" for how to pass an array into a function. In his example, it's an array of ints, but surely you can see how to change it for an array of whatever type you want to use?
"to pass" might actually refer to two things:

1. How to define that a function takes an array parameter.
void foo( int bar[], const size_t barsize )
While the function might be able to guess how many elements the bar has, it is safer to pass that info as parameter too.

2. How to call a function
1
2
3
4
const size_t gazsize = 3;
int gaz[gazsize] = {0, 4, 2};

foo( gaz, gazsize );

Here name 'gaz' refers to an array of int, and since the foo() takes array of int as first parameter, the call is syntactically correct.
Topic archived. No new replies allowed.