Lab 2: Structured Program Development in C

Similar documents
CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

C: How to Program. Week /Mar/05

Structured Program Development

Structured Program Development in C

Chapter 3 Structured Program Development

Chapter 2 - Introduction to C Programming

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Lab 1: Introduction to C Programming. (Creating a program using the Microsoft developer Studio, Compiling and Linking)

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

Control Statements: Part 1

Add Subtract Multiply Divide

Loops / Repetition Statements

1 EEE1008 C Programming

Chapter 3 Structured Program Development in C Part II

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 1 & 2 Introduction to C Language

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Fundamentals of Programming Session 14

Special Section: Building Your Own Compiler

LAB 5: REPETITION STRUCTURE(LOOP)

A Look Back at Arithmetic Operators: the Increment and Decrement

3 The L oop Control Structure

Fundamentals of Programming Session 7

Structured Programming. Dr. Mohamed Khedr Lecture 9

Loops / Repetition Statements

Introduction to C Final Review Chapters 1-6 & 13

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

Introduction to Java Applications

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

CS 101, Spring 2016 March 22nd Exam 2

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Questions Bank. 14) State any four advantages of using flow-chart

Dept. of CSE, IIT KGP

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

Control Structure: Loop

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

CpSc 1111 Lab 4 Formatting and Flow Control

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

In this chapter you ll learn:

Introduction to C Programming

Fundamentals of Programming. Lecture 6: Structured Development (part one)

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Lab ACN : C++ Programming Exercises

Decision Making -Branching. Class Incharge: S. Sasirekha

LAB 5: REPETITION STRUCTURE(LOOP)

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Lecture 10. Daily Puzzle

Unit 3. Operators. School of Science and Technology INTRODUCTION

Computer System and programming in C

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 2, Part III Arithmetic Operators and Decision Making

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

2. Numbers In, Numbers Out

CpSc 1111 Lab 5 Formatting and Flow Control

Individual research task. You should all have completed the research task set last week. Please make sure you hand it in today.

CHAPTER 2 PROBLEM SOLVING TECHNIQUES. Mr Mohd Hatta Bin Hj Mohamed Ali Computer Programming BFC2042

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Programming for Engineers Introduction to C

Control Statements. Musa M. Ameen Computer Engineering Dept.

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

Chapter 4 C Program Control

Flow Chart. The diagrammatic representation shows a solution to a given problem.

To become familiar with array manipulation, searching, and sorting.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Course Outline. Introduction to java

Chapter 5: Control Structures

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

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

Intro to Computational Programming in C Engineering For Kids!

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

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

DECISION CONTROL AND LOOPING STATEMENTS

Math 202 Test Problem Solving, Sets, and Whole Numbers 19 September, 2008

Module 4: Decision-making and forming loops

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 4: Basic C Operators

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

2. Numbers In, Numbers Out

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

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

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

Chapter 4 C Program Control

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

Fundamentals of Programming Session 4

C Functions. 5.2 Program Modules in C

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Programming for Engineers Iteration

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Transcription:

Lab 2: Structured Program Development in C (Part A: Your first C programs - integers, arithmetic, decision making, Part B: basic problem-solving techniques, formulating algorithms) Learning Objectives 1. To be able to use arithmetic operators. 2. To understand the precedence of arithmetic operators. 3. To be able to write simple decision-making statements. 4. To understand basic problem-solving techniques. 5. To be able to develop algorithms through the process of top-down, stepwise refinement. 6. To be able to use the if selection statement and if else selection statement to select actions. 7. To be able to use the while repetition statement to execute statements in a program repeatedly. 8. To understand structured programming. 9. To be able to use the increment, decrement and assignment operators. Lab Work General Note: Study every exercise and for each of them draw a structure diagram. Type in all these programs and run them inside your C development environment. In your log-book take note of all the results from the execution of these programs and write down any problems that you had to solve. For each program of the exercises, explain clearly what each section of the program is doing. You may need to perform a walk-through, line by line, where this is appropriate. Record all your explanations in the log-book. Remember to put a date in your log-book and also to offer some discussions and a brief conclusion after every week s work. Finally, complete the programming assignments at the end of the exercises, then enter comments in your logbook as you are doing this and save to your floppy disk for submission with your logbook. Part A: Your first C programs - integers, arithmetic, decision making The next C program uses the standard library function scanf to obtain two integers typed by a user at the keyboard, computes the sum of these values and prints the result using printf. Exercise 1 /* Fig. 2.5: fig02_05.c Addition program */ Stavros Dimitriou London South Bank University 1

