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

Similar documents
CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS 376b Computer Vision

My First Command-Line Program

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

Fast Introduction to Object Oriented Programming and C++

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CIS 190: C/C++ Programming. Classes in C++

Operator overloading

How to approach a computational problem

Program Organization and Comments

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

C and C++ I. Spring 2014 Carola Wenk

15: Polymorphism & Virtual Functions

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS24 Week 3 Lecture 1

Exercise 1.1 Hello world

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

CSE 333 Midterm Exam 7/25/16. Name UW ID#

Lab 1: First Steps in C++ - Eclipse

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Crash Course into. Prof. Dr. Renato Pajarola

CMSC 202 Midterm Exam 1 Fall 2015

Increases Program Structure which results in greater reliability. Polymorphism

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Scientific Computing

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Object Oriented Programming: Inheritance Polymorphism

Practicum 5 Maps and Closures

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

CSE 333 Midterm Exam 7/27/15 Sample Solution

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

6.096 Introduction to C++ January (IAP) 2009

The C standard library

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

C introduction: part 1

y

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

AN OVERVIEW OF C++ 1

C++_ MARKS 40 MIN

Engineering Tools III: OOP in C++

Why VC++ instead of Dev C++?

C++ for Java Programmers

Object-Oriented Programming (OOP) Fundamental Principles of OOP

PIC 10A Objects/Classes

Your first C and C++ programs

Inheritance, and Polymorphism.

pointers + memory double x; string a; int x; main overhead int y; main overhead

Algorithms and Programming I. Lecture#12 Spring 2015

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

CSCE 206: Structured Programming in C++

Dr. Md. Humayun Kabir CSE Department, BUET

CSC231 C Tutorial Fall 2018 Introduction to C

C++ For Science and Engineering Lecture 12

Computational Methods of Scientific Programming Lecture 8. Today s lecture Start C/C++ Basic language features

Welcome to... CS113: Introduction to C

OBJECT ORIENTED PROGRAMMING

Introduction to C ++

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

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

EC 413 Computer Organization

CS 11 C++ track: lecture 1

Separate Compilation of Multi-File Programs

C, C++, Fortran: Basics

Unit 14. Passing Arrays & C++ Strings

Separate Compilation Model

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

COMP322 - Introduction to C++ Lecture 01 - Introduction

Starting to Program in C++ (Basics & I/O)

Object Reference and Memory Allocation. Questions:

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

Reliable C++ development - session 1: From C to C++ (and some C++ features)

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS11 Introduction to C++ Fall Lecture 1

W3101: Programming Languages C++ Ramana Isukapalli

A SHORT COURSE ON C++

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

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

Object-Oriented Programming, Iouliia Skliarova

Lecture 23: Pointer Arithmetic

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

A Tour of the C++ Programming Language

GCC : From 2.95 to 3.2

Arrays in C++ Instructor: Andy Abreu

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

CSCE Practice Midterm. Data Types

Exceptions, Case Study-Exception handling in C++.

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

CS

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CE221 Programming in C++ Part 1 Introduction

A brief introduction to C++

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

A Tour of the C++ Programming Language

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

Transcription:

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

ANSI C Standards published by the American National Standards Institute (1983-1989). Initially developed by Dennis Ritchie between 1969 and 1973 Dennis Ritchie http://en.wikipedia.org/wiki/c_(programming_language)

Why is it a good idea to write your program in C? It gives you the ability to program in a very solid and portable language. It is very flexible and fast. If you want to create a very efficient application, it is the way to go. Lots of ready to use libraries. If you program in C you can program almost in any computer language.

Why is it NOT a good idea to write your program in C? It can be tedious if you don t know exactly what you are doing. If your are writing a small App that you are going to run just once, then is probably not worth it to spend time trying to put together a C++ program. Just use Matlab and you ll be fine But, when you need speed and efficiency, maybe you should think about writing your code in C.

How is C and C++ compared to other languages? (I) In terms of preconfigured features: Matlab, Visual Basic, Java ANSI C, C++

How is C and C++ compared to other languages? (II) In terms of speed and flexibility Matlab, Visual Basic ANSI C, C++

Are we going to learn C today? In today s class, we will try to give you an insight into how to use and interpret a C++ program, but we don t expect you stop using Matlab. To really learn how to program in C takes more than just one class, but maybe you would like to get started so, here we go

