"Write a program that reads in ten numbers to an array. The program then reads the array and displays distinct numbers (i.e. if a number appears multiple times, it is displayed only once)"

for example:

1 2 3 4 [B]5 5 [/B]6 7 [B]8 8[/B] 9

1 2 3 4 5 6 7 8 9

I am currently stuck in my program for about a couple of hours. So far, I can read in 10 numbers and print them out, but I don't know where to go from there. I'm not that experience at C++, so if someone can help me, I would appreciate it.

Here is my code:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

    double num[10];

    cout<<"Enter ten numbers "<<endl;
    cout<<""<<endl;

    for(int i = 0; i<10; i++)
    {
    cin>>num[i];

    }
    cout<<""<<endl;
    cout<<"The distinct numbers are "<<endl;
    cout<<""<<endl;

    system ("PAUSE");
    return 0;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

"Write a program that reads in ten numbers to an array. The program then reads the array and displays distinct numbers (i.e. if a number appears multiple times, it is displayed only once)"

for example:

1 2 3 4 5 5 6 7 8 8 9

1 2 3 4 5 6 7 8 9

I am currently stuck in my program for about a couple of hours. So far, I can read in 10 numbers and print them out, but I don't know where to go from there. I'm not that experience at C++, so if someone can help me, I would appreciate it.

Here is my code:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

    double num[10];

    cout<<"Enter ten numbers "<<endl;
    cout<<""<<endl;

    for(int i = 0; i<10; i++)
    {
    cin>>num[i];

    }
    cout<<""<<endl;
    cout<<"The distinct numbers are "<<endl;
    cout<<""<<endl;

    system ("PAUSE");
    return 0;
}

end quote.

You have at least two choices. Which approach you take depends on personal preference and the assignment requirements:

  1. Don't automatically read the number into the array. Only insert the number into the array if it not already there.
  2. Insert all numbers, including duplicates, into the array. Once the array is filled in, weed out duplicates and either place them into the same array or place them into a new array.

I'd say choice 2 fits better with the assignment requirements. As I said, you can either use the same array after weeding duplicates out or use a new one. Both are doable, but I'd use a new one. So your steps would be:

  1. Create an array that holds ten numbers.
  2. Create a second array that can hold ten numbers.
  3. Read ten numbers into first array.
  4. Go through first array an element at a time.
  5. Check whether element from first array is already in second array. If not, insert element into second array. if it is already in the second array, do nothing.
  6. Repeat steps 4 and 5 for next element of first array.
  7. Display second array.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Thanks for the replys...

How do I sort the arrays, so I can do it the easy way?

I kind of went left field with my code, and it didn't work right. The program comes up though.

Here is my updated code:

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{

bool numCopy = false;
int num[10];

cout<<"Enter ten numbers"<<endl;

for(int i = 0; i<10; i++)
{
for(int k = (i + 1); k < 10; k++)
{
cin>>num[k];

if (num[i] != num[k])
numCopy = false;
else
numCopy = true;
}
if (numCopy == false)
cout<<"The distinct numbers are "<<num[i]<<endl;
numCopy = false;
}

system ("PAUSE");
return 0;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

The primary mistake in your code is that it starts accepting num[] values from num[1] instead of num[0]. Its better to accept all the values of the array and then go into the next "for" loop for number comparison. As soon as a single repeated value is found, the program should omit that value from being printed. In your program, during the comparison, say, for the array {1,2,1,1,3,4,1}, when the for loop is being executed for num[0], numCopy becomes true for num[2] and num[3] but reverts back to false for num[4], thus printing the value. Also, for, say, i=5, the second loop will be doing unnecessary comparisons with num[6], num[7]... which will be junk values. Hope this helped :)

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{

bool numCopy = false;
int num[10];
int i;
cout<<"Enter ten numbers"<<endl;

for(i=0; i<10; i++)
{
cin>>num[i];
}

cout<<endl<<"The distinct numbers are: ";

for(i=0;i<10; i++)
{
for(int k = (i + 1); k < 10; k++)
{
if (num[i] != num[k])
numCopy = false;
else
{ numCopy = true; break;}
}
if (numCopy == false)
cout<<num[i]<<endl;
numCopy = false;
}

system ("PAUSE");
return 0;
}

If you indent your code, it'll be easier to tell where loops start and stop and easier to debug:

for(int i = 0; i<10; i++)
{
for(int k = (i + 1); k < 10; k++)
{
cin>>num[k];

if (num[i] != num[k])
numCopy = false;
else
numCopy = true;
}
if (numCopy == false)
cout<<"The distinct numbers are "<<num[i]<<endl;
numCopy = false;
}

l4z3r makes a good point. Your number input loop and your display loop should be different. There is more than one way to do this program, but you definitely don't want "The distinct numbers are " to be displayed inside of a loop. Also, look closely at where you are asking for user input and count how many times that is. If it is more than ten, see whether you need to take it outside of a loop.

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char **argv) {
	vector<double> num;
	vector<double>::iterator it;
	for (int i = 0; i < 10; ++i) {
		double tmp;
		cin >> tmp;
		num.push_back(tmp);
	}
	it = unique(num.begin(), num.end());
	num.resize(it - num.begin());
	for (size_t i = 0; i < num.size(); ++i) {
		cout << num[i] << ' ';
	}
	return 0;
}

