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;
}
|