H192 Midterm 1 Review. Tom Zajdel

Similar documents
Computer System and programming in C

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

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

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

C programming basics T3-1 -

Course Outline Introduction to C-Programming

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 3. More About C

C introduction: part 1

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

C Programming Multiple. Choice

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

PROGRAMMING IN C AND C++:

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Data Types and Variables in C language

BASIC ELEMENTS OF A COMPUTER PROGRAM

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

CSCI 171 Chapter Outlines

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Fundamentals of Programming

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Programming for Engineers Iteration

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Arithmetic Expressions in C

C: How to Program. Week /Mar/05

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Chapter 2: Using Data

ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

today cs3157-fall2002-sklar-lect05 1

Chapter 2 - Introduction to C Programming

OBJECT ORIENTED PROGRAMMING

Chapter 3. Fundamental Data Types

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

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

2. Numbers In, Numbers Out

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

Work relative to other classes

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

Reserved Words and Identifiers

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

Programming and Data Structures

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia 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:

Data Type Fall 2014 Jinkyu Jeong

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

C++ Programming: From Problem Analysis to Program Design, Third Edition

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

Functions. Arash Rafiey. September 26, 2017

Chapter 4: Basic C Operators

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

Chapter 2: Basic Elements of C++

Fundamental of Programming (C)

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS Kangmei Yang. Page 1

Unit 3. Operators. School of Science and Technology INTRODUCTION

Computer Programming C++ (wg) CCOs

Variables and Operators 2/20/01 Lecture #

COP 3223 Final Review

Numbers In, Numbers Out

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Chapter 3 Structure of a C Program

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

ET156 Introduction to C Programming

C Functions. 5.2 Program Modules in C

Operators and Control Flow. CS449 Fall 2017

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

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

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

Introduction to C Language

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Euclid s algorithm, 133

ANSI C Programming Simple Programs

DEPARTMENT OF MATHS, MJ COLLEGE

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

COP 3223 Final Review

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

2. Numbers In, Numbers Out

Basics of Programming

M1-R4: Programing and Problem Solving using C (JULY 2018)

Computer Programming. Decision Making (2) Loops

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

The Design of C: A Rational Reconstruction (cont.)

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Lecture 3 Tao Wang 1

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

C Programming Class I

Transcription:

H192 Midterm 1 Review Tom Zajdel

Declaring variables Need to specify a type when declaring a variable. Can declare multiple variables in one line. int x, y, z; float a, b, c; Can also initialize in same line (assign initial value) float radius = 5.0, area; What is in variable area?

Types Type Description Example int integer int a = 5; long long integer long a = 500; float Floating decimal point float a = 4.2; double long float double a = 5000.25; char character char a = 5 ; char a = A ;

Precision Precision deals with how much information a variable stores Precisions: int < float < double Int < long If an assignment puts a value of high precision into a lower precision variable, information is lost. Decimal truncation most common example int b = 5.99; What is the value in b?

Arithmetic Math operators: + addition - subtraction * multiplication / division % remainder (modulus) Other operators & address reference operator * used to declare pointers

Arithmetic The operand with highest precision is used, the other operand is made more precise A float has greater precision than an int. int x = 5; float y = 7.5, z; z = 5 + 7.5; -> converted to 5.0 + 7.5 z = 12.5;

Arithmetic Be careful with integer division 1/2 =? 1.0/2 =? 1/2.0 =? 1.0/2.0 =? Can use explicit type casting to solve problem 1 / (float) 2 1/2.0 Lets programmer explicitly change the value s type

Arithmetic How do you use exponents? #include <math.h> ^ Use this header before the main function Gives you access to pow function pow (x, n) = x n pow (2, 3) =?

More arithmetic num *= 2.0 num = num * 2.0; count++ count = count + 1; Know difference between count++ and ++count? Prove it! Assume x = 10, and y = 12, z = 0, all are ints; z = x - ++y; What does z equal? y? Why?!

Problems int v = 5, w, x =.99, y = 6.1, z; float a = 2.5, b = 3, c = 2/5, d, e; z = a * (y * x) + v + a; /* z =? */ d = pow(b, x) * a; /* d =? */ e = c + (y % v) / 2.0; /* e =? */

printf printf( Output string, a, b, c, ) Puts output to screen Takes an output string and any number of variable values to be printed Each variable to be outputted is indicated in the output string float = %f int = %d or %i char = %c

printf int x = 10; float y = 4.5323235232; char z = C ; printf( x = \ny = \nz =,,, ); What will this code print?

printf Answer: printf( x = %d\ny = %f\nz = %c, x, y, z); The code prints: x = 10 y = 4.5323235232 z = C How do you clean up the decimal? Instead of %f, use %.2f to format to two decimal places

scanf scanf( format string, &a, &b, &c, ) Gets user input Needs to get the format of input, and variable addresses where the values will go Addresses use the & address of or reference of operator Example format string %f%f%d%d Two floats, followed by two ints

scanf int x; float z; printf( Please enter an integer > ); scanf(, ); z = x / 2.0; printf( Your number is now, ); What will this code do?

scanf Answers int x; float z; printf( Please enter an integer > ); scanf( %d, &x); z = x / 2.0; printf( Your number is now %f, z);

File I/O fscanf and fprintf take input from files, and print input to files, respectively Must declare a FILE pointer. To do that, use the pointer * FILE * input, output; Once declared, initialize with fopen

File I/O fopen( name, r/w ) Opens a file with called name r for reading (input) w for writing (output) Pass this function to your FILE* variables input = fopen( data.dat, ); output = fopen( results.txt, );

