Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. print it only if it is not a duplicate [modified][solved]

print it only if it is not a duplicate [modified][solved]

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestionlounge
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Omegaclass
    wrote on last edited by
    #1

    what is wrong with my code it's kicking me in the head right now and if a kind soul could help me out i would be very grateful, thanks. here is the goal: Use a one dimensional 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 is 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.Well,

    #include <iostream>
    #include <iomanip>
    #include <ctime>
    #include <cstdlib>
    #include <limits>
    const int x = 20;
    using namespace std;

    int main()

    {

                  int i = 0;
                  int toss = 0;
                  int count=0;
                  int population\[20\];
                  int num=0;
                  bool duplicate = false;
                  
    
    	      while(1)
              { 
                  			   
                  for (int j = 0; j < x; j++)    // test for duplicate random num
                  {
                     
                      srand(time(NULL));
                      num = rand()%90+11;
                      
                    if (num == population\[j\])
    		    
                    {
    				duplicate = true;
    		        toss++;
    			    //break; 
                    }  // exit the loop if true 
    	        
                
                    if (!duplicate)
                    {   // if not duplicate
    		          
                     population\[j\] = num;  // fill the dynamic array
                     count++;
                     } 
                  }
    
    		   
                   
                   for (int ii = 0; ii <= count; ii++)
                   {
                   cout << population\[ii\]<<" ";
                   }
                   cin.clear();
                   cin.get();
                   }
                   }
    
    C 1 Reply Last reply
    0
    • O Omegaclass

      what is wrong with my code it's kicking me in the head right now and if a kind soul could help me out i would be very grateful, thanks. here is the goal: Use a one dimensional 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 is 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.Well,

      #include <iostream>
      #include <iomanip>
      #include <ctime>
      #include <cstdlib>
      #include <limits>
      const int x = 20;
      using namespace std;

      int main()

      {

                    int i = 0;
                    int toss = 0;
                    int count=0;
                    int population\[20\];
                    int num=0;
                    bool duplicate = false;
                    
      
      	      while(1)
                { 
                    			   
                    for (int j = 0; j < x; j++)    // test for duplicate random num
                    {
                       
                        srand(time(NULL));
                        num = rand()%90+11;
                        
                      if (num == population\[j\])
      		    
                      {
      				duplicate = true;
      		        toss++;
      			    //break; 
                      }  // exit the loop if true 
      	        
                  
                      if (!duplicate)
                      {   // if not duplicate
      		          
                       population\[j\] = num;  // fill the dynamic array
                       count++;
                       } 
                    }
      
      		   
                     
                     for (int ii = 0; ii <= count; ii++)
                     {
                     cout << population\[ii\]<<" ";
                     }
                     cin.clear();
                     cin.get();
                     }
                     }
      
      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      i think you need to re-think the problem. here's a hint: the population array is going to start out with 0 items in it. after the first number read, it will have one item in it. after the second, it can have one or two. after the third, one, two or three. you need to keep track. here's another hint: you're going to have two loops: one going from 0-19, to read the numbers, and another, inside the first, going from 0-N, where N is the number of items you've read and stored.

      image processing toolkits | batch image processing

      O 1 Reply Last reply
      0
      • C Chris Losinger

        i think you need to re-think the problem. here's a hint: the population array is going to start out with 0 items in it. after the first number read, it will have one item in it. after the second, it can have one or two. after the third, one, two or three. you need to keep track. here's another hint: you're going to have two loops: one going from 0-19, to read the numbers, and another, inside the first, going from 0-N, where N is the number of items you've read and stored.

        image processing toolkits | batch image processing

        O Offline
        O Offline
        Omegaclass
        wrote on last edited by
        #3

        I turned in my assignment, but it wasn't right, so i lost some points, i was wondering if you could give an example on how to do this the right way? i apologize for asking, but my professor is very hard to understand due to the fact he is not a native speaker of English so mush of what i do know is self taught.

        C 1 Reply Last reply
        0
        • O Omegaclass

          I turned in my assignment, but it wasn't right, so i lost some points, i was wondering if you could give an example on how to do this the right way? i apologize for asking, but my professor is very hard to understand due to the fact he is not a native speaker of English so mush of what i do know is self taught.

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          roughly, and in pseudo-code:

          int storage[20]
          int numStored = 0

          // loop over the 20 numbers you need to read
          loop 0 to 20, i

          // read a value
          int newVal = read_a_value;

          // see if you've already read that value
          bFound = false

          // loop over stored numbers
          loop 0 to numStored
          // if it's in the storage array, set the bFound flag
          if storage[i] = newVal
          bFound = true
          break
          .

          // if you didn't find it, store it
          if not bFound and numStored < 20
          storage[numStored] = newVal
          numStored = numStored + 1
          .

          image processing toolkits | batch image processing

          O 1 Reply Last reply
          0
          • C Chris Losinger

            roughly, and in pseudo-code:

            int storage[20]
            int numStored = 0

            // loop over the 20 numbers you need to read
            loop 0 to 20, i

            // read a value
            int newVal = read_a_value;

            // see if you've already read that value
            bFound = false

            // loop over stored numbers
            loop 0 to numStored
            // if it's in the storage array, set the bFound flag
            if storage[i] = newVal
            bFound = true
            break
            .

            // if you didn't find it, store it
            if not bFound and numStored < 20
            storage[numStored] = newVal
            numStored = numStored + 1
            .

            image processing toolkits | batch image processing

            O Offline
            O Offline
            Omegaclass
            wrote on last edited by
            #5

            Thanks Chris for taking your valuable time and helping me out, i really want to learn c++ and plan to take OOP next semester. so far my grades are in the low 90's and don't want to blow it towards the end of this semester.

            dose all this belong in the same for loop? i am not sure what you are
            saying in "read a value" could this be what you are
            saying, "newVal = read_a_value[i];" or is
            this it "newVal = rand()%91+10;"

            how do i implement "see if you've already read that number" if after the
            2nd number i will need to see if the previous number has been read and
            chick if it's a duplicate. and each time a new number is generated it
            must be checked to the others to see if its already there. i am having
            a hard time visualizing the logic and i really do appreciate your time
            to help me with this.

            for loop 0 to 20, i
            {

            // read a value
            int newVal = read_a_value;

            // see if you've already read that value
            bFound = false

            // loop over stored numbers
            loop 0 to numStored
            // if it's in the storage array, set the bFound flag
            if storage[i] = newVal
            bFound = true
            break

            }

            C 1 Reply Last reply
            0
            • O Omegaclass

              Thanks Chris for taking your valuable time and helping me out, i really want to learn c++ and plan to take OOP next semester. so far my grades are in the low 90's and don't want to blow it towards the end of this semester.

              dose all this belong in the same for loop? i am not sure what you are
              saying in "read a value" could this be what you are
              saying, "newVal = read_a_value[i];" or is
              this it "newVal = rand()%91+10;"

              how do i implement "see if you've already read that number" if after the
              2nd number i will need to see if the previous number has been read and
              chick if it's a duplicate. and each time a new number is generated it
              must be checked to the others to see if its already there. i am having
              a hard time visualizing the logic and i really do appreciate your time
              to help me with this.

              for loop 0 to 20, i
              {

              // read a value
              int newVal = read_a_value;

              // see if you've already read that value
              bFound = false

              // loop over stored numbers
              loop 0 to numStored
              // if it's in the storage array, set the bFound flag
              if storage[i] = newVal
              bFound = true
              break

              }

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              Omegaclass wrote:

              dose all this belong in the same for loop?

              you'll have two loops. one inside the other.

              Omegaclass wrote:

              i am not sure what you are saying in "read a value"

              that's where you get the next input. your original msg says "Read in 20 numbers, each of which is between 10 and 100, inclusive." so, if the test data is coming from a file, you'll need to open that file then read each number one at a time. or, if it's coming from the keyboard, or from stdin, or cin, etc.. rand() will work for testing, i guess. but that seems like a strange assignment. i know when i would get assignment like this, the instructor would have a set of data files that he'd send into the program, to make sure the app was doing what it was supposed to on known input.

              Omegaclass wrote:

              how do i implement "see if you've already read that number" if after the 2nd number i will need to see if the previous number has been read and chick if it's a duplicate.

              the previous number will be in the storage array - if it was duplicate, it was already there; if it was a new one, then you will have added it. so, you just need to loop through the items in the storage array and compare each one to the new value.

              image processing toolkits | batch image processing

              O 1 Reply Last reply
              0
              • C Chris Losinger

                Omegaclass wrote:

                dose all this belong in the same for loop?

                you'll have two loops. one inside the other.

                Omegaclass wrote:

                i am not sure what you are saying in "read a value"

                that's where you get the next input. your original msg says "Read in 20 numbers, each of which is between 10 and 100, inclusive." so, if the test data is coming from a file, you'll need to open that file then read each number one at a time. or, if it's coming from the keyboard, or from stdin, or cin, etc.. rand() will work for testing, i guess. but that seems like a strange assignment. i know when i would get assignment like this, the instructor would have a set of data files that he'd send into the program, to make sure the app was doing what it was supposed to on known input.

                Omegaclass wrote:

                how do i implement "see if you've already read that number" if after the 2nd number i will need to see if the previous number has been read and chick if it's a duplicate.

                the previous number will be in the storage array - if it was duplicate, it was already there; if it was a new one, then you will have added it. so, you just need to loop through the items in the storage array and compare each one to the new value.

                image processing toolkits | batch image processing

                O Offline
                O Offline
                Omegaclass
                wrote on last edited by
                #7

                Thank you Chris i get it now. i appreciate your time. this road i am on learning C++ has not been easy, but it is interesting.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups