CS 108 Computing Fundamentals. October/November Array Bootcamp

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

More Arrays. Last updated 2/6/19

CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso

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):

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

BİL200 TUTORIAL-EXERCISES Objective:

Programming in C. Pointers and Arrays

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Example: Pointer Basics

C Programming Lecture V

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

CSE 142 Wi01 Midterm 2 page 1 of 6

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Module 6: Array in C

CS16 Exam #1 7/17/ Minutes 100 Points total

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

Programming Language B

Decision Making -Branching. Class Incharge: S. Sasirekha

Dynamic memory allocation

Fundamental of Programming (C)

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

BSM540 Basics of C Language

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Programming for Electrical and Computer Engineers. Pointers and Arrays

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

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

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

Fundamentals of Programming. Lecture 14 Hamed Rasifard

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

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CSCI 2132 Software Development. Lecture 18: Functions

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

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Two Dimensional Array - An array with a multiple indexs.

Arrays. Systems Programming Concepts

ECE264 Fall 2013 Exam 1, September 24, 2013

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

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Language comparison. C has pointers. Java has references. C++ has pointers and references

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

Two Dimensional Arrays

Binghamton University. CS-211 Fall Pointers to Pointers

Variables, Pointers, and Arrays

LAB 5: REPETITION STRUCTURE(LOOP)

DS: CS Computer Sc & Engg: IIT Kharagpur 1. roblem Set III. Goutam Biswas

Midterm Review. Short Answer. Short Answer. Practice material from the Winter 2014 midterm. Will cover some (but not all) of the questions.

Principles of Programming. Chapter 6: Arrays

Introduction to C. Systems Programming Concepts

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Fundamentals of Programming Session 12

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Fundamental of Programming (C)

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Dynamic Allocation of Memory Space

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

Data Structures Code Handout By Ahmed Ibrahim Villa College

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

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

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

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

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

Pointers, Arrays, and Strings. CS449 Spring 2016

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

ESC101N Fundamentals of Computing

MTH 307/417/515 Final Exam Solutions

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

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

More Pointers Week 11

LAB 5: REPETITION STRUCTURE(LOOP)

Algorithms & Programming. Midterm Examination # 2. Student Name: Student ID Number: Duration of examination:

UNIVERSITY OF SWAZILAND

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Character Strings. String-copy Example

Chapter 4. Section 4.4 Passing Parameters to Functions. CS 50 Hathairat Rattanasook

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

LAB 7 FUNCTION PART 2

ECE264 Spring 2013 Exam 1, February 14, 2013

ESC101N: Fundamentals of Computing End-sem st semester

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

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

printf("this program adds the value 10 to a given integer number.\n\n");

Lab Session # 7 Pointers. ALQUDS University Department of Computer Engineering

Project 5 Handling Bit Arrays and Pointers in C

Fundamentals of Programming Session 8

Computer Science & Engineering 150A Problem Solving Using Computers

Solution for Data Structure

Example 3. Young Won Lim 11/22/17

POINTER & REFERENCE VARIABLES

Two Dimensional Array - An array with a multiple indexs.

Subject: Fundamental of Computer Programming 2068

Transcription:

CS 108 Computing Fundamentals October/November 2017 Array Bootcamp

For arrays: passing to a function "by value" means passing a single element's "contents" For arrays: no more than one element's contents can be sent to a function if passed "by value" For arrays: "passing by value" means one element's contents passed to the function at-a-time Remember: "passing by value" means a COPY of an element's value is sent to the function... the original value is untouchable Here's an example. #include <stdio.h> // 1.c void show ( int ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; for ( i = 0 ; i <= 7 ; i = i + 1 ) show ( grades [ i ] ) ; void show ( int p_grade ) printf ( "%d\n", p_grade ) ; return ;

The next program demonstrates that the original values are untouchable #include <stdio.h> // 2.c void show ( int ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; for ( i = 0 ; i <= 7 ; i = i + 1 ) show ( grades [ i ] ) ; printf ("\n\n") ; for ( i = 0 ; i <= 7 ; i = i + 1 ) printf ( "%d\n", grades [ i ] ) ; void show ( int p_grade ) p_grade = p_grade - 50 ; printf ( "%d\n", p_grade ) ; return ;

Now lets pass INDIVIDUAL ARRAY ELEMENT ADDRESSES to the function (aka "passing by address" but one address at a time) For arrays: passing to a function "by address" may mean a single element's address or the address of the array For arrays: when an address is passed, it's possible to access any array element and change its contents The program demonstrates that the original values are changed. #include <stdio.h> // 3.c void show ( int * ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; for ( i = 0 ; i <= 7 ; i = i + 1 ) show ( &grades [ i ] ) ; printf ("\n\n") ; for ( i = 0 ; i <= 7 ; i = i + 1 ) printf ( "%d\n", grades [ i ] ) ; void show ( int *p_grade ) *p_grade = *p_grade - 50 ; printf ( "%d\n", *p_grade ) ; return ;

