Array Session 9

Similar documents
Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

Review of Important Topics in CS1600. Functions Arrays C-strings

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Chapter 7 Array. Array. C++, How to Program

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

Introduction to Computer Science Midterm 3 Fall, Points

Exam 3 Chapters 7 & 9

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Homework #3 CS2255 Fall 2012

Arrays. Arizona State University 1

FORM 2 (Please put your name and form # on the scantron!!!!)

a data type is Types

Subject: Fundamental of Computer Programming 2068

Downloaded from

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

Data types. CISC 1600/1610 Computer Science I. Array syntax. Memory allocation. Zero-indexing 4/4/2016. Arrays

Tema 6: Dynamic memory

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

UNIT-I Fundamental Notations

A Quick Introduction to C Programming

Pointers in C/C++ 1 Memory Addresses 2

OBJECT ORIENTED PROGRAMMING

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Input And Output of C++

Creating a C++ Program

6. User Defined Functions I

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Introduction to C++ with content from

Pointers and Arrays 1

Arrays. Week 4. Assylbek Jumagaliyev

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Programming for Engineers Arrays

An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store

Pointers and Dynamic Arrays

CS2255 HOMEWORK #1 Fall 2012

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Lexical Considerations

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

C++ PROGRAMMING SKILLS Part 4: Arrays

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

Array. Arrays. Declaring Arrays. Using Arrays

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology

Lexical Considerations

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

CHAPTER 4 FUNCTIONS. 4.1 Introduction

7/21/10 MOTIVATION. Did you see Inception this weekend? COMP 10 EXPLORING COMPUTER SCIENCE. Lecture 6 Arrays

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

ME964 High Performance Computing for Engineering Applications

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

UIC. C Programming Primer. Bharathidasan University

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Boolean Algebra Boolean Algebra

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Introduction to Programming Using Java (98-388)

Arrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures.

UNIT- 3 Introduction to C++

Exercise 1.1 Hello world

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

Unit 3 Decision making, Looping and Arrays

Chapter 7: Arrays Copyrig opy ht rig 2012 Pea e rson a Educa Educ ti a on, Inc I. nc

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

CSI33 Data Structures

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

A structure is an aggregate data type which contains a fixed number of heterogeneous components.

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Chapter 2: Introduction to C++

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Pointers, Dynamic Data, and Reference Types

Exercise: Inventing Language

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

pointers + memory double x; string a; int x; main overhead int y; main overhead

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values

Functions, Arrays & Structs

Chapter 9. Pointers and Dynamic Arrays

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

What have we learned about when we learned about function parameters? 1-1

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Fundamentals of Programming Session 14

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Transcription:

Array Session 9

Arrays Array index starts with zero The last index in an array is num 1 where num is the no of elements in a array int a[5] is an array that stores 5 integers a[0] is the first element where as a[4] is the fifth element We can also have arrays with more than one dimension float a[5][5] is a two dimensional array. It can store 5x5 = 25 floating point numbers The bounds are a[0][0] to a[4][4]

Structures Structures are user defined data types It is a collection of heterogeneous data It can have integer, float, double or character data in it We can also have array of structures struct <<structname>> { members; }element; We can access element.members;

Arrays Arrays in C are composed of a particular type, laid out in memory in a repeating pattern. Array elements are accessed by stepping forward in memory from the base of the array by a multiple of the element size. /* define an array of 10 chars */ char x[5] = { t, e, s, t, \0 }; /* accessing element 0 */ x[0] = T ; /* pointer arithmetic to get elt 3 */ char elt3 = *(x+3); /* x[3] */ Brackets specify the count of elements. Initial values optionally set in braces. Arrays in C are 0-indexed (here, 0..9) x*3+ == *(x+3) == t (NOT s!) /* x[0] evaluates to the first element; * x evaluates to the address of the * first element, or &(x[0]) */ /* 0-indexed for loop idiom */ #define COUNT 10 char y[count]; int i; for (i=0; i<count; i++) { /* process y[i] */ printf( %c\n, y[i]); } What s the difference between char x[] and char *x? For loop that iterates from 0 to COUNT-1. Memorize it! Symbol Addr Value char x [0] 100 t char x [1] 101 e char x [2] 102 s char x [3] 103 t char x [4] 104 \0 4

Array definition: A collection of data of same type Introduction to Arrays First "aggregate" data type Means "grouping" int, float, double, char are simple data types Used for lists of like items Test scores, temperatures, names, etc. Avoids declaring multiple simple variables Can manipulate "list" as one entity

Declaring Arrays Declare the array allocates memory int score[5]; Declares array of 5 integers named "score" Similar to declaring five variables: int score[0], score[1], score[2], score[3], score[4] Individual parts called many things: Indexed or subscripted variables "Elements" of the array Value in brackets called index or subscript Numbered from 0 to size - 1

Access using index/subscript printf( %d,score*3+); Note two uses of brackets: In declaration, specifies SIZE of array Anywhere else, specifies a subscript Size, subscript need not be literal int score[max_scores]; score[n+1] = 99; If n is 2, identical to: score[3] Accessing Arrays

Array Usage Powerful storage mechanism Can issue command like: "Do this to i th indexed variable" where i is computed by program "Display all elements of array score" "Fill elements of array score from user input" "Find highest value in array score" "Find lowest value in array score"

Array Program Example: Display 5.1 Program Using an Array (1 of 2)

Array Program Example: Display 5.1 Program Using an Array (2 of 2)

for-loops with Arrays Natural counting loop Naturally works well "counting thru" elements of an array Example: for (idx = 0; idx<5; idx++) { cout << score[idx] << "off by " << max score[idx] << endl; } Loop control variable (idx) counts from 0 5

Array indexes always start with zero! Zero is "first" number to computer scientists C++ will "let" you go beyond range Unpredictable results Major Array Pitfall Compiler will not detect these errors! Up to programmer to "stay in range" Copyright 2006 Pearson Addison- Wesley. All rights reserved. 5-12

Major Array Pitfall Example Indexes range from 0 to (array_size 1) Example: double temperature[24]; // 24 is array size // Declares array of 24 double values called temperature They are indexed as: temperature*0+, temperature*1+ temperature*23+ Common mistake: temperature[24] = 5; Index 24 is "out of range"! No warning, possibly disastrous results Copyright 2006 Pearson Addison- Wesley. All rights reserved. 5-13

Defined Constant as Array Size Always use defined/named constant for array size Example: const int NUMBER_OF_STUDENTS = 5; int score[number_of_students]; Improves readability Improves versatility Improves maintainability

Uses of Defined Constant Use everywhere size of array is needed In for-loop for traversal: for (idx = 0; idx < NUMBER_OF_STUDENTS; idx++) { // Manipulate array } In calculations involving size: lastindex = (NUMBER_OF_STUDENTS 1); When passing array to functions (later) If size changes requires only ONE change in program!

As simple variables can be initialized at declaration: int price = 0; // 0 is initial value Arrays can as well: int children[3] = {2, 12, 1}; Equivalent to following: int children[3]; children[0] = 2; children[1] = 12; children[2] = 1; Initializing Arrays

As arguments to functions Indexed variables An individual "element" of an array can be function parameter Entire arrays Arrays in Functions All array elements can be passed as "one entity"

Indexed Variables as Arguments Indexed variable handled same as simple variable of array base type Given this function declaration: void myfunction(double par1); And these declarations: int i; double n, a[10]; Can make these function calls: myfunction(i); // i is converted to double myfunction(a[3]); // a[3] is double myfunction(n); // n is double

Entire Arrays as Arguments Formal parameter can be entire array Argument then passed in function call is array name Called "array parameter" Send size of array as well Typically done as second parameter Simple int type formal parameter

Entire Array as Argument Example:

Entire Array as Argument Example Given previous example: In some main() function definition, consider this calls: int score[5], numberofscores = 5; fillup(score, numberofscores); 1 st argument is entire array 2 nd argument is integer value Note no brackets in array argument!

What s really passed? Think of array as 3 "pieces" Address of first indexed variable (arrname[0]) Array base type Size of array Only 1 st piece is passed! Array as Argument: How? Just the beginning address of array Very similar to "pass-by-reference"

May seem strange No brackets in array argument Must send size separately Array Parameters One nice property: Can use SAME function to fill any size array! Exemplifies "re-use" properties of functions Example: int score[5], time[10]; fillup(score, 5); fillup(time, 10);

Array is collection of "same type" data Indexed variables of array used just like any other simple variables for-loop "natural" way to traverse arrays Programmer responsible for staying "in bounds" of array Array parameter is "new" kind Similar to call-by-reference Summary

Exercise 8.1 1. /*Materi : Type Data Array 2. Kasus : Menentukan bilangan terbesar dari bilangan-bilangan yang 3. tersimpan dalam variabel array. 4. */ 5. 6. #include<stdio.h> 7. #include<conio.h> 8. 9. void main() 10. { 11. int bil[15] ; 12. int x,max ; 13. clrscr();

Exercise 8.2 14. //Memberi nilai variabel array 15. bil[1]=17; bil[2]=23; bil[3]=20;bil[4]=10;bil[5]=15; 16. bil[6]=5; bil[7]=33; bil[8]=95;bil[9]=8;bil[10]=88; 17. // Menampilkan Bilangan dari var Array 18. for(x=1;x<=10;x++) { 19. printf("bilangan Array ke %2d adalah : %2d\n",x,bil[x]); 20. } 21. //Menentukan Bilangan Terbesar 22. Max=0; 23. for(x=1;x<=10;x++) { 24. if(bil[x] > Max) Max = bil[x]; 25. } 26. printf("\nbilangan Terbesarnya adalah : %d",max); 27. getch(); 28. }

Task 8 Write a program to find the minimum number from n numbers that entries from keyboard.