1 Introduction to Computing Lecture 15 Arrays (Part 1) Dr. Bekir KARLIK Yasar University Department of Computer Engineering

Slides:



Advertisements
Similar presentations
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Week 8 Arrays Part 2 String & Pointer
1 Introduction to Computing Lecture 16 Arrays (Part 2) Dr. Bekir KARLIK Yasar University Department of Computer Engineering
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Character Input and Output C and Data Structures Baojian Hua
1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
1 CSE1301 Computer Programming Lecture 19 Arrays (Part 2)
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Arrays Ethan Cerami New York University Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 1)
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
CSE1301 Computer Programming: Lecture 19 File I/O
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Computer Programming for Engineers
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 2)
Computer Science 210 Computer Organization
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
ECE Application Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Chapter 7 Text Input/Output Objectives
CSI 121 Structure Programming Language Lecture 18 Arrays (Part 2)
CSI 121 Structure Programming Language Lecture 17 Arrays (Part 1)
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Module 2 Arrays and strings – example programs.
Computer Programming Lecture 15 Text File I/O
Computer Science 210 Computer Organization
Arrays, Part 1 of 2 Topics Definition of a Data Structure
EKT150 : Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Computer Programming Lecture 14 Arrays (Part 2)
Introduction to Computing Lecture 10 Arrays (Part 1)
ECE 103 Engineering Programming Chapter 51 Random Numbers
EECE.2160 ECE Application Programming
Introduction to Computer Programming Lecture 16 Structures (Part 1)
Character Arrays char string1[] = “first”;
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CSCE 206 Lab Structured Programming in C
Introduction to Problem Solving and Programming
Presentation transcript:

1 Introduction to Computing Lecture 15 Arrays (Part 1) Dr. Bekir KARLIK Yasar University Department of Computer Engineering

2 Topics Arrays –Declaration –Initialization –Input/Output –Passing arrays to functions

3 Arrays A group of contiguous memory locations used to store a series of related values The array name is a pointer to the first element All values have the same type Individual elements of an array are accessed via an integer index: array[index] Element indices start at 0: array[0] is the first element

4 Initialization Arrays may be initialized with a list of suitable values No need to specify the number of elements for a 1D (1-dimensional) array

5 Example: MonthlyRainfall Problem: using Rainfall Table input month output mean rainfall for that month

6 #include int main() { int month; int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; printf("Enter month: "); scanf("%d", &month); printf("Average rainfall: %d mm.\n", table[month-1]); return 0; } rainfall1.c Example (cont): MonthlyRainfall (v.1)

7 #include int main() { int month; int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; printf("Enter month: "); scanf("%d", &month); printf("Average rainfall: %d mm.\n", table[month-1]); return 0; } rainfall1.c Example (cont): MonthlyRainfall (v.1)

8 Input / Output of Arrays Library functions printf() and scanf() do not know about arrays So we have to do I/O ourselves

9 #include #define NMONTHS 12 /* Store and print rainfall */ int main() { int data[NMONTHS]; int month; for ( month=0; month < NMONTHS; month++ ) { scanf("%d", &data[month] ); }... rainio1.c Example: IORainfall-1

10 #include #define NMONTHS 12 /* Store and print rainfall */ int main() { int data[NMONTHS]; int month; for ( month=0; month < NMONTHS; month++ ) { scanf("%d", &data[month] ); }... rainio1.c Example (cont): IORainfall-1

11 #include #define NMONTHS /* Print from January to December */ for ( month=0; month < NMONTHS; month++ ) { printf( "%d ", data[month] ); } printf("\n"); /* Print from December to January */ for ( month = NMONTHS - 1; month >= 0; month-- ) { printf( "%d ", data[month] ); } printf("\n"); return 0; } rainio1.c Example (cont): IORainfall-2 (v.1)

12 #include #define NMONTHS /* Print from January to December */ for ( month=0; month < NMONTHS; month++ ) { printf( "%d ", data[month] ); } printf("\n"); /* Print from December to January */ for ( month = NMONTHS - 1; month >= 0; month-- ) { printf( "%d ", data[month] ); } printf("\n"); return 0; } rainio1.c Example (cont): IORainfall-2 (v.1)

13 #include #define NMONTHS /* Print from January to December */ for ( month=0; month < NMONTHS; month++ ) { printf( "%5d ”, data[month] ); } printf("\n"); /* Print from December to January */ for ( month = NMONTHS - 1; month >= 0; month-- ) { printf( "%5d ”, data[month] ); } printf("\n"); return 0; } rainio2.c Example (cont): IORainfall-2 (v.2)

