• Advertisement

Archived

This topic is now archived and is closed to further replies.

Beginner Problem!!

This topic is 5918 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings, I am total novice as I only started learning programming at the beginning of January. I would therefore be grateful if anybody could offer me advice on the following: I am trying to write a program that picks six random numbers between 1 & 49, and then displays them in numerical order. This I have managed but am unable to stop two identical numbers coming out. Any ideas? Secondly, I have studied functions and briefly OOP, would these make the code simpler in any way. Finally I have read that goto statements are not very popular. Have I done the right thing by including them here? What would be the alternatives? Thanks in advance. #include <iostream> #include <ctime> #include <cstdlib> #include <vector> using namespace std; char no;// user exits program int main() { vectorNumbers(6); vectorSorted(6); srand(time(0)); short HighestNumber; short HighestIndex; short i, k; cout <<"Welcome to the Lottery Wizard." <>no; if (no == ''n'') { return (0); } else { Begin: for(i=0; i<6; i++) { Numbers = rand() % (49) + 1; } // sort the numbers for (i=0; i<6; i++) { HighestNumber=0; HighestIndex=0; for (k=0; k<6; k++) { if (Numbers[k] > HighestNumber) { HighestNumber = Numbers[k]; HighestIndex = k; } } Sorted[i] = HighestNumber; Numbers[HighestIndex] = 0; } } cout <<"You''re Lucky Numbers are:" <>no; if (no == ''n'') { return (0); } else { goto Begin; } } Here''s my code.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

char no;// user exits program

int main()
{
vector<short>Numbers(6);
vector<short>Sorted(6);
srand(time(0));
short HighestNumber;
short HighestIndex;
short i, k;

cout <<"Welcome to the Lottery Wizard." <<endl;
cout <<"Would you like me to pick your numbers? y for yes, n for no.\n";
cin >>no;
if (no == ''n'')
{
return (0);
}
else
{

do
{
for(i=0; i<6; i++)
{
Numbers = rand() % (49) + 1;
}

// sort the numbers
for (i=0; i<6; i++)
{
HighestNumber=0;
HighestIndex=0;
for (k=0; k<6; k++)
{
if (Numbers[k] > HighestNumber)
{
HighestNumber = Numbers[k];
HighestIndex = k;
}
}

Sorted[i] = HighestNumber;
Numbers[HighestIndex] = 0;
}
}

cout <<"You''re Lucky Numbers are:" <<endl;
cout <<endl;
cout <<Sorted[5] <<endl
<<Sorted[4] <<endl
<<Sorted[3] <<endl
<<Sorted[2] <<endl
<<Sorted[1] <<endl
<<Sorted[0];
cout <<endl;

cout << "Would you like another draw? y for yes, n for no.\n";
cin >> no;
} while( no != ''n'' )


return (0);
}



Share this post


Link to post
Share on other sites

  • Advertisement