Array program troubles. Need assistance.

I have to create a program for one of my classes. It is extra credit. I am stump as of right now. What is the best way to accomplish this using arrays and strings. And the loops have to be for and while...in case there are others i dont know. Any help or hints would be greatly appreciated.

the program must use a loop to generate 30 random integers between or including 1 and 100 and store those integers into an array.

With a separate loop the program must find and display the maximum value in the array.

Then the program must ask the user to input a value and then search the array for that value with a third loop. If the value is found, the program must display the array position in which the value was found (where the very first position in the array is position zero.) If the value is not found in the array, the program must display the message "Not found".

For full credit, your program must use three loops as described above even if you can simplify the algorithm into fewer loops. Also, you must use this statement srand(1); to initialize the random seed. Do not use srand(time(NULL)); even though it leads to more interesting and varied sets of random numbers.

No pseudocode or test plan must be submitted with this assignment, however your program must be consistent with any sample test plan cases that may have been developed or discussed in class.

You must add the line system("PAUSE"); right before return 0; in order to receive any credit for this assignment.

Preconditions:

You are guaranteed that the user will input a valid integer.
You must hand in the following on separate pages stapled in this specified order:

The hardcopy source code for this assignment. Staple multiple pages together, if applicable.
You must also upload copies of the source file (named a4.cpp) & the executable file (named a4.exe) to the appropriate drop box in Angel (look under the Lessons tab) by the beginning of the class period on the due date.
Did you manage to do something?
well i used a random numbers function that looks like this

int main()
{
int num = 0;

// srand(time(NULL));
srand(1);

for (int i = 0; i < 30; i++)
{
num = rand() % 30 + 1;
cout << num << " ";
}

return 0;
}


Next I tried a sort funtion that just flat out didnt work. So i guess this is where im currently stuck. I tried a few other ways but they didnt have any hope.
The first sentence of the assignment states that you should store the numbers in an array...

Please use [code][/code] tags http://www.cplusplus.com/articles/firedraco1/
how do i do that with what i have.
Declare an array with 30 elements and populate it in the loop
is it possible to see an example of what i need to add?
You only need to assign to an array element ( indexed with 'i' ) in the loop instead of assigning to 'num'
http://www.cplusplus.com/doc/tutorial/arrays/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>	// for rand and srand functions
#include <ctime>
using namespace std;

int main()
{
	int i = 0;

	// srand(time(NULL));
	srand(1);

	for (int i = 0; i < 30; i++)
	{
		i = rand() % 30 + 1;
		cout << i << " ";
	}

	return 0;
}


I'm assuming this is what you meant.

Now since this part is figured out, which is the easiest way that allows me to cout the max value of the random numbers.
I have to do a similar problem for an online class. Can I see how you saved it as an Array?
@ jdawg123
You are not using any array there, read the tutorial from the link I posted
Last edited on
Topic archived. No new replies allowed.