I have basically written the whole program but at the end of my program i wrote a function that will count duplicates in my array. Supposedly I am doing something wrong with that function.
My output is displayed as: [ this is wrong]
C:UsersambarDesktopproggram2prog>program2 number.txt
-21 -8 -8 2 3 3 5 5 23 99
99 99 99 100 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Duplicates: -21
C:UsersambarDesktopproggram2prog>
My output needs to be:running you program will show:
2 5 3 99 -85 99 99 100 -823 -21 3 99 5
C:cs222>prog2 numbers.txt-21 (1)-8 (2)2 (1)3 (2)5 (3)23 (1)99 (4)100 (1)
My code is:
Code:#include <stdio.h> #include <stdlib.h> int length=0; int array[1000]; int compare(const void *s, const void *t); int i,k; FILE * openinput(int argc, char * argv[]); void new_func(); int main (int argc, char * argv[]) { FILE * file = openinput(argc, argv); int total = 0; while (!feof(file)) { int number; fscanf(file, "%d", &number); if(!feof(file)) array[length++] = number; } fclose(file); qsort(array, length, sizeof(int), compare); new_func(); system("PAUSE"); } FILE * openinput(int argc, char * argv[]) { if (argc != 2) { printf("usage :sum filename \n"); exit(1); } FILE * returnvalue; returnvalue = fopen(argv[1], "r"); if (! returnvalue) { printf("unable to open %s \n", argv[1]); exit(1); } return returnvalue; } int compare(const void *s, const void *t) { return *(const int *)s - *(const int *)t; } void new_func() { for (i=0; i < 1000; i++) printf("%d \t", array[i]); }