int int integer1; /* first number to be input by user */ int integer2; /* second number to be input by user */ int sum; /* variable in which sum will be stored */ printf( "Enter first integer\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ printf( "Enter second integer\n" ); /* prompt */ scanf( "%d", &integer2 ); /* read an integer */ sum = integer1 + integer2; /* assign total to sum */ printf( "Sum is %d\n", sum ); /* print sum */ return 0; /* indicate that program ended successfully */ /* end function main */ 3. Create, compile and Run the program. Explain and analyze it in your logbook. Exercise 2 1. Analyze the structure of the following four C programs. 2. Understand each program and predict the outcome in your logbook. (i) The WHILE Loop example of a "while" loop is given below. int count; count = 0; while (count < 6) printf("the value of count is %d\n",count); count = count + 1; (ii) THE DO-WHILE LOOP The following program shows an example of a do-while loop. int i; i = 0; do printf("the value of i is now %d\n",i); i = i + 1; while (i < 5); (iii) THE FOR LOOP The for loop is an alternative way to describe the while loop. The format is slightly different in that the condition that determines whether the loop is performed is more complicated. Here is an example. Stavros Dimitriou London South Bank University 2

int index; for(index = 0;index < 6;index = index + 1) printf("the value of the index is %d\n",index); (iv) THE IF STATEMENT If statements are used to determine test conditions, in a manner quite similar to that discussed earlier for loops. An example of a program using the conditional If statement is shown below. int data; for(data = 0;data < 10;data = data + 1) if (data == 2) // if statement 1 printf("data is now equal to %d\n",data); if (data < 5) // if statement 2 printf("data is now %d, which is less than 5\n",data); else // else if statement2 in not is satisfied printf("data is now %d, which is greater than 4\n",data); /* end of for loop */ printf("data is now equal to %d\n",data); 3. Create, compile and Run the program. Explain and analyze it in your logbook. The next program uses six if statements to compare two numbers input by the user. If the condition in any of these if statements is satisfied, the printf statement associated with that if is executed. Exercise 3 /* Fig. 2.13: fig02_13.c Using if statements, relational operators, and equality operators */ int num1; /* first number to be read from user */ int num2; /* second number to be read from user */ printf( "Enter two integers, and I will tell you\n" ); printf( "the relationships they satisfy: " ); scanf( "%d%d", &num1, &num2 ); /* read two integers */ if ( num1 == num2 ) printf( "%d is equal to %d\n", num1, num2 ); if ( num1!= num2 ) printf( "%d is not equal to %d\n", num1, num2 ); Stavros Dimitriou London South Bank University 3

if ( num1 < num2 ) printf( "%d is less than %d\n", num1, num2 ); if ( num1 > num2 ) printf( "%d is greater than %d\n", num1, num2 ); if ( num1 <= num2 ) printf( "%d is less than or equal to %d\n", num1, num2 ); if ( num1 >= num2 ) printf( "%d is greater than or equal to %d\n", num1, num2 ); return 0; /* indicate that program ended successfully */ /* end function main */ 3. Create, compile and Run the program. Explain and analyse it in your logbook. The next program uses six if statements to compare two numbers input by the user. If the condition in any of these if statements is satisfied, the printf statement associated with that if is executed. Part B: basic problem-solving techniques, formulating algorithms Problem: Determine the class average of a class of ten students with grade integers in the rage of 0 to 100. Your algorithm makes use of counter-controlled repetition. Exercise 1 3. Provide a pseudocode algorithm to solve the class average problem. /* Fig. 3.6: fig03_06.c Class average program with counter-controlled repetition */ int int counter; /* number of grade to be entered next */ int grade; /* grade value */ int total; /* sum of grades input by user */ int average; /* average of grades */ /* initialization phase */ total = 0; /* initialize total */ counter = 1; /* initialize loop counter */ /* processing phase */ while ( counter <= 10 ) /* loop 10 times */ printf( "Enter grade: " ); /* prompt for input */ scanf( "%d", &grade ); /* read grade from user */ total = total + grade; /* add grade to total */ counter = counter + 1; /* increment counter */ Stavros Dimitriou London South Bank University 4

/* end while */ /* termination phase */ average = total / 10; /* integer division */ printf( "Class average is %d\n", average ); /* display result */ return 0; /* indicate program ended successfully */ /* end function main */ 4. Create, compile and Run the program. Explain and analyze it in your logbook. Programming assignments 1. Write a program that writes your name on the monitor ten times. Write this program three times, once with each looping method as you learned in Exercise 2. 2. Write a program that reads in five integers and then determines and prints the largest and the smallest integers in the group. 3. Write a program that reads an integer and determines and prints whether it is odd or even. [Hint: Use the remainder operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2]. Stavros Dimitriou London South Bank University 5