Exercise 3 Example> Write a C function that implements /* input : integer value n output : */ int f ( int n ) { int i, sum=0; for (i=1;i<=n;i++) sum+=i*i;

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Exercises : Testing Java with JUnit
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
Functions Prototypes, parameter passing, return values, activation frams.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Writing and Testing Programs Drivers and Stubs Supplement to text.
1 Lab Session-IV CSIT121 Fall 2000 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises Lab Exercise for Demo.
Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements.
CS150 Introduction to Computer Science 1
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Basic Input/Output and Variables Ethan Cerami New York
 Experiment 07 Function Dr. rer.nat. Jing LU
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
Computer Science 111 Fundamentals of Programming I Number Systems.
Functions Copyright © J. Mercer, A function is a number-machine that transforms numbers from one set called the domain into a set of new numbers.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 3 Slides By Dr. Daniyal Alghazzawi.
More While Loop Examples CS303E: Elements of Computers and Programming.
How to start Visual Studio 2008 or 2010 (command-line program)
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Chapter 4 Practice cont.. Practice with nested loops 1.What will be the output of the following program segment: 1.for (int i = 1; i
Program Design. The design process How do you go about writing a program? –It’s like many other things in life Understand the problem to be solved Develop.
How to design and code functions Chapter 4 (ctd).
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Homework 1 (due:April 10th) Deadline : April 10th 11:59pm Where to submit? eClass 과제방 ( How to submit? Create a folder. The name.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Homework 2 (due:April 17th) Deadline : April 17th 11:59pm Where to submit? eClass “ 과제방 ” ( How to submit? Create a folder. The.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Homework 1 (due:April 13th) Deadline : April 13th 11:59pm Where to submit? eClass 과제방 ( How to submit? Create a folder. The name.
Lecture 2. Algorithms and Algorithm Convention 1.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
Chapter 6: User-Defined Functions I
Homework 3 (due:May 27th) Deadline : May 27th 11:59pm
Chapter 2 Assignment and Interactive Input
Introduction to Programming
Quiz 6 The way I expected it..
PROGRAMMING Program Development.
Recursion.
Fundamental Operation
A function with one argument
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Introduction to C Programming
Homework 2 (due:May 5th) Deadline : May 5th 11:59pm
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Introduction to Functions
Presentation transcript:

Exercise 3 Example> Write a C function that implements /* input : integer value n output : */ int f ( int n ) { int i, sum=0; for (i=1;i<=n;i++) sum+=i*i; return sum; } #include int f(int); int main() { int x, square_sum; scanf(“%d”,&x); square_sum=f(x); printf(“%d\n”,square_sum); return 0; }

1. Write a function integerPower(base, exponent) that returns the value of base For example, integerPower(3,4)=3*3*3*3. Assume that exponent is a positive, nonzero integer, and base is an integer. Function integerPower should use for to control the calculation. Do not use any math library functions. #include int integerPower(int, int); int main() { int b, e; scanf(“%d %d”,&b,&e); printf(“%d\n”,integerPower(b,e)); return 0; } int integerPower(int base, int exponent) { // insert your code here } exponent

2. (a) Write a function fact that takes a nonnegative integer as a parameter and returns its factorial value. Function prototype : int fact(int n); (b) Write a function e that estimates the value of the mathematical constant e by using the formula : ( You cannot add unlimitedly. Give your own limitation for the number of values to add. (e.g. Use 8) ). Prototype: double compute_e(); (c) Write a function ex that takes a floating-point value x as a paramenter and computes the value of by using the formula. Function prototype: double compute_ex(double x); (d) Write a program that takes x as a keyboard input and prints as an output.

3. Write a C program that gets an arbitrary number of positive integer values from keyboard and prints the smallest value and the biggest value. To end keyboard input, give -1 as an input. For example, Key board Input : Output : 2 55