Dale Roberts, Lecturer IUPUI

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2007 Pearson Education, Inc. All rights reserved C Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
- Standard C Statements
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Arrays Declarations CSCI N305
C++ Programming Lecture 15 Arrays – Part II
آرايه ها اصول كامپيوتر 1.
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
C++ Programming Lecture 15 Arrays – Part II
C Arrays.
C Arrays Systems Programming.
Multidimensional Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Functions Recursion CSCI 230
Arrays Strings and Parameter Passing CSCI N305
Pointers Call-by-Reference CSCI 230
4.1 Introduction Arrays A few types Structures of related data items
6 C Arrays.
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
EECE.2160 ECE Application Programming
Arrays.
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Characters and Strings
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Strings and Parameter Passing Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu

Character Arrays Character arrays String is really a static array of characters, ex: “first” Character arrays can be initialized using string literals char string1[] = "first"; Null character '\0' terminates strings string1 actually has 6 elements It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; Can access individual characters string1[ 3 ] is character ‘s’ Array name is address of array, so & not needed for scanf char char string2[20]; scanf( "%s", string2 ); Can read a string with max of size 19 and a null character. Reads characters until whitespace (space, tab, carriage-return, newline, vertical tab) encountered Can write beyond end of array, be careful f i r s t \0 Null character (indicates string termination) & is NOT used, why?

Passing Arrays to Functions To pass an array argument to a function, specify the name of the array without any brackets int myArray[ 24 ]; ... myFunction( myArray, 24 ); Array size usually passed to function Arrays passed call-by-reference the called functions can modify the element values in the caller’s original array Name of array is the address of first element of the array Function knows where the array is stored. Therefore, when the called function modifies array elements in its function body, it is modifying the actual elements of array in the original memory locations Pass array name Size is also often sent as an argument main() … myFunction() myArray

Passing Arrays to Functions (cont.) Example: #include <stdio.h> main() { int a[10]; printf(“a = %p \n &a[0] = %p\n”, a, &a[0]); } output: a = FFEE &a[0] = FFEE Passing array elements Individual elements of an array are passed by call-by-value Pass subscripted name (i.e., myArray[ 3 ]) to function Example: compare(a[0], a[1]); An array is a vector while individual elements are scalars. Function prototype void modifyArray( int b[], int arraySize ); Parameter names optional in prototype int b[] could be written int [] int arraySize could be simply int will be passed by value

Pass array to a function 1 /* Fig. 6.13: fig06_13.c 2 Passing arrays and individual array elements to functions */ 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 } Function definitions Pass array to a function Pass array element to a function Print Entire arrays passed call-by-reference, and can be modified Array elements passed call-by-value, and cannot be modified

Function definitions Program Output 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 } Function definitions 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 Program Output