ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C...

19
ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS

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.

Transcript of ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C...

Page 1: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS

Page 2: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

Introduction

• In addition to arrays and structures, C supports creation and manipulation of the following data structures:

• Linked lists• Stackes• Queues• Trees

Data Types

Derived types Fundamental types User-defined types

• Arrays• Functions• Pointers

• Integral types• Float types• Character types

• Structures• Unions• Enumerations

Page 3: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

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.

Page 4: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

Arrays : DeclarationAn 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.

Arrays : Initialization

power[0] 1

power[1] 2

power[2] 4

power[3] 6

power[4] 8

power[5] 16

power[6] 32

power[7] 64

Page 5: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

Arrays : Run-time initialization

Month 1 has 31 days.Month 2 has 28 days.Month 3 has 31 days.Month 4 has 30 days.Month 5 has 31 days.Month 6 has 30 days.Month 7 has 31 days.Month 8 has 31 days.Month 9 has 30 days.Month 10 has 31 days.Month 11 has 30 days.Month 12 has 31 days.Using const with Arrays

The program treat each element in the array as a constant

Array can be explicitly initialized at run time.

Page 6: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

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

i no_data[i] 0 16 1 4204937 2 4219854 3 2147348480

i some_data[i] 0 14821 10662 03 0

Not initialized Partially initialized

If an array is initialized partially, the remaining elements are set to 0.

The compiler is not so forgiving if you have too many list values. This overgenerosity is considered an error.

Page 7: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

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 sizeof operator gives the size, in bytes, of the object, or type, following it. So sizeof days is the size, in bytes, of the whole array, and sizeof days[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.

Month 1 has 31 days.Month 2 has 28 days.Month 3 has 31 days.Month 4 has 30 days.Month 5 has 31 days.Month 6 has 30 days.Month 7 has 31 days.Month 8 has 31 days.Month 9 has 30 days.Month 10 has 31 days.Month 11 has 30 days.Month 12 has 31 days.

Page 8: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

Assigning Array Values

nonvalid array assignment array assignment

Array Boundsint 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.

Page 9: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

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.

Page 10: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

Specifying an Array Size

Page 11: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

Example• Write a program using a single-

subscripted variable to evaluate the following expression:

• The values of x1,x2,… are read from the terminal.

Page 12: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

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();

Page 13: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

Rand()• rand() return a pseudo-random integer between 0 and RAND_MAX

(32767)First run

Second run

Page 14: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

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

Page 15: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

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

Page 16: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

rand() and srand()

First run

Second run

Page 17: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

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.

First run Second run

Page 18: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

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.

First run Second run

Page 19: ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.

Problem• Generate 10 random numbers

• Print them out;• Calculate and print out their mean: • Calculate and print out their standard deviation: