Thread: Duplicate Elimination Program

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    Duplicate Elimination Program

    Use a single-subscripted array to solve the following problem.
    Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print
    it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20
    numbers are different. Use the smallest possible array to solve this problem.
    The code I wrote so far:

    Code:
    #include <stdio.h>#include <conio.h>
    
    
    int main() {
    	int num[20];
    	printf("Enter 20 numbers:\n");
    
    
    	for (int i=0; i<20; i++) {
    		scanf_s("%d", num[i]);
    		if (num[i]>10 && num[i]<100) {
    			for (int j=0; j<=i; j++) {
    				if (num[i]==num[j]) {
    					printf("Duplicate\n");
    				}
     			}
    		}
    	}
    	
    	_getch();
    	return 0;
    }
    The code breaks after I input the first value. What's wrong.

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Lots. conio.h. _getch();. scanf_s() instead of scanf(), not checking its return value, and specifying an integer rather than a pointer to a integer to store the result to. You check the input number, but do nothing if it is wrong. You compare the just-read integer to itself to find it a duplicate of itself. Although you were asked to print each number only on the first time it is seen, you instead print "Duplicate" for each duplicate.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Another thing:

    Read in 20 numbers, each of which is between 10 and 100, inclusive.
    Code:
    if (num[i]>10 && num[i]<100)
    That's not quite right.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    I use _getch() and scanf_s() because my compiler updated and won't let me use the other version of these.

    What about this? Still doesn't work and breaks

    Code:
    #include <stdio.h>
    #include <conio.h>
     
     
    int main() {
        int num[20]; bool found;
        printf("Enter 20 numbers:\n");
     
     
        for (int i=0; i<20; i++) {
            found=false;
            scanf_s("%d", num[i]);
            if (num[i]>=10 && num[i]=<100 && (num[i]<'0' || num[i]>'9')) { 
                for (int j=0; j<i; j++) {
                    if (i!=j && num[i]==num[j]) {
                        found=true;
                        break;
                    } 
                    if (found==false) {
                        printf("%d ", num[i]);
                    }
                 }
            }
        }
         
        _getch();
        return 0;
    }
    Matticus corrected, thanks.

    I'm not sure whether checking the type of input is right or not.
    Last edited by lmanukyan; 01-13-2016 at 08:19 AM. Reason: mistake

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by lmanukyan View Post
    I use _getch() and scanf_s() because my compiler updated and won't let me use the other version of these.

    What about this? Still doesn't work and breaks

    Matticus corrected, thanks.

    I'm not sure whether checking the type of input is right or not.
    If there was a time to check the input, it was when scanf_s() was returning. (Are you required to use scanf_s()? If it isn't a school rule, you ought to #define _CRT_SECURE_NO_DEPRECATE) You could, for example, do this for each num:
    Code:
    for (;;) {
        int ch, result = scanf_s ("%d%c", &num[i], &trail);
        if (result != 2 || trail != '\n') {
           printf ("Please enter a valid integer.\n");
           while ((ch = getchar()) != '\n' && ch != EOF) ;
        }
        else break;
    }
    Code:
    (num[i]<'0' || num[i]>'9')
    As num[i] is an integer, not a character, checks like this are misleading.

    The idea would be to process the input in stages. After the above scanf_s() code succeeds, then you can do this:
    Code:
     num[i] >= 10 && num[i] <= 100
    and it would be sufficient.
    Last edited by whiteflags; 01-13-2016 at 09:32 AM. Reason: OR, not AND

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gaussian Elimination
    By jhosie_g in forum C Programming
    Replies: 1
    Last Post: 11-13-2015, 01:39 AM
  2. Gaussian Elimination program
    By whatever125 in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2011, 05:11 AM
  3. Replies: 7
    Last Post: 07-28-2009, 03:15 PM
  4. Gaussian elimination in C
    By Meander14 in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 03:43 AM
  5. Gaussian Elimination
    By Fiverz in forum C Programming
    Replies: 1
    Last Post: 02-05-2003, 12:59 PM