File I/O What if file does not exist? input = fopen( data.dat, r ); Assume that data.dat does not exist! fopen returns NULL, so input == NULL Safest to check if fopen was successful before moving on if (input == ) printf( \nfile failed to open! );

File I/O fprintf(output, output string, a,b,c, ) fscanf(input, format string, &a, &b, &c, ) Note the similarities to printf and scanf as mentioned before. These functions take the additional FILE* parameters created using fopen

Let s take a compiling break Source: xkcd

if/else if (conditional) Check the conditional. If the condition is true, run the line of code immediately after the if. Otherwise, check the next else if or else. if (x _ b) printf( x is less than b! ); else printf( x is not less than b! );

if/else Can use compound conditions to combine them ( condition1 && condition2)? (condition1 condition2)? Choices for conditionals == < > >= <=!= Why shouldn t you use = in a comparison?

if/else To run more than one statement after an if, create a { functional block! } int tom = 1, awesome = 1; if (tom == awesome) { printf ( Tom is awesome! ); tom = 5; printf( Tom is no longer awesome! Bwahaha! ); }

switch/case Uses either an int or a char as a switch Selects the right case to run can be either conditional or value char letter; scanf( %c,&letter); switch(letter) { case A : case a : printf( A! ); break; } default: printf( Not A! );

switch/case Note: program keeps running all code under the case, until it reaches a break or the end of the switch. Be careful to only run cases you want! default is the case that occurs if all else fails

if/else switch/case if (number == 5) { do this code; } else if (number == 10) { do this code; } else { do this code; } switch ( ) { case ( ): do this code; ; } case ( ): do this code; ; default: do this code;

if/else switch/case if (number == 5) { do this code; } else if (number == 10) { do this code; } else { do this code; } switch (number) { case 5: do this code; break; } case 10: do this code; break; default: do this code;

Loops - while while (condition) { do this code } Keeps looping while condition is true

Loops - while char answer = y ; while ( ) { } printf( Do you still think Tom is awesome? > ); scanf(, ); Finish the loop!

Loops - while char answer = y ; while (answer == y ) { } printf( \ndo you still think Tom is awesome? > ); scanf( %c, &y);

Loops while What if we were afraid this would be an infinite loop? Use a counter. Let s say we want to run five times max. Finish the code! char answer = y ; int counter = 0; while (answer == y && ) { printf( \ndo you still think Tom is awesome? > ); scanf( %c, &y); ; }

Loops do while Runs at least once, then checks conditional after the code char answer; do { printf( Do you still think Tom is awesome? > ); scanf(, ); } while (answer == y ) ; //<-- Note the semicolon!!

Loops - for A definite loop Defines the following Initial counter value Looping condition Counter increment int k; for (k = 0; k < 20; k++) //This loops how many times? { CODE // What would I put here } // to print numbers 1 to 20?

Arrays An array is a line of values of the same data type, stored contiguously in memory int arr[7]; //Creates an array of integers of // length 7, i.e. 7 integers arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6]

Arrays int arr[ ] = {0, 1, 2, 3, 4, 5, 6}; //Initializing arrays char name[ ] = { F, E, H, 1, 9, 2 }; printf( arr[1] = \n name[5] =,, ); Finish the code! What does it display?

Arrays Can initialize array with a loop int array[10], i; for (i = 0; i < 10; i++) { array[i] = i * 2; } What is in array[ ] after this code runs?

Arrays Finish following loop to print values in array. Use array as defined in last slide, which is an array of ints with length of 10. int k; for ( ; ; ) { printf( ); }

Arrays Write a loop to display the information in name char name = { F, R, E, U, E, L, E, R }; for ( ; ; ;) { printf( ); }

Arrays Let s get input data from a file, using fscanf. The file s name is data.dat and it contains 20 floats, each on a new line. input = fopen(, ); float numbers[ ]; for (int i = 0; ; ) { fscanf( ); }

Functions You can write your own functions! Each function has 3 components Name Return type Parameter list, of names and types For example, double pow(double x, double n) pow takes two doubles and returns a double (which can usually be manipulated as floats)

Functions Write a function prototype at the top of your program (above main) Contains all info about function, it s like defining a variable before using it float min(float a, float b); This will be our function prototype. Let s define our function under main

Functions #include <stdio.h> float min(float a, float b); int main() { } float min(float a, float b) { } //FUNCTION PROTOTYPE //FUNCTION DEFINITION

Functions We want to return the smaller of two input values. Define the function! float min(float a, float b) { }

Functions #include <stdio.h> float min(float a, float b); int main() { float number = min(5.0, 6.0); //WHAT WENT INTO number? } float min(float a, float b) { if (a < b) return a; else return b; }

Functions Note a and b only live inside the function min. They cannot be used outside the function. Variable values may also be passed into a function. float x = 1.0, y = 2.0, z; z = min(x, y); NOTE: a and b are not usable in this segment they are only defined in min!

Functions Let s write our own power function, ipow. It will take a float and an int. If power = x n, x is the float, and n is the int. Return an the result as an int. What is the function s prototype?

Functions The prototype is int ipow(float x, int n); Now, define the function. HINT: x n = x * x * x n times int ipow( float x, int n) { for ( ; ; ) } return ;

Flowcharts Start/End START END General action process

Flowcharts Print to screen Number of students is, students printf( Number of students = %d, students);

Flowcharts Decision structure (if, switch) A<B? F T if (A < B) statement; else other_statement;

Flowcharts Indefinite loop while index <= limit n y while (index <= limit) looping_statement;

Flowcharts Definite loop k = 0 k++ <= limit n y for (k = 0; k <= limit; k++) looping statement

Thank you! Best of luck tomorrow!