Advanced Computer Programming

Similar documents
Computer Programming: C++

Chapter 6: Using Arrays

Advanced Computer Programming

Advanced Computer Programming

Arrays, Strings and Collections

Last Class. More on loops break continue A bit on arrays

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

4/29/03 Doc 25 C# Arrays, Indexers & Exceptions slide # 1

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Computer programming Code exercises [1D Array]

YEAHtzee. Tuesday Nov Aamnah Khalid and Peter Maldonado

Arrays. Eng. Mohammed Abdualal

Instructor: Eng.Omar Al-Nahal

Pointers, Arrays and Parameters

Declaring Array Variable

6: Arrays and Collections

Computer Science & Engineering 150A Problem Solving Using Computers

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

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

6: Arrays and Collections

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

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Programming Languages and Techniques (CIS120)

Principles of Programming. Chapter 6: Arrays

Procedural Programming

Arrays and Strings. Arash Rafiey. September 12, 2017

Object Oriented Programming. Java-Lecture 6 - Arrays

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Arrays and Collections. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Example: Computing prime numbers

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Object oriented programming C++

For example, when your method is added to the class with the main method below, the output should be:

Objectives of This Chapter

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Chapter 9 Introduction to Arrays. Fundamentals of Java

How to declare an array in C?

Array Basics: Outline

Lecture 14. 'for' loops and Arrays

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

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

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Arrays. Chapter 7. Walter Savitch Frank M. Carrano

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals

Errata for C++ and Algorithmic Thinking for the Complete Beginner

Chapter 8 Multi-Dimensional Arrays

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE SAMPLE CSE21

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

C Programming, Autumn 2013, Exercises for the Second Week

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

Last Class. While loops Infinite loops Loop counters Iterations

Arrays. 1 Index mapping

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

EXERCISES SOFTWARE DEVELOPMENT I. 04 Arrays & Methods 2018W

Module 7: Arrays (Single Dimensional)

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Steps of initialisation:

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

C++ Lecture 5 Arrays. CSci 588: Data Structures, Algorithms and Software Design.

Lecture 15. Arrays (and For Loops)

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Module 201 Object Oriented Programming Lecture 10 - Arrays. Len Shand

Declaring and ini,alizing 2D arrays

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

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

PROGRAMMING IN C AND C++:

Problems with simple variables

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

Functions. Arash Rafiey. September 26, 2017

Programming Basics. Digital Urban Visualization. People as Flows. ia

1. Find the output of following java program. class MainClass { public static void main (String arg[])

Array Basics: Outline

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Programming Studio #9 ECE 190

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

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

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

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

REFERENCES, POINTERS AND STRUCTS

Do not open this examination paper until instructed to do so. Section A: answer all the questions. Section B: answer all the questions.

egrapher Language Reference Manual

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Linked Memory. Pointers Linked Lists. January 19, 2018 Cinda Heeren / Geoffrey Tien 1

LECTURE 17. Array Searching and Sorting

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Tema 6: Dynamic memory

Transcription:

Hazırlayan Yard. Doç. Dr. Mehmet Fidan

ARRAYS A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays are has System.Array type. So that they could use all functions of System.Array class.

ARRAYS As a simple example, think of days within a week. This a one dimensional array whose first element is monday and last element is sunday. Another example is, the days within a month forms 2D (two dimensional) array. At horizontal there are the days of week and at horizontal dimention there are weeks. We may think days of a year as a 3D array. 1st dimension is days of week, 2nd dimension is week and 3rd dimension is months.

ARRAYS One dimensional array is defined with a variable name, type and its length given between square brackets. Exaple, int[] days = new int[ 7 ]; statement defines a one dimensional array named days with seven elements. Since this is a integer array, elements of it will take initial value of zero.

USE OF ARRAY After defining array, we access its elements with array name and index number within square brackets, e.g. name_of_array[index] In C# language first element in an array is at index zero (0). Forexample, we can access at least 0 and at most 6 th index in days array The numbers within square brackets are called index numbers.

INDEX NUMBERS int[] day = new int[7]; day[0] day[1] day[2] day[3] day[4] day[5] day[6] 2 6 6 4 12 54-10 Examples: day[5] = 1; if( day[5] == 4 ) break; day[5] = day[6] - 1;

EXAMPLE Variable i will take values from 0 to 6 within for loop

INITIALIZATION OF ARRAYS As in the case of variables, we could also set initial values to the arrays in the definition phase. static void Main(string[] args) { int[] day = { 0,2,4,6,8,10,11 };... }

INITIALIZATION OF ARRAYS static void Main(string[] args) { int[] day = { 0,2,4,6,8,10,11 };... } Compiler counts the integer values and decides the value in the[] brackets is 7 and compiles the program as int[] gun = new int[7]

INITIALIZATION OF ARRAYS If we are not assigning initial values to an array, then elements of array will take initial values depending on the type of the array. For example numerical types will be zero and string and other reference types will be null. Warning: Remember that a variable of type string having value of null null doesn t mean that it is an empty string. null means that there is no memory allocated for that variable and its value can not be used in expressions yet.

EXAMPLE Define an array of double type that has 10 elements, then assign values between 1.0 and 10.0. Finally swap elements at 0 and 9 with each other. static void Main(string[] args) { double[] arr = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; double tmp; tmp = arr[9]; arr[9] = arr[0]; arr[0] = tmp; }

EXERCISE Write a program that generates 100 random numbers and puts them to an array. Program should write smallest and biggest numbers in the array to the output. Also program should calculate the mean value of the array and print it to the output.

EXERCISE - SOLUTION The program below finds maximum and minimum values. Add calculation of mean value feature to the program.

Array.Length() Used in finding the length of the array. Called right after the. mark after the array name. int[] myarray = new int[5]; int len = myarray.length(); Console.WriteLine(len); Output : 5

Foreach Loop This is a loop that iterates for each element in a loop. It is forward only. It look like for loop

Foreach Loop foreach and for loops can be converted to each other.

MULTIDIMENSIONAL ARRAYS These are arrays with more than one dimension. As an example we can use multidimensional arrays for modeling a chess board.

STANDARD MULTIDIMENSIONAL ARRAYS Creation of multidimensional arrays are similar to one dimensional ones. The difference is there is a comma between the square brackets. int [,] numbers; string [,,] lines; As an example, a string type two by three multidimensional array can be created with string[,] array = new string[2,3];

STANDARD MULTIDIMENSIONAL ARRAYS All dimension lengths are equal in standard arrays. Therefore they look like matrices. array 1 2 3 9 20 5 2 4 0 6 7 0 4 2 11

STANDARD MULTIDIMENSIONAL ARRAYS Lets create a 2D multidimensional array and assign values to its elements in a loop.

ARRAY OPERATIONS Searching and Sorting Some of the most used array functions in programming; IndexOf() LastIndexOf() Sort() Reverse() They are defined within Array class.

IndexOf() LastIndexOf() IndexOf() : Starts searching from first element and returns the index number of element found. LastIndexOf() : Starts searching from last element and returns the index number of element found. If the element that we are looking for is not in the list, that these function return -1.

Array.IndexOf() Array.LastIndexOf() Result : 1 Result : 4

Array.Sort() Sorts the elements of the array by ascending order. Result : Ali Mehmet Pınar Zeynep

Array.Reverse() Reverses the order of the elements of the array. Result : 45 32 9 5 3 2 1 Not : Array.Sort and Array.Reverse does not have to be used together.

Problem If we don t know the size of the array in advance? What if array expands dynamically?