InpArray
50 60 70 70 72 80 85 85 90 100
then the output array will be:
OutArray
50 60 70 72 80 85 90 100
Your program should include a function named removedup that returns the new size of the array (without the duplicates). For this input array:
removedup (inpArray, inpArraySize, outArray) --> 8
In addition, your program should display both the input array and the output array."
I think I've got code that would work - except for one thing: using variables for the size of the arrays. I know it's incorrect, but I tried it anyway. Whadda ya know: it didn't work.
// removeD.cpp : This program will remove duplicates from an integer array, displaying the output in another array.
// -----------
#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include "stdio.h"
void initInArr(int inArr[], int inpArrSize);
int removeDup(int inArr[], int inpArrSize);
void compOutArr(int inArr[], int outSize, int inpArrSize);
main()
{
printf("This program will eliminate duplicates from a set of numbers, and will display both sets.");
int outSize, inpArrSize;
inpArrSize = 10;
printf("Array size? ");
inpArrSize = GetInteger();
int inArr[inpArrSize];
initInArr(inArr, inpArrSize);
outSize = removeDup(inArr, inpArrSize);
compOutArr(inArr, outSize, inpArrSize);
getchar();
}
//This function will intialize the input array (inArr[]) and will sort the numbers.
void initInArr(int inArr[], int inpArrSize)
{
int i, count, temp;
//Enter nums.
printf("Enter the numbers of the array (enter after each):\n");
for(i=0; i<inpArrSize; i++)
{
inArr[i] = GetInteger();
}
//Sort nums. using bubble sort.
while(true)
{
count = 0;
for(i=0; i<(inpArrSize-1); i++)
{
if (inArr[i] > inArr[(i+1)])
{
temp = inArr[i];
inArr[i] = inArr[(i+1)];
inArr[(i+1)] = temp;
count++;
}
temp = 0;
}
if (count == 0) break;
}
}
//This function will determine how many numbers will be in the output array (outArr[]) by returning the size of outArr (outSize).
int removeDup(int inArr[], int inpArrSize)
{
int i, count;
count = inpArrSize;
for(i=0; i<(inpArrSize-1); i++)
{
if(inArr[i] == inArr[(i+1)]) count--;
}
return(count);
}
//This function will input the non-duplicates into outArr[] and display both arrays.
void compOutArr(int inArr[], int outSize, int inpArrSize)
{
int outArr[outSize];
int i, j;
j = 0;
for(i=0; i<(inpArrSize-1); i++)
{
if(inArr[i]!=inArr[(i+1)]) outArr[j] = inArr[i];
}
//Display!!
printf("The input array was:\n");
for(i=0; i<inpArrSize; i++)
{
printf("%d\t", inArr[i]);
}
printf("\n\nThe output array is:\n");
for(i=0; i<outSize; i++)
{
printf("%d\t", outArr[i]);
}
}
This post has been edited by SPlutard: 11 September 2005 - 07:06 PM

New Topic/Question
Reply



MultiQuote




|