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

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

C: How to Program. Week /Apr/23

Programming for Engineers Arrays

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Lecture 04 FUNCTIONS AND ARRAYS

C Arrays Pearson Education, Inc. All rights reserved.

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

Array. Arrays. Declaring Arrays. Using Arrays

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

Content. In this chapter, you will learn:

Arrays and Applications

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

C Arrays. O b j e c t i v e s In this chapter, you ll:

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

C: How to Program. Week /Apr/16

C++ PROGRAMMING SKILLS Part 4: Arrays

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

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

C Functions. 5.2 Program Modules in C

Fundamental of Programming (C)

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

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

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

C Programming Language

Arrays. Systems Programming Concepts

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

Dynamic memory allocation

Two Dimensional Array - An array with a multiple indexs.

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

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

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

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Two Dimensional Array - An array with a multiple indexs.

Fundamentals of Programming Session 14

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

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

C: How to Program. Week /Mar/05

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

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

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

ESC101N: Fundamentals of Computing End-sem st semester

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

PROGRAMMAZIONE I A.A. 2017/2018

Principles of Programming. Chapter 6: Arrays

Introduction to Scientific Computing and Problem Solving

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

AMCAT Automata Coding Sample Questions And Answers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

A Fast Review of C Essentials Part I

ONE DIMENSIONAL ARRAYS

Array Initialization

Computer Programming Lecture 14 Arrays (Part 2)

More Arrays. Last updated 2/6/19

Technical Questions. Q 1) What are the key features in C programming language?

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

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


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

Objectives of This Chapter

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

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

Government Polytechnic Muzaffarpur.

Array. Prepared By - Rifat Shahriyar

BLM2031 Structured Programming. Zeyneb KURT

Solutions to Chapter 8

CSE123. Program Design and Modular Programming Functions 1-1

Computer Science & Engineering 150A Problem Solving Using Computers

Chapter 2 - Introduction to C Programming

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Chapter 5 C Functions

CS 0449 Sample Midterm

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

One Dimension Arrays 1

Approximately a Test II CPSC 206

Assignment 1 Clarifications

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

SOFTWARE Ph.D. Qualifying Exam Fall 2017

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

COMP s1 Lecture 1

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

ET156 Introduction to C Programming

Arrays in C C Programming and Software Tools. N.C. State Department of Computer Science

Structured programming

ARRAYS(II Unit Part II)

Question 1. [15 marks]

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.

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

Module 6: Array in C

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

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

CS201- Introduction to Programming Current Quizzes

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Lab 3. Pointers Programming Lab (Using C) XU Silei

Chapter 1 & 2 Introduction to C Language

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Transcription:

CS 2060 Week 5

1 Arrays Arrays Initializing arrays 2 Examples of array usage 3 Passing arrays to functions 4 2D arrays 2D arrays 5 Strings Using character arrays to store and manipulate strings 6 Searching arrays Searching arrays 7 Next Time

Arrays Arrays An array is a group of contiguous memory locations that all have the same type. To refer to a particular location or element in the array, we specify the array s name and the position number (or array index) of that particular element.

Arrays Arrays Array element Value c[0] 0 c[1] 1 c[2] 1 c[3] 2 c[4] 3 c[5] 5 c[6] 8 c[7] 13 c[8] 21 c[9] 34 Table: Array example: an array of 10 integers named c.

Arrays Accessing arrays Array indices begin at zero. Square brackets are used to access an array element: c[0] = 0; c[1] = 1; c[n] = c[n-2] + c[n-1]; printf("%d ",c[n]);

Arrays Defining arrays An array is defined like a normal variable, but with square brackets indicating the array size. int c[10]; The above statement defines an array of ten integers, with indices 0-9. Multiple arrays can be defined in a single statement: char a[100], b[2];

Arrays Array size The array size is fixed and cannot be changed after the definition. Typically, the array size must be a constant expression, not a variable: int a = 10; int b[a]; // not allowed However, this is now defined in the C99 standard, so modern compilers may allow it.

