Good day:
We've just commenced our chapter on arrays. The way how it was presented just didn't click to me. The following is the assignment:

" 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."

A few questions:
1. Since it's not practical to type down 1000 numbers in an 'inFile', would it be possible for me to use a randon number generator within the program?
2.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.

Hi,

I can see how to do it and in keeping with the ethics of this forum to provide guidance to strudents, I suggest you think about using a logical array of 90 items to use.

I can't provide you with C++ code as that is not my forte.

I would ask your teacher to provide the 1000 numbers to use, so all students use the same numbers.

Hope this helps - ask more questions if you want

Denis

Thanks Denis for your input...however our professor will not provide us with those numbers...so i guess i'll try to use a random number generator perhaps...

I think I would generate a set of numbers once and then keep them.

Either use generator as you suggest or use Excel, type in say 20 numbers in a column, then copy and paste until you get 1000 numbers. Then save to file.

Denis

just write a little prgram that generates 1000 numbers between 10 and 100 using rand() % 100 + 10 to generate the numbers and save them to a file. Then you can write the other program as your teacher instructed.

Ok.....thanks for all the help so far....so if I get the numbers as is needed...i'm planning to put them in an inFile....is it ok if I place the numbers in let's say 5 different columns?

just write a little prgram that generates 1000 numbers between 10 and 100 using rand() % 100 + 10 to generate the numbers and save them to a file. Then you can write the other program as your teacher instructed.

actually it would be `rand()%90 + 10`

Ok.....thanks for all the help so far....so if I get the numbers as is needed...i'm planning to put them in an inFile....is it ok if I place the numbers in let's say 5 different columns?

I would put them one per line because it is much easier to read them in when needed - no need to write code to separate them.

I would also have a small file with say 20 numbers in for testing purposes to use until I am sure code is working.

Denis

i've got all the numbers as was required an placed then in an inFile. With regards to if/else statements, how could i make the program so that if the number is a duplicate it wont print it out?

Did my first post make any sense, when I suggested using a one dimensional logical array ?

Denis

i've got all the numbers as was required an placed then in an inFile. With regards to if/else statements, how could i make the program so that if the number is a duplicate it wont print it out?

There are easier solutions (more like hacks) if you can use multiple arrays, but I'm assuming you can't, so a clean way of solving it is as follows:

Well, you know which way you are iterating through the array and you also know what number you are currently on to check if it has already been printed.

A simple solution would be to hold the index and compare the value at that index to all the values from 0 to that index's value (the numbers you have already checked) by using a double for loop and a boolean representing the print state.

That way, if your number that you're currently on isn't in the array up to the index you are on, you know you have not encountered it yet and you can print it. If your number is in the array up the the index you are on, you know that you have already encountered the number at least once and printed it.

That's a pretty abstract concept of how that problem can be solved, but the test conditions for the loops and if statement should be pretty clear. Now all you have to do is implement it.

Hope that helps :)

Well the first step is to make sure you can read the file properly before trying to do anything with the data. If the file reading produces garbage, it doesn't matter how wonderful the rest of the code is, it will still be garbage.

``````#include <fstream>
#include <iostream>
using namespace std;

int main ( ) {
ifstream in("foo.txt");
int num;
while ( in >> num ) {
cout << "Read " << num << endl;
}
return 0;
}``````

The next step is to refine that by adding the initial check to decide whether we want to print the number or not.

``````#include <fstream>
#include <iostream>
using namespace std;

bool seenBefore ( int num ) {
return false;
}

int main ( ) {
ifstream in("foo.txt");
int num;
while ( in >> num ) {
if ( !seenBefore(num) ) {
cout << "Read " << num << endl;
}
}
return 0;
}``````

At the moment, `seenBefore()` does nothing useful, but we would expect it to produce the same output as before. By adding a separate function, we can then work on addressing just one specific task of the whole assignment (over to you).

Further, by making it a separate function, you can now develop the code in isolation from the file reading code, eg.

``````int main ( ) {
int test[] = { 10, 20, 20 };
for ( i = 0 ; i < 3 ; i++ ) {
int num = test[i];
if ( !seenBefore(num) ) {
cout << "Read " << num << endl;
}
}
return 0;
}``````

Such code is much easier to post to a forum since everything is contained within the code itself. No need to worry about having to create/upload data files.

Think about what additional parameters and data storage `seenBefore()` might need in order to fulfil the requirement of detecting whether an int has been seen before.

``````[/TEX]
#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 :"<<num<<endl;
}
inFile.close();
outFile.close();

}

return 0;
}[TEX]``````

----------------------------------------------------------------------------------
Thats what i've got so far. My outFile...shows the number of integers (which is 90)...I've read up on the arrays and still kinna lost...imma read some more and see...Thx for all the help.

How can i use the array to hold the numbers printed?

actually.....it prints '90'....my mistake...1000 should be the # of ints

>How can i use the array to hold the numbers printed?

Declare an array and put them in.

You need to make the seenBefore() function actually DO something.

It had initially printed that '90' because it was the first number in my inFile.

``````#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;
int value[1000];
int range [10, 100];

ifstream inFile;
ofstream outFile;

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

while ( inFile >> num )
{

}
inFile.close();
outFile.close();

return 0;
}``````

----------------------------------------------------------------------------------
Why do i get the error codes:
1. error C2143: syntax error : missing ']' before ','
2. error C2059: syntax error : 'constant', (where i have the variable range?
3. With regards the seenBefore.....i aint really sure how to get it to work

>int range [10, 100];
That's not how you create a multidimensional array. At least, I assume you're trying to create a multidimensional array, which is like this:

``int range[10][100];``

As I understand it, the file random.dat already has 1000 values between 10 and 100 before you run this protocol. Now you want to read random.dat and determine what unique values between 10 and 100 are present in random.dat.
To know whether the current value is unique you will need a container that holds all the unique values found so far--you could use the arrray called value if you want.

Then you want to pass each number, num, to seenBefore() as you read it from random.dat. In addition you want to send the container holding the unique values to seenBefore() each time you read in a new value from the file. Then, in seenBefore(), loop through the container to see if num is already in there or not. If num is not in the container then num is currently unique so add it to the container.

NB, depending on the type of container you use to hold the unique values found in random.dat you may also need to send the current number of unique values in the container to seenBefore() in addition to the current value read from random.dat and the container holding the unique values found in random.dat.

> Read in 1000 numbers, each of which is between 10 and 100, inclusive.
Actually Narue, I think he was trying to declare an array with a subscript range.
It's still wrong, since all subscripts start at 0

So if you were going for an easy life, with the first 10 entries of the array wasted, it would be `int range[101]; // 100 is last valid subscript` For minimum space, it would be `int range[100-10+1];` but you would need to remember to subtract 10 from whatever value you use to subscript.

More generally

``````#define MIN 10
#define MAX 100
int range[MAX-MIN+1];
...
range[value-MIN] = 0;``````

> 3. With regards the seenBefore.....i aint really sure how to get it to work
- Grab a pack of playing cards.
- Shuffle them.
- For this test, you're ignoring the suit and just looking at the rank. So C3 and D3 are the same when it comes to determining if you've seen it before.
- Deal the cards one at a time (aka reading from the file), and then think about the steps you need to perform to work out whether you've seen the card before (based on rank only).

You should end up with 2 piles of cards, the 'seen' pile containing 13 cards, and the 'duplicates' containing the other 39 cards.

pretty much, the values that are read in from my inFile which contains the 1000 numbers already, the number should only be printed out if it's already been read and printed out...(in other words it shouldn't be a duplicate number)...

>Actually Narue, I think he was trying to declare an array with a subscript range.
That crossed my mind, but I figured I would go with the most likely intention. ;)

how could i loop through the elements of the array?

``````#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;
int value[1000];
int range [100-10+1];
int MIN =10;
int MAX= 100;

ifstream inFile;
ofstream outFile;

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

while ( inFile >> num )
{

}
inFile.close();
outFile.close();

return 0;
}``````

that's really easy, you can use many solution.. binary search is one thing that you may want to use.. :)

but there's also another algorithm that basically do the same thing, but it won't be as optimize...

find the number first, and save it, if there's any, then remove it.. :)

>binary search is one thing that you may want to use..
To loop through the elements of an array? Not likely. Maybe you should quote the question you're responding to.

why not?
i forgot, it will only work if the number is in order :)
sorry

``````#include <iostream>

using namespace std;

int main()
{

bool seenBefore(int, int *);

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

seenBefore(15,seen); // for testing

for (int i = 10; i<101; i++)
{ // loop thru 10-20
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;
}``````

---------------------------------------------------------------------------------

I guess that something like this could work.......just need some touch up-i guess. thanks for all the help.

With regards to the initial post...how could I 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 i find a match?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.