NUMERIC ARRAYS DEBBI HAMNER CIT 336 TEACHING PRESENTATION.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Introduction to arrays
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Programming Logic and Design Sixth Edition
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
1 Controlling Script Flow! David Lash Chapter 3 Conditional Statements.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft.
PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
INTERNET APPLICATION DEVELOPMENT For More visit:
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
NUMERIC ARRAYS IN PHP BY SKYLAR NEILSON. WHAT ARE NUMERIC ARRAYS? They are arrays with a numeric index and the values are stored and accessed in linear.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Flow of Control Part 1: Selection
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
HTML Table and PHP Array
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Web Programming Language Week 5 Dr. Ken Cosh Introducing PHP 1.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
1 Module 3 Conditional Statements. Objectives  Conditional test statements to compare numerical and string data values  Looping statements to repeat.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
CPS120: Introduction to Computer Science Lecture 14 Functions.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
Introduction to PHP.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
+ Arrays & Random number generator. + Introduction In addition to arrays and structures, C supports creation and manipulation of the following data structures:
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.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Fortran: Control Structures Session Three ICoCSIS.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. Lash.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Web Database Programming Using PHP
Session 2 Basics of PHP.
Web Database Programming Using PHP
Class06 Arrays MIS 3502 Jeremy Shafer Department of MIS
REBOL Writing Functions.
Arrays MIS 3502 Jeremy Shafer Department of MIS Fox School of Business
Arrays, For loop While loop Do while loop
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
PHP.
Web DB Programming: PHP
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Web Programming Language
Programming Logic and Design Fifth Edition, Comprehensive
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

NUMERIC ARRAYS DEBBI HAMNER CIT 336 TEACHING PRESENTATION

WHAT IS AN ARRAY? An array refers to an ordered list of elements (numbers, strings or objects) that will follow a specific pattern and are stored in one value. There are typically two types of arrays that sometimes have different names but follow specific rules. NUMERIC One is called a NUMERIC or indexed array. This is where a numeric value is used to identify or store the element as well as call the element. ASSOCIATIVE The other is an ASSOCIATIVE array. These elements are identified and called by a string. MULTI DIMENSIONAL There are MULTI DIMENSIONAL arrays that are either numeric or associative as well.

THE PARAMETERS OF A NUMERIC ARRAY: Start This is the first value of the sequence. End The sequence is ended upon reaching the end value. Step If a step value is given, it will be used as the increment between elements in the sequence. Step should be given as a positive number. If not specified, step will default to 1. Return The return will display the elements from start to end NUMERIC ARRAYS NUMERIC ARRAYS can store numbers, strings and any object but their index will be represented by numbers. By default, an array index starts from zero [0].

HOW TO WRITE A PHP ARRAY: It is important to declare an array before you use it. The following are two examples of an array with five elements: <?php // first method to create a numeric array $numbers = array(1, 2, 3, 4, 5); for each($numbers as $value) { echo "Value is $value "; } ?> <?php // second method to create a numeric array $numbers [0] = "one"; $numbers [1] = "two"; $numbers [2] = "three"; $numbers [3] = "four"; $numbers [4] = "five"; for each($numbers as $value) { echo "Value is $value "; } ?> This will show as: Value is 1 Value is 2 Value is 3 Value is 4 Value is 5 This will show as: Value is one Value is two Value is three Value is four Value is five

GETTING THE LENGTH OF AN ARRAY: <?php $numbers = array ("1", "2", "3", "4", "5"); echo count ($numbers); ?> The Result 5 Array index out of bounds This run-time error is reported whenever a formula references an array with an index that is outside the valid boundaries of the array. count() function The count() function is used to return the length (the number of elements) in an array. The number of elements is called the length of the array. The last element in the array has an index one less than the array length. To avoid bound errors, you will want to know how many elements are in an array. The length field returns the number of elements Example of a Bound Error : $array a[10] $array x = 10 $a[9] = 3; ok $a[x] = 4; 10 is not within the range 0..9

HOW TO LOOP THROUGH AN ARRAY: <?php $numbers = array("1", "2", "3", "4", "5"); $arrlength = count ($numbers); for($x = 0; < $arrlength; $x++) { echo $numbers[$x]; echo " "; } ?> Note: numbersvaluevalues The words “numbers”, “value” and “values” that have been used in the previous examples, can be replaced by any identifier. Loops can execute a block of code a number of times. Loops are handy, if you want to run the same code over and over again, each time with a different value.

ADDITIONAL EXAMPLES: simple array // simple array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) foreach (range(0, 12) as $number) { echo $number; } Note: Character sequence values Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used. step // The step parameter // array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) foreach (range(0, 100, 10) as $number) { echo $number; } character sequences // Usage of character sequences // array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i') foreach (range('a', 'i') as $letter) { echo $letter; } letter array // letter array('c', 'b', 'a'); foreach (range('c', 'a') as $letter) { echo $letter; }

DISPLAY ELEMENTS RANDOMLY: srand() The PHP srand() function is used to generate a random number or element. This function can generate numbers with-in a given range. The random number generator should be seeded to prevent a regular pattern of numbers being generated. srand() This is achieved using the srand() function that specifies the seed number as its argument. Shown above - Example 1 <?php srand( microtime() * ); $num = rand( 1, 4 ); switch( $num ) { case 1: $image_file = "/home/images/alfa.jpg"; break; case 2: $image_file = "/home/images/ferrari.jpg"; break; case 3: $image_file = "/home/images/jaguar.jpg"; break; case 4: $image_file = "/home/images/porsche.jpg"; break; } echo "Random Image : "; ?> This example shows how to display images in an array randomly. srand — Seed the random number generator Example 2

USING HTML FORMS: <?php if( $_POST["name"] || $_POST["age"] ) { echo "Welcome ". $_POST['name']. " "; echo "You are ". $_POST['age']. " years old."; exit(); } ?> " method="POST"> Name: Age: Note: This is not an array function but may be useful information that is relevant to this class especially since we are learning database format..

RESOURCES: Thank you for your time. I hope this helps you understand arrays. Have a great day.