Some remarks before we get started… Last time we learned about header files – in particular the math.h header file. You know that if you include math.h.

Slides:



Advertisements
Similar presentations
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Advertisements

1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
Guidelines for working with Microsoft Visual Studio.Net.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Guidelines for working with Microsoft Visual Studio 6.
Programming Functions. A group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a value A sub-program.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Computer Science 210 Computer Organization Introduction to C.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
WEEK 4 Class Activities Lecturer’s slides.
STANDARD FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
How to start Visual Studio 2008 or 2010 (command-line program)
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Scope: Portion of the program in which the identifier can be referenced. Various types of scope, my examples are of block scope and global scope.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
1 C Programming Week 2 Variables, flow control and the Debugger.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
The Software Construction Process. Computer System Components Central Processing Unit (Microprocessor)
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
Khalid Rasheed Shaikh Computer Programming Theory 1.
How to design and code functions Chapter 4 (ctd).
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
Minimal standard C program int main(void) { return 0 ; }
Digesting a big problem ONE BYTE AT A TIME. Quiz YOU WILL WORK WITH YOUR PARTNER (LAB PARTNER NOT FULL TEAM). FOR SOLOISTS OR THOSE WHOSE PARTNER IS NOT.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Introduction to Programming Lecture 5: Interaction.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
More on conditional statements. Conditionals In some situations the typical if-else statements may become cumbersome Depending on the situation, there.
Variables, Operators, and Expressions
Simple C Programs.
CS1010 Programming Methodology
EKT120: Computer Programming
Computer Science 210 Computer Organization
TMF1414 Introduction to Programming
Review of C… The basics of C scanf/printf if/elseif statements
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Functions in C Mrs. Chitra M. Gaikwad.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Computer Science 210 Computer Organization
Константе оператори Задаци....
CSCE 206 Lab Structured Programming in C
Lec 6.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions.
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
CSCE 206 Lab Structured Programming in C
CSCE 206 Lab Structured Programming in C
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Some remarks before we get started… Last time we learned about header files – in particular the math.h header file. You know that if you include math.h you will have access to various built-in functions like cos(x), sin(x), acos(x), pow(x), floor(x), exp(x), etc…

Some remarks before we get started… What you may not know... Is that the functions may operate on a different data type than you pass in. For example: float x = 0.0; /* declare float */ float y = 0.0; /* declare as float */ y = cos(x); /* casts x as double to do cosine */  Even though you pass in a float, x, the function cos will “cast” up the float to a double (higher precision float) to perform the cosine function and return a double. After the function call, y is still remains a float, unless its declaration is changed to a double.

Some remarks before we get started… If you are interested in what data types a function reads in, type at the Linux command prompt: man cos The man command (manual) gives a bunch of information on (in this case) the cos function.

One quick question… Which one is the correct way to read in a double? #include int main(void) { double x; printf (“Input a number”); scanf(“%f”, &x); return 0; } #include int main(void) { double x; printf (“Input a number”); scanf(“%lf”, &x); return 0; } #include int main(void) { double x; printf (“Input a number”); scanf(“%d”, &x); return 0; }

Debugging… int main(void) { int x=5; int y, div, mult, sub; y = 1; mult = x * y; mult = mult/100; div = x/mult; sub = div - mult; } 1. Call Professor Garvin This code gave me a weird error of “Floating point exception”. What is a good way of determining where the problem is? 2. Call Professor Garvin’s wife (who is also Prof. Garvin) – everyone knows she is the brains in the family 3. Put printf statements writing out y, mult, div, sub, in between each of the lines 4. Call Professor Garvin’s dad – he has a Masters in Computer Science

Debugging…continued #include int main(void) { int x=5; int y, div, mult, sub; y = 1; mult = x * y; printf(“mult is %d\n”, mult); mult = mult/100; printf(“mult #2 is %d\n”, mult); div = x/mult; printf(“div is %d\n”, div); sub = div - mult; printf(“sub is %d\n”, sub); } The output is: mult is 5 mult #2 is 0 Floating point exception So, the problem begins with the div = x/mult  You are dividing by 0

Will these print the same value? #include int getLength(); int computeSQR(int); int main(void) { printf("%d\n", computeSQR(getLength())); return 0; } int getLength() { return 5; } int computeSQR(int a) { return a*a; } #include int getLength(); int computeSQR(int a); int main(void) { int length, squareLength; length = getLength(); squareLength = computeSQR(length); printf("%d\n", squareLength); return 0; } int getLength() { return 5; } int computeSQR(int a) { return a*a; } YES – it prints out 25

What is the output? #include int doSomething(int, int); int main(void) { int a = 5, b =2, c; c = doSomething(a,b); printf(“In Main: a=%d, b=%d, c=%d\n", a, b, c); return 0; } int doSomething(int a, int b) { a = 10; b = 12; printf("In Function: a=%d, b=%d\n", a, b); return a+b; } 1. In Function: a=10, b=12 In Main: a=5, b=2, c=22 2. In Function: a=10, b=12 In Main: a=10, b=12, c=22 3. In Function: a=5, b=2 In Main: a=5, b=2, c=22 4. In Function: a=10, b=12 In Main: a=5, b=2, c=7