Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Review of C… The basics of C scanf/printf if/elseif statements"— Presentation transcript:

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

2 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;

3 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 !

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

5 #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

6 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

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

8 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

9 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

10 WHILE LOOPS AND FOR LOOPS

11 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

12 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

13 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

14 ARRAYS

15 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

16 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.

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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]?

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

26 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 */

27 POINTERS, PASS BY REFERENCE and PASS BY VALUE

28 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

29 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!

30 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

31 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 */

32 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

33 Assume the variable x is stored at memory location 1000 and y is stored at memory location 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

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

35 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)


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

Similar presentations


Ads by Google