Must use c must be completed with selections mathematical fu
Must use c++ (must be completed with selections, mathematical functions, loops, characters and strings and functions. That is the level we are at and can’t go beyond that)
(Print distinct numbers, file input, test eof, array, linear search)
Write a program that reads in up to 100 integer numbers from a file, and displays distinct numbers. (i.e. if a number appears multiple times, it is displayed only once.)
[Hint: 1. Read a number and store it to an array if it is new. Otherwise, discard it.
2. The array can be defined with the maximum size 100.
3. But, it may not be full. So, you need a counert to record the actual number of digits stored in the array.
4. After the input, the array contains distinct numbers.
Solution
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void printDistinct(int arr[], int n)
{
// Pick all elements one by one
for (int i=0; i<n; i++)
{
// Check if the picked element is already printed
int j;
for (j=0; j<i; j++)
if (arr[i] == arr[j])
break;
// If not printed earlier, then print it
if (i == j)
cout << arr[i] << \" \";
}
}
int main()
{
int data[ 100 ];// array of 100 elements
string STRING;
ifstream infile;
infile.open (\"test.txt\");
for(int i = 0; i < 100; i++)
infile >> data[i];
printDistinct(data, 100);
return 0;
}
------output-----
inputr file
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
--------output
1 2 3 4 5 6 7 8 9 10
//NOte feel free to ask questions/doubts. God bles you!!