14 Character Arrays vs. Character Strings A character string is a char array NOTE: a character string must have the terminating character ( ’\0’ ) The terminating character allows scanf() and printf() to handle character strings

15 Handling Indices Arrays have a fixed size There is no built-in way of checking if the supplied index is within range We must check for valid indices ourselves

16 #include #define MAXLEN 1024 int main() { int month; char line[MAXLEN]; char dummy[MAXLEN]; int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; while(1) { printf("Enter month or ctrl-c to end: "); fgets(line, MAXLEN, stdin); if (sscanf(line, "%d%s", &month, dummy) != 1) /* valid input? */ { printf("Invalid input. Try again.\n"); } else if (1 <= month && month <= 12)/* input in range? */ { printf("Average rainfall for month %d is %d mm.\n",month,table[month-1]); } else { printf("Month should be between 1 and 12. Try again.\n"); } return 0; } rainfall2.c Example (cont): MonthlyRainfall (v.2)

17 #include #define MAXLEN 1024 int rainfall(int month); /* Main program to test rainfall() function. */ int main() { int month; char line[MAXLEN]; char dummy[MAXLEN]; while(1) { printf("Enter month or ctrl-c to end: "); fgets(line, MAXLEN, stdin); if (sscanf(line, "%d%s", &month, dummy) != 1) { printf("Invalid input. Try again.\n"); } else if (1 <= month && month <= 12) { printf("Average rainfall for month %d is %d mm.\n",month,rainfall(month-1)); } else { printf("Month should be between 1 and 12. Try again.\n"); } return 0; } rainfall3.c Example (cont): MonthlyRainfall-1 (v.3)

18 /**********************************************************\ * NAME: * int rainfall(int month) * DESCRIPTION: * Returns the mean monthly rainfall (in millimeters) * in a given month * PRE: * The integer `month' must be between 0 and 11, where * 0 = January, 1 = February, etc. Otherwise, the behaviour * is undefined * The local array `table' should be initialized to contain * the average rainfall in a given month * POST: * It returns an integer value corresponding to the mean * rainfall (in millimeters) for the given `month' \**********************************************************/ int rainfall ( int month ) { int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; return (table[month]); } rainfall3.c Example (cont): MonthlyRainfall-2 (v.3)

19 /**********************************************************\ * NAME: * int rainfall(int month) * DESCRIPTION: * Returns the mean monthly rainfall (in millimeters) * in a given month * PRE: * The integer `month' must be between 0 and 11, where * 0 = January, 1 = February, etc. Otherwise, the behaviour * is undefined * The local array `table' should be initialized to contain * the average rainfall in a given month * POST: * It returns an integer value corresponding to the mean * rainfall (in millimeters) for the given `month' \**********************************************************/ int rainfall ( int month ) { int table[12] = { 30, 40, 45, 95, 130, 220, 210, 185, 135, 80, 40, 45 }; return (table[month]); } rainfall3.c Example (cont): MonthlyRainfall-2 (v.3)

20 Passing Arrays to Functions The array is passed –as an array of unspecified size ( int array[] ) OR –as a pointer ( int *array ) Changes to the array within the function affect the “original” array

21 Example (cont): IORainfall-1 (v.3) #include #define NMONTHS 12 void loadRain ( int arrayPtr[] ) { int month; for (month=0; month < NMONTHS; month++) { scanf("%d", &arrayPtr[month]); } rainio3.c

22 void printRain ( const int arrayPtr[] ) { int month; for (month=0; month < NMONTHS; month++) { printf("%5d", arrayPtr[month]); } printf("\n"); } rainio3.c Example (cont): IORainfall-2 (v.3)

23 #include #define NMONTHS 12 void loadRain ( int arrayPtr[] ); void printRain ( const int arrayPtr[] ); /* Store and print rainfall */ int main() { int data[NMONTHS]; loadRain(data); printRain(data); return 0; } rainio3.c Example (cont): IORainfall-3 (v.3)

24 #include #define NMONTHS 12 void loadRain ( int arrayPtr[] ); void printRain ( const int arrayPtr[] ); /* Store and print rainfall */ int main() { int data[NMONTHS]; loadRain(data); printRain(data); return 0; } /* Read in rainfall for each month*/ void loadRain ( int arrayPtr[] ) { int month; for (month=0; month < NMONTHS; month++) { scanf("%d", &arrayPtr[month]); } } /* Print rainfall for each month*/ void printRain ( const int arrayPtr[] ) { int month; for (month=0; month < NMONTHS; month++) { printf("%5d", arrayPtr[month]); } printf("\n"); } rainio3.c Example: IORainfall -- v.3 (cont)