Hello I need some help here. i need to make a second for loop to cycle through all the numbers in the array and compare each number to the number entered by the user. After that it need to display how many times the users number showed up in the list of 1000 numbers between 1 and 100. Any help at all getting me in the right direction would be really awesome. Thank you
#include <iostream>
#include <string>
#include <time.h>
usingnamespace std;
void randomizeSeed();
int randomRange(int min, int max);
int main()
{
randomizeSeed();
constint num = 1000;
int Numbers[num] = {};
constint minNumber = 1;
constint maxNumber = 100;
int Input = 0;
int End = 0;
cout << "Hello, I have thought of 1000 numbers between 1 and 100. Pick one number between 1 and 100 and I will let you know how many times it showed up.";
cin >> Input;
cout << " " << endl;
for (int i = 0; i < num; i++)
{
Numbers[i] = randomRange(minNumber, maxNumber);
//cout << Numbers[i] << endl;
}
for (int i = 0; i < num; i++)
{
if (Input == Numbers[i])
{
End++;
}
}
cout << End;
system("pause");
return 0;
}
void randomizeSeed()
{
srand(time(NULL));
}
int randomRange(int min, int max)
{
int randomValue = rand() % (max + 1 - min) + min;
return randomValue;
}
i need to
Step 1: make a second for loop to
Step 2: cycle through all the numbers in the array and
Step 3: compare each number to the number entered by the user. After that it need to
Step 4: display how many times the users number showed up in the list of 1000 numbers between 1 and 100.
Interestingly if I take your own words I can break them down to pseudocode. Why not try programming the first step? I bet you can. If you have trouble with that part let us know.
Note: One use functions such as randomizeSeed() is worthless. You can reduce the to 1 line inside the main. Functions are useful when you will call the same routine more than once.
for (int i = 0; i < num; i++)
{
if (Numbers[i] == Input)
{
End++;
}
cout << "Your number showed up " << End << " times." << endl;
First, I really don't understand this exercise. It promotes bad programming. You could simply place End++ to line 29 of your original code and do everything in one loop. This teaches you to waste your time and write needless code.
Second, in your original post you have 2 loops. It worked fine once I added #include <stdlib.h> to the preprocessor list...
Not sure why it showed up as on line 3 ill show you the whole block so you know where it is. The idea of the exercise is to get used to using functions, arrays, and for loops im thinking we were asked to use 2 loops. whats <stdlib.h> do?
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
usingnamespace std;
void randomizeSeed();
int randomRange(int min, int max);
int main()
{
randomizeSeed();
constint num = 1000;
int Numbers[num] = {};
constint minNumber = 1;
constint maxNumber = 100;
int Input = 0;
int End = 0;
cout << "Hello, I have thought of 1000 numbers between 1 and 100. Pick one number between 1 and 100 and I will let you know how many times it showed up." << endl;
cin >> Input;
cout << " " << endl;
for (int i = 0; i < num; i++)
{
Numbers[i] = randomRange(minNumber, maxNumber);
//cout << Numbers[i] << endl;
}
for (int i = 0; i < num; i++)
{
Numbers[i];
{
if (Numbers[i] == Input)
{
End++;
}
}
}
cout << "Your number showed up " << End << " times." << endl;
system("pause");
return 0;
}
void randomizeSeed()
{
srand(time(NULL));
}
int randomRange(int min, int max)
{
int randomValue = rand() % (max + 1 - min) + min;
return randomValue;
}
Also i understand that using making randomizeSeed() is pointless cause its only being used once but the idea is to practice making and using functions.
I dont think the input the user is entering is actually being compared to the array list and instead End is just outputting a random number depending on what time i enter the input
Easy enough... Add a cout statement in your loop. It works just fine. The variable "END" was never randomized... what makes you think that?
from line 32 -
1 2 3 4 5 6 7 8 9 10 11 12 13
for (int i = 0; i < num; i++)
{
if (Numbers[i] == Input)
cout << "I found one! " << endl;
{
End++;
}
}
cout << "Your number showed up " << End << " times." << endl;
system("pause");
return 0;
}
When putting the cout << "I found one!" << endl; the End in cout << "your number... always out puts 1000. The reason i think End is outputting a random number is because on line 29 if i take away the // and make it display the 1000 random numbers the output that End give me dose not add up the the amount of times the number the user entered really shows up in the list of 1000 numbers.
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
usingnamespace std;
void randomizeSeed();
int randomRange(int min, int max);
int main()
{
randomizeSeed();
constint num = 1000;
int Numbers[num] = {};
constint minNumber = 1;
constint maxNumber = 100;
int Input = 0;
int End = 0;
cout << "Hello, I have thought of 1000 numbers between 1 and 100. Pick one number between 1 and 100 and I will let you know how many times it showed up." << endl;
cin >> Input;
cout << " " << endl;
for (int i = 0; i < num; i++)
{ Numbers[i] = randomRange(minNumber, maxNumber);
}
for (int i = 0; i < num; i++) {
cout << i<< " ";
if (Numbers[i] == Input){
cout << Numbers[i] << " " << "Found one!" << endl;
End++;
}
else {
cout << Numbers[i] << endl;
}
}
cout << "Your number showed up " << End << " times." << endl;
system("pause");
return 0;
}
void randomizeSeed()
{
srand(time(NULL));
}
int randomRange(int min, int max)
{
int randomValue = rand() % (max + 1 - min) + min;
return randomValue;
}