Arrays Fibonacci example #include <stdio.h> int main() { int fibonacci[10]; fibonacci[0] = 0; fibonacci[1] = 1; for ( int i = 2; i < 10; i++ ) { fibonacci[i] = fibonacci[i-2] + fibonacci[i-1]; for ( int i = 0; i < 10; i++ ) { printf( "%d ", fibonacci[i] );

Initializing arrays Initializing arrays with a loop The array elements are uninitialized by default. The array can be initialized in several ways. With a loop: for ( int i = 0; i < 10; i++ ) a[i] = 0;

Initializing arrays Initializing arrays with an initializer list An initializer list can be used to explicitly initialize the array when it is defined. int a[5] = { 0, 1, 2, 3, 4 ; The initializer list can only be used when the array is first defined.

Initializing arrays Initializing arrays with an initializer list If the initializer list has fewer elements than the array size, the remaining elements will be initialized to zero. int a[5] = { 10 ; is equivalent to: int a[5] = { 10, 0, 0, 0, 0 ;

Initializing arrays Initializing arrays with an initializer list If the array size is omitted, then the size will be inferred from the initializer list. int a[] = { 0, 1, 2, 3, 4, 5 ; is equivalent to: int a[6] = { 0, 1, 2, 3, 4, 5 ;

Initializing arrays Specifying an array s size with a symbolic constant In some cases it is convenient to put an array s size in a symbolic constant which is available anywhere in the code file. #define BUF SIZE 1024 int buffer[buf SIZE]; #define is a preprocessor directive; the preprocessor will replace every instance of BUF SIZE with 1024. The #define directive must occur outside any function (usually at the top of the file after the #include statements). Again, Google and Apple would prefer kbuffersize instead of BUF SIZE.

Initializing arrays Specifying an array s size with a symbolic constant #include <stdio.h> #define kbuffersize 1024 int main(void) { int buffer[kbuffersize] = { 0 ; int numvaluesread = 0; int value; while ( scanf( "%d", &value ) == 1 && numvaluesread < kbuffersize ) { buffer[numvaluesread++] = value; printf("read %d values.\n", numvaluesread ); int sum = 0; for ( int i = 0; i < numvaluesread; i++ ) { sum += buffer[i]; int average = sum/numvaluesread; printf("average value: %d\n", average );

Using an array to count survey responses #include <stdio.h> int main() { // survey has responses on scale from 1-7 unsigned int responsecount[8] = { 0 ; int response; while ( scanf( "%d", &response ) == 1 ) { responsecount[response]++; printf( "%6s%17s\n", "Rating", "Frequency" ); for ( int rating = 1; rating <= 7; rating++ ) { printf( "%6d%17d\n", rating, responsecount[rating] );

Array out-of-bounds checking C does not provide any bounds checking on the array index. int a[10] = 0 ; a[10] = 10; // not a compiler error a[-1] = 10; // not a compiler error Trying to read or write a value outside of the array will probably crash the program but not always! This can lead to hard-to-find bugs.

Array out-of-bounds checking In the previous code example, we do not check the range of the input. while ( scanf( "%d", &response ) == 1 ) responsecount[response]++; response could be below 1 or above 7. We should explicitly check the range in the code.

Using an array to count survey responses #include <stdio.h> int main() { // survey has responses on scale from 1-7 unsigned int responsecount[8] = { 0 ; int response; while ( scanf( "%d", &response ) == 1 ) { if ( response >= 1 && response <= 7 ) responsecount[response]++; printf( "%6s%17s\n", "Rating", "Frequency" ); for ( int rating = 1; rating <= 7; rating++ ) { printf( "%6d%17d\n", rating, responsecount[rating] );

Printing array elements with a histogram #include <stdio.h> int main() { // survey has responses on scale from 1-7 unsigned int responsecount[8] = { 0 ; int response; while ( scanf( "%d", &response ) == 1 ) { if ( response >= 1 && response <= 7 ) responsecount[response]++; printf( "%6s%17s\n", "Rating", "Histogram" ); for ( int rating = 1; rating <= 7; rating++ ) { printf( "%6d ", rating ); for ( int i = 1; i <= responsecount[rating]; i++ ) { printf("%c", * ); puts("");

Counting die rolls #include <stdio.h> #include <stdlib.h> #include <time.h> #define knumtrials 60000000 int main() { unsigned int facecount[6] = { 0 ; for ( unsigned int i = 0; i < knumtrials; i++ ) { int face = rand() % 6; facecount[face]++; printf( "%6s%17s\n", "Face", "Frequency" ); for ( int face = 0; face < 6; face++ ) { printf( "%6d%17u\n", face+1, facecount[face] );

Passing arrays to functions Functions can receive arrays as arguments. int sumarray( int array[] ) { a[0] += 1; In the function prototype, you write the array name followed by square brackets. The array size is omitted.

Passing arrays to functions #define karraysize 5 void printarray( int b[] ); void modifyarray( int b[] ); void modifyelement( int e ); int main() { int a[karraysize] = { 0 ; for ( int i = 0; i < karraysize; i++ ) a[i] = i; printarray( a ); modifyarray( a ); printarray( a ); modifyelement( a[3] ); printarray( a ); void printarray( int b[] ) { for ( int i = 0; i < karraysize; i++ ) printf( "%3d", b[i] ); void modifyarray( int b[] ) { for ( int i = 0; i < karraysize; i++ ) b[i] *= 2; void modifyelement( int e ) { e *= 2;

const arrays Using the const modifier we can prevent modifications to an array. int myfn( const int a[] ) { int b = a[0]; // allowed a[0] += 1; // not allowed The const modifier is useful for: Specifying input arguments Creating arrays of fixed constants Any variable (not just arrays) can be declared const.

const array example #define karraysize 10 const int gfibonacci10[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ; void copyarray( int output[], const int input[] ); void squarearray( int output[], const int input[] ); int main() { int fib[karraysize] = { 0 ; int fib_squared[karraysize] = { 0 ; copyarray( fib, gfibonacci10 ); squarearray( fib_squared, fib ); void copyarray( int output[], const int input[] ) { for ( int i = 0; i < karraysize; i++ ) output[i] = input[i]; void squarearray( int output[], const int input[] ) { for ( int i = 0; i < karraysize; i++ ) output[i] = input[i]*input[i];

const array example #define karraysize 10 const int gfibonacci10[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ; void copyarray( int output[], const int input[] ); void squarearray( int output[], const int input[] ); int main() { int fib[karraysize] = { 0 ; copyarray( fib, gfibonacci10 ); squarearray( fib, fib ); void copyarray( int output[], const int input[] ) { for ( int i = 0; i < karraysize; i++ ) output[i] = input[i]; void squarearray( int output[], const int input[] ) { for ( int i = 0; i < karraysize; i++ ) output[i] = input[i]*input[i];

2D arrays Multidimensional arrays With multidimensional arrays we use multiple indices to refer to array elements. c[0][0] = 0; Two-dimensional arrays are common: Tables Matrices The first index indicates the row and the second index indicates the column.

2D arrays Two-dimensional array example Column 0 Column 1 Column 2 Column 3 Row 0 a[0][0] a[0][1] a[0][2] a[0][3] Row 1 a[1][0] a[1][1] a[1][2] a[1][3] Row 2 a[2][0] a[2][1] a[2][2] a[2][3] Row 3 a[3][0] a[3][1] a[3][2] a[3][3] Table: Two-dimensional array elements in a 4x4 array

2D arrays Row major order Like linear (1D) arrays, multidimensional arrays are contiguous blocks of memory. C stores multidimensional arrays in row major order. This means that the first row of data is stored before the second, and so on.

2D arrays Initializing 2D arrays with a loop A 2D array can be initialized in several ways. With a nested loop: int a[5][5]; for ( int row = 0; row < 5; row++ ) { for ( int col = 0; col < 5; col++ ) { a[row][col] = row+col;

2D arrays Initializing 2D arrays with an initializer list An initializer list can be used to explicitly initialize the 2D array when it is defined. int a[5][5] = { { 0, 1, 2, 3, 4, { 1, 2, 3, 4, 5, { 2, 3, 4, 5, 6, { 3, 4, 5, 6, 7, { 4, 5, 6, 7, 8 ; The initializer list can only be used when the array is first defined.

2D arrays Initializing arrays with an initializer list If a row in the initializer list has fewer elements than the array size, the remaining elements in that row will be initialized to zero. int a[2][3] = { { 10, { 10 ; is equivalent to: int a[2][3] = { { 10, 0, 0, { 10, 0, 0 ;

2D arrays Initializing arrays with an initializer list Only the number of rows can be inferred by omitting the size: int a[][2] = { { 1, 0, { 0, 1 ; The 2nd and following dimensions must be explicitly given.

2D arrays Passing multidimensional arrays to functions When passing a multidimensional array to a function, the size of the first dimension is omitted, but the remaining sizes must be given. void myfn( int a[][2] ) { return a[0][0]; Why?

2D arrays Passing multidimensional arrays to functions The second (and following) dimension sizes determine the indexing function for the multidimensional array. To illustrate this, we can replace a 2D array with a 1D array in the following way: int a[3][10]; a[0][2] = 10; a[1][2] = 10; int b[3*10]; b[0*10 + 2] = 10; b[1*10 + 2] = 10;

2D arrays Arrays of other types We can define an array on any data type, even an enum. enum Color { RED, BLUE, GREEN ; enum Color image[480][640]; bool tests[100][100]; float matrix[3][3];

2D arrays 2D array example #include <stdio.h> int sumrow( int magic[][3], int row ); int sumcol( int magic[][3], int col ); int main() { int magic[3][3]; for ( int row = 0; row < 3; row++ ) { for ( int col = 0; col < 3; col++ ) { scanf("%d",&magic[i][j]); int row_sums[3], column_sums[3], diagonal_sums[2]; for ( int row = 0; row < 3; row++ ) row_sums[row] = sumrow( magic, row ); for ( int col = 0; col < 3; col++ ) col_sums[col] = sumcolumn( magic, col ); diagonal_sums[0] = magic[0][0] + magic[1][1] + magic[2][2]; diagonal_sums[1] = magic[0][2] + magic[1][1] + magic[2][0];

2D arrays 2D array example int sumrow( int magic[][3], int row ) { int sum = 0; for ( int col = 0; col < 3; col++ ) sum += magic[row][col]; return sum; int sumcolumn( int magic[][3], int col ) { int sum = 0; for ( int row = 0; row < 3; row++ ) sum += magic[row][col]; return sum;

Using character arrays to store and manipulate strings Storing strings in character arrays We can use an array to store a string: char string1[] = "first";

Using character arrays to store and manipulate strings Storing strings in character arrays We can use an array to store a string: char string1[] = "first"; In C, strings end with a special string-termination character known as a null character. C strings are called null-terminated strings.

Using character arrays to store and manipulate strings Storing strings in character arrays We can use an array to store a string: char string1[] = "first"; In C, strings end with a special string-termination character known as a null character. C strings are called null-terminated strings. The above is equivalent to: char string1[] = { f, i, r, s, t, \0 ;

Using character arrays to store and manipulate strings Storing strings in character arrays Remember that: Single quotes are for single characters: char letter = a ; Double quotes are for null-terminated strings: char word[] = "hello";

Using character arrays to store and manipulate strings Storing strings in character arrays Remember that: Single quotes are for single characters: char letter = a ; Double quotes are for null-terminated strings: char word[] = "hello"; These statements will not work: char letter = "a"; char word[] = hello ;

Using character arrays to store and manipulate strings Accessing characters in a string The individual characters in a string are accessed with normal array indices: char word[] = "hello"; char letter = word[0];

Using character arrays to store and manipulate strings Accessing characters in a string The individual characters in a string are accessed with normal array indices: char word[] = "hello"; char letter = word[0]; How long is the array word?

Using character arrays to store and manipulate strings Accessing characters in a string The individual characters in a string are accessed with normal array indices: char word[] = "hello"; char letter = word[0]; How long is the array word? Six characters five letters plus the null-terminator.

Using character arrays to store and manipulate strings Printing strings We have already been using strings to output text and values with printf: char word[] = "hello"; printf("%s\n", word);

Using character arrays to store and manipulate strings Printing strings We have already been using strings to output text and values with printf: char word[] = "hello"; printf("%s\n", word); The format specifier itself is a string.

Using character arrays to store and manipulate strings Printing strings We have already been using strings to output text and values with printf: char word[] = "hello"; printf("%s\n", word); The format specifier itself is a string. Use %s to specify a string in the format specifier.

Using character arrays to store and manipulate strings Reading strings from input To read a string from input, we can use %s as well. char word[20]; scanf("%s", word); // OR: scanf("%19s", word); %19s means, read up to 19 characters (stopping at whitespace).

Using character arrays to store and manipulate strings Reading strings from input To read a string from input, we can use %s as well. char word[20]; scanf("%s", word); // OR: scanf("%19s", word); %19s means, read up to 19 characters (stopping at whitespace). Why 19 characters? Why not 20?

Using character arrays to store and manipulate strings Reading strings from input To read a string from input, we can use %s as well. char word[20]; scanf("%s", word); // OR: scanf("%19s", word); %19s means, read up to 19 characters (stopping at whitespace). Why 19 characters? Why not 20? We need to leave one element for the null character at the end of the string.

Using character arrays to store and manipulate strings Reading strings from input To read a string from input, we can use %s as well. char word[20]; scanf("%s", word); Why not scanf("%s", &word);?

Using character arrays to store and manipulate strings Reading strings from input To read a string from input, we can use %s as well. char word[20]; scanf("%s", word); Why not scanf("%s", &word);? We don t use the & when passing an array to a function.

Using character arrays to store and manipulate strings String example #include <stdio.h> int main() { char word[20]; scanf("%19s",word); int a_index = -1; // what is the error here? for ( int i = 0; i < 20; i++ ) { if ( word[i] == a ) { a_index = i; break; if ( a_index == -1 ) printf("no a found.\n"); else printf("first a found at index %d\n",a_index);

Using character arrays to store and manipulate strings String example #include <stdio.h> int main() { char word[20]; scanf("%19s",word); int a_index = -1; for ( int i = 0; i < 20; i++ ) { if ( word[i] == \0 ) break; if ( word[i] == a ) { a_index = i; break; if ( a_index == -1 ) printf("no a found.\n"); else printf("first a found at index %d\n",a_index);

Searching arrays Searching arrays Searching an array for particular values is a common operation. We may want to know: If a particular value occurs in the array. Where the first (or last) occurence of the value is. How many times the value occurs. The simplest approach is to use linear search, as demonstrated in the previous code example.

Searching arrays Linear search // finds the first occurence of key in array // returns -1 if key is not found int linearsearch( const int array[], int key, int size ) { for ( int n = 0; n < size; n++ ) { if ( array[n] == key ) return n; // return location of key return -1; // key not found

Searching arrays Linear search // counts the number of occurences of key in array int countfrequency( const int array[], int key, int size ) { int count = 0; for ( int n = 0; n < size; n++ ) { if ( array[n] == key ) count++; return count;

Next Time Homework 6 due Wednesday, March 2, 11:55 PM. Pointers