473,511 Members | 17,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

numbering arrays

19 New Member
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)...but 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.
Jul 12 '07 #1
8 1902
phiefer3
67 New Member
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)...but 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.
Jul 12 '07 #2
zandiago
19 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <string>
  6. #include<fstream>
  7.  
  8. using namespace std;
  9. bool seenBefore ( int num )
  10.     {
  11.     return false;
  12.     }
  13.  
  14. int main()
  15. {
  16.     int num;
  17.  
  18.     ifstream inFile;
  19.     ofstream outFile;
  20.  
  21.     inFile.open ("random.dat");
  22.     outFile.open ("randout");
  23.  
  24.     while ( inFile >> num ) 
  25.     {
  26.         if ( !seenBefore(num) )
  27.  
  28.     {
  29.         outFile << "Your non-repitive #'s :"<<endl;
  30.     }
  31.     inFile.close();
  32.     outFile.close();
  33.  
  34. }
  35.  
  36.     return 0;
  37. }
-------------------------------------------------------------------------------------------------------------------

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.
Jul 13 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jul 13 '07 #4
zandiago
19 New Member
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).
Jul 13 '07 #5
zandiago
19 New Member
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....
Jul 13 '07 #6
JosAH
11,448 Recognized Expert MVP
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
Jul 13 '07 #7
phiefer3
67 New Member
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.
Jul 14 '07 #8
zandiago
19 New Member
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
#include<fstream>


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.
Jul 14 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4005
by: Andy Glew | last post by:
I have long looked for (and occasionally posted questions to groups such as this about) a tool that can take a group of HTML pages (nowadays XHTML, or XML) and produce a nicely formatted...
6
2835
by: Stanimir Stamenkov | last post by:
So if the 'type' attribute of the OL element is deprecated and authors should rely only on stylesheets how one could use a reference (as clear text) in the content to a particular list element? ...
6
7380
by: Christian Roth | last post by:
Hello, how do I offset the numbering of a list in XHTML Strict (+CSS) in current browsers? What I want is something like: 5. Item a 6. Item b 7. Item c
5
4598
by: Charles McCaffery | last post by:
I have written a database with auto-numbering and now wish to remove alkl of my test data and set the auto-numbering back to one. How do I do this please? Charles McCaffery.
1
1898
by: Wayne Aprato | last post by:
I have a report that shows the results of a query. One of the fields is an autonumber field from the query which shows for instance: 120, 121 , 122 for 3 records. Is there a way to have another...
2
2293
by: Wayne Aprato | last post by:
I posted this yesterday and it seems like a moderator has thrown it in another thread. This is a totally different question to the one asked in that thread, so I'm posting it again. It is not a...
3
2915
by: Jim Bancroft | last post by:
Hi all, In VB6 I used a 3rd party tool for line numbering my source code, to help with debugging. However, in experimenting with VB .Net I've noticed that my exceptions automatically provide...
54
3200
by: MLH | last post by:
I use A97 and do not always insert line numbers while writing procedures. I find it necessary to go back and add them later to aid in debugging. Nearly 3 years ago, something was mentioned in...
9
9455
by: bintom | last post by:
Hi! I teach C++ in schools in India. I don't have a good answer when students ask me why arrays in C++ are numbered from 0 to n-1 for an array of n elements. I hope somebody can tell me. ...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.