Structured programming

Similar documents
Structured programming. Exercises 3

Fundamental of Programming (C)

Fundamentals of Programming

Programming and Data Structures

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

CS102: Variables and Expressions

Chapter 2. Introduction to C language. Together Towards A Green Environment

Data types, variables, constants

2. Numbers In, Numbers Out

Arithmetic Expressions in C

Lecture 5: C programming

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Basic Assignment and Arithmetic Operators

Structured programming

C: How to Program. Week /Mar/05

1. The keyword main in C language is used for

2. Numbers In, Numbers Out

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Lecture 3. More About C

Learning to Program with Haiku

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

Chapter 11 Introduction to Programming in C

BASIC ELEMENTS OF A COMPUTER PROGRAM

ET156 Introduction to C Programming

Chapter 11 Introduction to Programming in C

Datatypes, Variables, and Operations

2 nd Week Lecture Notes

Computer System and programming in C

BSM540 Basics of C Language

Programming for Engineers Introduction to C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Lesson 3 Introduction to Programming in C

Computer Programming CS F111

Informatica e Sistemi in Tempo Reale

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Fundamentals of Programming

Chapter 11 Introduction to Programming in C

Basics of Java Programming

UIC. C Programming Primer. Bharathidasan University

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Work relative to other classes

Chapter 2 - Introduction to C Programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

NWEN 241 Systems Programming Exercises (Set 1)

DEPARTMENT OF MATHS, MJ COLLEGE

Computer Programming : C++

ANSI C Programming Simple Programs

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Full file at C How to Program, 6/e Multiple Choice Test Bank

Chapter 11 Introduction to Programming in C

Inductive Data Types

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

C Programming

Chapter 11 Introduction to Programming in C

CSCI 2132 Software Development. Lecture 8: Introduction to C

Chapter 11 Introduction to Programming in C

Arithmetic Expressions Lesson #1 Outline

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

A First Program - Greeting.cpp

printf( Please enter another number: ); scanf( %d, &num2);

The C language. Introductory course #1

Procedures, Parameters, Values and Variables. Steven R. Bagley

ET156 Introduction to C Programming

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Computers Programming Course 5. Iulian Năstac

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

COSC121: Computer Systems: Runtime Stack

Computer Programming: Skills & Concepts (CP) Variables and ints

Structured programming

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Basic Types and Formatted I/O

Chapter 11 Introduction to Programming in C

Lecture 3 Tao Wang 1

CC112 Structured Programming

3. Types of Algorithmic and Program Instructions

Introduction to the C Programming Language

The C++ Language. Arizona State University 1

Basics of Programming

Number Systems, Scalar Types, and Input and Output

Computers and Programming Section 450. Lab #1 C# Basic. Student ID Name Signature

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Computer Skills (2) for Science and Engineering Students

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Introduction to C. Systems Programming Concepts

LAB 6: While and do-while Loops

Essar Placement Paper

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Course Outline Introduction to C-Programming

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Transcription:

Exercises 2 Version 1.0, 22 September, 2016

Table of Contents 1. Simple C program structure................................................... 1 2. C Functions.................................................................. 1 3. Comments usage............................................................. 1 3.1. Example (Hello world)................................................... 1 3.2. Program for summing two integers....................................... 2 4. Variables.................................................................... 2 4.1. Declaring variables...................................................... 2 4.2. Data types in C.......................................................... 3 4.3. Defining variable names................................................. 3 5. Named constants............................................................ 3 6. Printing on the standard output............................................... 4 6.1. Most used format placeholders........................................... 4 6.2. Usage of function printf................................................ 4 7. Operators................................................................... 5 7.1. Arithmetic operators..................................................... 5 8. Problems.................................................................... 5 8.1. Problem 1............................................................... 5 8.2. Problem 2............................................................... 6 8.3. Problem 3............................................................... 6 8.4. Problem 4............................................................... 6 8.5. Problem 5............................................................... 7 8.6. Problem 6............................................................... 7 8.7. Problem 7............................................................... 8 8.8. Problem 8............................................................... 8 9. Source code of the examples and problems..................................... 9

