Introduction to Computers and Programming Class 15 Introduction to C Professor Avi Rosenfeld.

Slides:



Advertisements
Similar presentations
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 4 Outline 4.0 Reading.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Do-while loop Syntax do statement while (loop repetition condition)
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
1 C Programming Week 2 Variables, flow control and the Debugger.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Computer Programming Control Structure
CSC Programming for Science Lecture 8: Character Functions.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Lecture2.
CSCE 206 Structured Programming in C
EKT120 COMPUTER PROGRAMMING
Chapter 2 Overview of C.
CSE1320 Loop Dr. Sajib Datta
Introduction to C CSE 2031 Fall /3/ :33 AM.
Introduction to C Programming
Visit for more Learning Resources
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Structured Programming (Top Down Step Refinement)
C Programming Variables.
Lec 5.
Chapter 8 The Loops By: Mr. Baha Hanene.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Incremental operators
Lecture3.
Week 2 Variables, flow control and the Debugger
Chapter 4 Managing Input and Output Operations
Primitive Types and Expressions
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Course Outcomes of Programming In C (PIC) (17212, C203):
Introduction to C Programming
Presentation transcript:

Introduction to Computers and Programming Class 15 Introduction to C Professor Avi Rosenfeld

#include void main() { int num, tenthou, thou, hund, ten, one; printf("Please enter a number\n"); scanf("%d",&num); tenthou=num/10000; thou=(num%10000)/1000; hund=(num%1000)/100; ten=(num%100)/10; one=(((num%10000)%1000)%100)%10; printf("%d%d%d%d%d",tenthou,ten, hund, thou, one); }

#include void main() { int num, tenthou, thou, hund, ten, one; printf("Please enter a number\n"); scanf("%d",&num); one=num%10; /*gets the last digit */ num=num / 10; ten=num%10; num=num / 10; hund=num%10; num=num / 10; thou=num%10; num=num / 10; tenthou=num%10; num=num / 10; printf("%d%d%d%d%d",tenthou,ten, hund, thou, one); }

Understanding Repetition #include #define digits 7 void main() { int num, temp; printf("Please enter a number\n"); scanf("%d",&num); for (int i = 1; i <= digits; i++) { temp=num % 10; num = num / 10; printf("Digit #%d is %d\n", i, temp); }

#include void main() { float num, high, low; printf("Please type the first number "); scanf("%f",&num); high = low = num; /* new notation same as one two lines */ for (int i = 2; i <=10; i++) { printf("Please enter number #%d ",i); scanf("%f",&num); if (num>high) high = num; if (num<low) low = num; } printf("The highest was %f\n",high); printf("The lowest was %f\n",low); }

#include void main() { int size; printf("Please enter the size\n"); scanf("%d",&size); for (int i = 0; i < size; i++) { for (int j = 0; j <= i; j++) printf("*"); printf("\n"); }

#include /* The following program converts a number that the user enters from Celcius to Fahrenheit (decimals allowed). The correct formula is: F = 9/5 C If the user enters a value less than 0, the program should print, "Error". */ int main() { float Celcius; printf("Please enter a value in Celcius "); scanf("%f",&Celcius); if (Celcius < 0) printf("Error"); else printf("%f degrees Celcius is %f degrees fahrenheit", Celcius, 9/5 * Celcius + 32); return 0; }

Short Answer a. What is the value after the following statements? int x = 5; x -= 1+2 * 3 % 2; Answer:1 or 4 b. If x = 1 and y = 3, what does the following print? printf("%d", 2 * x y + 1); Answer: 7 c. If a = 0 and b = 1, does the following expression in parentheses evaluate to true or false? ( !a && b ) Answer: T_____

Short Answer d. What will the following print: #include void main() { int x = 1, y = 2; if (x=2); printf("stage 1"); if (x == y) printf("stage 2"); }

Short Answer #include void main() { int x = 90; switch(x) { case 100: printf("Here "); case 90: printf("I "); case 80: printf("am "); break; default: printf("not! "); }

Character constant Characters are represented internally through int values. You can reference them by using single quotes around the character –E.g. ‘A’, ‘a’, ‘1’ or ‘\n’ What is actually happening is the computer stores these as numbers –The int value for ‘A’ is 65, and for ‘a’ it is 97, etc, See Appendix D, page 1198, in your text for a chart of the int values for each character

getchar() Function contained in stdio.h A function is like a small program other programs can use to perform actions (an example you’ve used is printf() ) getChar() returns an int code for a character typed on the standard input (the keyboard) char c; c = getchar() ;

How to print a char Can use printf with a format control string of %c. For example, printf( “ The character is %c\n ”, c ); Can use another function in stdio.h called putchar() For example, putchar( c ) ;

putchar() Prints a character to the standard output (screen) Is a function also referenced in stdio.h

Using getchar() example #include int main() { char c ; /* declare character variable */ /* read a character and store it in c */ c = getchar() ; /* print it twice, two different ways */ printf("%c\n", c ); putchar( c ) ; /* one character at a time so here ’ s the newline */ c = '\n' ; putchar( c ); } /* end program */

Char Input #include void main() { int count=0; char letter; printf("Please type some letters\n"); do { scanf("%s",&letter); if (letter=='a') count++; }while(letter != 'b'); printf("There were %d 'A's in the sentence\n",count); }

#include void main() { int count=0; char letter; printf("Please type some letters\n"); do { letter = getchar(); /*fflush(stdin);*/ if (letter=='a') count++; }while(letter != EOF); printf("There were %d 'a's in the sentence\n",count); }