PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

C Language.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Lecture 2 Introduction to C Programming
Introduction to C Programming
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Systems Programming - Recitation Omer Kotlicki Two instances: 1.Tuesdays 15:00-16:00; Kitot Wednesdays.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Programming with Collections Collections in Java Using Arrays Week 9.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Java Syntax Primitive data types Operators Control statements.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need.
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
JavaScript, Third Edition
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
C ARRAYS -a collection of same type data, 1D, 2D- © 1/25.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 8: Arrays.
UNIT II. -set of homogeneous data items. Eg: int arrays can hold only integers and char arrays can only hold characters. Arrays have a type, name, and.
Fundamental Programming: Fundamental Programming Introduction to C++
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Computer Programming for Engineers
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
For Friday Read No quiz Program 6 due. Program 6 Any questions?
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
26/06/ :14:35 CSC Alliance — 1 Kimera Richard Phone: INSTITUTE OF COMPUTER SCIENCE DEPARTMENT.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Basic concepts of C++ Presented by Prof. Satyajit De
INC 161 , CPE 100 Computer Programming
Arrays in C.
11/10/2018.
Arrays, For loop While loop Do while loop
Just Enough Java 17-May-19.
Presentation transcript:

PHYS 2020 Making Choices; Arrays

Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the same type (e.g. char, int, float, double).  Lets look at a 1-D array as a simple example.  Imagine that I have six children (I really only have four, but I am counting my two dogs) and I wish to store their ages in a computer program, to print out when necessary (in case I get Alzheimer's or something!)

Declaring Arrays  In this case I will declare the array, and also assign the values to each element.  How do we declare an array? Almost just like we declare any single variable, but with an important difference.  If I want to declare an int variable, I use:  int age  If I want to declare an array I use  int age[6]  The square brackets indicate that "age" is an array.  The six indicates that the array has six values.

Addressing Arrays  Arrays have an index variable, which starts at 0, and goes to the (maximum number of elements –1)  e.g. int my_array[5] has members  my_array[0] to my_array[4]  To print out the first member use:  Printf(“Here is the first value in the array, %d \m”, my_array[0] );

Reading Data into an Array  Very like reading data into any other variable, but the addressing is a bit different  Recall that the statement  Scanf(“%d”, &my_variable);  will read whatever is typed in at the keyboard into the variable my_variable, which in this case must be declared to be an integer. The & is the operator that returns the address of my_variable  The array is similar, but you don’t need the & as we always refer to an array by it address: my_array[0] is the address of the first element.

Arrays of Strings  Recall that in C there is no special string type. Instead we declare an array of char variables:  char my_variable[20]; is a variable which can hold up to 20 characters.  For an array, you need a second specification, which is the number of 20 character strings you want to store:  char my_variable[20][10]; is an array which can hold up to 10 strings of 20 characters each.

The IF statement  The IF statement is great for comparing things. To use it you need to know the C language comparison operators.  These are  == "is equal to"(note this is different to the assignment =, where you place a value in a variable or do a mathematical operation: e.g. number =5)  < "less than"  > "greater then"  <= "less than or equal to"  >= "greater then or equal to"  != "not equal to"

The IF statement has the following syntax:  if(compare one thing with another)  {  Do these things if comparison is true;  }  notice that there is no semicolon at the end of the brackets of the IF statement.

The break keyword  break is a very handy keyword. The break keyword allows you to get out of any C loop (e.g. for or while), which can be useful if you have accidentally sent the computer into an infinite loop.Of course, you can always get out by pressing control-C, but that terminates the whole program.