Structured programming

Similar documents
C++ Programming Lecture 11 Functions Part I

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Structured programming

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Fundamentals of Programming & Procedural Programming

Chapter 4: Basic C Operators

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Methods CSC 121 Fall 2014 Howard Rosenthal

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

Computer Science & Engineering 150A Problem Solving Using Computers

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

Standard Library Functions Outline

ECET 264 C Programming Language with Applications

CT 229 Java Syntax Continued

Introduction to Programming

Variable and Data Type 2

MYSQL NUMERIC FUNCTIONS

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Product Price Formula extension for Magento2. User Guide

ANSI C Programming Simple Programs

Introduction to Engineering gii


Chapter 1 Introduction to MATLAB

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Goals for This Lecture:

Methods CSC 121 Fall 2016 Howard Rosenthal

C Program Structures

Custom Variables (Virtual Variables)

Introduction to Python, Cplex and Gurobi

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Programming in MATLAB

C++ Overview. Chapter 1. Chapter 2

Maths Functions User Manual

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

2 Making Decisions. Store the value 3 in memory location y

Introduction to MATLAB

Python Programming: An Introduction to Computer Science

A. Introduction to Function 1. Modular Programming input processing output functions library functions 2. Function 1. Benefit of Using Functions

Methods CSC 121 Spring 2017 Howard Rosenthal

Structured programming

XQ: An XML Query Language Language Reference Manual

Structured Programming. Dr. Mohamed Khedr Lecture 4

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point

Intrinsic Functions Outline

Објектно ориентирано програмирање

Structured programming. Exercises 3

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

Introduction. What is function? Multiple functions form a larger program Modular programming

Structured programming

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Introduction To Computer Programming Laboratory Source code of a computer program is given below. What do you expect to be displayed?

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Quick MATLAB Syntax Guide

9 Using Equation Networks

Functions. Systems Programming Concepts

1. Basics 1. Write a program to add any two-given integer. Algorithm Code 2. Write a program to calculate the volume of a given sphere Formula Code

Process Optimization

TECH TIP VISION Calibration and Data Acquisition Software

USER-DEFINED ELEMENT IN ZMAN TM

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Sketchify Tutorial Properties and Variables. sketchify.sf.net Željko Obrenović

4. Modules and Functions

Engineering Problem Solving with C++, Etter/Ingber

Chapter 2. Outline. Simple C++ Programs

Introduction to MATLAB

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Introduction to MATLAB

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

Matlab Programming Introduction 1 2

Programming in QBasic

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

What is Matlab? The command line Variables Operators Functions

C Functions. 5.2 Program Modules in C

Introduction to Programming II W4260. Lecture 2

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

Assoc. Prof. Dr. Tansu FİLİK

Chapter 3 Mathematical Functions, Strings, and Objects

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Објектно ориентирано програмирање

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Program Structure and Format

Downloaded from

Structured programming

Structured programming

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Објектно ориентирано програмирање

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Assignment #3 Answers

Green Globs And Graphing Equations

LAB 1 General MATLAB Information 1

Consider this m file that creates a file that you can load data into called rain.txt

CS1010E Lecture 3 Simple C Programs Part 2

Extending Ninox with NX

Transcription:

Exercises 7 Version 1.0, 17 November, 2016

Table of Contents 1. Functions................................................................... 1 1.1. Reminder from lectures.................................................. 1 1.2. Functions from standard library math.h.................................. 2 1.3. Most used mathematical functions........................................ 2 1.4. Problem 1............................................................... 3 1.5. Problem 2............................................................... 3 1.6. Problem 3............................................................... 4 1.7. Problem 4............................................................... 5 1.8. Problem 5............................................................... 6 2. Source code of the examples and problems..................................... 8

