CECS 130 Mid-term Test Review

Similar documents
Sources: On the Web: Slides will be available on:

arrays C Programming Language - Arrays

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

5 Arrays and Pointers

The C Programming Language course syllabus associate level

Stacks. Linear data structures

Informatica e Sistemi in Tempo Reale

Introduction to Java

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Java Basics: Data Types, Variables, and Loops

Chapter 2: Elements of Java

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Chapter 2 Introduction to Java programming

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

6.087 Lecture 2 January 12, 2010

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

So far we have considered only numeric processing, i.e. processing of numeric data represented

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Introduction to Python

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Example of a Java program

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Java Interview Questions and Answers

JavaScript: Control Statements I

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

J a v a Quiz (Unit 3, Test 0 Practice)

The programming language C. sws1 1

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Passing 1D arrays to functions.

C++FA 5.1 PRACTICE MID-TERM EXAM

PL / SQL Basics. Chapter 3

Module 816. File Management in C. M. Campbell 1993 Deakin University

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Objective-C Tutorial

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Tutorial on C Language Programming

C++ Programming Language

Pemrograman Dasar. Basic Elements Of Java

In this Chapter you ll learn:

Introduction to Java. CS 3: Computer Programming in Java

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Numbering Systems. InThisAppendix...

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Chapter One Introduction to Programming

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

IC4 Programmer s Manual

AP Computer Science Java Subset

Chapter 13 Storage classes

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

1 Abstract Data Types Information Hiding

Chapter 3 Operators and Control Flow

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

DNA Data and Program Representation. Alexandre David

Computer Programming Tutorial

C++ Language Tutorial

VB.NET Programming Fundamentals

Computer Science Questions and Answers UNIT-A Chapter - 1 Configuring a Computer

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Chapter 2 Elementary Programming

Semantic Analysis: Types and Type Checking

Statements and Control Flow

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

System Calls and Standard I/O

The Payroll Program. Payroll

Number Representation

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

CS 241 Data Organization Coding Standards

Fundamentals of Programming

Parallel and Distributed Computing Programming Assignment 1

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

El Dorado Union High School District Educational Services

Moving from CS 61A Scheme to CS 61B Java

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

FEEG Applied Programming 5 - Tutorial Session

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Transcription:

CECS 130 Mid-term Test Review Provided by REACH Resources for Academic Achievement Presenter: A REACH Tutor REACH

Content on the Mid-term Exam ochapters 1-10 from C Programming for the Absolute Beginner, Second Edition by Michael Vine

So, we will be covering: Data types Pointers Conditions Strings Loops Data Structures Functions Dynamic Memory Arrays

Variable Types Data Type Description Declaration Example Integer Floatingpoint number Whole numbers, positive or negative All numbers, positive or negative, decimals and fractions int x = ; -3, 0, 3, 29 float x = ; -0.35543, 0.00, 554433.33281 Character Representations of integer values known as character codes char x = ; m, M, * To declare a constant (read only) value: const int x = 20; const float PI = 3.14;

Variable Types TYPE SIZE VALUES bool 1 byte true (1) or false (0) char 1 byte a to z, A to Z, 0 to 9, space, tab, etc. int 4 bytes -2,147,483,648 to 2,147,483,647 short 2 bytes -32,768 to 32,767 long 4 bytes -2,147,483,648 to 2,147,483,647 float 4 bytes + - (1.2 x 10^-38 to 3.4 x 10^38) double 8 bytes +- (2.3 x 10^-308 to -1.7 x 10^308)

Printf( ) ; Scanf( ) ; Can you explain what the code is doing? 1 #include <stdio.h> 2 3 int main() 4 { 5 int this_is_a_number = 0; 6 7 printf( Please enter a number: ); 8 scanf( %d, &this_is_a_number ); 9 printf( \nyou entered %d, this_is_a_number ); 10 getchar(); 11 12 return 0; 13

Conversion Specifiers Character - %c Integer - %d Float (decimal)- %f String - %s Printf Format Tags: Format: %[flags][width][.precision][length]specifier Example: %[.precision]specifier 7 float fboat = 12.123432; 8 printf( %.2f, %.3f, %.5f, boat, boat, boat ); Output: 12.12, 12.123, 12.12343

