Review of C… The basics of C scanf/printf if/elseif statements

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
Chapter 4 – C Program Control
Chapter 5: Structured Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
A bit of C programming Lecture 3 Uli Raich.
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
C Programming Tutorial – Part I
Will these print the same thing?
CSE1320 Loop Dr. Sajib Datta
Quiz 11/15/16 – C functions, arrays and strings
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Lecture 13 & 14.
Pointers.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
INC 161 , CPE 100 Computer Programming
FUNCTIONS.
Arrays in C.
Lecture 6 C++ Programming
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Arrays, For loop While loop Do while loop
C Stuff CS 2308.
Chapter 2 - Introduction to C Programming
C Arrays.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Chapter 2 - Introduction to C Programming
Chapter 4 - Program Control
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Outline Defining and using Pointers Operations on pointers
Chapter 2 - Introduction to C Programming
Chapter 16 Pointers and Arrays
Your questions from last session
Arrays.
By C. Shing ITEC Dept Radford University
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 2 - Introduction to C Programming
Exercise Arrays.
CS150 Introduction to Computer Science 1
Arrays.
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
Programming Languages and Paradigms
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
Dale Roberts, Lecturer IUPUI
CSCE 206 Lab Structured Programming in C
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Review of C… The basics of C scanf/printf if/elseif statements for/while loops Functions Arrays Strings/characters Pointers open files

Reminder on syntax/proper format: if/else if/else statements: while loop: If (conditions) { statements; } else if (different conditions) else while (conditions) { statements; } Nested while statements: while (conditions) { statements; } Common Programming Error: 1) A syntax error will occur if the two symbols in any of the operators ==, !=, >= and <= are separated by spaces. 2) A syntax error will occur if the two symbols in any of the operators !=, >= and <= are reversed as in =!, => and =<, respectively. 3) Confusing the equality operator == with the assignment operator =. 4) for loop: for (initialization; conditions; increment) { statements; } Various actions: printf(“This is an action”); scanf(“%d”,&variable); newvariable = algebraic statements;

Are these statements equivalent? Output: A equals 1 NO ! #include <stdio.h> int main(void) { int A = 1; if (A==1) printf("A equals 1\n"); } if (A == 2) printf("A equals 2\n"); else printf("A is not 2\n"); return 0; #include <stdio.h> int main(void) { int A = 1; if (A==1) printf("A equals 1\n"); } else if (A == 2) printf("A equals 2\n"); else printf("A is not 1 or 2\n"); return 0; Output: A equals 1 A is not 2 Are these statements equivalent? Output: A equals 1 NO !

Some Arithmetic Operators KNOW ALL OF THESE – INCLUDING THE MOD (REMAINDER) FUNCTION, %

#include <stdio.h> int main(void) { int num1 = 10; int num2 = 20; if (num1 >= num2) { num1 = num1%num1; } else { num1 = num1%num2+num1*num1; } return 0; 1. num1 = 110 2. num1 = 120 3. num1 = 11 4. num1 = 300

What do these flowcharts denote? conditions conditions True True Actions Actions False False Left = if statement Right = while loop 2. Both while loops 3. Left = while loop Right = if statement 4. Both if statement

What does this flowchart denote? conditions False True Actions Actions While loop 2. For loop 3. if-else 4. case-switch

What type of while loop for this moving rock example? initialize: numrocks = 0 numrocks < 100 True Move a rock: numrocks = numrocks+1 False Counter-controlled 2. Sentinel controlled 3. Conditional while 4. It is an if statement

What type of while loop for this moving rock example? Rock is not smooth Move a rock Ask rock mover if the next rock is smooth True False Counter-controlled 2. Sentinel controlled 3. Conditional while 4. It is an if statement

WHILE LOOPS AND FOR LOOPS

