Need help with comparing an input to an array list

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <time.h>


using namespace std;

void randomizeSeed();
int randomRange(int min, int max);

int main()
{
	
	randomizeSeed();
	
	const int num = 1000;
	int Numbers[num] = {};
	const int minNumber = 1;
	const int 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;
}
Last edited on
closed account (48T7M4Gy)
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.
Last edited on
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.
Last edited on
This is the loop i attempted to make but it just displays a random number as End++ depending on the time i enter an input.

1
2
3
4
5
6
7
8
9
10
11
12
 for (int i = 0; i < num; i++)
    {
        Numbers[i];

        {
            if (Numbers[i] == Input)
            {
                End++;
            }
        }
    }
 cout << "Your number showed up " << End << " times." << endl;

Why do you have line 3 ??

1
2
3
4
5
6
7
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>

using namespace std;

void randomizeSeed();
int randomRange(int min, int max);

int main()
{

    randomizeSeed();

    const int num = 1000;
    int Numbers[num] = {};
    const int minNumber = 1;
    const int 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.
Last edited on
standard library... on my system it allows all those crappy "system" calls to function. :)

What does it do? http://www.cplusplus.com/reference/cstdlib/

Okay, so now it's on line 34... what's the purpose of it? It does nothing...

It doesn't == anything, you're not changing it... it's just there...
Last edited on
closed account (48T7M4Gy)
so what's the problem with this latest version?
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;
}
Last edited on
closed account (48T7M4Gy)
Are you sure about End? It looks a lot like a counter to me especially because I can't see the code to randomise it.

Similar deal with the comparison at line 37
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.
Run this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>

using namespace std;

void randomizeSeed();
int randomRange(int min, int max);

int main()
{

    randomizeSeed();

    const int num = 1000;
    int Numbers[num] = {};
    const int minNumber = 1;
    const int 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;
}
Topic archived. No new replies allowed.