Okay first let me say that I an new to c++ programing so keep that in mind. Okay I am working on some code, for school class = intro to c++ (I'll be honest) that will take in 10 integers into an array, sort the array in ascending order, count the duplicates if any then delete them so that there is only one of them, Then this code outputs the array and the corresponding count of duplicates if there is any if
example:
input: 10 24 65 10 78 78 10 97 63 48
output:
10 3
24 1
48 1
63 1
65 1
78 2
97 1
here is the code I have so far, I get the array and I sort it, and I can even count the duplicates but only if there is one and it's the first array element. can some one please help!
#include <iostream>
usingnamespace std;
void selectionSort(int list[10], int length);
int main()
{
int i = 0;
int j = 0;
int c = 0;
int list[10];
int count[10];
bool found = false;
cout << "Please enter 10 integers." << endl;
for (i = 0; i < 10; i++)
cin >> list[i];
selectionSort(list, 10);
cout << "The list after it's sorted looks liks this: ";
for (i = 0; i < 10; i++)
cout << list[i] << " ";
cout << endl;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if(list[i] >= list[j])
found = true;
break;
}
if (found)
if (list[i] == list[j])
{
c = c + 1;
count[i] = c;
}
else
count[i] = 1;
else
count [i] = 1;
}
for (i = 0; i < 10; i++)
cout << count[i] << " ";
cout << endl;
return 0;
}
void selectionSort(int list[10], int length)
{
int index;
int smallestIndex;
int minIndex;
int temp;
for (index = 0; index < length - 1; index++)
{
smallestIndex = index;
for (minIndex = index + 1; minIndex < length; minIndex++)
if (list[minIndex] < list[smallestIndex])
smallestIndex = minIndex;
temp = list[smallestIndex];
list[smallestIndex] = list[index];
list[index] = temp;
}
}
What if you created a nested for loop that would start with the first element in the array, then inspect all subsequent elements, counting duplicates then deleting them?
1 2 3 4 5 6 7 8 9 10
int duplicates[10]; //indecies match up with list array
for(i = 0; i < 10; i++){
for(j = i; j < 10; j++){
if((arr[j] != NULL) && (arr[j] == arr[i])){
duplicates[i]++;
arr[j] = NULL;
}
}
}
then delete them so that there is only one of them
Notice that you can't really "delete" the elements of an array because arrays have a fixed size. You can only change their values. You could choose a value to mean "nothing", but that wouldn't allow you to work with just any integer. You could also use a dynamically allocated array and substitute it (or let std::vector do that for you), but it seems the homework assignment wasn't properly worded.
Here are some links that might be helpful. First I don't see anything in the assignment that says that you must write your own sort. Why not use std::sort? http://cplusplus.com/reference/algorithm/sort
Moreover the std algorithms are rather easy to use. You can easily count elements using std::count. If your instructor really wants you to learn c++ then he or she should be impressed that you took the time to learn how to use std algorithms that are part of the language. http://cplusplus.com/reference/algorithm/count
Here is an article that I wrote on std solutions to problems. It is worth a read. Some of it might overwhelm you but part 1 should be easy to understand and I show an example on how std::count works with both c-arrays and sequence containers. http://cplusplus.com/forum/articles/10879/
Now the only thing left is the removal of duplicates. std::unique can help but since your c-array cannot be resized you can't chop off the end. Instead you can use the iterator that is returned which tells you the new end of the array which is one beyond the last unique element. then you can use that to print the non-duplicates even though technically the duplicate values were simply moved to the end. In this case the iterator is simply a pointer to the array element so there is nothing to worry about here. http://cplusplus.com/reference/algorithm/unique/
std::set will remove unique values automatically as well. If you are able to use that then std::unique wouldn't be necessary. Again I would hope that your instructor would appreciate you finding std solutions and actually learning to write c++ code.
Wow thanks guys for the fast responces!!! I'm at work right now (on break) so I will look over and try the stuff you mentioned when I get home tonight. Again thanks so much!
kk so taking the advice of the so kind people of this forum here I have kind of fixed my code. I can input the vector, sort it and remove the duplicates, but I still need to count the duplicates before removing them so I can show how many there were:
sample input:
10 12 34 65 12 10 65
sample output:
10 2
12 2
34 1
65 2
something like the above. so here is the code I have so far:
#include <iostream>
#include <vector>
#include <algorithm>
usingnamespace std;
int main()
{
int i;
int sizea;
// int sizeb;
int temp = 0;
vector<int> scores;
vector<int>::iterator sortedunique;
cout << "Please enter the test scores." << endl;
cout << "If you are finished please enter the value -999." << endl;
while (temp != -999)
{
cin >> temp;
scores.push_back (temp);
}
sort (scores.begin(), scores.end());
scores.erase (scores.begin());
//nedd code here to count the duplicates of the sorted list I am assuming.
sortedunique = unique (scores.begin(), scores.end());
scores.resize( sortedunique - scores.begin() );
sizea = scores.size();
cout << "The list after it's sorted looks liks this: "; // this is here simply to test the sorting and duplite removal for now
for (i = 0; i < sizea; i++)
cout << scores[i] << " ";
cout << endl;
return 0;
}
The ideal solution for your problem is a std::map, as m4ster r0shi suggested. Read his post and use the reference if you need to learn more: http://www.cplusplus.com/reference/stl/map/