Personal SE. Functions & Arrays

Similar documents
Personal SE. Functions, Arrays, Strings & Command Line Arguments

CS1150 Principles of Computer Science Methods

CS1150 Principles of Computer Science Methods

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CS111: PROGRAMMING LANGUAGE II

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Object Oriented Programming. Java-Lecture 6 - Arrays

Arrays and Pointers in C & C++

Array. Prepared By - Rifat Shahriyar

Lecture 9 - C Functions

The Warhol Language Reference Manual

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

An Introduction to Subtyping

Arrays and Pointers (part 1)

Arrays IT 1033: Fundamentals of Programming

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

CPSC 427a: Object-Oriented Programming

Chapter 9 Introduction to Arrays. Fundamentals of Java

Methods CSC 121 Fall 2014 Howard Rosenthal

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

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

Arrays and Pointers (part 1)

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

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

by Pearson Education, Inc. All Rights Reserved. 2

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Lecture 5: Methods CS2301

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

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

C Programming Review CSC 4320/6320

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Computer Programming: C++

Procedural Programming & Fundamentals of Programming

Methods. CSE 114, Computer Science 1 Stony Brook University

Exam 3 Chapters 7 & 9

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

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.

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

CS111: PROGRAMMING LANGUAGE II

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

Last Class. More on loops break continue A bit on arrays

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Arrays. Eng. Mohammed Abdualal

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CMPT 102 Introduction to Scientific Computer Programming

Computer Science & Engineering 150A Problem Solving Using Computers

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

CSCI 111 Second Midterm Exam Spring Solutions 09.05am 09.55am, Monday, May 07, 2018

CS240: Programming in C

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Introduction to Programming Using Java (98-388)

Chapter 7 Functions. Now consider a more advanced example:

CS-201 Introduction to Programming with Java

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

CS159. Nathan Sprague

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Outline. iterator review iterator implementation the Java foreach statement testing

Java Primer 1: Types, Classes and Operators

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal

Array. Array Declaration:

Protection Levels and Constructors The 'const' Keyword

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CS240: Programming in C

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Name :. Roll No. :... Invigilator s Signature :.. CS/B.TECH (NEW)/SEM-2/CS-201/ BASIC COMPUTATION & PRINCIPLES OF COMPUTER PROGRAMMING

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Spring 2010 Java Programming

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

9. Subprograms. 9.2 Fundamentals of Subprograms

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

1 Lexical Considerations

Industrial Programming

Array Basics: Outline

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Transcription:

Personal SE Functions & Arrays

Functions in C Syntax like Java methods but w/o public, abstract, etc. As in Java, all arguments (well, most arguments) are passed by value. Example: void try_swap( int x, int y ) { int t = x ; x = y ; y = t ; Doesn't work: x and y are copies of the arguments in the caller. Changing the copy has no effect in the caller.

Functions in C Functions must be declared before use: Declare means specify name, return value, and argument types. Technically functions can default to an implicit declaration. Never rely on implicit declaration! Indeed, in C everything must be declared before use!

Functions in C Functions must be declared before use: Declare means specify name, return value, and argument types. Indeed, in C everything must be declared before use! extern int min(int x, int y) ; // Declaration of min static int max(int x, int y) ; // Declaration of max int max_div_min(int x, int y) { return max(x, y) / min(x, y) ; int min(int x, int y) { return (x <= y)? x : y ; // Definition of min static int max(int x, int y) { // Definition of max return (x >= y)? x : y ;

Functions in C extern: defined elsewhere (possibly this file). Functions must be declared before use: Declare means specify name, return value, and argument types. Indeed, in C everything must be declared before use! extern int min(int x, int y) ; // Declaration of min static int max(int x, int y) ; // Declaration of max int max_div_min(int x, int y) { return max(x, y) / min(x, y) ; int min(int x, int y) { return (x <= y)? x : y ; // Definition of min static int max(int x, int y) { // Definition of max return (x >= y)? x : y ;

Functions in C static: defined and known only in this C source file. Functions must be declared before use: Declare means specify name, return value, and argument types. Indeed, in C everything must be declared before use! extern int min(int x, int y) ; // Declaration of min static int max(int x, int y) ; // Declaration of max int max_div_min(int x, int y) { return max(x, y) / min(x, y) ; int min(int x, int y) { return (x <= y)? x : y ; // Definition of min static int max(int x, int y) { // Definition of max return (x >= y)? x : y ;

Generic form: type name[size] ; Examples: #define MAX_SAMPLES (100) int samples[max_samples] ;

Generic form: type name[size] ; Examples: Array of 100 integers. Indices run 0.. 99 NO SUBSCRIPT CHECKS! NOTE THE USE OF SYMBOLIC CONSTANT! #define MAX_SAMPLES (100) int samples[max_samples] ;

Generic form: type name[size] ; Examples: Simple summation of array values. #define MAX_SAMPLES (100) int samples[max_samples] ; int sum = 0 ; int i ; for ( i = 0 ; i < MAXSAMPLES ; ++i ) { sum += samples[ i ] ;

#define DIMENSION (50) ; double m1[dimension][dimension] ;

A matrix or a 2 dimensional array Access by m1[ i ][ j ] #define DIMENSION (50) ; double m1[dimension][dimension] ;

Matrix multiplication to show use of double indices. #define DIMENSION (50) ; double m1[dimension][dimension] ; double m2[dimension][dimension] ; double product[dimension][dimension] ; int i, j, k ; for ( i = 0 ; i < DIMENSION ; ++i ) { for ( j = 0 ; j < DIMENSION ; ++j ) { product[ i ][ j ] = 0.0 ; for ( k = 0 ; k < DIMENSION ; ++k ) { product[i][j] += m1[i][k] * m2[k][j] ;

Arrays are passed to functions by reference.

Arrays are passed to functions by reference. Changes to the array contents in the function will be visible to the caller, e.g., array copy.

Arrays are passed to functions by reference. Changes to the array contents in the function will be visible to the caller, e.g., array copy. void acopy( int to[], int from[], int size ) { int i ; for( i = 0 ; i < size ; i++ ) { to[ i ] = from[ i ] ;

Arrays are passed to functions by reference. Changes to the array contents in the function will be visible to the caller, e.g., array copy. void acopy(int to[], int from[], int size) { int i ; for( i = 0 ; i < size ; i++ ) { to[ i ] = from[ i ] ; Need not, but may, give the array size.

- Review Array size fixed at definition time. Good practice (that is, OUR practice) is to use symbolic constants to define array sizes. Array indices are integers. Legal indices run from 0 to arraysize - 1 C will not prevent you from indexing outside the bounds of the array (no subscript checks). Arrays are passed by reference.