LOGIC for Distinct Numbers is wrong...help!

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include <iostream>
#include <fstream>

using namespace 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;


}


Any help on sorting this out would be great. Class over!
Last edited on
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.
 
const int 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.
Last edited on
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.
To check if a number has already been encountered:

Create a vector:
std::vector<int> nums

Check if number hasn't already been encountered. If so print it and add it to the list of encountered numbers:
1
2
3
4
5
if (std::find(nums.cbegin(), nums.cend(), file_num) == nums.cend())
{
    std::cout << file_num << '\n';
    nums.push_back(file_num);
}

Put this in a loop to go through all the numbers in the file.

The <vector> and <algorithm> need to be included for this.
Last edited on
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);
@Chervil

I didn't know what std::set was until now. Thanks for letting me know.
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...

q3.cpp(87): error C2059: syntax error: ')' (line 76 here)

q3.cpp(91): error C2059: syntax error: '}' (line 80 here)

q3.cpp(91): error C2143: syntax error: missing ';' before '}' (line 80 here)



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++)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>

using namespace 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;


}
Last edited on
@rdigney
Your latest code has many of the right ingredients, but there are a number of errors scattered throughout.
Line 55 to 58 are an empty loop:
55
56
57
58
        for (int i = 0; i < count; i++)
        {

        }

Line 60, the == should be the assignment operator =
 
store = false;

Both lines 60 and 61 should be enclosed within the same open/close braces, { } because those two lines are both controlled by the previous if.

I already gave you the exact code for that part in my previous post. Here it is again:
55
56
57
58
59
60
61
62
        for (int i = 0; i < count; i++)
        {
            if (check[i] == fromFile)
            {
                store = false;
                break;
            }
        }


Line 63, remove the semicolon at the line end
 
    if (store == true)


Line 71 the closing brace } for the end of function readArray() is missing.

Line 72 remove the semicolon at the end of the line:
 
void displayDistinctNumbers(int check[], int distNums)


Line 75 there is an extra closing brace } which should be deleted.

Hope that helps.

Sorry about that. I get it now.
Thank you so much.
Topic archived. No new replies allowed.