Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Similar documents
AMCAT Automata Coding Sample Questions And Answers

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

COMP s1 Lecture 1

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

Control Structure: Loop

Functions. Arash Rafiey. September 26, 2017

3. Types of Algorithmic and Program Instructions

C: How to Program. Week /Mar/05

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

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

Chapter 2 - Introduction to C Programming

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

WARM UP LESSONS BARE BASICS

CS242 COMPUTER PROGRAMMING

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

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

DEPARTMENT OF MATHS, MJ COLLEGE

BLM2031 Structured Programming. Zeyneb KURT

Slides adopted from T. Ferguson Spring 2016

CSc Introduction to Computing

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Unit 7. Functions. Need of User Defined Functions

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

CS110: PROGRAMMING LANGUAGE I

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

n Group of statements that are executed repeatedly while some condition remains true

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

C introduction: part 1

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Preview from Notesale.co.uk Page 6 of 52

Lecture 3 Tao Wang 1

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Dept. of CSE, IIT KGP

Principles of Programming. Chapter 6: Arrays

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Flow Control. CSC215 Lecture

Data Types and Variables in C language

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

Computer Programming 3 th Week Variables, constant, and expressions

Decision Making -Branching. Class Incharge: S. Sasirekha

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Fall 2017 CISC124 9/16/2017

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

BSM540 Basics of C Language

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Computer Programming Lecture 12 Pointers

Basic Elements of C. Staff Incharge: S.Sasirekha

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

Lecture 6. Statements

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Programming with Java

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

C Programming Primer 8/31/15 1

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

CS16 Exam #1 7/17/ Minutes 100 Points total

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

The C language. Introductory course #1

User Defined Functions

Chapter 1 & 2 Introduction to C Language

Lecture 02 C FUNDAMENTALS

Subject: Fundamental of Computer Programming 2068

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

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

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

Introduction to Programming Using Java (98-388)

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Chapter 5: Control Structures

Arrays and Strings. Arash Rafiey. September 12, 2017

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Introduction to C An overview of the programming language C, syntax, data types and input/output

Technical Questions. Q 1) What are the key features in C programming language?

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Lecture 5. Daily Puzzle. Variables and Identifiers. Place 10 lumps of sugar in 3 cups so there would be an odd number of lumps in every cup.

The Hyderabad Public School, Begumpet, Hyderabad, A.P

COMP 208 Computers in Engineering

Getting started with C++ (Part 2)

CS Week 2. Jim Williams, PhD

Lab 2: Structured Program Development in C

CSE / ENGR 142 Programming I

COMPUTER APPLICATION

BRANCHING if-else statements

BASIC ELEMENTS OF A COMPUTER PROGRAM

Basic and Practice in Programming Lab7

An overview of Java, Data types and variables

Review for Test 1 (Chapter 1-5)

Structured Programming. Dr. Mohamed Khedr Lecture 9

Fundamentals of Programming CS-110. Lecture 2

BİL200 TUTORIAL-EXERCISES Objective:

Creating a C++ Program

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

This exam is to be taken by yourself with closed books, closed notes, no calculators.

C Programming

Transcription:

60-140-1 Lecture for Thursday, Sept. 18, 2014. *** Dear 60-140-1 class, I am posting this lecture I would have given tomorrow, Thursday, Sept. 18, 2014 so you can read and continue with learning the course material with minimal disruption even on this even of a morning half day WUFA strike. Please, read and continue to work on your assignment 1 using this method of problem solving although your assignment 1 requires you to provide only the algorithm and the C program with its compilation and execution. Assignment 1 is not asking you to provide all 6 problem solving steps. Dr. C.I. Ezeife. Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem. B. Starting Ch. 3 Objectives: Basic Data types and instructions for an algorithm and a C program. 1. What are variables, constants & data types? 2. What are algorithmic and program instructions? 3. What the syntax for Read (scanf) and Print (printf) and assignment instructions? Example: Solve the Example 2.1 on Slide 33 also given below. Example 2.1: Management wants to see the patterns in absenteeism across its two departments, dept1 and dept2 for one week. It is interested in knowing the total absenteeism in each department in the one week it collected data. You are required to identify the input and output data of this problem and attempt to define an algorithm and a program. Step 1: Requirements Analysis (R): This involves defining the problems clearly. With example 2.1, the problem definition is clear with the work week consisting of 5 days.