1. Functions 1.1. Reminder from lectures 1.1.1. Defining a function in C return_type name(arguments_list) { function_body return_type - type of the return value of the function name - name of the function arguments_list - list of arguments with names and their types, coma separated function_body - the body of the function 1.1.2. Calling (using) function name(arguments_values); name - name of already defined function arguments_values - list of values passed as arguments, coma separated 1.1.3. Example of user defined function Write a program with a separate function for computing cube of number n^3 за вчитан природен број n. Example ex7_1_en.c double cube(int x) { return x * x * x; int n; scanf("%d", &n); double result = cube(n); printf("%d^3 = %.2f\n", n, result); 1. Functions 1

1.2. Functions from standard library math.h C has standard library math.h with many useful mathematical functions To be used, first it needs to be included: #include <math.h> All functions from the standard library math.h accepts arguments of type double and return values of same type. 1.3. Most used mathematical functions Function sqrt(x) exp(x) Explanation square root of x exponential function е^х log(x) natural logarithm of x (with base е) log10(x) logarithm of x of base 10 fabs(x) ceil(x) absolute value of х rounding х to the smallest integer not smaller than х floor(x) rounding х to the largest integer not larger than х + 1 pow(x, y) fmod(x, y) sin(x) cos(x) tan(x) x to the power of y residue of х/у as real number sine of х (in radians) cosine of х (in radians) tangent of х (in radians) 1.3.1. Example using function from math.h Write a program that will compute cube n^3 for an integer n. Example ex7_2_en.c #include <math.h> int n; scanf("%d", &n); double result = pow(n, 3); printf("%d^3 = %.2f\n", n, result); 2 1.2. Functions from standard library math.h

1.4. Problem 1 Да се напишат соодветни функции за пресметување на дијаметар, периметар и плоштина на круг чиј што радиус се предава како аргумент. Потоа да се напише и програма во која за внесен (од тастатура) радиус ќе се повикаат овие функции за да се пресметаат дијаметарот, периметарот и плоштината на соодветниот круг. Solution p7_4.c #define PI 3.14 double dijametar(double radius); double perimetar(double radius); double ploshtina(double radius); double radius, D, L, P; printf("vnesete radius na krugot: "); scanf("%lf", &radius); D = dijametar(radius); L = perimetar(radius); P = ploshtina(radius); printf("dijametar na krugot = %.2f\n", D); printf("perimetar na krugot = %.2f\n", L); printf("ploshtina na krugot = %.2f\n", P); double dijametar(double radius) { return 2 * radius; double perimetar(double radius) { return 2 * radius * PI; double ploshtina(double radius) { return radius * radius * PI; 1.5. Problem 2 Write a program that will print all four digit numbers that are divisible with the sum of the numbers formed from the first two and last two digits of the original number. Print how many such numbers exist. Example: 3417 is divisible with 34 + 17 5265 is divisible with 52 + 65 6578 is divisible with 65 + 78 1.4. Problem 1 3

Solution p7_5_en.c int first_two(int n) { while(n > 99) { n /= 10; return n; int last_two(int n) { return n % 10; int divisible(int n, int x) { return n % x == 0; int count = 0; for(i = 1000; i <= 9999; ++i) { if(divisible(i, first_two(i) + last_two(i))) { printf("%d\n", i); ++count; printf("total: %d\n", count); 1.6. Problem 3 Write a program that for a given natural number will compute the difference between that number and the following prime number. Example: For the number 573, the program should print 577 573 = 4 4 1.6. Problem 3

Solution p7_6_en.c int prime(int n) { for(i = 2; i * i <= n; ++i) { if(n % i == 0) { return 1; int first_larger_prime(int n) { ++n; while(!prime(n)) { ++n; return n; int n; scanf("%d", &n); int larger_prime = first_larger_prime(n); printf("%d - %d = %d\n", larger_prime, n, larger_prime - n); 1.7. Problem 4 Write a program that will print all pairs of primes up to 1000 that differentiate between themselves for 2. At the end print the count. 1.7. Problem 4 5

Solution p7_7_en.c int is_prime(int n) { if(n < 4) return 1; else { if(n % 2 == 0) else { for(i = 3; i * i <= n; i += 2) { if(n % i == 0) { return 1; int sum_digits(int n) { int sum = 0; while(n!= 0) { sum += n % 10; n /= 10; return sum; int i, count = 0; for(i = 2; i <= 9999; ++i) { if(is_prime(i) && is_prime(sum_digits(i))) { printf("%d\t", i); ++count; printf("\ntotal: %d\n", count); 1.8. Problem 5 Compute the sum: 1! + (1 + 2)! + (1 + 2 + 3)! + + (1 + 2 + + n)! Use function for computing the sum of the first k natural numbers Use a function for computing factorial of k 6 1.8. Problem 5

Solution p7_9_en.c int sum(int n) { int s = 0; for(i = 1; i <= n; ++i) { s += i; return s; int factorial(int n) { int result = 1; for(i = 1; i <= n; ++i) { result *= i; return result; int n; scanf("%d", &n); if(n > 0) { int result = 0; int s; for(i = 1; i < n; ++i) { s = sum(i); result += factorial(s); printf("%d! + ", s); s = sum(n); result += factorial(s); printf("%d! = %d\n", s, result); else { printf("invalid value\n"); 1.8. Problem 5 7

2. Source code of the examples and problems https://github.com/finki-mk/sp/ Source code ZIP 8 2. Source code of the examples and problems