BBS 514 YAPISAL PROGRAMLAMA (STRUCTURED PROGRAMMING)

Similar documents
Array. Arrays. Declaring Arrays. Using Arrays

Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays

Arrays. Systems Programming Concepts

C Arrays Pearson Education, Inc. All rights reserved.

C++ PROGRAMMING SKILLS Part 4: Arrays

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

C: How to Program. Week /Apr/23

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

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Lecture 04 FUNCTIONS AND ARRAYS

Multiple-Subscripted Arrays

Content. In this chapter, you will learn:

Chapter 5 - Functions

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Arrays. Outline. Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays Prentice Hall, Inc. All rights reserved.

ONE DIMENSIONAL ARRAYS

Computer Science & Engineering 150A Problem Solving Using Computers

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

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

C: How to Program. Week /Apr/16

Programming for Engineers Arrays

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

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

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

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

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

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

Arrays Introduction. Group of contiguous memory locations. Each memory location has same name Each memory location has same type

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Principles of Programming. Chapter 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.

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

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

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Procedural Programming

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

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

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

Two Dimensional Array - An array with a multiple indexs.

"'"' .;, b) F or ( c = l ; c <= 10,"' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Module 6: Array in C

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

Two Dimensional Array - An array with a multiple indexs.

Chapter 6 Arrays and Strings Prentice Hall, Inc. All rights reserved.

Chapter 7 Functions. Now consider a more advanced example:

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

Lesson 7. Reading and Writing a.k.a. Input and Output

Arrays. Week 4. Assylbek Jumagaliyev

CS 108 Computing Fundamentals. October/November Array Bootcamp

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Single Dimension Arrays

How to declare an array in C?

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

C Programming Review CSC 4320/6320

Arrays (Deitel chapter 7)

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Object Oriented Programming. Java-Lecture 6 - Arrays

ECE264 Fall 2013 Exam 1, September 24, 2013

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

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

BITG 1113: Array (Part 1) LECTURE 8

Introduction to Computer Science Midterm 3 Fall, Points

PROGRAMMAZIONE I A.A. 2017/2018

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

More Arrays. Last updated 2/6/19

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Computer Programming Lecture 14 Arrays (Part 2)

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Chapter 3: Arrays and More C Functionality

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

FORTRAN - ARRAYS. For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Subject: Fundamental of Computer Programming 2068

BSM540 Basics of C Language

C++ Arrays. C++ Spring 2000 Arrays 1

by Pearson Education, Inc. All Rights Reserved. 2

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CSC 211 Intermediate Programming. Arrays & Pointers

Programming for Electrical and Computer Engineers. Pointers and Arrays

One Dimension Arrays 1

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

Preview from Notesale.co.uk Page 2 of 79

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

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

Transcription:

1 BBS 514 YAPISAL PROGRAMLAMA (STRUCTURED PROGRAMMING) 6 LECTURE 7: ARRAYS Lecturer: Burcu Can BBS 514 - Yapısal Programlama (Structured Programming)

Arrays (Diziler) Array (Dizi) Group of consecutive memory locations (Birgrup ardışık bellek konumları) Same name and type (Aynı ad ve türe sahip) To refer to an element, specify (Belli bir elemana ulaşmak için) Array name (Dizi adı) Position number (Pozisyon numarası) Format: (Format) arrayname[ position number ] First element at position 0 (1. eleman 0. konumdadır) n element array named c: (c adında bir dizi) n c[0], c[1]...c[n 1] 2

Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] -45 6 0 72 1543-89 0 62-3 1 6453 78 Position number of the element within array c 3

Array Elements (Dizi Elemanları) Array elements are like normal variables (Dizi elemanları normal değişken gibidir.) c[0] = 3; printf( "%d", c[0] ); We can perform operations in subscript. e.g. If x equals 3 c[5-2] == c[3] == c[x] c[x+1] == c[4] c[x-1] == c[2] 4

Declaring Arrays (Dizi Tanımlamak) When declaring arrays, specify (Dizi tanımlanırken) Name (Dizinin adı) Type of array (Dizinin türü) Number of elements (Eleman sayısı tanımlanmalıdır) arraytype arrayname[ numberofelements ]; Examples: (Örnekler:) int c[10]; float myarray[3284]; Declaring multiple arrays of same type Format similar to regular variables Example: int b[100], x[27]; 5

Examples Using Arrays (Dizilerin Kullanımına İlişkin Örnekler) Initializers (Başlangıç değerlerinin verilmesi) int n[5] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0 int n[5] = { 0 } n All elements 0 If too many a syntax error is produced syntax error C arrays have no bounds checking If size omitted, initializers determine it (Eğer dizi boyu verilmezse, başlangıç değerlerine göre boyut belirlenir.) int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array 6