Step 2: Components Analysis (C): Define the problem components of input data set, output data set and their types with relationships. Input Data Set: absent_day1_dept1, absent_day1_dept2, absent_day2_dept1, absent_day2_dept2, absent_day3_dept1, absent_day3_dept2, absent_day4_dept1, absent_day4_dept2, absent_day5_dept1, (integer) Output Data Set: total_absent_dept1, total_absent_dept2; (integer) Relationships: total_absent_dept1 = absent_day1_dept1 + absent_day2_dept1 + absent_day3_dept1 + absent_day4_dept1 + absent_day5_dept1; total_absent_dept2 = absent_day1_dept2 + absent_day2_dept2 + absent_day3_dept2 + absent_day4_dept2 + Step 3: Breaking down into modules possibly (M): For example 2.1, we are not breaking down into modules yet. Step 4: Algorithm Design(A): Writing the algorithmic solution of the problem using all the data gathered in steps 1 to 3 above. The format of the algorithm to follow is given on slide 54 of lecture notes and page 72 of text. The algorithm of Example 2.1 is given below: MainModule Begin

/* Declare Input and Output variables */ Input data: absent_day1_dept1, absent_day1_dept2 absent_day2_dept1, absent_day2_dept2 absent_day3_dept1, absent_day3_dept2 absent_day4_dept1, absent_day4_dept2 absent_day5_dept1, (integers) absent_day5_dept2 /*Note that this is the list of input data as in step 2 */ */ Output data: total_absent_dept1, total_absent_dept2 (integers) /* Note that this is list of output data as in step 2 /* Next, define instructions by first reading */ /* data values into input variables. */ a)read (absent_day1_dept1, absent_day2_dept1, absent_day3_dept1, absent_day4_dept1, absent_day5_dept1); b) read (absent_day1_dept2, absent_day2_dept2, absent_day3_dept2, absent_day4_dept2, absent_day5_dept2); /* Now, use input data values to obtain values for */ /* output variables. */ c) total_absent_dept1= absent_day1_dept1+absent_day2_dept1+ absent_day3_dept1+absent_day4_dept1+absent_day5_dep t1; d) total_absent_dept2= absent_day1_dept2+absent_day2_dept2+

absent_day3_dept2+absent_day4_dept2+absent_day5_dep t2; /* Next, print the output data values for the user. */ e) Print (total_absent_dept1, total_absent_dept2); End Step 5: Coding(C): Translate the algorithm of step 4 above into a C program. Use the format of a C program given on slide 55 of course note and on page 73 of course book. The C program for Example 2.1 is given below: #include <stdio.h> int main (void) { /* Input and output data are declared first in a program*/ int int absent_day1_dept1, absent_day1_dept2, absent_day2_dept1, absent_day2_dept2, absent_day3_dept1, absent_day3_dept2, absent_day4_dept1, absent_day4_dept2, absent_day5_dept1, total_absent_dept1, total_absent_dept2; /*Now present the sequence of instructions*/ scanf ( %d %d %d %d %d, &absent_day1_dept1, &absent_day2_dept1, &absent_day3_dept1, &absent_day4_dept1, &absent_day5_dept1); scanf ( %d %d %d %d %d, &absent_day1_dept2, &absent_day2_dept2, &absent_day3_dept2, &absent_day4_dept2,

} &absent_day5_dept2); total_absent_dept1 = absent_day1_dept1 + absent_day2_dept1 + absent_day3_dept1 + absent_day4_dept1 + absent_day5_dept1; total_absent_dept2 = absent_day1_dept2 + absent_day2_dept2 + absent_day3_dept2 + absent_day4_dept2 + printf ( Number of people absent in dept1 is %d and Number return 0; of people absent in dept2 is %d\n, total_absent_dept1, total_absent_dept2); Step 6: Testing and Verification (T): In this step, you can test the correctness of your program by: (i) tracing the program, which means executing it by hand with given data to see what results the program produces the way the CPU does. Tracing helps to identify logical errors so you can correct them. (ii) Also, you can compile run the program with the CPU and so it can produce results. This method is good if there are no logical errors in the program or it may be producing incorrect results. Tracing the above program by executing the insructions after the variables are initialized will have the following outcomes: scanf ( %d %d %d %d %d, &absent_day1_dept1, &absent_day2_dept1, &absent_day3_dept1, &absent_day4_dept1, &absent_day5_dept1); After first scanf above, the values of the variables are as given below: absent_day1_dept1 = 2,

