Creating arrays and initiating a search within an array?

Hello all. I am fairly new to C++ and computer programming in general. Right now, I am working with arrays, and functions (arrays being a concept I can't quite grasp just yet), and I can't seem to figure out how to write this program.The assignment is to:

write a program that searches an array of a given element and, if found, returns the element. -1 otherwise.

-Create 100 random numbers between 1 to 100.
-Put all numbers in a one dimensional array.
-Ask the user to enter what number to search for.
-Call function, "find", to search the requested number.
-inform user if the number is found or not.
-Ask if the user wishes to search any other numbers within the specified array.


I understand most of the directions (kind of), except how to take the 100 randomized number and place them into the array. I also do not know how to initiate a find for an array. Can someone help me with my program?


This is what I have so far (sad, isn't it?):

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
 
int main()
{
int matrix[99]; //array defined?
int i;
//Fill array with numbers?
for (i=0, i<100, i++)
{
srand(time(NULL));
x = (rand()%100)+1;
}
 
for (int i=0, i<100, i++)
 
 
 
}
First there are several things wrong with the following snippet:
1
2
3
4
5
6
7
8
int matrix[99]; //array defined?
...
//Fill array with numbers?
for (i=0, i<100, i++)
{
srand(time(NULL));
x = (rand()%100)+1;
}

First if you try to actually use this loop to fill your array you will access your array out of bounds. In C/C++ arrays start at zero and end at size - 1. So you should be either using an array size of 100 or the loop should contain 99 not 100.

Second this snippet will not produce a random number, it will produce the same number over and over because you keep reseeding the random number generator. The srand() function should bonly be called once, before the loop.

Third I suggest you actually try to write your program, you may want to study some of the links contained in this tutorial: http://www.cplusplus.com/doc/tutorial/ there are several good links that should help you better understand arrays.

(Please try formatting your program using indentation and use those little source code tag marked '<>' at the right of the editor window of this page).

1. If you need to assign 100 numbers into matrix you should reserve space for 100 values - not only 99.

2. You may want to call srand() only once (at the beginning of) during process execution. In your example it initializes the random number generator to generate different values on each process execution. If you'll omit a call to srand() the random number generator will produce the same sequence of numbers on each process execution.

3. Did you ever tried compiling your program? You never assigned any value to your matrix - but to a nowhere declared variable x. This will result in a compilation error.

4. Have a look into cplusplus.com reference manual for Input/Output to learn how to get/put input/output from/to a user.

5. You may also want to look at the reference for Container/algorithm/find to learn how to use find.
closed account (3qX21hU5)
You might get bonus points if you write your own function for find also. Here is a example of how one might look like using vectors.

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
#include <iostream>
#include <vector>

using namespace std;

bool findNumber(const vector<int> &vec, const int &searchNumber);

int main()
{
    vector<int> numbers;
    const int number = 10;

    // Adds 0 through 99 to the vector
    for (auto index = 0; index != 100; ++index)
        numbers.push_back(index);

    // Searches the vector for the number 10
    if (findNumber(numbers, number))
        cout << "There is that number in the vector!";
}

// Searches for a given number in a vector<int>.
// Only returns true if it found it or false if it didn't
bool findNumber(const vector<int> &vec, const int &searchNumber)
{
    for (auto index = 0; index != vec.size(); ++index)
    {
        if (vec[index] == searchNumber)
            return true;
    }

    return false;
}


Maybe that can give you some ideas on the rest of your assignment to. Remember vectors and arrays share a lot of the same properties so you can rework this to use arrays instead. The find() function in the algorithm header is also a good feature to use like tcs said.

Last edited on
Topic archived. No new replies allowed.