this program is supposed to sort an input list and then remove all duplicates.
so I'm not quite sure what is wrong with my program, but the output for the original list is a very long list of the same random number(it doesn't change with the input numbers) and the output list is weird as well.

Here is the code:

/*This program removes duplicates from a sorted array of integers*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
#include "math.h"

#define size 100
#define sentinel 0

void getArray(int arr[size]);
int maxCycles(int arr[size]);
void sortArray(int arr[size],int cycles);
int indexMin(int arr[size],int low,int high);
void swap(int arr[size],int loc1,int loc2);
int removedup(int inpArray[size],int inpArraySize,int outArray[size]);
 

/*Main Program*/
int main()
{
 int inpArray[size],outArray[size];
 int n,q,i;
 
 printf("Please enter a list of integers.\n");
 printf("Indicate the end of list by entering 0.\n");
 getArray(inpArray);
 n=maxCycles(inpArray);
 sortArray(inpArray,n);
 q=removedup(inpArray,n+1,outArray);
 
 /*Print Input Array*/
 printf("\nThe list you entered was:\n{");
 for (i=0;i<n;i++)
 {
  printf("%d, ",inpArray[i]);
 }
 printf("%d}\n",inpArray[n+1]);
 
 /*Print Output Array*/
 printf("\nThe list without repeats is:\n{");
 for (i=0;i<q-1;i++)
 {
  printf("%d, ",outArray[i]);
 }
 printf("%d}\n",outArray[q-1]);
}
void getArray(int arr[size])
{
	int i,n;
	n=1;
	i=0;
	while(n!=0)
	{
		printf("Enter next integer: ");
		arr[i]=GetInteger();
		n=arr[i];
		i++;
	}
}
int maxCycles(int arr[size])
{
	int count,i;
	count=0;
	for(i=0;i<size;i++)
	{
		if (arr[i]!=0) count++;
	}
	return count;
}
void sortArray(int arr[size],int cycles)	
{		
	int i, minInd;	
	for (i=0;  i<3; i++)	
	{	
		minInd = indexMin (arr, i, size-1);
		swap (arr,i,minInd );
	}	
}		

int indexMin(int arr[size],int low,int high)
{		
	int i, minInd;	
	minInd=low;	
	for (i=low; i<=high; i++)	
	{	
		if (arr[i] < arr[minInd]) minInd =i;
	}	
	return (minInd);	
}
void swap(int arr[size],int loc1,int loc2)
{		
	int temp;
	temp=arr [loc1];
	arr [loc1]=arr[loc2];
	arr [loc2] = temp;
}	

int removedup(int inpArray[size],int inpArraySize,int outArray[size])
{
 int i,n;
 n=1;
 
 for (i=1;i<=inpArraySize;i++)
 {
  if (inpArray[i]!=inpArray[i-1])
  {
   outArray[n-1]=inpArray[i-1];
   n++;
  }
 }
 outArray[n-1]=inpArray[i-1];
 
 return n;
}

any help would be greatly appreciated. Thanks!

Recommended Answers

All 11 Replies

*Sorry for posting again. Im not sure how to edit. here is my post with the code as per specifications.
this program is supposed to sort an input list and then remove all duplicates.
so I'm not quite sure what is wrong with my program, but the output for the original list is a very long list of the same random number(it doesn't change with the input numbers) and the output list is weird as well.

Here is the code:

