please help me with this progam, its a c++ program, "(Printing distinct numbers) Write a program that reads in 10 numbers and display distinct numbers(i.e.,if a number appears multiple times, it is displayed only once ). Hint: Read a number and store it in an array if it is new. If the number is already in the array, discard it. After the input, the array contains the distinct numbers. Please just post it here.......if u do thank u in advance and god bless
C++ program help?
This is actually not hard at all. I'll set this up for you, but remember that if you really want to be a computer programmer, this type of thing has to become second nature to you, so take this apart and put it back together again until you really understand it. I'm ignoring error checking and the like for clarity.
int main()
{
int foundArray[10] = {0}; //allocate array for found values
int valuesInArray = 0;
bool valueWasFound;
cout %26lt;%26lt; "enter ten values" %26lt;%26lt; endl;
// get ten values from the user
for (int input = 0; i %26lt; 10; i++)
{
valueWasFound = false;
int newValue;
cin %26lt;%26lt; newValue;
//search array for this value
for (int search = 0; search %26lt; valuesInArray; search++)
{
if (foundArray[i] == valuesInArray)
{
valueWasFound = true;
break; //exiting now improves performance.
}
} // end search
if (!valueWasFound)
{
// value not found, let's add it.
foundArray[++valuesInArray] = newValue;
}
} //end loop for user input
// output any values in the array
for (int i = 0; i %26lt; valuesInArray; i++)
{
// output foundArray[i];
}
return 0;
}
This kind of thing comes up in programming all the time, where you need to know how many unique values you have. The basic idea here is to have an empty array and add only the unique values to it. It's not much different than the simpler problem "input ten numbers, add them to an array," except that you're first searching the array and then adding values.
Now, if this weren't for a class, the problem becomes much easier:
#include %26lt;iostream%26gt;
#include %26lt;algorithm%26gt;
#include %26lt;iterator%26gt;
#include %26lt;set%26gt;
using namespace std;
int main()
{
std::set%26lt;int%26gt; uniqueValues;
cout %26lt;%26lt; "enter ten values." %26lt;%26lt; endl;
copy (istream_iterator%26lt;int%26gt;(cin), istream_iterator%26lt;int(), back_inserter(uniqueValues));
cout %26lt;%26lt; "unique values:" %26lt;%26lt; endl;
copy (uniqueValues.begin(), uniqueValues.end(), ostream_iterator%26lt;int%26gt;(coutn, "\n"));
return 0;
}
You can see that this version is much, much smaller. For larger arrays (100+ values, for example) it would also be faster. I'm guessing that you haven't covered STL or templates yet, though. Still, I dare you to submit this version to your teacher. :)
Reply:First. Do your own homework.
Second. As an over view
First you need to take in user input.
Second you need a way of storing this input in something. (Maybe an array)
Then you need to check if that input has already been inputed.
If it has ignore it. Otherwise print it.
Good Luck
Reply:May be you can contact a C++ expert to get homework help. Check websites like http://getafreelnacer.com/ as example.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment