EL2310 Scientific Programming

Similar documents
EL2310 Scientific Programming

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

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

The Warhol Language Reference Manual

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

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

void setup(){ void loop() { The above setup works, however the function is limited in the fact it can not be reused easily. To make the code more gene

EL2310 Scientific Programming

High Performance Computing and Programming, Lecture 3

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Scientific Programming in C IV. Pointers

The C standard library

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

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

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

2. Numbers In, Numbers Out

Why arrays? To group distinct variables of the same type under a single name.

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

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Compiling and Running a C Program in Unix

C introduction: part 1

Coursework 2: Basic Programming

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

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

C++ Important Questions with Answers

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

For instance, we can declare an array of five ints like this: int numbers[5];

2. Numbers In, Numbers Out

Introduction to C: Pointers

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Character Strings. String-copy Example

Program Organization and Comments

Final CSE 131B Spring 2004

Arrays in C++ Instructor: Andy Abreu

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Study Guide for Test 2

Arrays and Pointers in C. Alan L. Cox

Types II. Hwansoo Han

Computers Programming Course 10. Iulian Năstac

A Fast Review of C Essentials Part I

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

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Object-Oriented Programming

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

CS 32. Lecture 2: objects good?

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

EE 301 Signals & Systems I MATLAB Tutorial with Questions

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Functions. Systems Programming Concepts

BLM2031 Structured Programming. Zeyneb KURT

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Computers Programming Course 11. Iulian Năstac

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Chapter 11 Introduction to Programming in C

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

QUIZ. What are 3 differences between C and C++ const variables?

Introduction to C++ Systems Programming

EL2310 Scientific Programming

Data Type Fall 2014 Jinkyu Jeong

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

EL2310 Scientific Programming

Functions & First Class Function Values

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Programming for Engineers Functions

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

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

CSC231 C Tutorial Fall 2018 Introduction to C

/* defines are mostly used to declare constants */ #define MAX_ITER 10 // max number of iterations

CS Programming In C

CS 61c: Great Ideas in Computer Architecture

Pointers in C/C++ 1 Memory Addresses 2

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

C Programming Language (Chapter 2 of K&R) Variables and Constants

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

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Chapter 11 Introduction to Programming in C

1 Lab 2: C Programming II

ESC101N: Fundamentals of Computing End-sem st semester

AMCAT Automata Coding Sample Questions And Answers

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Computer Programming: C++

CS2141 Software Development using C/C++ C++ Basics

JTSK Programming in C II C-Lab II. Lecture 3 & 4

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

CS 61C: Great Ideas in Computer Architecture Introduction to C

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

Chapter 11 Introduction to Programming in C

Fortran. (FORmula TRANslator) History

Transcription:

Florian Pokorny Yasemin Bekiroglu

Overview Overview Matlab project Presentations Arrays Wrap Up Functions and return values Other tasks and useful stuff Strings

Matlab project Matlab Project Coin detection. Available on the course website

Presentations Presentation 8 and 9 Hashing and Locality-sensitive Hashing Spatial data structures

Presentations Presentation 8 on 29 Sept: Hashing What are hash tables and what are they good for? Describe example hashing functions. Implement a hash function in a matlab example and discuss it. How is this related to Matlab Map Containers? Show example code. Describe Locality-sensitive Hashing and implement or show an example in Matlab

Presentations Presentation 9 on 30 Sept: Spatial Data Structures A lot of data can be represented in vectorial form. Discuss spatial search, in particular the problem of nearest neighbour search. Discuss the complexity of finding a nearest neighbour naively (big O notation). Discuss what an OctTree is and show examples in matlab (you can use a library). What is the complexity of nearest neighbour lookup? Discuss what a KD tree is and show examples in matlab (you can use a library). What is the complexity of nearest neighbour lookup?

Arrays Matlab project Presentations Arrays Wrap Up Functions and return values Other tasks and useful stuff Strings

Wrap Up Last time printf for, while, do-while if-else, switch

Wrap Up for-loop Can repeat code with for-loop Syntax: for(variable=value1; <expression>; variable++) <statement> Need to declare variable and value1 above <expression> is typically something that test the value of the variable against some limits Ex: for (i = 0; i < 10; i++) { printf("i=%d\n",i); }

Wrap Up Arrays You declare an array by adding [size] after the variable name Ex: int values[10]; Note: In C the index into an array starts at 0 You set/get elements using syntax values[i]

Wrap Up Assigning initial values to arrays You can assign values to the array when you declare them int values[3] = {1,2,3}; You do not have to assign all values but you cannot assign too many You can also let the assignment define the number of elements double matrix[] = {1,2,3,4}; will give you an array with 4 elements

Wrap Up Character arrays The most commonly used array in C is the character array Ex: char myname[32]; Assigning initial value to a character array: char myname[]="this is my name";

Wrap Up Multidimensional arrays You can have more than one dimension in the array You add more [] at the end Ex: double matrix[3][3]; You set/get elements using syntax matrix[i][j]

Wrap Up Assigning initial values to arrays cont d For two dimensional arrays double matrix[3][2] = {1,2,3,4,5,6}; or a bit more clear double matrix[3][2] = {{1,2}, {3,4}, {5,6}}; Can let assigned value define size (but only one of them!) double matrix[][2] = {1,2,3,4}; will give you a 2x2 matrix

Wrap Up Task 1 Write a program that multiplies two matrices and prints the result

Functions and return values Matlab project Presentations Arrays Wrap Up Functions and return values Other tasks and useful stuff Strings

Functions and return values Functions Functions provide a way to encapsulate a piece of code Gives it a well defined input and output Makes code easier to read Often can assume the contents of a function based on its description

Functions and return values Functions, cont d Syntax: return-type function-name([arguments]) { declarations statements } If the function does return anything you give it return-type void If you return something you leave the function with statement: return value; where value is of the return-type If the function has return-type void you leave with return if you want to leave before the function ends, otherwise you do not have to give an explicit return NOTE: If your function has a return type and you do not have an explicit return the function will return something undefined.

Functions and return values return of main? main should return an int The return value can be read by whoever is calling main e.g. the OS When you have run a program in a bash shell you can see the return value in the special variable $? Ex:./hello echo $?

Functions and return values Arguments to functions Can pass arguments into functions like in Matlab double convert to fahrenheit(double tempc); double convert(double in, int type); The arguments become independent local variables inside function

Functions and return values Declaring functions A function just like a variable need to be declared before it is used Either put the definition of the function before it is used or, add a declaration of it first and then later define it File example: #includes #defines function declarations main() {...} function definitions

Other tasks and useful stuff Matlab project Presentations Arrays Wrap Up Functions and return values Other tasks and useful stuff Strings

Other tasks and useful stuff Task 2 Write function that returns the probability to draw a certain value x given that it is from a normal distribution N (µ, σ) double getprob(double x, double mean, double sigma); Print a table with x and p(x) Hint: You will have to include <math.h> and link with libm (math)

Other tasks and useful stuff Linking to extra libraries Often use function defined in other libraries, such as cos, sin, exp from libm Need to tell linker that it should use libm as well gcc -o mymathprg mymathprg.c -lm

Other tasks and useful stuff enum enumeration constant An alternative to using many #define Ex: enum state { STATE START, STATE RUN, STATE STOP}; First name assigned value 0, next 1, etc The same with #define #define STATE START 0 #define STATE RUN 1 #define STATE STOP 2 Can give value to all names manually Unassigned names will be assigned last + 1

Other tasks and useful stuff Task 3 Test enum What if you add as a last item NUMBER OF ITEMS in the enum?

Strings Matlab project Presentations Arrays Wrap Up Functions and return values Other tasks and useful stuff Strings

Strings char array: C style strings Ex: char name[] = "Tulou"; strlen(...) return length of a string A string is terminated by \0 The variable name will be of length 6 where last character has value \0 Hint: You have to include <string.h>

Strings Task 4 Experiment with char arrays, strlen and sizeof What if char [] name= "John Smith", what is the string length? What is the array size in bytes? What happens if you set name[4] = 0;

Strings Relational operators > greater than >= greater than or equal to < less than <= less than or equal to == equal to!= not equal to

Strings Task 5 What will the following do? i = 4; printf("i=%d\n", i); if (i = 1) printf("i=%d\n", i); printf("i=%d\n", i);

Strings Assignment Assignment returns value Therefore, we can assign multiple variables Ex: x = y = 0; Assigns from right to left

Strings Question Make the following expression clear by adding parentheses x = y = z = 4;