/*This program removes duplicates from a sorted array of integers*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
#include "math.h"

#define size 100
#define sentinel 0

void getArray(int arr[size]);
int maxCycles(int arr[size]);
void sortArray(int arr[size],int cycles);
int indexMin(int arr[size],int low,int high);
void swap(int arr[size],int loc1,int loc2);
int removedup(int inpArray[size],int inpArraySize,int outArray[size]);


/*Main Program*/
int main()
{
int inpArray[size],outArray[size];
int n,q,i;

printf("Please enter a list of integers.\n");
printf("Indicate the end of list by entering 0.\n");
getArray(inpArray);
n=maxCycles(inpArray);
sortArray(inpArray,n);
q=removedup(inpArray,n+1,outArray);

/*Print Input Array*/
printf("\nThe list you entered was:\n{");
for (i=0;i<n;i++)
{
printf("%d, ",inpArray[i]);
}
printf("%d}\n",inpArray[n+1]);

/*Print Output Array*/
printf("\nThe list without repeats is:\n{");
for (i=0;i<q-1;i++)
{
printf("%d, ",outArray[i]);
}
printf("%d}\n",outArray[q-1]);
}
void getArray(int arr[size])
{
int i,n;
n=1;
i=0;
while(n!=0)
{
printf("Enter next integer: ");
arr[i]=GetInteger();
n=arr[i];
i++;
}
}
int maxCycles(int arr[size])
{
int count,i;
count=0;
for(i=0;i<size;i++)
{
if (arr[i]!=0) count++;
}
return count;
}
void sortArray(int arr[size],int cycles)	
{	
int i, minInd;	
for (i=0; i<3; i++)	
{	
minInd = indexMin (arr, i, size-1);
swap (arr,i,minInd );
}	
}	

int indexMin(int arr[size],int low,int high)
{	
int i, minInd;	
minInd=low;	
for (i=low; i<=high; i++)	
{	
if (arr[i] < arr[minInd]) minInd =i;
}	
return (minInd);	
}
void swap(int arr[size],int loc1,int loc2)
{	
int temp;
temp=arr [loc1];
arr [loc1]=arr[loc2];
arr [loc2] = temp;
}	

int removedup(int inpArray[size],int inpArraySize,int outArray[size])
{
int i,n;
n=1;

for (i=1;i<=inpArraySize;i++)
{
if (inpArray[i]!=inpArray[i-1])
{
outArray[n-1]=inpArray[i-1];
n++;
}
}
outArray[n-1]=inpArray[i-1];

return n;
}

any help would be greatly appreciated. Thanks!

Does it look like that in your code editor?
Or did you just lazily copy your previous post.

Because without indentation, adding code tags doesn't buy much.

once again, sorry. here's the code:

/*This program removes duplicates from a sorted array of integers*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
#include "math.h"

#define size 100
#define sentinel 0

void getArray(int arr[size]);
int maxCycles(int arr[size]);
void sortArray(int arr[size],int cycles);
int indexMin(int arr[size],int low,int high);
void swap(int arr[size],int loc1,int loc2);
int removedup(int inpArray[size],int inpArraySize,int outArray[size]);
 

/*Main Program*/
int main()
{
 int inpArray[size],outArray[size];
 int n,q,i;
 
 printf("Please enter a list of integers.\n");
 printf("Indicate the end of list by entering 0.\n");
 getArray(inpArray);
 n=maxCycles(inpArray);
 sortArray(inpArray,n);
 q=removedup(inpArray,n+1,outArray);
 
 /*Print Input Array*/
 printf("\nThe list you entered was:\n{");
 for (i=0;i<maxCycles(inpArray);i++)
 {
  printf("%d, ",inpArray[i]);
 }
 printf("%d}\n",inpArray[n+1]);
 
 /*Print Output Array*/
 printf("\nThe list without repeats is:\n{");
 for (i=0;i<maxCycles(outArray);i++)
 {
  printf("%d, ",outArray[i]);
 }
 printf("%d}\n",outArray[q-1]);
}
void getArray(int arr[size])
{
	int i,n;
	n=1;
	i=0;
	while(n!=0)
	{
		printf("Enter next integer: ");
		arr[i]=GetInteger();
		n=arr[i];
		i++;
	}
}
int maxCycles(int arr[size])
{
	int count,i;
	count=0;
	for(i=0;i<size;i++)
	{
		if (arr[i]!=0) count++;
	}
	return count;
}
void sortArray(int arr[size],int cycles)	
{		
	int i, minInd;	
	for (i=0;  i<3; i++)	
	{	
		minInd = indexMin (arr, i, size-1);
		swap (arr,i,minInd );
	}	
}		

int indexMin(int arr[size],int low,int high)
{		
	int i, minInd;	
	minInd=low;	
	for (i=low; i<=high; i++)	
	{	
		if (arr[i] < arr[minInd]) minInd =i;
	}	
	return (minInd);	
}
void swap(int arr[size],int loc1,int loc2)
{		
	int temp;
	temp=arr [loc1];
	arr [loc1]=arr[loc2];
	arr [loc2] = temp;
}	

