+ Arrays & Random number generator. + Introduction In addition to arrays and structures, C supports creation and manipulation of the following data structures:

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
An Introduction to Programming with C++ Fifth Edition
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Programming Assignment 8 CS113 Spring Programming Assignment 8 Implement the sorting routine of your choice. Compare average performance of your.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Copyright Curt Hill Arrays in C/C++ What? Why? How?
ICS103 Programming in C Lecture 11: Arrays I
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
1D Arrays and Random Numbers Artem A. Lenskiy, PhD May 26, 2014.
Arrays.
Computer Programming for Engineers
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.
Module 1: Array ITEI222 - Advance Programming Language.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays, For loop While loop Do while loop
CMPE 152: Compiler Design October 4 Class Meeting
Lecture 18 Arrays and Pointer Arithmetic
Lecture 14: Problems with Lots of Similar Data
Presentation transcript:

+ Arrays & Random number generator

+ 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. 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 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. 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. Using const with Arrays The program treat each element in the array as a constant

+ 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

+ 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

+ Run-time initialization: Example (cont)

+ Frequency Group s

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

+ Assigning Array Values nonvalid array assignment 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.

+ 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. Again! The compiler doesn't check to see whether the indices are valid.

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

+ Operations with array: Example  Write a program to evaluate the following expression  The values of x1,x2,… 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 differnet.

+ rand() and srand() First run Second run

+ 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() % produces random numbers in the range 1 to 6. First run Second run

+ 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() % produces random numbers in the range 1 to 6. First run Second run

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

+ Solution for the lab problems