1 of 4 CpSc 1111 Lab 8 Arrays Overview This week, you will gain some experience with arrays, using loops to do the following declaring and initializing arrays eprinting out the values in an array e scarching for values in an aray manipulating values in an array Background Information In lecture, you have leaned that arrays are data structures that store sets of same-type data items under a single name declare and initialize arrays, including using a declaration, You have seen how to values of an array. (Random number generators are discussed below.) random number generator to in You have also seen the use of loops to iterate through arrays to perform various tasks, such as finding the largest value in the array, swapping values of two elements in the array, ctc. Don't forget that the index values for the elements in the anray begin with 0 For this lab, you will use loops to initialize 2 arrays, print the values of cach array, search for a certain value in the array and to calculate the inner product of the 2 arrays Random Number Generators rand) and srand O The function rand() will generate a (pseudo) random number from 0 to RAND MAX, which is a constant defined in
with a value of 32,767 You use the rand) function to return a value in a range by using module of the returned value by the range span+ nitial value of the range rand() % range-span + initial value where range span high low 1) For example, if you wanted 100 numbers from 0 to 99 If you wanted 100 numbers from 1 to 100 1985-2016 (nclusive) 10-30 (inclusive): rand () 100 (rand100) 1 (rand() 32) +1985 (rand 21) +10 But, using rand as explained above will produce the same setof "random" values every time you run it because it automatically seeds the random number generator with 1. You can seed the random number generator with the srand ) function using something other than 1The most common way to seed the random number generator is to use the time function, which is in the library file. This will seed the random number generator with the current time, which will be different every time you run the program, thus producing different sets of random values every time you run the program. The way to do this is by including the following line of code in your program, somewhere above the rand call: function srand (nt)te(0) Don't forget to Typically, you would only seed the random sumber generator once per every set of calls to cand #include both and at the top of your file