i dont think ivailosp's program will work properly. Try putting in 1,x,1,x,1,x,1,x... or something like that. It'll just output the entire vector again.

A small modification will set it right, though. I'll let you have the pleasure of doing it ;)

Vector, is obviously, the most powerful way of solving this problem, which is easily ascertained by comparison of lines of code required. :)

add std::sort or don't use vector :P

#include <iostream>
using namespace std;

int main(int argc, char **argv) {
	double num[10];
	for (int i = 0; i < 10; ++i) {
		cin >> num[i];
	}
	sort(num, num+10);
	double* poss = unique(num, num+10);
	for (double* i = num; i < poss; ++i) {
		cout << num[i-num] << ' ';
	}
	return 0;
}

Since I'm learning C++ I decided I would try to solve this. I think I solved:

// This script will generate an array of 10 ints from the user
// It will then sort them and will display the list, excluding duplicates
#import <iostream>
using namespace std;

void sortArray(int array[], int size);

int main(void)
{
    int numbers[10]; // an array of 10 integers
    int lastNum;
    char symbol;
    
    for (int i = 0; i < 10; i++) 
    {
        cout << "Enter number " << (i + 1) << ": ";
        cin >> numbers[i];
    }
    
    cout << "Array input complete." << endl;
    
    sortArray(numbers, 10);
    
    for (int i = 0; i < 10; i++)
    {
        if (lastNum != numbers[i])
        {
           cout << numbers[i] << endl;
           lastNum = numbers[i];
        } 
    }
 
    cout << "Array display complete." << endl;
       
    cin >> symbol;
}

// ###############################
void sortArray(int array[], int size) // C++ passes arrays by reference
{
    int temp;
    
    for (int i = 0; i < size; i++) // loop through the entire array
    {
        for (int j = i; j < size; j++) // loop through the item being checked until the end
        {
            if (array[j] < array[i]) // if the item being checked is greater than the item in the array..
            {
                       temp = array[i]; // swap places
                       array[i] = array[j];
                       array[j] = temp;
            }
        }
    }
        
    return;
}

The sorting method you are using now is Bubble Sort. You can use another popular sorting methods like Insertion Sort, Selection Sort, Merge Sort and Quick Sort. Actually these sorting are a lot more efficient than Bubble sort. ;)

very good, Rachmaninov! :)

For the others: The process used by Rachmaninov is called "Bubble sort" as the smaller elements are "bubbled up" in the process. Another method called "Selection sort" may also be used. In this sorting technique, the smallest element and its position is found at each pass (say, i th pass) and is then exchanged with the i th element. Clearly, this method will be more longer and more time consuming.

Clearly, this method will be more longer and more time consuming.

Insertion sort takes less time (in most cases) then bubble-sort. Bubble-sort is (with the exception of bogusort) the slowest sorting algorithm there is (to my knowledge). They're both quadratic algorithms which won't work very well on large arrays.
Here's the link I give everyone when it comes to sorting. It has all you need to know and more :)

Insertion sort takes less time (in most cases) then bubble-sort. Bubble-sort is (with the exception of bogusort) the slowest sorting algorithm there is (to my knowledge). They're both quadratic algorithms which won't work very well on large arrays.
Here's the link I give everyone when it comes to sorting. It has all you need to know and more :)

You just want +rep from Narue. :)

You just want +rep from Narue. :)

Narue doesn't give +rep. She's an expert meanie ;)

But if you can find any site that describes sorting better/more complete then eternallyconfuzzled, be my guest to prove me wrong and I'll never link to Narue's site again!

Comments
Haha! I proved you wrong. ;-)

Narue doesn't give +rep. She's an expert meanie ;)

But if you can find any site that describes sorting better/more complete then eternallyconfuzzled, be my guest to prove me wrong and I'll never link to Narue's site again!

Actually I can't. It's a damn good link. I'm going to start linking to it myself. Its only drawback is that, while it mentions bogosort, it doesn't give an implementation.

Back to the original problem, I think Bubble Sort is fine for this. It's only ten numbers.

Its only drawback is that, while it mentions bogosort, it doesn't give an implementation.

I've made one myself, perhaps I'll post it when I'm behind my other PC :)

Back to the original problem, I think Bubble Sort is fine for this. It's only ten numbers.

Agreed, I just wanted to point out that insertion isn't slower then bubble.

>Its only drawback is that, while it mentions bogosort, it doesn't give an implementation.
For two reasons. First, there's little point in providing an implementation of a sort that would never be used in practice. My site is about practical programming. Second, bogosort is a pain to verify for correctness. ;)

Your sort is close. See this to improve the sort. Hint: Your loop values are slightly off. Otherwise, good job!

I've never heard of a bubble sort... I thought I was doing selection sort.. lol, whatever.

I see from the link you gave that the values are a bit different but I think mine is working with all possible combinations.

I looked at is some and realized that I could make this statement:

for (int j = i; j < size; j++)

this

for (int j = i + 1; j < size; j++)

Is that what you meant?

oh. thats right now, Rachmaninov. I didnt notice that on my first look. Good going, WaltP.

offtopic: I just assumed Narue was a guy, somehow. Lol. Another misconception corrected ;)

This article has been dead for over six months. Start a new discussion instead.