Let's practice some pointer arithmetic. The next program demonstrates more pointer arithmetic. #include <stdio.h> // 5.c void show ( int * ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; printf("\n\nenter an index number between 0 and 7: ") ; scanf("%d", &i ); show ( &grades [ i ] ) ; printf ("\n\n") ; for ( i = 0 ; i <= 7 ; i = i + 1 ) printf ( "%d\n", grades [ i ] ) ; void show ( int *p_grade ) int input = 0 ; printf("\n\nenter an integer to be used to adjust the pointer: ") ; scanf("%d", &input ) ; *(p_grade + input) = 0 ; return ;

The next program print memory locations and the values found there. #include <stdio.h> // 6.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; for ( i = 0 ; i <= 7 ; i = i + 1 ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", &grades[ i ] ) ; printf ( "integer value: %d \n", grades[ i ] ) ;

We don't need to use a for loop with arrays #include <stdio.h> // 7.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; while ( i <= 7 ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", &grades[ i ] ) ; printf ( "integer value: %d \n", grades[ i ] ) ; i++ ;

Next, we access the array using a pointer and pointer notation. #include <stdio.h> // 8.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; int *iii = &grades[ 0 ] ; while ( i <= 7 ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", (iii + i ) ) ; printf ( "integer value: %d \n", *( iii + i ) ) ; i++ ;

Next, we access the array using the array name and pointer notation. #include <stdio.h> // 9.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; while ( i <= 7 ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", (grades + i ) ); printf ( "integer value: %d \n", *(grades + i ) ); i++ ;

We can pass a 1D array "by address/reference" using array notation #include <stdio.h> // 10.c void show ( int [ ], int ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; show ( grades, 7 ) ; printf ("\n\n") ; void show ( int p_grade[ ], int size ) while ( i <= size ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", &p_grade[ i ] ); printf ( "integer value: %d \n", p_grade[ i ] ); i++ ; return ;

Or we can pass a 1D array "by address/reference" using pointer notation #include <stdio.h> // 11.c void show ( int *, int ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; show ( grades, 7 ) ; printf ("\n\n") ; void show ( int *p_grade, int size ) while ( i <= size ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", ( p_grade + i ) ); printf ( "integer value: %d \n", *( p_grade + i ) ); i++ ; return ;

Teaching point: given a 1D integer array named grades, all of the following notations are equivalent. grades[ i ] *(grades + i ) *(i + grades ) Teaching point: given a 1D integer array named grades, all of the following notations are equivalent. &grade[ i ] (grade + i ) (i + grade ) #include <stdio.h> // 12.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; while ( i <= 7 ) printf ( "Element %d ", i ) ; printf ( "address: %p ", &grades[ i ] ); printf ( "%p ", ( grades + i ) ); printf ( "%p ", ( i + grades) ); printf ( " value: %d ", grades[ i ] ); printf ( "%d ", *( grades + i ) ); printf ( "%d \n", *( i + grades ) ); i++ ;

We can create/use 2D arrays. #include <stdio.h> // 13.c int student = 0, test = 0 ; int grades [ 6 ] [ 3 ] = 1, 4, 5, 13, 15, 19, 22, 23, 27, 35, 36, 38, 44, 45, 49, 50, 51, 52 ; for ( student = 0 ; student <= 5 ; student++ ) printf("\n") ; for ( test = 0 ; test <= 2 ; test++ ) printf("\t%3d ", grades[ student ] [ test ] ) ; printf("\n\n") ;

We can easily pass a 2D array to a function using array notation. Notice: do not generally provide the first dimension in the prototype and the formal parameters list to allow the function to be more general in nature. Notice: you must provide the second dimension in the prototype and the formal parameters list. #include <stdio.h> // 14.c void show ( int [ ] [ 3 ], int, int ) ; int grades [ 6 ] [ 3 ] = 1, 4, 5, 13, 15, 19, 22, 23, 27, 35, 36, 38, 44, 45, 49, 50, 51, 52 ; show (grades, 5, 3 ) ; printf("\n\n") ; void show (int p_grades [ ] [ 3 ], int num_students, int num_tests ) int row = 0, col = 0 ; for ( row = 0 ; row < num_students ; row++ ) printf("\n") ; for ( col = 0 ; col < num_tests ; col++ ) printf("\t%3d ", p_grades[ row ] [ col ] ) ; return ;