Join to C language Session 2 By Reza Gholamzadeh - 1387 1st Semester.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Modular Programming With Functions
Computer Programming w/ Eng. Applications
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
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.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Basic Input/Output and Variables Ethan Cerami New York
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Instructor - C. BoyleFall Semester
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Iterations Very Useful: Ability to repeat a block of code Example:
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
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.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Numbers in ‘C’ Two general categories: Integers Floats
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
CSCI206 - Computer Organization & Programming
IDENTIFIERS CSC 111.
Introduction to CS Your First C Programs
CS111 Computer Programming
Week 2 Variables, flow control and the Debugger
C Programming Getting started Variables Basic C operators Conditionals
Chapter 2: Introduction to C++.
WEEK-2.
In C Programming Language
Fundamental Programming
Fundamental Programming
Chap 2. Identifiers, Keywords, and Types
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Join to C language Session 2 By Reza Gholamzadeh - 1387 1st Semester

We have an overview to: Anatomy of a C program What is a function? Declaration of variables Data types Choose correct names Initialize Assignment Blocks Comments Input and output functions Conditional statements if switch Loops while do – while for By Reza Gholamzadeh - 1387 1st Semester

Preprocess instructions Anatomy of a C program Preprocess instructions Typical C program Prototypes Global variables main() function function A() function B() statements Declarations Assignment Function Control null By Reza Gholamzadeh - 1387 1st Semester

What is a function? A function is a self-contained unit of program code designed to accomplish a particular task a function can both produce actions (such as procedures) and provide values they save you from repetitious programming it makes a program more modular, hence easier to read and easier to change or fix By Reza Gholamzadeh - 1387 1st Semester

Declaration of variables Declaration structure: DataType Var1Name = [initialization],[Vare2Name] = [initialization],…; For example: int sum = 2; float avg = 0, var = 1.0; char mark = ‘A’; Available data types: int , char , float , double , long , short , ... Available names for variables: The characters at your disposal are lowercase letters, uppercase letters, digits, and the underscore (_) The first character must be a letter or an underscore firstName , _sumOf , taxi1 , ... By Reza Gholamzadeh - 1387 1st Semester

Assignment By value By expressions Example: X = 12; sum = 1298.998; ch = ‘q’; By expressions avg = ( a + b + c ) / 3; x = sin(y); var1 = var2; By Reza Gholamzadeh - 1387 1st Semester