int removedup(int inpArray[size],int inpArraySize,int outArray[size])
{
 int i,n;
 n=1;
 
 for (i=1;i<=inpArraySize;i++)
 {
  if (inpArray[i]!=inpArray[i-1])
  {
   outArray[n-1]=inpArray[i-1];
   n++;
  }
 }
 outArray[n-1]=inpArray[i-1];
 
 return n;
}

Im not sure how this works, i paswted in my text, but the post still doesn't have indents.

Took me 5 minutes to indent your code

/*This program removes duplicates from a sorted array of integers*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
#include "math.h"

#define size 100
#define sentinel 0

void getArray(int arr[size]);
int maxCycles(int arr[size]);
void sortArray(int arr[size],int cycles);
int indexMin(int arr[size],int low,int high);
void swap(int arr[size],int loc1,int loc2);
int removedup(int inpArray[size],int inpArraySize,int outArray[size]);


/*Main Program*/
int main()
{
	int inpArray[size],outArray[size];
	int n,q,i;

	printf("Please enter a list of integers.\n");
	printf("Indicate the end of list by entering 0.\n");

	getArray(inpArray);
	n=maxCycles(inpArray);
	
	sortArray(inpArray,n);
	q=removedup(inpArray,n+1,outArray);

	/*Print Input Array*/
	printf("\nThe list you entered was:\n{");	

	for (i=0;i<maxCycles(inpArray);i++) {
		printf("%d, ",inpArray[i]);
	}
	
	printf("%d}\n",inpArray[n+1]);

	/*Print Output Array*/
	printf("\nThe list without repeats is:\n{");

	for (i=0;i<maxCycles(outArray);i++) {
		printf("%d, ",outArray[i]);
	}
	
	printf("%d}\n",outArray[q-1]);
}

void getArray(int arr[size])
{
	int i,n;
	
	n=1;
	i=0;
	
	while(n!=0) {
		printf("Enter next integer: ");
		arr[i]=GetInteger();
		n=arr[i];
		i++;
	}
	
}

int maxCycles(int arr[size])
{
	int count,i;
	count=0;
	
	for(i=0;i<size;i++) {
		if (arr[i]!=0) count++;
	}
	return count;
}

void sortArray(int arr[size],int cycles)
{
	int i, minInd;
	
	for (i=0; i<3; i++) {
		minInd = indexMin (arr, i, size-1);
		swap (arr,i,minInd );
	}
}

int indexMin(int arr[size],int low,int high)
{
	int i, minInd;
	minInd=low;
	
	for (i=low; i<=high; i++) {
		if (arr[i] < arr[minInd]) minInd =i;
	}
	
	return (minInd);
}
void swap(int arr[size],int loc1,int loc2)
{
	int temp;
	
	temp=arr [loc1];
	arr [loc1]=arr[loc2];
	arr [loc2] = temp;
}

int removedup(int inpArray[size],int inpArraySize,int outArray[size])
{
	int i,n;
	n=1;

	for (i=1;i<=inpArraySize;i++) {
		if (inpArray[i]!=inpArray[i-1]) {
			outArray[n-1]=inpArray[i-1];
			n++;
		}
	}
	
	outArray[n-1]=inpArray[i-1];

	return n;
}

> Im not sure how this works, i paswted in my text, but the post still doesn't have indents.
Well this time, you got \code at the end, not /code.

There's a button at the bottom called "preview post"
USE it to make sure everything looks OK, and don't be in such a hurry to press submit.
Wasted too much time on this already.

Member Avatar for iamthwee

>Took me 5 minutes to indent your code

You wasted 5 minutes!?

1) You could have just clicked the quote button on his post to get the indented code.
2) You could have just ran it through an automatic beautifier such as astyle?

To the OP stick a load of printfs in strategic places and please avoid the use of all those non-standard header files, or include their libraries because we can't compile it.

commented: *nods* +19

>Took me 5 minutes to indent your code
1) You could have just clicked the quote button on his post to get the indented code.
2) You could have just ran it through an automatic beautifier such as astyle?

Ouch! Didn't know this trick before

There is a std::sort, and std::unique function. That should solve your
problem. std::sort -- sorts data, std::unique -- removes duplicates
from a sorted data.

Thanks, I got the program working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.