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;

Slides:



Advertisements
Similar presentations
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
 #include  Using a directive to include a header file  Stdio.h = standard input output header file.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
Basic Input/Output and Variables Ethan Cerami New York
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
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.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
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.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
/* 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.
Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CECS 130 Mid-term Test Review Provided by REACH – Resources for Academic Achievement Presenter: [REACH Tutor]
CECS 121 Midterm Review Presented by REACH (If you have not signed the attendance sheet, please do so now!)
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Lecture2.
‘C’ Programming Structures and Commands
EKT120: Computer Programming
Computer Science 210 Computer Organization
EKT120 COMPUTER PROGRAMMING
Chapter 2 - Introduction to C Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
BASIC ELEMENTS OF A COMPUTER PROGRAM
Quiz 11/15/16 – C functions, arrays and strings
PGT 106: Computer Programming
Week 5 – Functions (1) EKT120: Computer Programming.
EKT120: Computer Programming
Chapter 2 - Introduction to C Programming
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Computer Science 210 Computer Organization
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Chapter 2 - Introduction to C Programming
C Programming Getting started Variables Basic C operators Conditionals
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 2 - Introduction to C Programming
Programming Languages and Paradigms
DATA TYPES There are four basic data types associated with variables:
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Introduction to C Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

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;

TYPESIZEVALUES bool1 bytetrue (1) or false (0) char1 byte‘a’ to‘z’, ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on int4 bytes-2,147,483,648 to 2,147,483,647 short2 bytes-32,768 to 32,767 long4 bytes-2,147,483,648 to 2,147,483,647 float4 bytes+ - (1.2 x 10^-38 to 3.4 x 10^38) double8 bytes+- (2.3 x 10^-308 to -1.7 x 10^308)

#include main() { printf(“\nC you later\n”); system(“pause”); }

Serve to control program execution and functionality. Must end with a semicolon(;) with the exception of:  Comments: /* */  Preprocessor Directives: #include or #define  Begin and end program identifiers: { }  Function definition beginnings: main()

 Functions allow you to group program statements under one name  C and C++ are case-sensitive so main(), MAIN(), and Main() are all different functions.  The main function is special because the values it returns are returned to the operating system  Most main functions in this course do not take or pass information to the operating system

 #include  Using a directive to include a header file  stdio.h = standard input output header file  stdlib.h = ‘system’ commands  Iostream.h= Input/Output stream header library  Math.h= The Math library header file

 Definition: Escape sequences are specially sequenced characters used to format output  \”  Ex: printf(“ \ “This is quoted text \ “ “);  \’  Ex: printf(“ \n A single quote looks like \’ \n”);  \* *\ Comment Block

 Character - %c  Integer - %d  Float (decimal)- %f  String - %s  Printf Format Tags: %[flags][width][.precision][length]specifier %[.precision]specifer -> %.2f

#include int main() { int x; for ( x = 0; x < 10; x++ ){ printf( "%d\n", x ); } getchar(); }

 int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977); printf ("floats: %4.2f \n", ); printf ("%s \n", "A string"); return 0; } }

printf (“%c %c \n", 'a', 65); aA printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); 1977 printf ("%010d \n", 1977); printf ("floats: %4.2f \n", ); 3.14 printf ("%s \n", "A string"); A string  Can you create a tabular data using printf?

#include main() { int iOperand1 = 0; int iOperand2 = 0; printf(“\n Enter first operand: “); scanf(“%d”, &iOperand1); printf(“\n Enter second operand: “); scanf(“%d”, &iOperand2); printf(“The result is %d \n”, 24/(iOperand1 * iOperand2)+6/3); }

#include main() { int x = 4; int y = 9; int result1, result2; result1 = y/x; result2 = y%x; printf(“The result is %d.%d \n”, result1, 25*result2); }

x++;Tells the function to use the current value of x and increment it by 1. ++x;Increment the value of x by 1 and use the new value for calculations. x--; Tells the function to use the current value of x and decrease its value by 1. --x; Decrease the value of x by 1 and use the new value for calculations. x=0; printf(“The Value of x is: %d”, x++); printf(“\n The Value of x is: %d”,++x); Would results in:The Value of x is: 0 The Value of x is: 2

Do you know the answers to these?  A. !( 1 || 0 )  B. !( 1 || 1 && 0 )  C. !( ( 1 || 0 ) && 0 )

 A. !( 1 || 0 ) ANSWER: 0  B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)  C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

 What’s the syntax of  While(Logical Condition){  }  Do{  } While(Logical Condition)  What’s the difference between While and Do- While()?

 while ( condition ) { Code to execute while the condition is true }  do { } while ( condition );  Do{} while() executes code at least once!

 Use when the number of iterations is already known  Syntax: for ( variable initialization; condition; variable increment/decrement) { Code to execute while the condition is true }

 Write a program using a FOR Loop to display all of the multiples of 5 from 0 to 100.

#include int main() { int x; for ( x = 0; x < =20; x++ ) { printf( "%d\n", x*5 ); } getchar(); }

 Use to manipulate flow in loops  What does a Break statement do when executed within a loop?  What does a Continue statement do when executed within a loop?

#include main() { int x; for ( x = 10; x >5; x-- ) { if (x==7) break; } printf( “\n %d \n ”, x ); } #include main() { int x; for ( x = 10; x >5; x-- ) { if (x==7) continue; printf( “\n %d \n ”, x ); }

 Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN)  Function Prototypes tell you the data type returned by the function, the data type of parameters, how many parameters, and the order of parameters  Function definitions implement the function prototype  Where are function prototypes located in the program?  Where do you find function definitions?

 Where are function prototypes located in the program?  Answer: before the Main(){} Function!  Function Definitions are self contained outside of the Main(){} function

#include int mult (int,int); int main() { int x; int y; printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your two numbers is %d\n", mult( x, y ) ); getchar(); } int mult (int a, int b) { return a * b; }

#include Void printReportHeader(); main() { printReportHeader; } void printReportHeader() { printf(“\n Column1\tColumn2\tColumn3\tColumn4 \n”); }

#include void printNumbers(); int iNumber; main() { int x; for(x=0, x<10,x++){ printf(“\n Enter a number:”); scanf(“%d”, &iNumber); printNumbers(); } void printNumbers() { printf(“\n Your number is: %d \n”, iNumber); }

 Variable scope defines the life time of a variable  Local Scope: defined within functions and loses scope after function is finished. Can reuse in other functions (ex. p.123)  Global Scope: defined outside of functions and can be accessed by multiple functions