1. i= 0, i*j= 0 2. i= 0, i*j= 0 3. i= 0, i*j= 0 4. i= 0, i*j= 0 #include <stdio.h> main(void) { int i = 0; int j = 0; /* print out i and j as the while loop loops */ while(i<4) { j = 0; while(j<4){ printf("i= %d, i*j= %d\n", i, i*j); j = j + 2; } i = i + 2; 1. i= 0, i*j= 0 i= 0, i*j= 2 i= 2, i*j= 0 i= 2, i*j= 4 2. i= 0, i*j= 0 i= 2, i*j= 0 i= 2, i*j= 2 3. i= 0, i*j= 0 i= 0, i*j= 2 i= 2, i*j= 0 i= 2, i*j= 2 4. i= 0, i*j= 0 i= 0, i*j= 0 i= 2, i*j= 0 i= 2, i*j= 4

General for loop Flowchart/Syntax counter initialized conditions True statements counter in(de)cremented False for (initialization; conditions; increment/decrement) { statements; } NOTE: It is best to have the counter be an integer and not a float

1. x = 16 2. x = 8 3. x = 4 4. x = 32 int main(void) { int x = 2, xinitial = 2; int p = 4; int i = 1; for (i = 1; i<=p; i=i+2) x = x*xinitial; } return 0; 1. x = 16 2. x = 8 3. x = 4 4. x = 32

ARRAYS

1. num=2.00 num=2.00 num=2.50 num=2.25 num=4.00 2. num=2.00 num=1.00 #include <stdio.h> int main() { float num[5]; int i; for (i=0;i<5;i++) num[i] = 0.; } num[0] = 2.; num[4] = 4.; for(i=1;i<4;i++) num[i] = (num[i+1]+num[i-1])/2.; for(i=0;i<5;i++) printf("num=%.2f\n", num[i]); 1. num=2.00 num=2.00 num=2.50 num=2.25 num=4.00 2. num=2.00 num=1.00 num=0.50 num=2.25 num=4.00 3. num=2.00 num=1.00 num=1.50 num=2.25 num=0.00 4. num=0.00 num=1.00 num=0.50 num=2.25 num=4.00

Functions Some notes: Suppose we have a function, myFunction, that returns an integer and takes in as inputs a double array called square and an integer called arraylength has the following prototype: int myFunction (double square[], int arraylength); 2. When this function is called from main() (or from another function), the call statement does not include the ‘[]’ for the array nor does it include the data types (i.e. double and int), i.e.: returnvalue = myFunction(square, arraylength); 3. The function definition has the following syntax, in this case returns as int: int myFunction (double square[], int arraylength) { perform actions; return somevalue; } 4. Also note that the variables declared in a function are local variables.

Passing arrays to functions Some notes: Whole arrays are passed by reference – which means that when they are changed in the function, that change is also seen in main() – or in the function from which it was called Related to #1 above, the address of the first element of an array is passed to a function 3. Array elements are passed by value 4. Typically the size of the array (unless it is a character array with a terminating character, i.e. string) is passed to the function because functions do not know what the size of an array is

What will this print out? #include <stdio.h> void change(int array[], int val); int main(void) { int val = 3; int array[1] = {3}; change(array, val); printf(“array=%d, val=%d”,array[0],val); } void change(int array[], int val) int i; array[0] = array[0]*2; val = val * 2; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6

What will this print out? #include <stdio.h> int change(int array[], int val); int main(void) { int val = 3; int array[1] = {3}; val = change(array, val); printf("array=%d, val=%d",array[0],val); } int change(int array2[], int val) array2[0] = array2[0]*2; val = val * 2; return val; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6

In the previous example, array was passed by ____1______, while val was are passed by ___2_____? 1. reference, value 2. value, reference 4. Oh shoot, don’t know, I’d better ask Garvin’s other half as she is the one who knows everything! 3. reference, reference

What will this print out? #include <stdio.h> void change(int array, int val); int main(void) { int val = 3; int array[1] = {3}; change(array[0], val); printf(“array=%d, val=%d”,array[0],val); } void change(int not_an_array, int val) not_an_array = not_an_array*2; val = val * 2; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6 Notice that the function no longer takes in a whole array, but an array element – which is passed by value

What will this print out? #include <stdio.h> int change(int array[], int val); int main(void) { int val = 3; int array[1] = {3}; val = change(array, val); printf("array=%d, val=%d",array[0],val); } int change(int array2[], int val) array2[0] = array2[0]*2; val = val * 2; return val; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6