Can you predict the printout? 1 int main() 2 { 3 printf( %c%c\n, a,65); 4 printf( %10d\n,1997); 5 printf( %010d\n, 1997); 6 printf( floats: %4.2f \n, 3.1416); 7 printf( %s\n, A string ); 8 printf( %f\n, 55.55); 9 return 0; 10

Can you predict the printout? 1 int main() 2 { 3 printf( %c%c\n, a,65); 4 printf( %10d\n,1997); 5 printf( %010d\n, 1997); 6 printf( floats: %4.2f \n, 3.1416); 7 printf( %s\n, A string ); 8 printf( %f, 55.55); Output: aa 1977 0000001997 floats: 3.14 A string 55.55 9 return 0; 10

Scanf( ); Conversion Specifies Description %d Receives integer value %f Receives floating-point number %c Receives character

Predict the printout: User enters 2 and 4 : 1 #include <stdio.h> 2 3 int main() 4 { Output: Please enter first number: 2 Enter second number: 4 The result is 5. 5 int inum_1= 0; 6 int inum_2= 0; 7 8 printf( Please enter first number: ); 9 scanf( %d, &inum_1 ); 10 printf( \nenter second number: ); 11 scanf( %d, &inum_2 ); 12 printf( \n\nthe result is %d. \n, 24 / (inum_1 * inum_2) + 6 / 3); 13 14 return 0; 15

Predict the printout: User enters 2 and 4 : 1 #include <stdio.h> 2 3 int main() 4 { Output: Please enter first number: 2 Enter second number: 4 The result is 5. 5 int inum_1= 0; 6 int inum_2= 0; 7 8 printf( Please enter first number: ); 9 scanf( %d, &inum_1 ); 10 printf( \nenter second number: ); 11 scanf( %d, &inum_2 ); 12 printf( \n\nthe result is %d. \n, 24 / (inum_1 * inum_2) + 6 / 3); 13 14 return 0; 15

Arithmetic and Order of Precedence Operator Description * Multiplication / Division % Modulus (remainder) + Addition - Subtraction Order of Precedence Description ( ) Parentheses are evaluated first, from innermost to outermost *, /, % Evaluated second, from Left to Right +, - Evaluated last, from Left to Right

Can you predict the printout? Output: 1 #include <stdio.h> 2 3 int main() 4 { 5 int x = 0; 6 int y = 0; 7 int result1, result2; The result is 2.25 8 9 result1 = y/x; 10 result2 = y%x; 11 12 printf( \n\nthe result is %d.%d \n, result1, 25 * result2); 13

Can you predict the printout? Output: 1 #include <stdio.h> 2 3 int main() 4 { 5 int x = 0; 6 int y = 0; 7 int result1, result2; The result is 2.25 8 9 result1 = y/x; 10 result2 = y%x; 11 12 printf( \n\nthe result is %d.%d \n, result1, 25 * result2); 13

Conditions and Operators Operator Description == Equal to!= Not Equal > Greater Than < Less Than >= Greater Than or Equal to Order of Precedence Description && AND condition OR condition

Comparisons > greater than 5 > 4 is TRUE < less than 4 < 5 is TRUE >= greater than or equal 4 >= 4 is TRUE <= less than or equal 3 <= 4 is TRUE == equal to 5 == 5 is TRUE!= not equal to 5!= 4 is TRUE

Boolean Operators Do you know the answer to these? A.!( 1 0 ) B.!( 1 1&& 0 ) C.!(( 1 0 ) && 0) =!( 1 ) = 0 =!( 1 0 ) = 0 =!(1 && 0) =!0 = 1

Boolean Operators Do you know the answer to these? A.!( 1 0 ) B.!( 1 1&& 0 ) C.!(( 1 0 ) && 0) =!( 1 ) = 0 =!( 1 0 ) = 0 =!(1 && 0) =!0 = 1

Quiz Using if-statements, write a program that will ask a user to enter a number 1, 2, or 3, and print out the following: User Input Printout 1 Smitty Werbenjagermanjensen was Number 1. 2 Fool me 2 times, can t put the blame on you. 3 Gimme 3 steps.

Possible Answer : 1 #include <stdio.h> 2 int main() { 4 printf( Enter one of the following: %d, %d, or %d\n,1,2,3 ); 5 scanf( %d, &a ); 6 if(a==1 a==2 a==3) { 7 if(a==1){ 8 printf( \nsmitty Werbenjagermanjensen was Number %d.\n, 1); 9 10 if(a==2){ 11 printf( \nfool me %d times, can t put the blame on you.\n, 2); 12 10 if(a==3){ 11 printf( \ngimme %d steps.\n, 3); 12 16 else 17 printf( Sorry, you entered an invalid value\n ); 18 return 0;

Switch-Case Statement 1 switch ( <var> ) 2 { 3 case <this-value>: 4 code to execute if <var> == this-value; 5 break; 6 case <that-value>: 7 code to execute if <var> == that-value; 8 break; 9... 10 default: 11 code executed if <var> does not equal any of the values; 12 break; 13

Loops while ( condition ) { Code to execute while the condition is true Quiz: Can you write a program that prints x while x increments from 0 to 10?

Possible Answer : 1 #include <stdio.h> 2 int main() 3 { 4 int x = 0; 5 6 while ( x < 10 ) 7 { 8 printf( %d, x ); 9 x++; 10 printf( \nfool me %d times, can t put the blame on you.\n, 2); 11 12 getchar(); 13

For Loop Often used when the # of iterations is already known. Contains 3 separate expressions: 1. Variable initialization 2. Conditional expression 3. Increment/Decrement Write a program with a for loop that counts down from 10.

Possible Answer : 1 #include <stdio.h> 2 3 int main() 4 { 5 int x = 0; 6 for( x=10; x>=0; x-- ) 7 { 8 printf( %d\n, x ); 9 10 11 system( pause ); 12

Break/Continue Statements break; Used to exit a loop. Once this statement is executed the program will execute the statement immediately following the end of the loop. continue; Used to manipulate program flow in a loop. When executed, any remaining statements in the loop will be skipped and the next iteration of the loop will begin.

Function Prototypes & Definitions 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?

Function Prototypes & Definitions Where are function prototypes located in the program? Answer: before the main( ) { function. Where do you find function definitions? Answer: function definitions are self-contained outside of the main( ) { function, usually written below it.

Calling Functions #include <stdio.h> int mult ( int x, int y ); // function prototype 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(); return 0; int mult (int x, int y) //function definition { return x * y;

Declaring a 1-D Array Can you declare a one-dimensional array made up of 10 integers? Answer: int iarray[10] How to declare an Array: int iarray[10]; float faverages[30]; double dresults[3]; short ssalaries [9]; char cname[19]; // 18 characters and 1 null character

Declaring a 1-D Array Why do we initialize? Because memory spaces may not be cleared from previous values when arrays are created. Can initialize an array directly. Example: int iarray[5]={0,1,2,3,4; Can initialize an array with a loop such as FOR( )

Initializing a 1-D Array with a for( ) loop #include <stdio.h> main() { int x; int iarray[5]; for( x=0; x < 5 ; x++) { iarray[x] = 0;

Initializing a 1-D Array with a for( ) loop Now add printing: #include <stdio.h> main() { int x; int iarray[5]; for( x=0; x < 5 ; x++ ) { iarray[x] = 0; for( x=0 ; x<5; x++ ) { printf( \n iarray[%d] = %d \n, x, iarray[x] );

Searching an array #include <stdio.h> main() { int x; int ivalue; int ifound = -1; int iarray[5]; // initialize the array for( x=0; x < 5 ; x++) { iarray[x] = (x+x); printf( \n Enter value to search for: ); scanf( %d, &ivalue); // search for number for(x=0 ; x<5; x++) { if( iarray[x] ==ivalue){ ifound = x; break; if(ifound >-1) printf( \n I found your search value in element %d \n, ifound); else printf( \n Sorry, your search value was not found \n );

Pointers Pointer variables, simply called pointers, are designed to hold memory addresses as their values. Normally, a variable contains a specific value, e.g., an integer, a floating-point value, or a character. However, a pointer contains the memory address of a variable that in turn contains a specific value.

Pointers int val = 5; int *val_ptr = &val; val *val_ptr 5 0x3F 0x3F 0x83

Pointer Syntax datatype *pointer_name = &variable_name; OR datatype *pointer_name = NULL; OR datatype *pointer_name = 0; It s important to initialize pointers to prevent fatal runtime errors or accidentally modifying important data.

Pointers When an ampersand (&) is prefixed to a variable name, it refers to the memory address of this variable. val *val_ptr 5 0x3F &val = 0x3F &val_ptr = 0x83

Ampersand example #include <stdio.h> int main() { char somechar = 'x'; printf( %p\n", &somechar); system("pause"); return 0; Output: 0022FF47

Passing variables by value void exchange(int x, int y); main() { int a = 5; int b = 3; exchange(a,b); [print a and b] void exchange(int x, int y); { [print x and y] int temp = x; int x = y; int y = temp; [print x and y]

Passing variables by value void exchange(int x, int y); main() { int a = 5; int b = 3; exchange(a,b); [print a and b] void exchange(int x, int y); { [print x and y] int temp = x; int x = y; int y = temp; [print x and y] Output: x = 5, y = 3 x = 3, y = 5 a = 5, b = 3

Passing variables with pointers void exchange(int*, int*); main() { int a = 5; int b = 3; exchange(&a,&b); [print a and b] void exchange(int *x, int *y); { [print *x and *y] int temp = *i; int *x = *y; int *y = temp; [print *x and *y]

Passing variables with pointers void exchange(int*, int*); main() { int a = 5; int b = 3; exchange(&a,&b); [print a and b] void exchange(int *x, int *y); { [print *x and *y] int temp = *i; int *x = *y; int *y = temp; [print *x and *y] Output: *x = 5, *y = 3 *x = 3, *y = 5 a = 3, b = 5

Pointers Pointing to Pointers int val = 5; int *val_ptr = &val; int **val_ptr_ptr = &val_ptr; val *val_ptr **val_ptr_ptr 5 0x3F 0x83 &val = 0x3F &val_ptr = 0x83 &val_ptr_ptr = 0xF5

Pointers Pointing to Pointers val = 5 *val_ptr = 5 **val_ptr_ptr = 5 **val_ptr_ptr = *val_ptr = val = 5 *val_ptr_ptr = val_ptr = &val = 0x3F val_ptr_ptr = &val_ptr = 0x83 &val_ptr_ptr = 0xF5 val *val_ptr **val_ptr_ptr 5 0x3F 0x83 &val = 0x3F &val_ptr = 0x83 &val_ptr_ptr = 0xF5

Pointers to Arrays An array variable without a bracket and a subscript actually represents the starting address of the array. An array variable is essentially a pointer. Suppose you declare an array of int value as follows: int list[6] = {11, 12, 13, 14, 15, 16; *(list + 1) is different from *list + 1. The dereference operator (*) has precedence over +. So, *list + 1 adds 1 to the value of the first element in the array, while *(list + 1) dereferences the element at address (list + 1) in the array.

Pointers to Arrays main() { int list[3] = {1, 0, 5; int k = 0; main() { int list[3] = {1, 0, 5; int k = 0; k = *list + 1; k = *(list + 1); [print k] [print k]

Pointers to Arrays main() { int list[3] = {1, 0, 5; int k = 0; main() { int list[3] = {1, 0, 5; int k = 0; k = *list + 1; k = *(list + 1); [print k] [print k] Output: k = 2 Output: k = 0

Strings Function strlen() Description Returns numeric string length up to, but not including null character tolower() and toupper() Converts a single character to upper or lower case strcpy() strcat() strcmp() strstr() Copies the contents of one string into another string Appends one string onto the end of another Compares two strings for equality Searches the first string for the first occurrence of the second string

Strings #include <stdio.h> #include <string.h> main() { char *str1 = REACH ; char str2[] = Tutoring ; printf( \nthe length of string 1 is %d \n, strlen(str1)); printf( The length of string 2 is %d\n,strlen(str2));

Strings #include <stdio.h> #include <string.h> main() { char *str1 = REACH ; char str2[] = Tutoring ; printf( \nthe length of string 1 is %d \n, strlen(str1)); printf( The length of string 2 is %d\n,strlen(str2)); Output: The length of string 1 is 5 The length of string 2 is 8

Strings #include <stdio.h> #include <string.h> void convertl(char *); main() { char name1[] = Barbara Bush ; convertl(name1); void convertl(char *str) { int x; for ( x = 0; x <=strlen(str) ; x++) str[x] = tolower(str[x]); printf( \nthe name in l.c. is %s\n, str);

Strings #include <stdio.h> #include <string.h> void convertl(char *); Output: main() { char name1[] = Barbara Bush ; convertl(name1); The name in l.c. is barbarabush void convertl(char *str) { int x; for ( x = 0; x <=strlen(str) ; x++) str[x] = tolower(str[x]); printf( \nthe name in l.c. is %s\n, str);

Data Structures Arrays require that all elements be of the same data type. Many times it is necessary to group information of different data types. An example is a list of materials for a product. The list typically includes a name for each item, a part number, dimensions, weight, and cost. C and C++ support data structures that can store combinations of character, integer floating point and enumerated type data. They are called - structs.

Structure Syntax struct my_example { int assignment; char grade; char name[20]; mystruct; mystruct students[3]; stu1.assignment = 3; stu1.grade = A ; stu1.name = Patrick ;

Thank you! This presentation was provided by REACH Resources for Academic Achievement