I am a beginner struggling with the very last homework question for semester!
Test on Friday and I was hoping to study this question for the array section.
I need to open a file of 100 numbers, and go through the file and only print distinct numbers (no repeats).
So far, this code opens the file and shows all 100 numbers...
I am having trouble showing distinct numbers..
please see below comments in the code
#include <iostream>
#include <fstream>
usingnamespace std;
//prototypes
int readArray(ifstream&, int[], int);
void displayDistinctNumbers(int check[], int distNums);
int main()
{
//parameter types
ifstream inFile;
// Open a file
inFile.open("100 numbers.txt");
if (inFile.fail())
{
cout << "File does not exist" << endl;
cout << "Exit program" << endl;
return 1;
}
// setting all to 0
int check[100] = { 0 };
int numbers = 100;
//call - takes arguments, which have names
int distNum = readArray(inFile, check, 100);
displayDistinctNumbers(check, distNum);
return 0;
}
//to use function - definition
//named parameters
int readArray(ifstream &iF, int check[], int size
)
{
int count = 0;
int fromFile;
iF >> fromFile;
while (iF)
{
bool store = true;
for()//I need to loop through the numbers stored and use [0, count]
{
}
if()//need if statement: current # == fromFile
// set store ==false
(if store == true)
{
// put fromFile in the check array, and increase count
}
{
check[count] = fromFile;
count++;
}
iF >> fromFile;
}
return count;
}
void displayDistinctNumbers(int check[], int distNums)
{
cout << "The distinct numbers are " << distNums << endl;
for (int i = 0; i < distNums; i++)
cout << check [i] << endl;
}
In your function readArray() there is the array check. You also have the size of that array, which really represents its maximum capacity. You need another number which will represent the count of how many numbers are currently stored in the array. At the start that will be zero.
After reading a number from the file, search the current contents of the array (subscripts from 0 to count-1). If the number is already present, just continue to read the next number from the file. However if it is not there, store it in the array (provided the capacity was not exceeded) and increment the count. After reading/testing/storing all the numbers. just loop through the array and write out its contents.
By the way, in your current code you have the number 100 three times, at lines 28, 30, 34. That's not the best way to handle this. If the number ever needed to change, you'd have to search through the code looking for all the places which needed to be changed. Instead, use a constant.
constint arraysize = 100;
(choose whichever name you prefer).
Then use that constant when defining the array at line 28 and calling the function at line 34.
Thank you,
I sort of understand that ??
But I am new to this, so I don't know how to loop through the array or the numbers stored.
And I already have int count = 0 ....?? is this what you meant?
I am unsure how to construct these for () and if() statements as I commented in the code above...
How do I do that? It's the code language I don't know how to use.
I will work on changing the array size to a constant, after I get the rest of the program working. I can't manage changes until it's working,or won't know where t begin to fix things if I make a mistake.
For the part of the code that has the logic for only selecting distinct numbers....
This part is not right yet.... the logic is wrong.
(from lines 51 - 70)
It still prints all 100 numbers. I need it to print just the distinct numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
while (iF)
{
bool store = true;
for(int i = 0; i < size; i++)
{
// Not sure what needs to go here either
}
if (int i = 1)
store == false;
if (store == true);
{
check[count] = fromFile;
count++;
}
iF >> fromFile;
}
Your code is heading in the right direction. However, in that last code posted, at line 5 you have
for(int i = 0; i < size; i++)
but it should be
for(int i = 0; i < count; i++)
... as said previously:
After reading a number from the file, search the current contents of the array (subscripts from 0 to count-1).
Here's what I said next:
If the number is already present, just continue to read the next number from the file. However if it is not there, store it in the array (provided the capacity was not exceeded) and increment the count.
Here, you have a good idea, the variable bool store = true; is just what is needed. Thus the first part of the code, including the loop (lines 3 to 8) could look something like this:
1 2 3 4 5 6 7 8 9 10
bool store = true; // set the indicator to mean "not found"
for (int i=0; i<count; ++i) // search the numbers which are already stored
{
if (check[i] == fromFile) // is it the same?
{
store = false; // yes. Set the indicator to mean "found".
break; // no need to keep looking, we already found it. exit from the loop.
}
}
The rest of your code is roughly correct, though for robustness, to avoid overflowing the array, you should also check that count<size before storing the value.
I guess for this homework project vectors are not to be used. But still ,there are a variety of solutions possible using standard containers - std::set is a possibility, though it doesn't preserve the original sequence of the values from the file.
1 2 3 4
std::set<int> nums;
for (int num; inFile >> num; )
nums.insert(num);
Here is the code so far. I think I am close now...
Here are the errors it says ( some are syntax, but I don't see what is wrong) For example, it says I am missing a ';' before a }...but there is already a ';' there...
It is underlining the:
1) 'i' in if (check[i] == fromFile)
2) 'store' in if (store == true);
3) 'return' in return count;
4) 'for' in for (int i = 0; i < distNums; i++)
#include <iostream>
#include <fstream>
usingnamespace std;
//prototypes
int readArray(ifstream&, int[], int);
void displayDistinctNumbers(int check[], int distNums);
int main()
{
//parameter types
ifstream inFile;
// Open a file
inFile.open("100 numbers.txt");
if (inFile.fail())
{
cout << "File does not exist" << endl;
cout << "Exit program" << endl;
return 1;
}
// setting all to 0
int check[100] = { 0 };
int numbers = 100;
//call - takes arguments, which have names
int distNum = readArray(inFile, check, 100);
displayDistinctNumbers(check, distNum);
return 0;
}
//to use function - definition
//named parameters
int readArray(ifstream &iF, int check[], int size
)
{
int count = 0;
int fromFile;
iF >> fromFile;
while (iF)
{
bool store = true;
for (int i = 0; i < count; i++)
{
}
if (check[i] == fromFile)
store == false;
break;
}
if (store == true);
{
check[count] = fromFile;
count++;
}
iF >> fromFile;
}
return count;
void displayDistinctNumbers(int check[], int distNums);
{
cout << "The distinct numbers are " << distNums << endl;
}
for (int i = 0; i < distNums; i++)
cout << check [i] << endl;
}