What does this print out? #include <stdio.h> int myFunction(int x[], int num);   int main(void){ int result=0; int array[3] = {5, 2, -3}; result = myFunction(array,3); printf(“%d, %d\n”, array[1], array[2]); } int myFunction(int x[], int num){ x[1] = 4; x[2] = 7; return(x[1]+x[2]); a) 5, 2 b) 5, 11 c) 4, 7 d) 2, 11

9 8 ‘C’ STRINGS: A character array with a terminating null character ‘\0’ Various ways of defining a character array: char chararray[]=“biscuits”; char chararray[] = {‘b’,’i’,’s’,’c’,’u’,’i’,’t’,’s’,’\0’}; NOTE, this does not work: char chararray[9]; chararray = “biscuits”; How many elements are in chararray? What does strlen(chararray) return? What is chararray[3]?

MORE ON STRINGS: Know what strcpy does. char theMan[30]; strcpy(theMan, “Dr. Garvin is the Man!"); Assigns character arrays.

Some more string stuff: Strings are character arrays (example): #include <stdio.h> #include <string.h> int main() { char sentence[80]; /* large array length */ int i; sentence[0] = 'B'; sentence[1] = 'y'; sentence[2] = 'e'; sentence[3] = '\0'; printf("%s", sentence); /* prints Bye */ for (i = 0; i < strlen(sentence); i++) printf("%c", sentence[i]); /* prints Bye */ } Caution when using scanf with strings: scanf(“%s”, string) /* no & is needed w/ %s*/ scanf(“%c”, &char) /* & is needed with %c */

POINTERS, PASS BY REFERENCE and PASS BY VALUE

Let’s review pass by value: #include <stdio.h> void doSomething(int a, int b); int main() { int a = 1, b =0; doSomething(a,b); printf(“a=%d, b=%d ", a, b); } void doSomething(int a, int b) a = 7; b = 8; Is this going to print: 1. a=7, b=8 2. a=1, b=0

What if we wanted main to print out the modified values of a and b? Need to know each memory address! Need to use pointers!

What is the output now?: #include <stdio.h> void doSomething(int *a, int *b); int main() { int a = 1, b =0; doSomething(&a,&b); printf(“a=%d, b=%d ", a, b); } void doSomething(int *a, int *b) *a = 7; *b = 8; Is this going to print: 1. a=7, b=8 2. a=1, b=0

A little more on pointers Declaring a pointer needs a *: float * xPtr; This means xPtr should store an address If xPtr gets changed the address gets changed However, if *xPtr gets changed, that means xPtr is now “pointing” to that value Passing the address (i.e. pass by reference) of a regular variable needs an & in front of it (like scanf). int variable = 7; /* stores a value */ printf(“%p”,&variable); /* prints out address */

What does this print? void mystery(int * x); main(){ int a = 5;   main(){ int a = 5; mystery(&a); printf("%d\n",a); } void mystery(int * x){ int y; y = 2 * *x; *x = 3; a) 5 b) 10 c) 6 d) 3

Assume the variable x is stored at memory location 1000 and y is stored at memory location 1004. What are the values of x, y, and *y after executing the following block of C code? int x = 5; int *y;   y = &x; x = 7; a) x = 7, y = 1004, *y = 7 b) x = 7, y = 1000, *y = 5 c) x = 7, y = 1000, *y = 7 d) x = 7, y = 5, *y = 5

(FILE I/O or FILE INPUT/OUTPUT) C File Processing (FILE I/O or FILE INPUT/OUTPUT)

Opening a File Define a FILE pointer called rfPtr; FILE *rfPtr; The fopen function links a FILE pointer to the file to read: rfPtr = fopen(“sData.txt”, “r”); Or write: rfPtr = fopen(“sData.txt”, “w”); fopen Takes two arguments Filename -- file to open (“sData.txt”) File open mode -- “r” or “w” – there are others as well If open fails, NULL returned (i.e. there is nothing there)