1D Arrays and Random Numbers Artem A. Lenskiy, PhD May 26, 2014

Embed Size (px)

DESCRIPTION

Introduction An array is a fixed-size sequenced collection of elements of the same data type. List of temperatures recorded every hour in a day, or a month, or a year. List of employees in an organization. List of products and their cost sold by a store. Test scores of a class of students. List of customers and their telephone numbers Table of daily rainfall data.

Citation preview

1D Arrays and Random Numbers Artem A. Lenskiy, PhD May 26, 2014 Introduction In addition to arrays and structures, C supports creation and manipulation of the following data structures: Linked lists Stacks Queues Trees Data Types Derived typesFundamental typesUser-defined types Arrays Functions Pointers Integral types Float types Character types Structures Unions Enumerations Introduction An array is a fixed-size sequenced collection of elements of the same data type. List of temperatures recorded every hour in a day, or a month, or a year. List of employees in an organization. List of products and their cost sold by a store. Test scores of a class of students. List of customers and their telephone numbers Table of daily rainfall data. Arrays : Declaration An array declaration tells the compiler how many elements the array contains and what the type is for these elements. The number enclosed in the brackets indicates the number of elements in the array. The numbering starts with 0. Candy[0] is the first element. Candy[364] is the 365th and last element. type variable-name [size] Arrays : Initialization When you use empty brackets to initialize an array, the compiler counts the number of items in the list and makes the array that large. sizeof days is the size, in bytes, of the whole array, and sizeof days[0] is the size, in bytes, of one element. Using const with Arrays The program treat each element in the array as a constant Array can be explicitly initialized at run time. i threshold[i] We can use scanf() to initialize array Arrays Run-time initialization Arrays Run-time initialization: Example Given the list of marks obtained by a class of 50 students in an annual examination. 43,65,51,27,79,11,56,61,82,09,25,36,07,49,55,63,74,81,49,37, 40,49,16,75,87,91,33,24,58,78,65,56,76,67,45,54,36,63,12,21 73,49,51,19,39,49,68,93,85,59 Write a program to count the number of students belonging to each of following groups of marks: Frequency Groups 0-9, 10-19, 20-29,,90-100 Arrays Run-time initialization: Example Run-time initialization: Example (cont) Frequency Groups Arrays : Not initialized If an array is not initialized, the elements might have any value. The compiler just uses whatever values were already present at those memory locations Arrays : Partially initialized 11 If an array is initialized partially, the remaining elements are set to 0. Non-valid array assignment Array Bounds int doofi[20]; It's your responsibility to make sure the program uses indices only in the range 0 through 19, because the compiler won't check for you. The compiler doesn't check to see whether the indices are valid. The result of using a bad index is, in the language of the C standard, undefined. So, the program, might seem to work, it might work oddly, or it might abort (Run-time error). Non-valid array assignment Correct indexes Incorrect indexes Specifying an Array Size wrong correct wrong Example Write a program to evaluate the following expression The values of x 1, x 2, are read from the terminal Random numbers generator A random number generator (RNG) is a computational or physical device designed to generate a sequence of numbers or symbols that lack any pattern, i.e. appear random. In C language library provides two function to generate random numbers: rand() return a pseudo-random integer between 0 and RAND_MAX (32767) srand(unsigned int seed) sets seed as the seed for a new sequence of pseudo-random integers to be return by rand(); Rand() rand() return a pseudo-random integer between 0 and RAND_MAX (32767) First run Second run Seed definition Observation: The results of several runs are identical Explanation: The initial seed used by rand() is identical each time. The seed: Used for the generation of the random numbers. Explicitly specified using the srand function srand() srand(unsigned int seed) sets seed as the seed for a new sequence of pseudo-random integers to be return by rand(); The sequences can be repeatable by calling srand() with the same seed value. The system time is a good choice as an argument for srand(). It will be different each time the program is run. Thus, results will also be different. rand() and srand() First run Second run Functions rand() and srand() are defined in. You dont need to implement them just #include. Example: Dices with rand() a[ i ] is a histogram and counts how many times a pair of dice rolled i. rand() % 6 produces random numbers in the range 0 to 5, rand() % produces random numbers in the range 1 to 6. First run Second run Example: Dices with rand() and srand() a[ i ] is a histogram and counts how many times a pair of dice rolled i. rand() % 6 produces random numbers in the range 0 to 5, rand() % produces random numbers in the range 1 to 6. First run Second run Review questions Identify errors, assuming that ROW and COLUMN are declared as symbolic constants: What happens when an array with a specified size is assigned With values fewer than the specified size; and With values more than the specified size. 23