1 / 23

Arrays & Random number generator

Arrays & Random number generator. Introduction. Data Types. In addition to arrays and structures, C supports creation and manipulation of the following data structures: Linked lists Stacks Queues Trees. Derived types. Fundamental types. User-defined types. Arrays Functions Pointers.

livana
Download Presentation

Arrays & Random number generator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays & Random number generator

  2. Introduction Data Types • In addition to arrays and structures, C supports creation and manipulation of the following data structures: • Linked lists • Stacks • Queues • Trees Derived types Fundamental types User-defined types • Arrays • Functions • Pointers • Integral types • Float types • Character types • Structures • Unions • Enumerations

  3. 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.

  4. Arrays : Declaration An array declaration tells the compiler how many elements the array contains and what the type is for these elements. type variable-name [size] 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. Arrays : Initialization

  5. 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. • The sizeofoperator gives the size, in bytes, of the object, or type, following it. So sizeofdays is the size, in bytes, of the whole array, and sizeofdays[0] is the size, in bytes, of one element. Dividing the size of the entire array by the size of one element tells us how many elements are in the array. Using const with Arrays The program treat each element in the array as a constant

  6. Arrays : Run-time initialization • Array can be explicitly initialized at run time. • The first 50 elements of the array sum are initialized to zeros while the remaining 50 elements are initialized to 1.0 at run time. • We can use scanf() to initialize array

  7. 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: 0-9, 10-19, 20-29,……,90-100

  8. Run-time initialization: Example (cont)

  9. Run-time initialization: Example (cont) Frequency Groups

  10. Arrays : Not initialized Not initialized Partially 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 If an array is initialized partially, the remaining elements are set to 0.

  11. Assigning Array Values array assignment nonvalid array assignment Array Bounds • 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 is not so forgiving if you have too many list values. This overgenerosity is considered an error.

  12. Again! The compiler doesn't check to see whether the indices are valid. 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. That means when you run the program, it might seem to work, it might work oddly, or it might abort.

  13. Specifying an Array Size • The array size can be any constant value greater or equal to 1.

  14. Operations with array: Example • Write a program to evaluate the following expression • The values of x1,x2,… are read from the terminal

  15. 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 intseed) sets seed as the seed for a new sequence of pseudo-random integers to be return by rand();

  16. Rand() • rand() return a pseudo-random integer between 0 and RAND_MAX (32767) First run Second run

  17. 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

  18. srand() srand(unsigned intseed) • 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 differnet.

  19. rand() and srand() First run Second run

  20. Example: Dices with rand() a[i] counts how many times a pair of dice rolled i. rand() % 6 produces random numbers in the range 0 to 5, rand() % 6 + 1 produces random numbers in the range 1 to 6. Second run First run

  21. Example: Dices with srand() a[i] counts how many times a pair of dice rolled i. rand() % 6 produces random numbers in the range 0 to 5, rand() % 6 + 1 produces random numbers in the range 1 to 6. Second run First run

  22. Problem for self-study • Generate 10 random numbers • Print them out • Calculate and print out their mean: • Calculate and print out their standard deviation

  23. Solution for the lab problems

More Related