First, define "efficient". I can think of solutions that are efficient in terms of speed, and in terms of memory usage.
As to solving the probem, yes, an array will almost certainly be needed for any solution. It's a matter of what you put in the array, and what you do with it.
If you store all the inputs, you then have to search through it in some manner and eliminate duplicates. You could set them to be a negative value, which will be skipped in the printing phase. But, you would have to make many passess through the array to find duplicates. Also, how big do you make the input array - your problem only state the limit on the size of the values, not the size of the file!
I would look for a way to test each value as it's read in, determine if you've already seen it or not, print it if it's unique. That way you don't worry about storing all the input or accessing it multiple times. There's an easy way to do this.
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6
Why do you read the file to count items, then use that count to limit the for loop?
Why not just use that while in place of the first for? In fact, I don't think your code will successfully run as is, since you read the file to end, then start reading it again without resetting in any manner.
Your array a[]. You set its size with variable k, which has no value. So how big is your array?
This bit
for (int j=0; j<i; j++) {
if (a[i]!=a[j])
integerFile2<<a[i];
Is going to write the newly read value a[i] to the output many times, each time it is tested against a number it doesn't match. You should instead keep track of finding a match, if not, then output the number.
As I mentioned earlier, efficiency has many ways of being measured. In terms of time, you have something that's in the n^2 category, about the same time efficency as bubble sort. And you have the problem of not knowing how bit to make your array a[]. Is there a limit to how many numbers can be in the input file? 100, 1,000, 1,000,000?
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6
You can always use the sorted set from STD:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <set>
using namespace std;
int main() {
set<int> a;
ifstream fin ("filename.txt", ios::in);
int nr;
string line;
while (getline(fin, line)){
stringstream token(line);
cout<<line<<endl;
token>>nr;
a.insert(nr);
}
set<int>::iterator it;
for (it=a.begin();it!=a.end();it++)
cout<<*it<<" ";
cout<<endl;
return 0;
}
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 11
I had thought to tell him that but the problem states to print the numbers in the order they appear. If his compiler supports c++11 he could use an unordered map and use the value for the key.
NathanOliver
Posting Virtuoso
1,513 posts since Apr 2009
Reputation Points: 269
Solved Threads: 277
Skill Endorsements: 3
marnum, you're still making a few classic mistakes, and writing code that loses on the efficiency standpoint.
Assuming you make array a[] big enough for any possible input;
1. be sure to reset count to 0 for each new pass in the for loop.
2. don't write duplicate numbers to the array, you'll just be comparing to them numerous times.
How about:
end = 0;
while( integerFile >> temp )
{
count = 0;
for (int j = 0; j < end; j++)
{
if (temp == a[j])
count++;
}
if (count == 0)
{
a[end] = temp;
integerFile2 << a[end];
}
}
This is still not the most time efficient method, but I hope these corrections to your chosen path help.
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6