Initializing an Array (Bir Diziye İlk Değerlerinin Verilmesi) #include <stdio.h> int main() { int n[100], i; for (i=0; i < 100; i++) n[i] = i; } for (i=0; i < 100; i++) printf( Element %d has value %d.\n,i, n[i]); return 1; 7

Examples (Örnekler) Reading values into an array int i, x[100]; for (i=0; i < 100; i=i+1) { printf("enter an integer: "); scanf("%d",&x[i]); } Summing up all elements in an array int sum = 0; for (i=0; i<=99; i=i+1) sum = sum + x[i]; 8

Examples (contd.) Shifting the elements of an array to the left. /* store the value of the first element in a * temporary variable */ temp = x[0]; for (i=0; i < 99; i=i+1) x[i] = x[i+1]; //The value stored in temp is going to be the value of //the last element: x[99] = temp; 9

Examples Finding the location of a given value (item) in an array. i = 0; while ((i<100) && (x[i]!= item)) i = i + 1; if (i == 100) loc = -1; // not found else loc = i; // found in location i 10

1 /* Histogram printing program */ 2 3 #include <stdio.h> 4 #define SIZE 10 5 6 int main() 7 { 8 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; 9 int i, j; 10 11 printf( "%s%13s%17s\n", "Element", "Value", "Histogram" ); 12 13 for ( i = 0; i <= SIZE - 1; i++ ) { 14 printf( "%7d%13d ", i, n[i]) ; 15 16 for ( j = 1; j <= n[ i ]; j++ ) /* print one bar */ 17 printf( "%c", '*' ); 18 19 printf( "\n" ); 20 } 21 22 return 0; 23 } 11

Program Output Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 * 12

Passing Arrays to Functions (Fonksiyona Dizinin Geçirilmesi) Passing arrays (Dizileri fonksiyona parametre olarak geçirmek) To pass an array argument to a function, specify the name of the array without any brackets (Köşeli parantezler kullanılmadan ) int myarray[ 24 ]; //declaration in main myfunction( myarray, 24 ); //calling the function n Array size is usually passed to function Arrays passed call- by- reference (Diziler referans olarak fonksiyonlara geçirilir.) Name of array is address of first element (Dizinin adı ilk elemanın adresidir.) Function knows where the array is stored n Modifies original memory locations Passing array elements (Dizi elemanlarını parametre olarak geçirmek) Passed by call- by- value (Dizi elemanları değer olarak geçer.) Pass subscripted name (i.e., myarray[ 3 ]) to function 13

Passing Arrays to Functions Function prototype void modifyarray( int b[], int arraysize ); Parameter names optional in prototype n int b[] could be written int [] n int arraysize could be simply int 14

Example (Örnek) int sum(int a[], int n) { int j, s=0; for (j=0; j <n ; j++) s = s+ a[j]; return s; } Note: a[] is a notational convenience. In fact int a[] º int *a Calling the function: int total, x[100]; total = sum(x, 100); total = sum(x, 88); total = sum(&x[5], 50); 15

1 /* Passing arrays and individual array elements to functions */ 2 3 #include <stdio.h> 4 #define SIZE 5 5 6 void modifyarray( int [], int ); /* appears strange */ 7 void modifyelement( int ); 8 9 int main() 10 { 11 int a[ SIZE ] = { 0, 1, 2, 3, 4 }, i; 12 13 printf( "Effects of passing entire array call " 14 "by reference:\n\nthe values of the " 15 "original array are:\n" ); 16 17 for ( i = 0; i <= SIZE - 1; i++ ) 18 printf( "%3d", a[ i ] ); 19 20 printf( "\n" ); 21 modifyarray( a, SIZE ); /* passed call by reference */ 22 printf( "The values of the modified array are:\n" ); 23 24 for ( i = 0; i <= SIZE - 1; i++ ) 25 printf( "%3d", a[ i ] ); 26 27 printf( "\n\n\neffects of passing array element call " 28 "by value:\n\nthe value of a[3] is %d\n", a[ 3 ] ); 29 modifyelement( a[ 3 ] ); 30 printf( "The value of a[ 3 ] is %d\n", a[ 3 ] ); 31 return 0; 32 } Entire array s passed call-byreference, and can be modified Array element is passed call-byvalue, and cannot be modified 16

33 34 void modifyarray( int b[], int size ) 35 { 36 int j; 37 38 for ( j = 0; j <= size - 1; j++ ) 39 b[ j ] *= 2; 40 } 41 42 void modifyelement( int e ) 43 { 44 printf( "Value in modifyelement is %d\n", e *= 2 ); 45 } Effects of passing entire array call by reference: The values of the original array are: 0 1 2 3 4 The values of the modified array are: 0 2 4 6 8 Effects of passing array element call by value: The value of a[3] is 6 Value in modifyelement is 12 The value of a[3] is 6 17

Two Dimensional Arrays (İki Boyutlu Diziler) Multiple subscripted arrays Tables with rows and columns (m by n array) Like matrices: specify row, then column Column 0 Column 1 Column 2 Column 3 Row 0 Row 1 Row 2 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Array name Row subscript Column subscript 18

Two Dimensional Arrays (İki Boyutlu Diziler) Initialization int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; Initializers grouped by row in braces If not enough, unspecified elements set to zero int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; Referencing elements 1 2 3 4 1 0 3 4 Specify row, then column printf( "%d", b[ 0 ][ 1 ] ); 19

Examples (Örnekler) Reading values into a two- dimensional array: int a[10][20]; for (row=0; row < 10; row = row+1){ for(col=0; col < 20; col = col+1) { printf( Enter a number: "); scanf("%d",&a[row][col]); } } 20

1 /* Two-dimensional array example 2 */ 3 #include <stdio.h> 4 #define STUDENTS 3 5 #define EXAMS 4 6 7 int minimum( int [][ EXAMS ], int, int ); 8 int maximum( int [][ EXAMS ], int, int ); 9 double average( int [], int ); 10 void printarray( int [][ EXAMS ], int, int); 11 12 int main() 13 { 14 int student; 15 int studentgrades[ STUDENTS ][ EXAMS ] = 16 { { 77, 68, 86, 73 }, 17 { 96, 87, 89, 78 }, 18 { 70, 90, 86, 81 } }; 19 20 printf( "The array is:\n" ); 21 printarray( studentgrades, STUDENTS, EXAMS ); 22 printf( "\n\nlowest grade: %d\nhighest grade: %d\n", 23 minimum( studentgrades, STUDENTS, EXAMS ), 24 maximum( studentgrades, STUDENTS, EXAMS ) ); 25 26 for ( student = 0; student <= STUDENTS - 1; student++ ) 27 printf( "The average grade for student %d is %.2f\n", 28 student, 29 average( studentgrades[ student ], EXAMS ) ); 30 31 return 0; 32 } Each row is a particular student, each column is the grades on the exam. 21

33 34 /* Find the minimum grade */ 35 int minimum( int grades[][ EXAMS ], 36 int pupils, int tests ) 37 { 38 int i, j, lowgrade = 100; 39 40 for ( i = 0; i <= pupils - 1; i++ ) 41 for ( j = 0; j <= tests - 1; j++ ) 42 if ( grades[ i ][ j ] < lowgrade ) 43 lowgrade = grades[ i ][ j ]; 44 45 return lowgrade; 46 } 47 48 /* Find the maximum grade */ 49 int maximum( int grades[][ EXAMS ], 50 int pupils, int tests ) 51 { 52 int i, j, highgrade = 0; 53 54 for ( i = 0; i <= pupils - 1; i++ ) 55 for ( j = 0; j <= tests - 1; j++ ) 56 if ( grades[ i ][ j ] > highgrade ) 57 highgrade = grades[ i ][ j ]; 58 59 return highgrade; 60 } 61 62 /* Determine the average grade for a particular exam */ 63 double average( int setofgrades[], int tests ) 64 { 22

65 int i, total = 0; 66 67 for ( i = 0; i <= tests - 1; i++ ) 68 total += setofgrades[ i ]; 69 70 return ( double ) total / tests; 71 } 72 73 /* Print the array */ 74 void printarray( int grades[][ EXAMS ], 75 int pupils, int tests ) 76 { 77 int i, j; 78 79 printf( " [0] [1] [2] [3]" ); 80 81 for ( i = 0; i <= pupils - 1; i++ ) { 82 printf( "\nstudentgrades[%d] ", i ); 83 84 for ( j = 0; j <= tests - 1; j++ ) 85 printf( "%-5d", grades[ i ][ j ] ); 86 } 87 } 23

Program Output (Program Çıktısı) The array is: [0] [1] [2] [3] studentgrades[0] 77 68 86 73 studentgrades[1] 96 87 89 78 studentgrades[2] 70 90 86 81 Lowest grade: 68 Highest grade: 96 The average grade for student 0 is 76.00 The average grade for student 1 is 87.50 The average grade for student 2 is 81.75 24