Introduction To Computer Science PDF Fall 2024

Summary

This document is an introduction to computer science. It covers functions and programming concepts. The document is from Fall 2024 and seems to be from ETH Zurich.

Full Transcript

INTRODUCTION TO COMPUTER SCIENCE 252-0032, 252-0047, 252-0058 Document authors: Manuela Fischer and Felix Friedrich Department of Computer Science, ETH Zurich Fall 2024 1 Functions...

INTRODUCTION TO COMPUTER SCIENCE 252-0032, 252-0047, 252-0058 Document authors: Manuela Fischer and Felix Friedrich Department of Computer Science, ETH Zurich Fall 2024 1 Functions 86 Section 7 Functions In this section we introduce functions. Functions encapsulate functionality that is frequently used (e.g. computing powers) and make it easily accessible. Functions structure a program by partitioning into small sub-tasks, each of which is implemented as a function. Program- ming using functions is also known as procedural programming (procedure is an alternative word for function). The following example shows a program to compute the n-th power of a number. Example 7.1 (Computing Powers) double a; int n; std::cin >> a; // Eingabe a std::cin >> n; // Eingabe n double result = 1.0; if (n < 0) { // a^n = (1/a)^(-n) a = 1.0/a; n = -n; } for (int i = 0; i < n; ++i) result *= a; std::cout b // POST: returns true if [a1, b1],[a2, b2] overlap bool intervals_overlap(int a1, int b1, int a2, int b2){ return max(a1, b1) >= min(a2, b2) && min(a1, b1) b // POST: returns true if [a1, b1],[a2, b2] overlap bool intervals_overlap(int a1, int b1, int a2, int b2){ return std::max(a1, b1) >= std::min(a2, b2) && std::min(a1, b1) b // POST: returns true if [a1, b1],[a2, b2] overlap bool intervals_overlap(int a1, int b1, int a2, int b2){ return std::max(a1, b1) >= std::min(a2, b2) && std::min(a1, b1) x1 >> y1 >> w1 >> h1; int x2, y2, w2, h2; std::cin >> x2 >> y2 >> w2 >> h2; bool clash = rectangles_overlap(x1,y1,w1,h1,x2,y2,w2,h2); if (clash) std::cout

Use Quizgecko on...
Browser
Browser