Goodbye Duplicates! <-How to program?

This is a discussion on Goodbye Duplicates! <-How to program? within the C++ Programming forums, part of the General Programming Boards category; Write a program that will read in N numbers, each of which is between 1 and 100, inclusive. As each ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    12

    Goodbye Duplicates! <-How to program?

    Write a program that will read in N numbers, each of which is between 1 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read.

    Specs:

    Use functions for the user inputs as well as displaying the output and array manipulation.
    Use c++ language.

    INPUT:
    The first lin of inputs is the number of inputs cases (N), which is a positive integer that does not exceed 100. the succeeding N lines specify the integers that the program reads.

    OUTPUT:
    Print out each unique element of thee array preceded by "#x - ", where x is the element number. Then, print out the elements with duplicates, in the order of their occurrence during input, separated by a space.

    This is my current code:
    Code:
    #include<iostream>
    #include<conio.h>
    #include<stdlib.h>
    
    using namespace std;
    
    int inputs(int);
    int numero[101];
    main()
    {
        int numbers;
        cin>>numbers;
        if (numbers>100 || numbers<1) { system("cls"); main(); }
        else { inputs(numbers);  }
        system("cls");
        main();
    }  
    int inputs(int numbers)
    {
         int a;
         for (int x=1; x<=numbers; x++)
        {
            cin>>numero[x];
            if (numero[x]==numero[x]) { a=numero[x]; }
            else if (numero[x]==numero[x]) { a=numero[x]; }
        }    
        cout<<"b "<<a;
    }
    Last edited by Salem; 12-14-2006 at 05:25 AM. Reason: Remove the YELLING

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    First, do you know how to solve this? Apparently you will need to use an array. Do you know how you will use an array to identify duplicates? Solve that part first before you even start coding.

    Once you are ready to code, do small pieces at a time. For example, you can start with an empty main. Then add the array you will need. Then add an empty input function, then fill it in with code to input the data into the array. Compile and test each step along the way. When you run into a specific problem, feel free to show your code and ask the question here.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    Daved... try to check my codes it is correct for the first the?? the inputs part???
    how do i compara the entered numbers so that the compiler would know which is the same and not the same??

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    system("cls"); main();
    don't call main() directly from your code

    don't use endless recursion - your stack is not infinite

    read while/do-while/for loops description and select suitable

    if (numero[x]==numero[x])
    is always true
    if you want to compare the new number with every number stored in the array - use loop
    Last edited by vart; 12-14-2006 at 12:14 PM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    if (numero[x]==numero[x]) { a=numero[x]; }
    else if (numero[x]==numero[x]) { a=numero[x]; }
    So what did you want this code to do? Come up with a plan in english (or spanish or whatever) first. Follow your plan on paper to make sure it does what you want. Only when you have your plan thought up, then you should start coding it. If you want, you can post your plan for help and critiquing here.

    As vart said, one idea is to loop through the existing numbers to see if the current number has been added already. See if you can use that idea as part of your plan, or come up with something different.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    Code:
    if (numero[x]==numero[x]) { a=numero[x]; }
    else if (numero[x]==numero[x]) { a=numero[x]; }
    is this the correct way to compare entered numbers??
    do i need to create another loop on the same function?

  7. #7
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Code:
    if (numero[x]==numero[x]) { a=numero[x]; }
    else if (numero[x]==numero[x]) { a=numero[x]; }
    Just think logically here. E.g. if I inputted 1 in numero[x] where x=1, then numero[1]=1. And in this code, you are trying to compare between numero[x] and numero[x]. So if x=1, the code will become:

    Code:
    if (numero[1]==numero[1]) { a=numero[1]; }
    else if (numero[1]==numero[1]) { a=numero[1]; }
    And based on my example (numero[1]=1), it will be:

    Code:
    if (1==1) { a=1; }
    else if (1==1) { a=1; }
    Understand it now? BTW, don't ever call a "main()" function from a code like what vart just said. Use loop instead.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> is this the correct way to compare entered numbers??
    No.

    >> do i need to create another loop on the same function?
    That is one idea.

    Come up with a plan in english (or spanish or whatever) first. Follow your plan on paper to make sure it does what you want. Only when you have your plan thought up, then you should start coding it. If you want, you can post your plan for help and critiquing here.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    hey, this is my final code for the subject::

    Code:
    #include<iostream>
    #include<conio.h>
    #include<stdlib.h>
    
    using namespace std;
    int cstay=0, cgarb=0;
    int numero[100];
    int io(int);
    main()
    {
        int numbers;
        cin>>numbers;
        if (numbers>100 || numbers<1) { system("cls"); main(); }
        else { io(numbers);  }
        getch();
    }  
    int io(int numbers)
    {
        int garb[cgarb], stay[cstay], temp=0;
         for (int x=0; x!=numbers; x++)
        {
            cin>>numero[x];
        }    
        for (int y=0; y!=numbers; y++)
        {
            if (temp==numero[y]) {  garb[cgarb]=numero[y]; cgarb++; }
            else { stay[cstay]=numero[y]; cstay++;}
            temp=numero[y];
        }
        system("cls");
        for (int a=0; a!=cstay; a++)
        {
            
            cout<<"#"<<a+1<<" - "<<stay[a]<<endl; 
        }
        cout<<"\nDuplicates are: ";
        for (int b=0; b!=cgarb; b++)
        {
            cout<<" "<<garb[b]; 
        }
        
    }

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    You still have errors, some of them already were mentioned.

    1. don't call main directly
    2. don't use global vars
    3. don't use arrays of length 0
    4. don't use memmory out of bound of an array
    5. don't use conio.h
    6. don't forget return statement in the function returning int
    7. test your code. Do you think it is working?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Your definition of main is incorrect. It must be preceeded by int.

  12. #12
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    If it weren't for the specification "show array manipulation", you could just use std::list (or vector) with a search algorithm. Unfortunately, you can't. But I'm bored so I'll write this anyway:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <list>
    #include <algorithm>
    
    int get_num();   //returns number
    bool print_num(int num,std::list<int> & nums);   //true if it prints
    
    int main(/*stuff*/)
    {
       std::list<int> nums;
       int num_of_num;
    
       std::cout << "How many will you be giving me?" << std::endl;
       std::cin >> num_of_num;
    
       int num;
       for(int c = 0; c < num_of_nums; c++) 
       {
          num = get_num();   //won't return until valid number is entered
          if( print_num(num,nums) ) nums.push_back(num);
          //if the number prints, then it is new, so add it to the list
       }
    
       std::system("PAUSE");
       return EXIT_SUCCESS;
    }
    
    int get_num()
    {
       int num;
    
       do {
          std::cin >> num;
       }while(num < 1 || num > 100);
    
       return num;
    }
    
    bool print_num(int num,std::list<int> & nums)
    {
       if(std::binary_search(nums.begin(), nums.end(), num)) return false;
       else std::cout << '\t' << num << std::endl;
       return true;
    }
    Last edited by CodeMonkey; 01-04-2007 at 01:14 AM. Reason: redundancy
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,430
    CodeMonkey, I am afraid your example has at least two problems. Firstly, you declare num_of_num but use num_of_nums in the for loop in main(). The other problem is that the list of numbers is not sorted, but you attempt a binary search on it.

    Why use a std::list at all? From what I understand, std::vector is usually the default container, but here we can use a std::set and make use of its find() member function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Whoops. You're right. And sorting it every time would make this program the absolute least efficient way to do this. There you go -- that's what happens when I don't follow the assignment.
    Oh, list because push_back doesn't require reallocation (in a vague sense of the term). I suppose vector with a healthy reserve would be better, wouldn't it.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Actually, the re-allocation done by vector is quite efficient even without the use of reserve (see the last paragraph here: http://www.research.att.com/~bs/bs_f...low-containers).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21