1. Simple C program structure All the source code written in C is organized in functions variable_declarations; expressions; 2. C Functions main - C main function In parentheses () we put the input arguments The return type of the functions is before the name (int the function returns integer) The function body starts with {, and ends with All declarations and expressions are separated with ; 3. Comments usage Comments are used for extra explanation or documenting the source code. C supports two types of comments: one line comments that start with double slash // // comment in one line multiple line comments that can span across multiple lines and start with /* and end with */ /* Longer comment in multiple lines */ 3.1. Example (Hello world) 1. Simple C program structure 1

Example 1 // main function /* Printing a message on the standard output (the screen) */ printf("welcome to FINKI!\n"); #include - directive for including external libraries stdio.h - library for accessing standard input/output streams (keyboard/screen) printf - function for printing on the standard output (screen) 3.2. Program for summing two integers Example 2 int a = 5; int b = 10; int c = a + b; 4. Variables Variables are symbolic names for places in memory where are stored some values. Before using it, each variable must be declared. With each assignment of new value, the old value in the variable is lost. 4.1. Declaring variables data_type variable_name = initial_value; Example int a = 5; float x = 2.3; char c = 'a'; 2 3.2. Program for summing two integers

4.2. Data types in C Integers Characters Real numbers int char float short double long 4.3. Defining variable names In naming variables you can use: lowercase letters from a to z; uppercase letters from A to Z; digits from 0 to 9 (the name can not start with digit); When choosing a variable name, pick ones that clearly describe the value they store. С is case sensitive 5. Named constants Names constants are created using the keyword const Example 3 const long double PI = 3.141592653590L; const int DAYS_IN_WEEK = 7; const SUNDAY = 0; // by default int DAYS_IN_WEEK = 7; // error Named constants can be created also by using the preprocessor and with all uppercase letters by convention. Using #define #define TEXT_TO_SEARCH_FOR REPLACEMENT_TEXT 4.2. Data types in C 3

Example 4 #define PI 3.141592653590L #define DAYS_IN_WEEKS 7 #define SUNDAY 0 long number = PI; int day = SUNDAY; 6. Printing on the standard output For printing on the standard output (screen) in C we use the function printf from the library stdio.h (Standard Input Otput) The signature of the function is: int printf(control_array, list_of_arguments) The control array contains text of any kind, and format placeholders with leading % or special characters with leading \. The format placeholders are determined from the variable type we want to print. 6.1. Most used format placeholders Format Usage %d integers (int) %i integers (int) %f real numbers (float, double) %c characters (char) %s characters (стринг, char[], char*) %% character % 6.2. Usage of function printf Print on the standard output the following sentences: 4 6. Printing on the standard output

First sentence. Second sentence. Third sentence. Example 5 printf("first sentence.\n"); printf("second sentence.\nthird sentence.\n"); Example 6 printf(" is a word long %d letters.\n", printf("macedonia")); 7. Operators 7.1. Arithmetic operators Are used on numbers (integers or real) Operator Operation + Addition - Subtraction * Multiplication / Division % Modulo (residue after division) 8. Problems 8.1. Problem 1 Write a program that will compute the value of the mathematical expression: x = 3/2 + (5 46*5/12) 7. Operators 5

Solution 1 float x = 3.0 / 2 + (5-46 * 5.0 / 12); printf("x = %.2f\n", x); 8.2. Problem 2 Write a program that for given value of x (during the declaration) will compute and print the value of x 2. Solution 2 int x = 7; printf("number %d squared is %d\n", x, x * x); 8.3. Problem 3 Write a program that for a given sides of one triangle, it will print the perimeter and area squared (values are a = 5, b = 7.5, c = 10.2). Solution 3 float a = 5.0; float b = 7.5; float c = 10.2; float L = a + b + c; float s = L / 2; float P = s * (s - a) * (s - b) * (s - c); printf("perimeter is: %.2f\n", L); printf("area is: %.2f\n", P); 8.4. Problem 4 Write a program for computing the arithmetic mean of the numbers 3, 5 and 12. 6 8.2. Problem 2

Solution 4 int a = 3; int b = 5; int c = 12; float as = (a + b + c) / 3.0; printf("the arithmetic mean is %2.f\n", as); 8.5. Problem 5 Write a program that will print the remainder from the division of number 19 with 2, 3, 5 and 8. Solution 5 int a = 19; printf("the residue of division with 2 is: %d\n", a % 2); printf("the residue of division with 3 is: %d\n", a % 3); printf("the residue of division with 5 is: %d\n", a % 5); printf("the residue of division with 8 is: %d\n", a % 8); 8.6. Problem 6 Write a program for computing and printing the circle area and perimeter. The circle radius is read as decimal number. Solution 6 #define PI 3.14 float radius; scanf("%f", &radius); float perimeter = 2 * radius * PI ; float area = radius * radius * PI; printf("l = %f\n", perimeter); printf("p = %f\n", area); 8.5. Problem 5 7

8.7. Problem 7 Write a program that reads from standard input two integers and prints their sum, difference, product and division remainder. Solution 7 int x, y; scanf("%d %d", &x, &y); printf("%d + %d = %d\n", x, y, x + y); printf("%d - %d = %d\n", x, y, x - y); printf("%d * %d = %d\n", x, y, x * y); printf("%d %% %d = %d\n", x, y, x % y); 8.8. Problem 8 Write a program that reads uppercase letter from standard input and prints out in lowercase. Each character is represented with its ASCII code. example A = 65, a = 97 Solution 8 char c; printf("enter an uppercase letter: "); scanf("%c", &c); printf("%c lowercase is: '%c'\n", c, c + ('a' - 'A')); 8 8.7. Problem 7

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