absent_day2_dept1= 0, absent_day3_dept1 = 3, absent_day4_dept1 = 1, absent_day5_dept1 = 4. scanf ( %d %d %d %d %d, &absent_day1_dept2, &absent_day2_dept2, &absent_day3_dept2, &absent_day4_dept2, &absent_day5_dept2); After second scanf above, the values of the variables are as given below: absent_day1_dept2 = 0, absent_day2_dept2= 0, absent_day3_dept2 = 1, absent_day4_dept2 = 1, absent_day5_dept2 = 2. The effect of the 2 next instructions are given below: &absent_day5_dept2); total_absent_dept1 = absent_day1_dept1 + absent_day2_dept1 + absent_day3_dept1 + absent_day4_dept1 + absent_day5_dept1; = 2 + 0 + 3 + 1 + 4 = 10; total_absent_dept2 = absent_day1_dept2 + absent_day2_dept2 + absent_day3_dept2 + absent_day4_dept2 + = 0 + 0 + 1 + 1 + 2 = 4;

The last instruction prints the total for the two departments which arfe 10 and 4 and are the desired results. printf ( Number of people absent in dept1 is %d and Number of people absent in dept2 is %d\n, total_absent_dept1, total_absent_dept2); END OF CHAPTER 2. (GOOD BYE TO CHAPTER 2). ** Ch. 3 Objectives: Basic Data types and instructions for an algorithm and a C program. 1.What are variables, constants & data types? 2.What are algorithmic and program instructions? 3.What the syntax for Read (scanf) and Print (printf) and assignment instructions? Variables, Constants and data types. Primitive (basic) data types for representing real life data we use in solving our problems are: 1. integer called int in programming language C. 2. real called float in C. 3. character called char in C 4. string seen as a string of characters in C and declared as char variable[number of specified characters]. 5. logical (or Boolean) declared in C as data of type into with 0 to represent FALSE and!0 (that is not zero) representing TRUE. Read course lecture slides 40 to 45 for format of declaring variables of type int, float

or char. General format for declaring a variable in C is: datatype variablenames separated by commas and ending with a semicolon. It is: datatype variablename; datatype variablename, variablename; Eg. int num1, num2; int sum; int num1, num2, num3; float newnum, newnum1, diff; A constant variable is one that cannot change its value during the one program it is declared in. It is used to protect input data (such as a conversion value, eg. pi = 3.141) we do not want to destroy. The format for declaring a constant input data is: const datatype variablename = value; Eg. const float pi = 3.141; Format of an algorithm is on slide 54. Format of a C program is on slide 55. Read slides 56 to 60 for the format for writing a scanf and a printf General format of a scanf is: scanf ( format specifiers, &variable1, &variable2,, &variable); Eg. scanf( %f %f, &newnum, &nenum1);

The format specifiers for the different primitive data types are: Data type format specifier int %d float %f char %c string %s logical %d You have enough with your lab exercise 1 this week to start working on your assignment 1 and see you in class next Tuesday.