HelpRemove duplicated elements from input to output [Insertion Sort]

Posts 1–3 of 3 · Page 1 of 1
Remove duplicated elements from input to output [Insertion Sort]
Hi guys.
I need some help.

I need to delete DUPLICATED strings/chars from an input file.
Code:
// C Code

#include <assert.h>
#include <complex.h>
#include <ctype.h>
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <inttypes.h>
#include <iso646.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tgmath.h>
#include <time.h>
#include <wchar.h>
#include <wctype.h>

void strings_fprintfln(FILE *f, char **s, int n, const char *fmt)
{
  strings_fprintf(f, s, n, fmt);
  fprintf(f, "\n");
}
void strings_printfln(char **s, int n, const char *fmt)
{
  strings_fprintfln(stdout, s, n, fmt);
}

char *str_dup(const char *s)
{
  char *result = (char *) malloc(strlen(s) + 1);
  strcpy(result, s);
  return result;
}

int strings_readwords(FILE *f, char **a)
{
  int result = 0;
  char word[MAX_LINE_LENGTH];
  while (fscanf(f, "%s", word) != EOF)
    a[result++] = str_dup(word);
  return result;
}

int strings_getwords(char **a)
{
  return strings_readwords(stdin, a);
}

//Insertion sort
void strings_sort_last(char **a, int n)
{
  int i = n-1;
  while (i > 0 && strcmp(a[i-1], a[i]) > 0)
  {
    strings_exchange(a, i-1, i);
    i--;
  }
}
void strings_isort(char **a, int n)
{
  for (int i = 2; i <= n; i++)
    strings_sort_last(a, i);
}

void test_stringsisort(void)
{
  char *a[1000];
  int n;
  while ((n = strings_getwords(a)) != 0)
  {
    strings_isort(a, n);
    strings_printfln(a, n, " ");
    //removeDuplicates(? , ?); *I NEED THIS!!!*
    freopen("CON", "r", stdin);
    printf("------\n");
  }
}

int main(void)//int argc, string argv[])
{
  test_stringsisort();
  return 0;
}
With this code i get:

example input.txt:
Code:
Bugh
Aure
Cure
Modew
Cure
Kire
Losa
Modew
Kurds
output:
Code:
Aure
Bugh
Cure
Cure
Kire
Kurds
Losa
Modew
Modew
i need:
Code:
Aure
Bugh
Cure
Kire
Kurds
Losa
Modew
Windows cmd (folder path)>program.exe < input.txt > output.txt
btw, sometimes my output is ------------------
I actually just finished the chapter that was all about the algorithm header. This is one of the exercises

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::cin; using std::cout; using std::endl; using std::string; using std::vector;
using std::equal; using std::copy; using std::begin; using std::end;
using std::fill_n; using std::sort; using std::unique;

void remove_duplicates(vector<string> &vec)
{
	sort(vec.begin(), vec.end());  // Sort all the words so they're right next to each other in a vector
	cout << "Vector sorted....";
	auto unique_end = unique(vec.begin(), vec.end());  // I think this creates an iterator, if I remember correctly
	cout << "Unique iterator created....";
	vec.erase(unique_end, vec.end());  // Uses the .erase member from the vector type
	cout << "Duplicated words erased...." << endl;;
}

void main()
{
	vector<string> words{ "the", "brow", "cow", "says", "moo", "the", "fox", "says", "moo" };  // This would basically be your input

	remove_duplicates(words);

	for (auto i : words)  // For your output
		cout << i << " ";
	cout << '\n';
}

/* Exercise 10.9: Implement your own version of elimDups. Test your
*  program by printing the vector after you read the input, after the call to
*  unique, and after the call to erase. */
Thanks, but i cannot use vectors and that is a C++ code.
On my university, we still learn C (i dunno why)

But i will try to change the code and put this working (properly). i just need a void funtion to remove this goddamn copies!
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?