8/21/2015
A First Look At C
Lets have a first look at C
We examine a simple program that instructs the computer to output a greeting (Hello World) to
the screen.
It is stored in a plain text file with a suffix of .c to indicate that the file contains a C
06/11/2013
2013
From C to Fortran  Part 3
2
Parameter Passing Example
FROM C TO FORTRAN
PART 3
Nathan Friedman
2013
program testing
implicit none
integer : x, y, add
x = 13
y = 4
write (*,*) x, y
write (*,*) add(x,y)
write (*,*) x, y
end program
integer
REVIEW OF
RECURSION
2010
Recursion
2
Factorial
The factorial function is a classic example of the
use of recursion
int fact(int n)cfw_
if (n > 1)
return n * fact(n1);
else
return 1;
2010
Recursion
3
Recursive Binary Search
int recursive_binary_search
(i
NUMBERS
Nathan Friedman
2013
Computer Structure
Earlier we saw that computers are made up of
switches of some kind that can be on or off
We can represent these two values by 0 and 1
We build circuits to store binary sequences
We encode instructions as
Nathan Friedman
2012
Computing for Engineers
Reason #1:
My faculty made me!
Reason #2:
Its exciting to learn how to understand
computers better
2011
3
There are three components to this course
Computing for Engineers
2
Reason #1:
My faculty made me!
Comp
2009
Multidimensional Arrays
2
Power Output (Etter chapter 5.8)
We want to study the power output of an
MULTIDIMENSIONAL
ARRAYS
electrical plant over time
Given a file with the following structure
Each line represents the data acquired in a week
There
2010
Recursion
2
What is Recursion?
Recursion is a programming technique that
allows us to approach programming in a
different style
To solve a problem, we make an
assumption that the computer is able to
solve smaller instances of the same
problem
RECUR
02/10/2013
2013
Pointers in C
2
Addresses
Variables are stored in memory locations
THEY WENT THAT
AWAY!
Each memory location has a unique address, a positive
integer value
This address is determined when the program is compiled
and loaded
Pointers in C
2011
Subprograms
2
Functions
There are many useful operations which
DELEGATING TASKS
are not part of the basic instruction set of
the computer
C has many libraries containing such
functions for our use. We have seen some
of these intrinsic or predefined
9/2/2013
2013
Files and Arrays
2
Files
Large amounts of data are usually stored in files
These files can be created by programs that
DATA COLLECTIONS
generate values or capture data from sensing
devices
Programs can read and process the data stored
in
2011
Searching and Sorting
2
Wheres Waldo?
SEARCHING AND
SORTING IN C
A common use for computers is to search for the
Nathan Friedman
2013
We are given an array along with its size and a
whereabouts of a specific item in a list
The most straightforward
10/25/2013
2011
From Fortran to C  The Basics
2
Our Goals
How are Fortran and C similar?
FORTRAN TO C
THE BASICS
What are the differences?
How can I use my knowledge of C to program in
Fortran?
Nathan Friedman
2013
2011
From Fortran to C  The Basics
ARRAYS AND
SUBPROGRAMS
Nathan Friedman
2013
2013
Arrays and Files
2
Arrays
Arrays can be defined in Fortran just as they can
in C
The concept is the same but there are a couple of
differences
The syntax is slightly different
The major idea to get used
Recall Function Definitions
Declare function name, inputs, and
outputs
Introduction to MATLAB
Part 3
Syntax:
function [y1, ., yN] = myfun(x1, ., xM)
Inputs: x1,xM
Outputs: y1,yN
Save code in a text file with .m extension, and
name the file name match
8/24/2015
Back to the roots of a quadratic
Lets have another look at our program for finding the roots of a quadratic
equation
Choices, Choices,
Choices
We used the classic formula that involved finding the square root of the
discriminant
What if the disc
8/24/2015
Light from the Sun
How long (in minutes) does it take light to travel from the sun to earth?
Assume the following:
1. Light travels 9.46 x 1012 km a year
(2 + 2 = ?)
2. A year is 365 days, 5 hours, 48 minutes and 45.9747 seconds long
3. The aver
COMP 208: Computers in Engineering
Winter 2016: Assignment 1
This assignment is due on January 26, 2016 at 11:59 PM. It must be submitted using
MyCourses.
The cutoff time is automated. Assignments submitted within the next hour will be
accepted but marked
COMP 208: Computers in Engineering
Winter, 2016
Assignment 2: Backwards and Forwards
Due Date
Assignment 2 is due on Thursday, February 11 at 23:59. The cutoff is automated and is
exactly at this time. Assignments submitted within the next hour will be co
ROOT FINDING
Friedman
2013
2013
Root Finding
2
Root Finding
Many applications involve finding the roots of a
function f(x).
That is, we want to find a value or values for x
such that f(x)=0
2013
Root Finding
3
Roots of a Quadratic
We have already seen a
Introduction to MATLAB
Fall 2013
Wenbo He
Part II: More on Matlab
Basics
M Files
Operations and Buildin functions
Plotting
Save, Load and Delete
Script
What is a script file or m file?
Simple text with a sequence of Matlab
commands
Why we use a sc
Introduction to MATLAB
Fall 2013
Wenbo He
Basics
MATLAB Environment
Getting Help
Variables
Vectors, Matrices, and Linear Algebra
What is Matlab?
Matrix Laboratory Package
High Level Language
Interactive Programming
Environment.
Matlab
High Level
Langua
06/11/2013
2013
Numerical Integration
2
Integration
NUMERICAL
INTEGRATION
Many applications require evaluating the integral
Nathan Friedman
2013
As we have seen, we may not even have an
2013
of a function
The integrals of many elementary functions
cann
Basics
Introduction to MATLAB
Flow Control
Loops
Fall 2013
What is Matlab?
Matrix Laboratory Package
High Level Language
Interactive Programming
Environment.
MATLAB Environment
Getting Help
Variables
Vectors, Matrices, and Linear Algebra
Script(.m fi
9/2/2013
2011
Do Loops and Arrays
2
Repetition
OVER AND OVER AND
OVER AGAIN
Repetition
Nathan Friedman
2011
Do Loops and Arrays
3
A table of temperature conversions
void main() cfw_
float celsius, fahrenheit;
To fully take advantage of the speed of a
com
2013
Introduction to C Programming
2
Recall Our First Program
#include <stdio.h>
INTRODUCTION TO C
void main () cfw_
/* This is my first C program */
Nathan Friedman
2013
printf ("Hello World\n");
2013
Introduction to C Programming
Concepts We Introduced
Tuesday, September 20, 2016
Chp 3 Variables/Assignments
3.1 Floatingpoint numbers (double)
 A floatingpoint literal is a number with a fractional part
Language
 A variable defined as type double stores a floatingpoint number.
double variableName = 0
Tuesday, October 18, 2016
Chp 10 Input/output
10.1 The stdout file pointer
 FILE*, called a "file pointer," is a pointer to a FILE structure that allows programs to
read and write to files. FILE* is available via #include <stdio.h>.

Language:
 cds
10.
COMP 208
Computers in Engineering
Winter, 2014
McGill University values academic integrity. Therefore, all students must understand
the meaning and consequences of cheating, plagiarism and other academic offences
under the Code of Student Conduct and Disc
4/8/2016
2011
From Fortran to C  The Basics
2
Our Goals
How are Fortran and C similar?
C TO FORTRAN
THE BASICS
What are the differences?
How can I use my knowledge of C to program in
Fortran?
Nathan Friedman
2013
2011
From Fortran to C  The Basics
3
Final Examination
Version 1
COMP 208  Computers in Engineering
Friday, December 12, 2014
9:00  12:00
Examiner:
Professor Nathan Friedman
Assoc Examiner:
Professor Amir Rabbani
INSTRUCTIONS:
This is a CLOSED BOOK examination.
This examination consists of
Final Examination
Version 1
COMP 208  Computers in Engineering
Monday, December 21, 2015
18:00  21:00
Examiner:
Professor Nathan Friedman
Assoc Examiner:
Professor Morteza Rezanejad
INSTRUCTIONS:
This is a CLOSED BOOK examination.
This examination consi
Final Examination
Version 1
COMP 208  Computers in Engineering
Tuesday, April 30, 2013
9:00  12:00
Examiner:
Prof. Nathan Friedman
Assoc Examiner: Prof. C. Verbrugge
INSTRUCTIONS:
This is a CLOSED BOOK examination.
This examination consists of 25 multip
Recursion
NATHAN FRIEDMAN
2015
What is Recursion?
Recursion is a programming technique that
allows us to approach programming in a
different style
To solve a problem, we make an assumption
that the computer is able to solve smaller
instances of the same p