Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers
|
|
- Sandra Green
- 6 years ago
- Views:
Transcription
1 Maltepe University Computer Engineering Department BİL 133 Algoritma ve Programlama Chapter 8: Arrays and pointers
2 Basics int * ptr1, * ptr2; int a[10]; ptr1 = &a[2]; ptr2 = a; // equivalent to ptr2 = &a[0]; Memory Addresses a a+1?? 0 1 An array variable is actually a pointer to the first element of the array. ptr2 points to the first element of the array and get others by offset. Referring a[i] is same as referring *(a+i). a+2..??????
3 Array Elements as Function arguments If we want to print the ith element of the array x[i], then we can do the following: printf( %d\n, x[i]); scanf( %d, &x[i]); We can also pass array elements as arguments to functions that we write. For example: swap1(x[i], x[i+1]); swap2(&x[i], &x[i+1]); 3
4 Having arrays as function arguments Besides passing individual array elements to functions, we can write functions that take entire arrays as arguments. There are several ways of passing arrays to functions but in each case we only pass the address of the array. This is very similar to what we did during passing variables by reference As we are not passing a copy of the array any changes to the array made within the function will also effect the original array. When an array name with no subscript appears in the argument list of a function call, what is actually stored in the function s corresponding parameter is the address of the array. Example, int a[10]; foo(a); foo(&a[0]); // same as above 4
5 Array Arguments We can also pass an entire array as the input argument for a function. However, the function does not copy the array but just manipulates the original array. An assignment to one of the array elements by a statement in the function changes the contents of the original array. 5
6 Passing array as function parameter Array names are pointers. The name of an array contain the address of its initial element, a = &a[0]; Passing an array as function argument copies the value of the pointer to the formal array parameter and points to the same location. Where is the array actually located? At the data area of the function that declared it. For an array declared as formal parameter, space is allocated in the function data area only for the address of the initial array element of the array passed as actual argument. 6
7 Using arrays in formal parameter list void foo(int x[10]) Store the address of the corresponding array argument (a in previous slide) to variable x, compiler ignores 10 since x is a pointer. void foo(int x[]) This is just a different format to make code easy to read, for compiler this format or the others have the same meaning. void foo(int *x) Function foo can take any integer array as argument. You should also give the size as a parameter to make the function more portable. void foo(int *x, int size) 7
8 Arrays as Input Arguments ANSI C provides a qualifier that we can include in the declaration of the array formal parameter in order to notify the C complier that the array is only an input to the function and that the function does not intend to modify the array. This qualifier allows the compiler to mark as an error any attempt to change an array element within the function. void foo(const int x[], int size) The const tells C not to allow any element of the array x to change in the function foo. 8
9 C code Example int main(void){ int a[5]={1,2,3,4,5}; int i; } clear1(a,5); clear2(a,5); for(i=0; i<5; i=i+1) printf("%d ", a[i]); return 0; void clear1(int x[], int size){ int i; for(i=0; i<size; i=i+1) x[i] = 0; } void clear2(int *x, int size){ int *p; for(p=x; p<(x+size); p=p+1) // for(p=&x[0]; p<&x[size]; p=p+1) *p = 0; } 9
10 An Example of Array Arguments We have the flexibility to pass an array of any size to the function. Because C does not allocate any memory space for the array, and thus the compiler does not need to know the size. int list[] is the declaration for passing an int array 10
11 Data Areas After Calling fill_array (x, 5, 1); The value of each element in the original array are changed to 1. 11
12 Alternative Formats of Array Arguments fill_array(x, 5, 1) is the same as fill_array(&x[0], 5, 1). This call may lead to misunderstanding of the reader. In the declaration for function fill_array, we can declare the array argument as either int list[] or int *list. If you wish to prevent the modification of the array argument, you can write fill_array(const int list[], ) 12
13 Using Array Elements as Function Arguments (1) Suppose that we have a function prototype as shown below. void do_it(double arg_1, double *arg2_p, double *arg3_p); Let x be an array of type double elements. We can call the function do_it as follows. do_it(x[0], &x[1], &x[2]); The modification on arg2_p and arg3_p will change the values of x[1] and x[2]. 13
14 Using Array Elements as Function Arguments (2) arg_1 has a copy of the value from x[0] arg_3 is a pointer pointed to the memory of x[2]. 14
15 Returning an Array from a function(1) Returning arrays should work too - right? int a[5]; a = foo();.. int* foo() { int c[5] = {1,2,3,4,5}; return c; } Wrong! In C, it is not legal for a function s return type to be an array. 15
16 Returning an Array from a function(2) Type problem between int[] and int* a is a constant pointer type, you can t change it. Array c is stored in function foo s stack frame. Return just copies the pointer, not the array. Memory where the array c resides may be overwritten once function foo finishes its execution. To do it properly Pass an array as a function argument from your calling function and modify it inside the called function. 16
17 Returning an Array Result The add_arrays function computes the sum of corresponding elements in arrays ar1 and ar2, and then stores results into ar3. Input array arguments Output array arguments 17
18 Memory for calling add_arrays(x, y, x_plus_y) Indirectly read the values of array x Indirectly read the values of array y Indirectly write the values of x+y into x_plus_y 18
19 Searching and Sorting an Array Sometimes we may need to search a particular value in an array, or sort an array to rearrange the array elements in numerical order. Commonly used searching methods: Sequential search, binary search, etc. Commonly used sorting methods: Selection sort, bubble sort, quick sort, etc. 19
20 Sequential Search in an Array target is the value we want to locate in the array Traverse the array to look for the value identical to target. 20
21 Selection Sort in an Array Selection sort is an intuitive sorting algorithm. Find the index of the smallest element in the array. Swap the smallest element with the first element. Repeat the above steps for the 2 nd, 3 rd,, smallest elements. Not included 21
22 A Function for Selection Sort Suppose we have a function that can find the minimal element. Swap the minimal element with the previous larger element. 22
23 Multidimensional Arrays A multidimensional array is an array with two or more dimensions. We will use two-dimensional arrays to represent tables of data, matrices, and other two-dimensional objects. Thus int x[3][3] would define a three by three matrix that holds integers. Initialization is bit different int x[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}}; Both indices starts at 0. x[0][0]=1 x[0][1]=2 x[0][2]=3 Think of it as x[rows][cols]. x[1][0]=4 x[1][1]=5 x[1][2]=6 x[2][0]=7 x[2][1]=8 x[2][2]=9 Row Col inside a row 23
24 Multidimensional Arrays Multidimensional arrays stand for the arrays with two or more dimensions. Syntax for normal usage: element-type aname[size 1 ][size 2 ] [size n ]; Syntax for parameter in function prototype: element-type aname[][size 2 ] [size n ] e.g., char tictac[3][3]; A 2-dimensional array with three rows and three columns. e.g., tictac[][3]; The declaration for the function prototype. 24
25 The Memory Allocated for tictac[3][3] The memory allocated for array tictac[3][3]is shown below. The number of total allocated element is 3*3=9 elements. 25
26 Initialization of Multidimensional Arrays The multidimensional array can also be initialized in declarations. The initialized values are grouped in rows. e.g., char tictac[3][3]={{ a, b, c }, { d, e, f }, {,, }}; We can also declare arrays with more dimensions. e.g., declare a 3-dimensional array. int enroll[course][campus][cls_rank]; 26
27 2D array in address space(1) How to store a multidimensional array into a onedimensional memory space. Row major ordering assigns successive elements, moving across the rows and then down the columns, to successive memory locations. Base_Address is the address of the first element of the array (A[0][0] in this case). char *base = &A[0][0]; Referring A[i][j] is same as referring *(base+4*i+j); //4 is column here 27
28 2D array in address space(2) int a[2][3]; // 2 rows and 3 cols Alternative interpretation: Three 1D array of 4 integers. The base address of the array is &a[0][0]. The array name a by itself is equivalent to &a[0]. This time it is a pointer to an array of 3 elements. Different ways to access (i,j) th element: a[i][j] *(a[i] + j) *((*(a+i)) + j) *(&a[0][0] + 3*i + j) a[1][2] a[1][1] a[1][0] a[0][2] a[0][1] a+1 a[0][0] a 28
29 Multi-dimensional Arrays. Declaration int a[n]; int b[m][n]; int c[l][m][n]; Array element a[i] b[i][j] c[i][j][k] Pointer to element a b[i] c[i][j] Pointer to 1D array of N element Pointer to 2D array of M*N element b c[i] c 29
30 Passing Multidimensional array arr[7][9][2] = {0}; sum(arr,7); For a multidimensional array we must declare all but the first dimension. Only 7 can be omitted. int sum(int a[][9][2], int asize){ int i, j, k, sum = 0 for( i = 0; i<asize; ++i) { for( j = 0; j<9; ++j) { for( k = 0; k<2; ++k) { sum += a[i][j][k]; return sum; } 30
31 The Memory Allocated for enroll[100][5][4] enroll[100][5][4] consists of 100*5*4 =2000 elements. 31
32 Figure 8.9 Function to Add Two Arrays 32
33 Figure 8.10 Function Data Areas for add_arrays(x, y, x_plus_y, 5); 33
34 Partially Filled Arrays Frequently, a program will need to process many lists of similar data These list may not all be the same length In order to reuse an array for processing more than one data set, the programmer often declares an array large enough to hold the largest data set anticipated. This array can be used for processing shorter lists as well, provided that the program keeps track of how many array elements are actually in use. One way to do this is to have the original array size declared as the highest needed, and then store a sentinel value after the last value inputted. 34
35 Sorting an Array Selection Sort Similar to sorting cards. 35
36 Figure 8.16 Trace of Selection Sort Not included 36
37 37
38 Common Programming Errors The most common error in using arrays is a subscriptrange error. An out-of-range reference occurs when the subscript value is outside the range specified by the array declaration. In some situations, no run-time error message will be produced the program will simply produce incorrect results. Other times, you may get a runtime error like segmentation fault or bus error Remember how to pass arrays to functions. Remember that the first index of the array is 0 38
39 Study (1) Write a function that computes the average grades and sorts the grades of two classes. Your program should input two lists of grades for each class. Your program should output: (1) average grades for each class; (2) a list of sorted grades of two classes. You can use the sorting function in p.401 in the text book. You can either assume the number of students in each class is fixed or prompt the user to input the number. 39
40 Study (2) e.g., Please input grades of 1 st class: Please input grades of 2 nd class: Output: The average grades of 1 st class is: 65 The average grades of 2 nd class is: 60 The sorted grades of two classes are:
Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays
Maltepe University Computer Engineering Department BİL 133 Algorithms and Programming Chapter 8: Arrays What is an Array? Scalar data types use a single memory cell to store a single value. For many problems
More informationComputer Science & Engineering 150A Problem Solving Using Computers
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using
More informationLecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)
Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of
More informationComputer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];
Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data
More informationArrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Array Many applications require multiple data items that have common
More informationLab 3. Pointers Programming Lab (Using C) XU Silei
Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions
More informationArrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Array Many applications require multiple data items that have common
More informationChapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:
Chapter 8 Arrays and Strings Objectives In this chapter, you will: Learn about arrays Declare and manipulate data into arrays Learn about array index out of bounds Learn about the restrictions on array
More informationIntroduction to Computer Science Midterm 3 Fall, Points
Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do
More informationComputer Programming: C++
The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #7 Arrays Part II Passing Array to a Function
More informationPrinciples of Programming. Chapter 6: Arrays
Chapter 6: Arrays In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning values to array elements Reading values from array elements Simple Searching
More informationDynamic memory allocation
Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes
More informationArrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)
1 Arrays General Questions 1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A. The element will be set to 0. B. The compiler would
More informationModule 6: Array in C
1 Table of Content 1. Introduction 2. Basics of array 3. Types of Array 4. Declaring Arrays 5. Initializing an array 6. Processing an array 7. Summary Learning objectives 1. To understand the concept of
More informationAPSC 160 Review Part 2
APSC 160 Review Part 2 Arrays September 11, 2017 Hassan Khosravi / Geoffrey Tien 1 Arrays A collection of data elements of the same type Stored in consecutive memory locations and each element referenced
More informationGovernment Polytechnic Muzaffarpur.
Government Polytechnic Muzaffarpur. Name of the Lab: COMPUTER PROGRAMMING LAB (MECH. ENGG. GROUP) Subject Code: 1625408 Experiment: 1 Aim: Programming exercise on executing a C program. If you are looking
More informationSubject: Fundamental of Computer Programming 2068
Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find
More informationArrays and Pointers. CSE 2031 Fall November 11, 2013
Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example
More informationArrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept
Arrays in C Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur 1 Basic Concept Many applications require multiple data items that have common characteristics.
More informationWhy arrays? To group distinct variables of the same type under a single name.
Lesson #7 Arrays Why arrays? To group distinct variables of the same type under a single name. Suppose you need 100 temperatures from 100 different weather stations: A simple (but time consuming) solution
More informationLearning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays
Chapter 5 Arrays Learning Objectives Introduction to Arrays Declaring and referencing arrays For-loops and arrays Arrays in memory Arrays in Functions Arrays as function arguments, return values Programming
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 8 Array typical problems, Search, Sorting Department of Computer Engineering Outline
More informationPointers and Arrays 1
Pointers and Arrays 1 Pointers and Arrays When an array is declared, The compiler allocates sufficient amount of storage to contain all the elements of the array in contiguous memory locations The base
More informationARRAYS(II Unit Part II)
ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful
More informationArray Initialization
Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]
More informationA First Book of ANSI C Fourth Edition. Chapter 8 Arrays
A First Book of ANSI C Fourth Edition Chapter 8 Arrays Objectives One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional
More informationArrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.
Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example
More informationIntroduction to Scientific Computing and Problem Solving
Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:
More informationCOP 3223 Introduction to Programming with C - Study Union - Fall 2017
COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................
More informationOutput of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8
Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within
More informationC Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function
More informationl Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationCOP 3223 Introduction to Programming with C - Study Union - Fall 2017
COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................
More informationComputers Programming Course 10. Iulian Năstac
Computers Programming Course 10 Iulian Năstac Recap from previous course 5. Values returned by a function A return statement causes execution to leave the current subroutine and resume at the point in
More informationFunctions. Arash Rafiey. September 26, 2017
September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic
More informationOutline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary
Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays 1 What are Arrays? Arrays are our first example of structured data. Think of a book with pages numbered 1,2,...,400.
More informationPointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables
Chapter 11 Pointers The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into bytes, with each byte capable
More informationObjectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:
Objectives Chapter 8 Arrays and Strings In this chapter, you will: Learn about arrays Declare and manipulate data into arrays Learn about array index out of bounds Learn about the restrictions on array
More informationAn 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.
Data Structures Introduction 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. Representation of a large number of homogeneous
More informationPointers. Introduction
Pointers Spring Semester 2007 Programming and Data Structure 1 Introduction A pointer is a variable that represents the location (rather than the value) of a data item. They have a number of useful applications.
More informationCSCI 2132 Software Development. Lecture 17: Functions and Recursion
CSCI 2132 Software Development Lecture 17: Functions and Recursion Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 15-Oct-2018 (17) CSCI 2132 1 Previous Lecture Example: binary
More informationFinal CSE 131B Spring 2004
Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)
More informationAMCAT Automata Coding Sample Questions And Answers
1) Find the syntax error in the below code without modifying the logic. #include int main() float x = 1.1; switch (x) case 1: printf( Choice is 1 ); default: printf( Invalid choice ); return
More informationTo declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows
Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful
More informationC Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,
More informationC How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.
C How to Program, 7/e This chapter serves as an introduction to data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of
More informationC Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables
1 6 C Arrays 6.2 Arrays 2 Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name + position number arrayname[ position number ] First element at position
More informationPointers. Pointers. Pointers (cont) CS 217
Pointers CS 217 Pointers Variables whose values are the addresses of variables Operations address of (reference) & indirection (dereference) * arithmetic +, - Declaration mimics use char *p; *p is a char,
More informationLecture 04 Introduction to pointers
Lecture 04 Introduction to pointers A pointer is an address in the memory. One of the unique advantages of using C is that it provides direct access to a memory location through its address. A variable
More informationITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University
ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 10: Arrays Readings: Chapter 9 Introduction Group of same type of variables that have same
More informationBITG 1113: Array (Part 1) LECTURE 8
BITG 1113: Array (Part 1) LECTURE 8 1 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional (1 D)
More informationUnit 7. Functions. Need of User Defined Functions
Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have
More informationPointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory
Pointers A pointer is simply a reference to a variable/object Compilers automatically generate code to store/retrieve variables from memory It is automatically generating internal pointers We don t have
More informationC for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.
C for Java Programmers 1 Last Week Very short history of C Overview of the differences between C and Java The C language (keywords, types, functies, etc.) Compiling (preprocessor, compiler, linker) C for
More informationReview of Important Topics in CS1600. Functions Arrays C-strings
Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why
More informationBITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)
BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional
More informationChapter 7 C Pointers
Chapter 7 C Pointers Objectives of This Chapter Definition and Operations with Pointers Using Pointers to pass arguments as call by reference call. Using Pointers to deal with arrays and strings. Character
More informationHow to declare an array in C?
Introduction 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. Representation of a large number of homogeneous values.
More informationArrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures.
CSE / ENGR 142 Programming I Arrays Chapter 8 8.1 Declaration and Referencing 8.2 Subscripts 8.3 Loop through arrays 8.4 & 8.5 Arrays arguments and parameters 8.6 Example 8.7 Multi-Dimensional Arrays 1998
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More informationChapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.
Chapter 12 Pointers and Arrays 1 Introduction C allows us to perform arithmetic addition and subtraction on pointers to array elements. This leads to an alternative way of processing arrays in which pointers
More information! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Fall 2018 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationCSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming
CSE101-Lec#18 Multidimensional Arrays Application of arrays Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Defining and processing 1D array 2D array Applications of arrays 1-D array A
More informationLesson 7. Reading and Writing a.k.a. Input and Output
Lesson 7 Reading and Writing a.k.a. Input and Output Escape sequences for printf strings Source: http://en.wikipedia.org/wiki/escape_sequences_in_c Escape sequences for printf strings Why do we need escape
More informationCSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. Amr Goneid, AUC 1 Arrays Prof. Amr Goneid, AUC 2 1-D Arrays Data Structures The Array Data Type How to Declare
More informationOne Dimension Arrays 1
One Dimension Arrays 1 Array n Many applications require multiple data items that have common characteristics In mathematics, we often express such groups of data items in indexed form: n x 1, x 2, x 3,,
More informationPointers. Pointer References
Pointers Pointers are variables whose values are the addresses of other variables Basic operations address of (reference) indirection (dereference) Suppose x and y are integers, p is a pointer to an integer:
More informationMemory, Arrays & Pointers
1 Memory, Arrays & Pointers Memory int main() { char c; int i,j; double x; c i j x 2 Arrays Defines a block of consecutive cells int main() { int i; int a[3]; i a[0] a[1] a[2] Arrays - the [ ] operator
More informationProgramming for Electrical and Computer Engineers. Pointers and Arrays
Programming for Electrical and Computer Engineers Pointers and Arrays Dr. D. J. Jackson Lecture 12-1 Introduction C allows us to perform arithmetic addition and subtraction on pointers to array elements.
More informationArrays and functions Multidimensional arrays Sorting and algorithm efficiency
Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same
More informationCSCI 2132 Software Development. Lecture 18: Functions
CSCI 2132 Software Development Lecture 18: Functions Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 18-Oct-2017 (18) CSCI 2132 1 Previous Lecture Example: binary search Multidimensional
More information2-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):
2-D Arrays We define 2-D arrays similar to 1-D arrays, except that we must specify the size of the second dimension. The following is how we can declare a 5x5 int array: int grid[5][5]; Essentially, this
More informationArrays. Systems Programming Concepts
Arrays Systems Programming Concepts Arrays Arrays Defining and Initializing Arrays Array Example Subscript Out-of-Range Example Passing Arrays to Functions Call by Reference Multiple-Subscripted Arrays
More informationCS16 Exam #1 7/17/ Minutes 100 Points total
CS16 Exam #1 7/17/2012 75 Minutes 100 Points total Name: 1. (10 pts) Write the definition of a C function that takes two integers `a` and `b` as input parameters. The function returns an integer holding
More informationDepartment of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04
Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Arrays and Strings Practice Sheet #04 Date: 24-01-2017 Instructions: For the questions consisting code segments,
More informationMore on Arrays CS 16: Solving Problems with Computers I Lecture #13
More on Arrays CS 16: Solving Problems with Computers I Lecture #13 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #12 due today No homework assigned today!! Lab #7 is due on Monday,
More informationArrays and Pointers in C. Alan L. Cox
Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including
More informationArrays and Applications
Arrays and Applications 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2014 Instructor: Dr. Asish Mukhopadhyay What s an array Let a 0, a 1,, a n-1 be a sequence
More informationAPS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:
Collecting Elements How to collect elements of the same type? Eg:., marks on assignments: APS105 Arrays Textbook Chapters 6.1-6.3 Assn# 1 2 3 4 5 6 Mark 87 89 77 96 87 79 Eg: a solution in math: x 1, x
More informationOutline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.
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
More informationProgramming. Pointers, Multi-dimensional Arrays and Memory Management
Programming Pointers, Multi-dimensional Arrays and Memory Management Summary } Computer Memory } Pointers } Declaration, assignment, arithmetic and operators } Casting and printing pointers } Relationship
More informationC Arrays Pearson Education, Inc. All rights reserved.
1 6 C Arrays 2 Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end:
More informationMore Arrays. Last updated 2/6/19
More Last updated 2/6/19 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16 3 1 4 rows x 5 columns 2 tj 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16
More informationArrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array
Arrays Array is a collection of similar data types sharing same name or Array is a collection of related data items. Array is a derived data type. Char, float, int etc are fundamental data types used in
More informationUNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.
UNIT 2 ARRAYS Arrays Structure Page Nos. 2.0 Introduction 23 2.1 Objectives 24 2.2 Arrays and Pointers 24 2.3 Sparse Matrices 25 2.4 Polynomials 28 2.5 Representation of Arrays 30 2.5.1 Row Major Representation
More informationAPSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr
CPSC 259: Data Structures and Algorithms for Electrical Engineers APSC 160 Review Hassan Khosravi Borrowing many questions from Ed Knorr CPSC 259 Pointers Page 1 Learning Goal Briefly review some key programming
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 3 Monday, April 17, 2017 Total - 100 Points B Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 3 Monday, April 17, 2017 Total - 100 Points A Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationProtection Levels and Constructors The 'const' Keyword
Protection Levels and Constructors The 'const' Keyword Review: const Keyword Generally, the keyword const is applied to an identifier (variable) by a programmer to express an intent that the identifier
More informationCS113: Lecture 5. Topics: Pointers. Pointers and Activation Records
CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",
More informationObjectives of This Chapter
Chapter 6 C Arrays Objectives of This Chapter Array data structures to represent the set of values. Defining and initializing arrays. Defining symbolic constant in a program. Using arrays to store, list,
More informationPDS Class Test 2. Room Sections No of students
PDS Class Test 2 Date: October 27, 2016 Time: 7pm to 8pm Marks: 20 (Weightage 50%) Room Sections No of students V1 Section 8 (All) Section 9 (AE,AG,BT,CE, CH,CS,CY,EC,EE,EX) V2 Section 9 (Rest, if not
More informationCSE2301. Arrays. Arrays. Arrays and Pointers
Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who
More informationContent. In this chapter, you will learn:
ARRAYS & HEAP Content In this chapter, you will learn: To introduce the array data structure To understand the use of arrays To understand how to define an array, initialize an array and refer to individual
More informationGoals of this Lecture
C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing
More informationArrays. CSE 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures. Arrays
CSE 142 Programming I Chapter 8 8.1 Declaration and Referencing 8.2 Subscripts 8.3 Loop through arrays Arrays 8.4 & 8.5 Arrays arguments and parameters 8.6 Example 8.7 Multi-Dimensional Arrays 2000 UW
More informationCS 2461: Computer Architecture I
Next: Pointers, Arrays, Structs... : Computer Architecture I The real fun stuff in C.. Pointers and Arrays Read Chapters 16, 18 of text Functions, Arrays, Pointers Dynamic data structures Allocating space
More informationExam 3 Chapters 7 & 9
Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False
More informationExercise 1.1 Hello world
Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example
More information