numbering arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zandiago
    New Member
    • Jul 2007
    • 19

    numbering arrays

    Good day:

    My assignment is as follows:


    " Use a one-dimensional array to solve the following problem. Read in 1000 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 (and printed). Use the smallest possible array to solve this problem."

    I barely but understand arrays..a few questions though.....I have an inFile with the required numbers (1000 numbers between 10 & 100)...

    Since it's obvious that the numbers will repeat (though dupilcates should not be printed out), I presume that loops will have to be used(according to my instructor)...b ut how can you use loops when there is no set condition to test statements by? (I hope I stated that correctly).

    Any assistance is appreciated. I'll be posting my source code within 24-48 hrs, since i'll need to go read up on this array stuff.

    Thanks.
  • phiefer3
    New Member
    • Jun 2007
    • 67

    #2
    Originally posted by zandiago
    Good day:

    My assignment is as follows:


    " Use a one-dimensional array to solve the following problem. Read in 1000 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 (and printed). Use the smallest possible array to solve this problem."

    I barely but understand arrays..a few questions though.....I have an inFile with the required numbers (1000 numbers between 10 & 100)...

    Since it's obvious that the numbers will repeat (though dupilcates should not be printed out), I presume that loops will have to be used(according to my instructor)...b ut how can you use loops when there is no set condition to test statements by? (I hope I stated that correctly).

    Any assistance is appreciated. I'll be posting my source code within 24-48 hrs, since i'll need to go read up on this array stuff.

    Thanks.
    You do have a condition to test by. You know how many numbers will be read from the file, 1000. I'd suggest using a for loop that loops 1000 times and reads one number at a time. Or assuming the file only contains those 1000 numbers, you could use a while loop and use !infile.eof() as the condition to have it read until the end of the file (infile being replaced by whatever your ifstream variable is). I'd personally use the for loop that loops 1000 times, but the end of file method would be able to handle any amount of numbers without needing to be altered.

    Comment

    • zandiago
      New Member
      • Jul 2007
      • 19

      #3
      [code=cpp]#include <iostream>
      #include <cstdlib>
      #include <cmath>
      #include <ctime>
      #include <string>
      #include<fstrea m>

      using namespace std;
      bool seenBefore ( int num )
      {
      return false;
      }

      int main()
      {
      int num;

      ifstream inFile;
      ofstream outFile;

      inFile.open ("random.dat ");
      outFile.open ("randout");

      while ( inFile >> num )
      {
      if ( !seenBefore(num ) )

      {
      outFile << "Your non-repitive #'s :"<<endl;
      }
      inFile.close();
      outFile.close() ;

      }

      return 0;
      }[/code]
      -------------------------------------------------------------------------------------------------------------------

      A couple things: I have the 1000 numbers in an inFile that I had generated....so how can i get the program to see if the numbers have been repeated and how will the array come into play? Thanks for all the help.
      Last edited by sicarie; Jul 13 '07, 03:50 PM. Reason: Please use [code=cpp] and [/code] tags around your code.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by zandiago
        A couple things: I have the 1000 numbers in an inFile that I had generated....so how can i get the program to see if the numbers have been repeated and how will the array come into play? Thanks for all the help.
        Use the array to hold the numbers printed. When you read a number, check the array before you print it. If you print it, add it to the array.

        Comment

        • zandiago
          New Member
          • Jul 2007
          • 19

          #5
          Originally posted by weaknessforcats
          Use the array to hold the numbers printed. When you read a number, check the array before you print it. If you print it, add it to the array.
          ok thx.....i'll check it...however when i did
          outFile << "Your non-repitive #'s :"<<num<<endl;. ....it printed out the number of ints (which is 90).

          Comment

          • zandiago
            New Member
            • Jul 2007
            • 19

            #6
            Originally posted by zandiago
            ok thx.....i'll check it...however when i did
            outFile << "Your non-repitive #'s :"<<num<<endl;. ....it printed out the number of ints (which is 90).
            Actually it prints '90'.....my mistake...the number of ints if 1000....

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Here's another approach: for 1000 numbers all in the range [10, 100] there must
              be duplicates in that series because of the Pigeon Hole principle. Have a bool
              array of 100-10+1 == 91 elements that register if a number in the range [10, 100]
              has been seen before; if not, print it; in both cases set the appropriate bool
              entry in that array to true.

              kind regards,

              Jos

              Comment

              • phiefer3
                New Member
                • Jun 2007
                • 67

                #8
                I would use an array of 91 elements, and a counter variable to count how many number's you've added to the array. (yes there are 91 numbers from 10 to 100 including both 10 and 100). initialize the counter to 0.

                Read each number from the file into a temporary variable, then use a loop to compare it to all of the numbers in the array, until you find a match. If you find a match do nothing. If you don't find a match, print the number, add it to the array (use the counter as the index) and then increment the counter.

                if you want, you can add a condition before all of that to check if the counter is 91, as that would mean that the rest of the numbers are repeats, and you can stop reading numbers.

                There's your algorithm, It shouldn't be too difficult to right the code.

                Comment

                • zandiago
                  New Member
                  • Jul 2007
                  • 19

                  #9
                  #include <iostream>
                  #include <cstdlib>
                  #include <cmath>
                  #include <ctime>
                  #include <string>
                  #include<fstrea m>


                  using namespace std;


                  int main()
                  {
                  int num;


                  ifstream inFile;
                  ofstream outFile;

                  inFile.open ("random.dat ");
                  outFile.open ("randout");



                  bool seenBefore(int, int *);

                  int seen[91];
                  for(int i=0; i<91; i++)
                  { // set all bools to false.
                  seen[i]=false;
                  }

                  while ( inFile >> num )
                  {
                  outFile << "Your non-repitive #:"<<endl;

                  }

                  for (int i = 10; i<101; i++)
                  {
                  cout << i << " " << seenBefore(i, seen) << "\n";// 0=not seen before, 1 = seen before.
                  }
                  }

                  bool seenBefore(int num, int *seen)
                  {
                  if(seen[num-10])
                  {
                  return true;
                  }

                  seen[num-10] = true;

                  return 0;
                  }
                  ----------------------------------------------------------------------------------
                  Ok....this is what i kinna figured...so couldn't I just let the program check the inFile(which contains the random #'s already) ?to see how much time they appear? But could I adjust this so that when each # is read from the inFile, print it only if it's not a duplicate of a number already read (and printed)?? Thx for all the assistance.

                  Comment

                  Working...