Blocks A block starts with ‘{‘ and ends with ‘}’ Variables defined in a block , can`t be used outside of the block Each function have a block Blocks used for grouping statements Blocks can be used in blocks A block: { statement1; statement2; … } By Reza Gholamzadeh - 1387 1st Semester

Comments The parts of the program enclosed in the /* */ symbols are comments Using comments makes it easier for someone (including yourself) to understand your program they can be placed anywhere Everything between the opening /* and the closing */ is ignored by the compiler use the symbols // to create comments that are confined to a single line By Reza Gholamzadeh - 1387 1st Semester

Input and output functions Output function printf() Examples: printf(“Hello C language!”); printf(“My name is:\nReza Gholamzadeh”); printf(“your code is %d”,code); printf(“\t a + b = %f”,a+b); Input function scanf() scanf(“%d”,&input); scanf(“%f%f%f”,&number1,&number2,&number3); scanf(“(%d,%d)”,&pointX,&pointY); By Reza Gholamzadeh - 1387 1st Semester

Conditional statements if statement structures if (condition) statement; if (condition1) { statement1; ... } else if(condition2) else if(condition3) . else if (condition) { statement1; ... } if (condition) statement1; else statement2; if (condition) { statement1; ... } else By Reza Gholamzadeh - 1387 1st Semester

Conditional statements switch statement structure switch (variable) { case value1: statement1; ... break; case value2: case value3: . [default:] } By Reza Gholamzadeh - 1387 1st Semester

Loops while loop structures do – while loop structures while (condition) statement; while (condition) { statement1; statement2; ... } do statement; while (condition); do { statement1; statement2; ... } while (condition); By Reza Gholamzadeh - 1387 1st Semester

Loops for loop structures for (initializeCounter; condition; step) statement; for (initializeCounter; condition; step) { statement1; statement2; ... } By Reza Gholamzadeh - 1387 1st Semester

Now ready to write some programs By Reza Gholamzadeh - 1387 1st Semester

Example 1 A program which gets n number and calculates the average. Output: Enter value of n: 5 Enter number 1: 6 Enter number 2: 7 Enter number 3: -1 Enter number 4: 16 Enter number 5: -3 Average is: 5.00 By Reza Gholamzadeh - 1387 1st Semester

Example 1 #include <stdio.h> int main() { int n,input,sum = 0,i; float avg = 0.0; for(i=0; i<n; i++) printf(“enter number %d: “,i+1); scanf(“%d”,&input); sum+=input; } avg = sum/n; printf(“average is %.2f”,avg); return 0; By Reza Gholamzadeh - 1387 1st Semester

Example 2 A program which gets natural numbers of n and r and calculates the c(n,r).This should include a function to calculate the factorial. Output: Enter n and r in format (n,r): (5,3) Result of c(5,3) is: 10 By Reza Gholamzadeh - 1387 1st Semester

Example 2 #include <stdio.h> int fact(int); int main() { int r,n; float c; printf(“enter n and c in (c,r) format: “); scanf(“(%d,%d)”,&n,&r); if(n<r) printf(“incorrect values.”); return 1; } c = fact(n)/(fact(r)*fact(n-r)); printf(“Result of c(%d,%d) is: %f“,n,r,c); return 0; By Reza Gholamzadeh - 1387 1st Semester

Example 2 int fact(int input) { int output = 1 , i; if(input == 0) return 1; for(i = input; i>=1; i--) output *= i; return output; } By Reza Gholamzadeh - 1387 1st Semester

Example 3 A program which gets float point numbers and calculates the minimum & maximum of them. Output: Enter value of n: 3 Enter number 1: -1 Enter number 2: 2.5 Enter number 3: 2 Max is 2.50 and min is -1.00 By Reza Gholamzadeh - 1387 1st Semester

Example 3 #include <stdio.h> int main() { int n,i; float input,Min,Max; printf(“Enter value of n: “); scanf(“%d”,&n); if(n == 0) return 1; printf(“Enter number 1: “); scanf(“%f”,&input); Max = input; Min = input; for(i = 2; i<=n; i++) if(input < Min) else if(input > Max) } printf(“\nMax is &f and Min is %f”,Max,Min); return 0; By Reza Gholamzadeh - 1387 1st Semester

Example 4 A program which gets three numbers and shows us that numbers can be the lengths of a triangle or not. Output: Enter a,b,c in {a,b,c} format: {2,4,3} Yes. By Reza Gholamzadeh - 1387 1st Semester

Example 4 #include <stdio.h> int main() { int a,b,c; printf(“enter a,b,c in {a,b,c} format: “); scanf(“{%d,%d,%d}”,&a,&b,&c); if(a+b>c && a+c>b && b+c>a) printf(“\nYes.”); else printf(“\nNo.”); return 0; } By Reza Gholamzadeh - 1387 1st Semester

Example 5 A program which gets the coordinatios of vector A & B, and calculate the A*B. Output: Enter A in (a,b,c) format: (2,2,3) Enter A in (d,e,f) format: (1,4,-2) A*b is: (-16,7,6) By Reza Gholamzadeh - 1387 1st Semester

Example 5 #include <stdio.h> int main() { int a,b,c,d,e,f; printf(“enter A in (a,b,c) format: “); scanf(“(%d,%d,%d)”,&a,&b,&c); printf(“enter B in (d,e,f) format: “); scanf(“(%d,%d,%d)”,&d,&e,&f); printf(“\nA*B is: (%d,%d,%d)”,(b*f-c*e),(c*d-a*f),(a*e-b*d)); return 0; } By Reza Gholamzadeh - 1387 1st Semester

Exercise 1 A program which gets 4 numbers and shows the maximum and minimum of them . This program should include two functions for calculation of maximum and minimum of two numbers. Output: Enter numbers: -1 , 12.05 , 11 , 0 Minimum is -4.00 and maximum is 12.05) By Reza Gholamzadeh - 1387 1st Semester

Exercise 2 A program which gets a special character and a number (n).It will get other inputs(in character mode) until the nth character of that special character is inputted. Output: Enter your character: C Enter number of repeating: 2 1.Enter character: D 2.Enter character: C 3.Enter character: A 4.Enter character: 5 5.Enter character: C Program finished after 5 inputs. By Reza Gholamzadeh - 1387 1st Semester

Exercise 3 A program which gets 2 numbers and will show all the prime numbers between these two number .The program should include a function to calculate the prime number. Output: Enter A and B: 25 4 Result is: 5,7,11,13,17,19,23 I find 7 prime number between 4 and 25. By Reza Gholamzadeh - 1387 1st Semester

Exercise 4 A program which reads a number in binary base and converts it into decimal base. Output: Enter your binary number: 10011 Your number in decimal: 19 By Reza Gholamzadeh - 1387 1st Semester

Notes Codes must be executable Use less variables as you can (the less memory) Codes should be readable (similar to example’s codes) Codes should give the right answer (by having bugs, it gets point by the amount of errors) Must be mailed in the time given( Till 1:00 PM of next Saturday) By Reza Gholamzadeh - 1387 1st Semester