What is the difference between C and C++? The language structure is the same, but C++ introduces the Object Oriented Programming (OOP) concept. OOP introduces the following: Objects (classes) that have associated properties and methods. It is possible to create as many instances of an object as there is memory in a computer. This means, that you can create copies of your objects an let them interact with other objects, procedures, functions, etc.

OOP in 2 minutes Encapsulation: restriction to access certain procedures and properties. Inheritance: one object can have other objects inside and can inherit their properties without having to write more code. Polymorphism: create procedures for objects whose exact type is not known until runtime. A very usefull extension of polymorphism is overloading.

Encapsulation & Inheritance http://www.c-sharpcorner.com/uploadfile/e881fb/learn-object-oriented-programming-using-c-sharp-part-5/

Polymorphism

Function Overloading Some languages (like C and C++) allows us to overload a function or operator depending on what arguments we pass. void eat(apple apple1) { bite(apple1); chew(); swallow(); } void eat(banana banana1) { banana1.peel(); bite(banana1); chew(); swallow(); } // take a bite of the apple // chew // then swallow. // peel the banana first // take a bite of the banana // chew // then swallow.

A simple C program #include <stdio.h> int main() { int i=0; for(i=1; i<5; i++) printf( Hello World\n ); } return 0; To compile, just type: gcc main.c o main.o

A simple C++ program #include <iostream> using namespace std; int main() { int i=0; for(i=1; i<5; i++) cout << Hello World << endl; } return 0; To compile, just type: c++ main.c o main.o

Libraries and Includes There are lots of free libraries to do EVERYTHING in C and C++. These include: matrix multiplication, FFT, image processing, audio handling, etc. To include a library can be non-trivial, but if everything works OK, it should be as simple as typing: #include <library>

There is a library for everything If you want to do math operations (like, take a sine, cosine or square root) you will need to include math.h The language does minimal operations (like + - *) but the compiler doesn t know how to interpret sin(x) unless you include the right library.

C, C++ operators i++; means i = i + 1; i--; means i = i - 1; i += j; means i = i + j; i *= j; means i = i * j; i = pow(j,2); means i = j^2. It requires math.h All statements are terminated with a ;

Overloaded Operators In C, you can overload almost any operator. For example, << means left shift. But, including iostream it can mean output. Another example: A*B can be a normal multiplication or a matrix multiplication or a scalar time matrix multiplication depending on what A and B are.

C, C++ Decision Blocks int i=0; [other code in here ] if (i == 2) { cout << i = << i << endl; }else{ cout << i is not 2 << endl; }

C, C++ For Loops int i=0; double result=0; [other code in here ] for(i=1; i<5; i++) { [Other operations in here ] cout << Result << result << endl; }

Doing Something Useful Copy to your work dir the directory named armadillo from gaia/ddgomez/public/ Create a new file named main.cpp in the same directory and open it in xcode Type the following

Using Armadillo to Multiply Matrices #include <iostream> #include armadillo/include/armadillo using namespace std; using namespace arma; int main() { mat A=randu<mat>(3,3); mat B=eye(3,3); cout << Matrix A: << endl << A << endl; cout << Matrix B: << endl << B << endl; cout << Multiplication << endl << A*B << endl; return 0; } To compile use: c++ main.c -o main.o To use Armadillo visit: http://arma.sourceforge.net/

Solving a Linear System of Equations Create a matrix called A like this mat A= 1 2 4; 2 0 1; 1 2-2 ; Create a matrix L like this mat L= 7; 3; 1 ; Use the inv() function to solve the system Ax = L and display the output (x). To compile this program, use: c++ main.cpp -o main -llapack

Your code should look like this... #include <iostream> #include "../armadillo/include/armadillo" using namespace std; using namespace arma; int main() { mat C="7; 3; 1"; mat D="1 2 4; 2 0 1; 1 2-2";......

Advanced Stuff (I) Pointers: indirect addressing technique to read and write data from the computer memory. They are declared with an * int *A=0; double *vector=0;

Advanced Stuff (II) Address of operator:

Pointer to char Example #include <iostream> using namespace std; int main() { const char *str="hi there! This is a pointer test."; for (int i=0; i<strlen(str); i++) { cout << str[i]; } cout << endl; cout << "This string was stored in memory address: " << &str << endl; } return 0;

C++ Reference Website Reference guide http://www.cplusplus.com/reference/ Tutorial: http://www.cplusplus.com/doc/tutorial/