Programming Language A
|
|
- George Reynolds
- 5 years ago
- Views:
Transcription
1 Programming Language A Takako Nemoto (JAIST) 30 October Takako Nemoto (JAIST) 30 October 1 / 29
2 From Homework 3 Homework 3 1 Write a program to convert the input Celsius degree temperature into Fahrenheight with 1 floating digit. 2 Write a such program s.t. it asks the elevator A s location (floor); the elevator B s location (floor); the elevator C s location (floor) and your current location (floor), and prints the closest elevator. Hint: Function abs. You should include stdlib.h. 3 See the next page. 4 Read ch.2-4 (pp ) of the textbook. Takako Nemoto (JAIST) 30 October 2 / 29
3 Homework 3 Rewrite the following using switch instead of if: int no; printf("input an integer: "); if(no % 2) printf("it is odd."); else printf("it is odd."); Takako Nemoto (JAIST) 30 October 3 / 29
4 Elevator problem Basically, there are two variations: If the closest elevator is not unique, answer ALL closest ones; or answer only one of them with some priority. Since I did not specified, both are fine. Almost all programs works well, only if we input POSITIVE floor number. How to modify for buildings with basements? Japanese floor counting system (...B2, B1, 1, 2, 3...) or European counting system (... -2, -1, 0, 1, 2,...)? Takako Nemoto (JAIST) 30 October 4 / 29
5 Some examples & re-submission for Homework 2, 3 See additional material. Re-submission of Homework 2 and 3 will be accepted by 5 November. Takako Nemoto (JAIST) 30 October 5 / 29
6 if and switch if is suitable for a simple case distinction. For the distinctions of many cases, if needs nests and so switch is simpler. #include <stdlib.h> #include <stdlib.h> int x = rand(); if(x % 4 == 0 x % 4 == 1) if(x % 4 == 0) printf("case 0"); else printf("case 1"); else if(x % 4 == 2) printf("case 2"); else printf("case 3"); int x = rand(); switch(x % 4){ case 0 : printf("case 0"); break; case 1 : printf("case 1"); break; case 2 : printf("case 2"); break; case 3 : printf("case 3"); break; Takako Nemoto (JAIST) 30 October 6 / 29
7 Loop: 2 ways of repeat a process do loop while loop and for loop Start Start Process 1 Process 1 Process 2 Condition Yes Process 2 Yes Condition No Process 3 Process 3 End No End Observation In do loop, Process 2 is done at least once. In while loop Process 2 might not be done. Takako Nemoto (JAIST) 30 October 7 / 29
8 do statement int no; do{ printf("%d ", no); no = no - 1; while (no >= 0); printf("\n"); Input a positive integer: The... part is loop body. It is repeated as long as the condition inside while (...) holds. Inside while is called control sequence. First, the executed once. part is Note that after once execution of..., the value of no is decreased by 1. If the new value of no satisfies no >= 0, then the... part is repeated once more. Takako Nemoto (JAIST) 30 October 8 / 29
9 while statement int no; while (no >= 0){ printf("%d ", no); no = no - 1; printf("\n"); Input a positive integer: If the condition inside while(...) holds, the part is executed. Note that the decision is made before executing loop body... is executed. Depending on the value of no, the part... is not executed at all. Therefore, you have to assign appropriate value to variables inside while(...). Otherwise, undefined value cause unexpected behavior. Takako Nemoto (JAIST) 30 October 9 / 29
10 do and while int no; do{ printf("%d ", no); no = no - 1; while (no >= 0); printf("\n"); int no; while (no >= 0){ printf("%d ", no); no = no - 1; printf("\n"); Input a positive integer: Input a positive integer: Takako Nemoto (JAIST) 30 October 10 / 29
11 do and while int no; do{ printf("%d ", no); no = no - 1; while (no >= 0); printf("\n"); int no; while (no >= 0){ printf("%d ", no); no = no - 1; printf("\n"); Input a positive integer: 0 0 Input a positive integer: 0 0 Takako Nemoto (JAIST) 30 October 11 / 29
12 do and while int no; do{ printf("%d ", no); no = no - 1; while (no >= 0); printf("\n"); int no; while (no >= 0){ printf("%d ", no); no = no - 1; printf("\n"); Input a positive integer: -1-1 Input a positive integer: -1 Takako Nemoto (JAIST) 30 October 12 / 29
13 while and for The following two programs behaves same as the previous one. int no, i; i = no; while (i >= 0){ printf("%d ", i); i = i -1; printf("\n"); Input a positive integer: int no, i; for (i = no; i >= 0; i = i -1){ printf("%d ", i); printf("\n"); Input a positive integer: Takako Nemoto (JAIST) 30 October 13 / 29
14 for statement int no, i; for (i = no; i >= 0; i = i -1){ printf("%d ", i); printf("\n"); for(a; B; C)... A is preprocessing. B is control sequence. C is post-processing. It can be written using while A while(b){... C Conversely, every while statement can be expressed by while. If no preprocessing is needed, it can be omitted. Takako Nemoto (JAIST) 30 October 14 / 29
15 Useful operators: Compound assignment operators By the way, sum = sum + t; means almost the same as sum += t; Such a operator is called a compound assignment operator. Compound assignment operators b has almost same meaning as a = b, is one of them: * / % + - << >> & ˆ Takako Nemoto (JAIST) 30 October 15 / 29
16 Useful operators: Postfixed increment & decrement Similarly cnt = cnt + 1; and cnt = cnt - 1; mean almost the same as, respectively, cnt++; and cnt--;. Exercise Verify that sum = sum + t and cnt = cnt +1 in the previous program can be replaced by sum += t and cnt++, respectively. Takako Nemoto (JAIST) 30 October 16 / 29
17 Useful operators: Postfixed increment & decrement Similarly cnt = cnt + 1; and cnt = cnt - 1; mean almost the same as, respectively, Warning! cnt++; and cnt--;. For a variable var, var++ alone acts the same as var = var + 1. But if it appears in some other statement like A(var);, then it acts as A(var); var = var + 1;. For example, printf("%d", var++); acts as Same as var--. printf("%d", var); var = var + 1; Takako Nemoto (JAIST) 30 October 16 / 29
18 Use of postfixed decrement operator The following program behave in the same way. int no; while (no >= 0){ printf("%d ", no--); printf("\n"); int no; while (no >= 0){ printf("%d ", no--); printf("\n"); Takako Nemoto (JAIST) 30 October 17 / 29
19 Example int retry; do { int no; printf("input an integer: "); if(no % 2) printf("it is odd.\n"); else printf("it is even.\n"); printf("continue? (yes = 0/no = 9): "); scanf("%d", &retry); while (retry == 0); Inside of do{... is repeated while the condition inside while(...) is satisfied. The variable no is used only in the do block, and so declared inside it. Takako Nemoto (JAIST) 30 October 18 / 29
20 Choose a valid number! int main(void) { int x = 0; do { printf("choose a nonnegative integer x < 3: "); scanf("%d", &x); while(x < 0 3 <= x); printf("you chose %d.", x); Choose a nonnegative integer x < 3: 4 Choose a nonnegative integer x < 3: 3 Choose a nonnegative integer x < 3: 2 You chose 2. Takako Nemoto (JAIST) 30 October 19 / 29
21 Example: Start hand, hand0, hand1 rand() generates a random integer. Using it, you can create a simple program for. rand() hand1 hand hand0 Yes No End Takako Nemoto (JAIST) 30 October 20 / 29
22 Multiple addition and the average int main(void){ int sum = 0; int cnt = 0; int retry; do{ int t; printf("input an integer: "); scanf("%d", &t); sum += t; cnt++; printf("continue? <yes = 0 / no = 9>: "); scanf("%d", &retry); while (retry == 0); printf("the sum of %d inputs ", cnt); printf("is %d and ", sum); printf("the average is %d.", (double)sum / cnt); While the user choose yes, the process is repeated, which allows unfixed number of inputs. cnt acts as a counter for the number of inputs. Recall that sum += t; cnt++ acts same as sum = sum + t; cnt = cnt + 1 Takako Nemoto (JAIST) 30 October 21 / 29
23 Another version of average int i = 0; int sum = 0; int num, temp; printf("number of items: "); scanf("%d", &num); while(i < num){ i = i + 1; printf("no.%d: ", i); scanf("%d", &temp); sum += temp; printf("total: %d\n", sum); printf("average: %.2f\n", (double)sum / num); If the number of items is 0, no loop process is executed. If you know the number of items in advance, this is easier to use than the previous one. (No need to choose yes.) Takako Nemoto (JAIST) 30 October 22 / 29
24 Useful operators: Prefixed increment & decrement i = i + 1; and i = i - 1; mean almost the same as, respectively, Warning! ++i; and ++i;. For a variable var, ++var alone acts the same as var = var + 1. But if it appears in some other statement like A(var);, then it acts as var = var + 1;. A(var); For example, printf("%d", ++var); acts as var = var + 1; printf("%d", var); Same as --var. Takako Nemoto (JAIST) 30 October 23 / 29
25 Useful operators: Prefixed increment & decrement i = i + 1; and i = i - 1; mean almost the same as, respectively, ++i; and ++i;. Exercise Run the following program and verify the behaviors of each operators #include<stdio.h> int i = 0; int j = 0; int k = 0; int l = 0; printf("i=%d ", ++i); printf("j=%d\n", j++); printf("i=%d ", i); printf("j=%d\n", j); printf("k=%d ", ++k); printf("l=%d\n", l++); printf("k=%d ", k); printf("l=%d\n", l); Takako Nemoto (JAIST) 30 October 24 / 29
26 Use of prefixed increment operator int i = 0; int sum = 0; int num, temp; printf("number of items: "); scanf("%d", &num); while(i < num){ printf("no.%d: ", ++i); scanf("%d", &temp); sum += temp; printf("total: %d\n", sum); printf("average: %.2f\n", (double)sum / num); Takako Nemoto (JAIST) 30 October 25 / 29
27 Example: Print the input number of * Note that int no; while (no-- > 0) putchar( * ); putchar( \n ); while (no-- > 0) putchar( * ); is same as while (no > 0) no = no - 1; putchar( * ); The function putchar is to print a single character *. Use single quotation. Takako Nemoto (JAIST) 30 October 26 / 29
28 Example: Prints all power of 2 below the input (Ex. 4-7) int no, i; for(i = 1; i < no; i *= 2){ printf("%d ", i); Set the value i as 0 before starting the loop. If i < no hold, then printf("%d ", i) and i *= 2 are executed. Takako Nemoto (JAIST) 30 October 27 / 29
29 /* Decide prime or not*/ int no, i; do{ if (no <= 0) printf("do not input any non-negative integer!"); while (no <= 0); for(i = 2;!(no % i == 0); i++); /*empty statement*/ if (!(i == no)){ puts("it is not prime."); printf("the least nontrivial factor is %d.", i); else puts("it is prime."); Takako Nemoto (JAIST) 30 October 28 / 29
30 Today s homework 1 Write a program which answers, for the input of the year and month, it answers how many days it has. For example, February 2018 has 28 days. Do not forget bissextile years having 29 February, which are years s.t. divisible by 4 but not by 100 or; divisible by Rewrite the program at the right of the page of do and while (p.11) so that it does not output \n for any negative input. Simplify it with the postfixed decrement operator. 3 Write a program s.t., for a positive integer-valued input, it prints all positive factors of it in ascending order. Use for. 4 (optional) Do any exercise you like, exept for the one explained in the lecture. 5 (optional) 3 Send the programs as attached files to me via , with the title Homework Lecture4 by the next lecture. Please make sure to include your name and student ID number. Takako Nemoto (JAIST) 30 October 29 / 29
Programming Language A
Programming Language A Takako Nemoto (JAIST) 22 October Takako Nemoto (JAIST) 22 October 1 / 28 From Homework 2 Homework 2 1 Write a program calculate something with at least two integer-valued inputs,
More informationProgramming Language B
Programming Language B Takako Nemoto (JAIST) 7 January Takako Nemoto (JAIST) 7 January 1 / 13 Usage of pointers #include int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko;
More informationProgramming Language B
Programming Language B Takako Nemoto (JAIST) 3 December Takako Nemoto (JAIST) 3 December 1 / 18 Today s topics 1 Function-like macro 2 Sorting 3 Enumeration 4 Recursive definition of functions 5 Input/output
More informationProgramming Language A
Programming Language A Takako Nemoto (JAIST) 12 November Takako Nemoto (JAIST) 12 November 1 / 25 From Quiz 5 // A program to print the double of the input. int no printf("input an integer: "); scanf("%d",
More informationProgramming Language B
Programming Language B Takako Nemoto (JAIST) 17 December Takako Nemoto (JAIST) 17 December 1 / 17 A tip for the last homework 1 Do Exercise 9-5 ( 9-5) in p.249 (in the latest edtion). The type of the second
More informationProgramming Language B
Programming Language B Takako Nemoto (JAIST) 10 December Takako Nemoto (JAIST) 10 December 1 / 15 Strings A string literal is a strings of characters included by double quotations. Examples String literals
More informationStructured programming. Exercises 3
Exercises 3 Table of Contents 1. Reminder from lectures...................................................... 1 1.1. Relational operators..................................................... 1 1.2. Logical
More informationC/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)
C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 By the end of this lecture, you will be able to identify the
More informationC/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for
C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 1 By the end of this lecture, you will be able to identify
More informationChapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.
Chapter 4: Expressions Chapter 4 Expressions 1 Chapter 4: Expressions Operators Expressions are built from variables, constants, and operators. C has a rich collection of operators, including arithmetic
More informationLecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions
Lecture 02 Summary C/Java Syntax Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 1 2 By the end of this lecture, you will be able to identify the
More informationProgramming Language A
Programming Language A Takako Nemoto (JAIST) 26 November Takako Nemoto (JAIST) 26 November 1 / 12 Type char char is a datatype for characters. char express integers in certain region. For each ASCII character,
More informationOperators And Expressions
Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication
More informationProgramming Language B
Programming Language B Takako Nemoto (JAIST) 16 January Takako Nemoto (JAIST) 16 January 1 / 15 Strings and pointers #include //11-1.c char str[] = "ABC"; char *ptr = "123"; printf("str = \"%s\"\n",
More informationECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control
ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 4: Flow of Control Outline of this Lecture Examples of Statements in C Conditional Statements The if-else Conditional Statement
More informationComputer Programming. Decision Making (2) Loops
Computer Programming Decision Making (2) Loops Topics The Conditional Execution of C Statements (review) Making a Decision (review) If Statement (review) Switch-case Repeating Statements while loop Examples
More informationWeek 3 More Formatted Input/Output; Arithmetic and Assignment Operators
Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators Formatted Input and Output The printf function The scanf function Arithmetic and Assignment Operators Simple Assignment Side Effect
More informationOperators and expressions. (precedence and associability of operators, type conversions).
Programming I Laboratory - lesson 0 Operators and expressions (precedence and associability of operators, type conversions). An expression is any computation which yields a value. When discussing expressions,
More informationA. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS
Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization
More informationStructured programming
Exercises 6 Version 1.0, 25 October, 2016 Table of Contents 1. Arrays []................................................................... 1 1.1. Declaring arrays.........................................................
More informationLecture 6. Statements
Lecture 6 Statements 1 Statements This chapter introduces the various forms of C++ statements for composing programs You will learn about Expressions Composed instructions Decision instructions Loop instructions
More informationProgramming for Electrical and Computer Engineers. Loops
Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly
More informationUniversity of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key
University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key Last Name (PRINT): First Name (PRINT): University Directory ID (e.g., umcpturtle) I pledge on my honor that
More informationCOP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
COP 3275: Chapter 04 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA Operators C emphasizes expressions rather than statements. Expressions are built from variables, constants, and
More informationCOL 100 Introduction to Programming- MINOR 1 IIT Jammu
COL 100 Introduction to Programming- MINOR 1 IIT Jammu Time 1 Hr Max Marks 40 03.09.2016 NOTE: THERE 4 QUESTIONS IN ALL NOTE: 1. Do all the questions. 2. Write your name, entry number and group in all
More informationProgramming for Engineers Iteration
Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers
More informationProgramming Language B
Programming Language B Takako Nemoto (JAIST) 21 January Takako Nemoto (JAIST) 21 January 1 / 18 Today s quiz The following is a which returns 0 if s1 and s2 are same, a negative number if s1 is prior to
More informationChapter 13 Control Structures
Chapter 13 Control Structures Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iteration executing code multiple times, ending
More informationITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University
ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 08: Control Statements Readings: Chapter 6 Control Statements and Their Types A control
More informationProgramming Language B
Programming Language B Takako Nemoto (JAIST) 28 January Takako Nemoto (JAIST) 28 January 1 / 20 Today s quiz The following are program to print each member of the struct Student type object abe. Fix the
More informationIntroduction to Computing Lecture 07: Repetition and Loop Statements (Part II)
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics
More informationBranching is deciding what actions to take and Looping is deciding how many times to take a certain action.
3.0 Control Statements in C Statements The statements of a C program control the flow of program execution. A statement is a command given to the computer that instructs the computer to take a specific
More informationChapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.
Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop
More informationWeek 2 / Lecture 2 15 March 2017 NWEN 241 Control constructs, Functions. Alvin Valera
Week 2 / Lecture 2 15 March 2017 NWEN 241 Control constructs, Functions Alvin Valera School of Engineering and Computer Science Victoria University of Wellington Admin stuff Tutorial #2 Expectations on
More informationComputer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)
CP Lect 5 slide 1 Monday 2 October 2017 Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont) Cristina Alexandru Monday 2 October 2017 Last Lecture Arithmetic Quadratic equation
More informationAssignment: 1. (Unit-1 Flowchart and Algorithm)
Assignment: 1 (Unit-1 Flowchart and Algorithm) 1. Explain: Flowchart with its symbols. 2. Explain: Types of flowchart with example. 3. Explain: Algorithm with example. 4. Draw a flowchart to find the area
More informationFundamentals of Programming
Fundamentals of Programming Introduction to the C language Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) The C language
More informationFlow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch
Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps 1 True and False in C False is represented by any zero value The int expression having the
More informationChapter 13 Control Structures
Chapter 13 Control Structures Highlights Sequence action_1 action_2 Conditional Iteration 13-2 Control Structures Conditional making a decision about which code to execute, based on evaluated expression
More informationStatements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14
Statements Control Flow Statements Based on slides from K. N. King Bryn Mawr College CS246 Programming Paradigm So far, we ve used return statements and expression statements. Most of C s remaining statements
More informationDECISION CONTROL AND LOOPING STATEMENTS
DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of
More informationInformatica e Sistemi in Tempo Reale
Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 5, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction
More informationCpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps
CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps Overview By the end of the lab, you will be able to: use fscanf() to accept inputs from the user and use fprint() for print statements to the
More informationName Roll No. Section
Indian Institute of Technology, Kharagpur Computer Science and Engineering Department Class Test I, Autumn 2012-13 Programming & Data Structure (CS 11002) Full marks: 30 Feb 7, 2013 Time: 60 mins. Name
More informationConcept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs
Morteza Noferesti Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs We want to solve a real problem by computers Take average, Sort, Painting,
More informationIS12 - Introduction to Programming Lecture 12: Loops and Tables. Relational Operators > < >= <=
IS12 - Introduction to Programming Lecture 12: Loops and Tables Peter Brusilovsky http://www2.sis.pitt.edu/~peterb/0012-051/ Relational Operators > < >= 3
More information16.216: ECE Application Programming Fall 2011
16.216: ECE Application Programming Fall 2011 Exam 2 Solution 1. (24 points, 6 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling
More informationIntroduction to C Programming (Part A) Copyright 2008 W. W. Norton & Company. All rights Reserved
Introduction to C Programming (Part A) Copyright 2008 W. W. Norton & Company. All rights Reserved Overview (King Ch. 1-7) Introducing C (Ch. 1) C Fundamentals (Ch. 2) Formatted Input/Output (Ch. 3) Expressions
More informationAssignment #3 Answers
Assignment #3 Answers Introductory C Programming UW Experimental College Assignment #3 ANSWERS Question 1. How many elements does the array int a[5] contain? Which is the first element? The last? The array
More informationC Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }
C Program Output #include main () { printf ( Hi everyone\n ); Hi everyone #include main () { printf ( Hi everyone\n ); #include and main are Keywords (or Reserved Words) Reserved Words
More informationConditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:
Conditionals For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations: final int MAX = 25, LIMIT = 100; int num1 = 12, num2 = 25, num3 = 87; 1. if (num1 < MAX)
More informationProgramming & Data Structure Laboratory. Day 2, July 24, 2014
Programming & Data Structure Laboratory Day 2, July 24, 2014 Loops Pre and post test loops for while do-while switch-case Pre-test loop and post-test loop Condition checking True Loop Body False Loop Body
More informationECE 15 Fall 16 Midterm Solutions
ECE 15 Fall 16 Midterm Solutions This is a closed-book exam: no notes, books, calculators, cellphones, or friends are allowed. In problems 4 6, you can assume that the user s input is correct. If you need
More informationn Group of statements that are executed repeatedly while some condition remains true
Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,
More informationChapter 13. Control Structures
Chapter 13 Control Structures Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iteration executing code multiple times, ending
More informationComputers Programming Course 7. Iulian Năstac
Computers Programming Course 7 Iulian Năstac Recap from previous course Operators in C Programming languages typically support a set of operators, which differ in the calling of syntax and/or the argument
More informationQ1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Date:.FN / AN Time: 2 hrs Full marks: 60 No. of students: 643 Spring Mid Semester Exams, 2011 Dept: Comp. Sc & Engg. Sub No: CS11001 B.Tech 1 st Year (Core) Sub
More informationDay06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26
Day06 A Young W. Lim 2017-09-20 Wed Young W. Lim Day06 A 2017-09-20 Wed 1 / 26 Outline 1 Based on 2 C Program Control Overview for, while, do... while break and continue Relational and Logical Operators
More informationSummary of Lecture 4. Computer Programming: Skills & Concepts (INF-1-CP1) Variables; scanf; Conditional Execution. Tutorials.
Summary of Lecture 4 Computer Programming: Skills & Concepts (INF-1-CP1) Variables; scanf; Conditional Execution Integer arithmetic in C. Converting pre-decimal money to decimal. The int type and its operators.
More informationDiscussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:
Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;
More informationSubject: PIC Chapter 2.
02 Decision making 2.1 Decision making and branching if statement (if, if-, -if ladder, nested if-) Switch case statement, break statement. (14M) 2.2 Decision making and looping while, do, do-while statements
More informationFundamentals of Programming. Lecture 14 Hamed Rasifard
Fundamentals of Programming Lecture 14 Hamed Rasifard 1 Outline Two-Dimensional Array Passing Two-Dimensional Arrays to a Function Arrays of Strings Multidimensional Arrays Pointers 2 Two-Dimensional Array
More informationComputer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018
Computer Programing Class 02: 25 Jan 2018 [SCPY204] for Physicists Content: Data, Data type, program control, condition and loop, function and recursion, variable and scope Instructor: Puwis Amatyakul
More informationUnit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION
INTRODUCTION Decision Making and Looping Unit 5 In the previous lessons we have learned about the programming structure, decision making procedure, how to write statements, as well as different types of
More informationLectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.
Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations 4th October, 2010 Integer arithmetic in C. Converting pre-decimal money to decimal. The
More informationMODULE 2: Branching and Looping
MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written
More informationCOP 3223 Introduction to Programming with C - Study Union - Spring 2018
COP 3223 Introduction to Programming with C - Study Union - Spring 2018 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................
More informationDept. of CSE, IIT KGP
Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of
More informationOperators in C. Staff Incharge: S.Sasirekha
Operators in C Staff Incharge: S.Sasirekha Operators An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C
More informationC Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function
More informationAt 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.
Objective of this lecture: 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. Fundamental Programming Structures in C
More informationQ1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Date:.FN / AN Time: 2 hrs Full marks: 60 No. of students: 643 Spring Mid Semester Exams, 2011 Dept: Comp. Sc & Engg. Sub No: CS11001 B.Tech 1 st Year (Core) Sub
More informationLecture 3. More About C
Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants
More informationUnit 3. Operators. School of Science and Technology INTRODUCTION
INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.
More informationComputer Programming Unit 3
POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it s very
More informationDecision Making and Loops
Decision Making and Loops Goals of this section Continue looking at decision structures - switch control structures -if-else-if control structures Introduce looping -while loop -do-while loop -simple for
More informationITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University
ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 10: Arrays Readings: Chapter 9 Introduction Group of same type of variables that have same
More informationMA 511: Computer Programming Lecture 2: Partha Sarathi Mandal
MA 511: Computer Programming Lecture 2: http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html Partha Sarathi Mandal psm@iitg.ernet.ac.in Dept. of Mathematics, IIT Guwahati Semester 1, 2010-11 Largest
More informationCourse Outline Introduction to C-Programming
ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to C-Programming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,
More informationPhysics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu
Physics 2660: Fundamentals of Scientific Computing Lecture 3 Instructor: Prof. Chris Neu (chris.neu@virginia.edu) Announcements Weekly readings will be assigned and available through the class wiki home
More informationMake sure the version number is marked on your scantron sheet. This is Version 1
Last Name First Name McGill ID Make sure the version number is marked on your scantron sheet. This is Version 1 McGill University COMP 208 -- Computers in Engineering Mid-Term Examination Tuesday, March
More informationRepetition Structures
Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements
More informationCondition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.
Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes
More informationLOOPS. 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 ).
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 ). 2-Give the result of the following program: #include
More informationLecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch
Lecture 3 CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions Review Conditions: if( ) / else switch Loops: for( ) do...while( ) while( )... 1 Examples Display the first 10
More information1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays
Overview The C Programming Language Part 2 Input/Output Structures and Arrays (with material from Dr. Bin Ren, William & Mary Computer Science) 1 2 character-based putchar (c) output getchar () input formatted
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers
More informationControl Structures. Chapter 13 Control Structures. Example If Statements. ! Conditional. if (condition) action;
Chapter 13 Control Structures Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Control Structures! Conditional n making a decision
More informationPart I Part 1 Expressions
Writing Program in C Expressions and Control Structures (Selection Statements and Loops) Jan Faigl Department of Computer Science Faculty of Electrical Engineering Czech Technical University in Prague
More information1. 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
1. Basics 1. Write a program to add any two-given integer. Algorithm - 1. Start 2. Prompt user for two integer values 3. Accept the two values a & b 4. Calculate c = a + b 5. Display c 6. Stop int a, b,
More informationLab 2: Structured Program Development in C
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
More informationThe C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)
The C Programming Language Part 2 (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Input/Output Structures and Arrays 2 Basic I/O character-based putchar (c) output getchar
More information16.216: ECE Application Programming Fall 2011
16.216: ECE Application Programming Fall 2011 Exam 1 Solution 1. (24 points, 6 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling
More informationUNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING
UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS 105 Computer Fundamentals Midterm Examination October 20, 2011 6:15 p.m. 8:00 p.m. (105 minutes) Examiners: J. Anderson, T. Fairgrieve,
More informationOperators & Expressions
Operators & Expressions Operator An operator is a symbol used to indicate a specific operation on variables in a program. Example : symbol + is an add operator that adds two data items called operands.
More informationModule 4: Decision-making and forming loops
1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The
More informationCGS 3460 Summer 07 Midterm Exam
Short Answer 3 Points Each 1. What would the unix command gcc somefile.c -o someotherfile.exe do? 2. Name two basic data types in C. 3. A pointer data type holds what piece of information? 4. This key
More informationLecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting
Lecture 4 Console input/output operations 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting Header files: stdio.h conio.h C input/output revolves around
More informationBasic Assignment and Arithmetic Operators
C Programming 1 Basic Assignment and Arithmetic Operators C Programming 2 Assignment Operator = int count ; count = 10 ; The first line declares the variable count. In the second line an assignment operator
More informationCS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code
CS102: Standard I/O Our next topic is standard input and standard output in C. The adjective "standard" when applied to "input" or "output" could be interpreted to mean "default". Typically, standard output
More information