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.
8 1902
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.
- #include <iostream>
-
#include <cstdlib>
-
#include <cmath>
-
#include <ctime>
-
#include <string>
-
#include<fstream>
-
-
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;
-
}
-------------------------------------------------------------------------------------------------------------------
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.
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.
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).
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....
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
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.
#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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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?
...
|
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
|
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.
|
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...
| |
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...
|
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...
|
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...
|
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.
...
|
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,...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |