7 Replies - 3286 Views - Last Post: 12 September 2005 - 06:45 AM Rate Topic: -----

#1 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 17
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Is there any way to allow the user to determine...

Posted 11 September 2005 - 06:34 PM

Ok. I've got an assignment to "Write a program that removes all duplicate values from a sorted array of integers, leaving only a single copy of each value. For example if the input array is inpArray with the following values:
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. :sleepy: Is there any alternative (in C) that I could use to make this work? Any suggestions otherwise are also welcome.... :) Code:

// 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


Is This A Good Question/Topic? 0
  • +

Replies To: Is there any way to allow the user to determine...

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Is there any way to allow the user to determine...

Posted 11 September 2005 - 07:14 PM

I don't have a compiler handy...what is the problem exactly?
Was This Post Helpful? 0
  • +
  • -

#3 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 17
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: Is there any way to allow the user to determine...

Posted 11 September 2005 - 07:29 PM

You can't declare arrays with non-constants (like variables). So, when I say:
int inArr[Size]
it's wrong because 'Size' isn't a constant. I can't use a constant (ex.: #define whatever 10), though, because the size of the 2nd array has to be dynamic. There's no way to know how big it's going to be, so its size has to be a variable, right? I dunno what to do.
Was This Post Helpful? 0
  • +
  • -

#4 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: Is there any way to allow the user to determine...

Posted 11 September 2005 - 07:52 PM

use dynamic arrays then
http://cplusplus.com...ial/tut3-4.html
Was This Post Helpful? 0
  • +
  • -

#5 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 17
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: Is there any way to allow the user to determine...

Posted 11 September 2005 - 07:56 PM

I appreciate it, but I need something for C, and not C++. The C info on that site is too advanced for me, and my teacher'll think I cheated or something. Sorry, but thanks.
Was This Post Helpful? 0
  • +
  • -

#6 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: Is there any way to allow the user to determine...

Posted 11 September 2005 - 08:07 PM

That can be accomplished using malloc and or calloc

http://www.cs.cf.ac....e/C/node11.html
Was This Post Helpful? 0
  • +
  • -

#7 sridhar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-September 05

Re: Is there any way to allow the user to determine...

Posted 12 September 2005 - 12:55 AM

Hello friend ,
;) You can have a variable size array .
The code for this is ,
int main()
{
const int i=GetInt();
int array[i];
}
int GetInt()
{
int i;
scanf("%d",&i);
return i;
}



This code will be compiled by newer versions of gcc compiler.
C99 standard supprots this variable length array declarations.

Have a nice time...:blink:

-----------Sridhar ;)
Was This Post Helpful? 0
  • +
  • -

#8 SPlutard  Icon User is offline

  • code.ninja
  • member icon

Reputation: 17
  • View blog
  • Posts: 615
  • Joined: 13-August 05

Re: Is there any way to allow the user to determine...

Posted 12 September 2005 - 06:45 AM

Ok, guys. Thanks! I appreciate it. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1