CECS 121 EXAM 2. Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type...

41
CECS 121 EXAM 2

Transcript of CECS 121 EXAM 2. Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type...

Page 1: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

CECS 121 EXAM 2

Page 2: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

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?

Page 3: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Function Prototypes are placed after preprocessor directives and before the main(){} function.

Function Definitions are self contained outside of the main(){} function

Page 4: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h> int mult ( int x, int y );

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 x, int y) { return(x * y); }

Page 5: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h>void print_char( char *, int);

main() { char name[20]=“REACH CRC”;

print_char(name, 3);getchar();

}

void print_char (char *name, int x) { printf(“The character specified is: %c”, name[x-1]); }

Page 6: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h> void printReportHeader();

main(){

printReportHeader;}

void printReportHeader(){

printf(“\n Column1\tColumn2\tColumn3\tColumn4 \n”)}

Page 7: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

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

Page 8: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h>

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);}

Page 9: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Can you declare a one-dimensional array made up of 10 integers? data_type name[size_of_array] Answer: int iArray[10];

How to declare an Array int iArray[10]; float fAverages[30]; char cName[19]; 18 characters and 1 null

character

Page 10: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

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

Page 11: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h>

main(){

int x;int iArray[5];

for( x=0; x < 5 ; x++) { iArray[x] = 0;}

}

Page 12: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Can you add code that will print out the value of each element of iArray?

#include <stdio.h> main(){

int x;int iArray[5];

for( x=0; x < 5 ; x++) { iArray[x] = 0;}

}

Page 13: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#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 The value of iArray index %d is %d \n”, x,

iArray[x]);}

}

Page 14: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

How do you search through an array?

Page 15: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h> main(){

int x;int iValue;int iFound = -1;int iArray[5];

for( x=0; x < 5 ; x++) iArray[x] = (x+x);printf(“\n Enter value to search for:”);scanf(“%d”, &iValue);

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”);

}

Page 16: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Declaring: data_type name[size_dim_1]

[size_dim_2]▪ size_dim_1 is known as “ROW” count.▪ size_dim_2 is known as “COLUMN” count.

int double_array[20][10]Accessing:

printf(“Element 2,5 is: %d”, double_array[2][5]);

Page 17: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Initializing: Use a second, nested FOR() loop#include <stdio.h>

main(){

int x, y, double_array[10][20];for( x=0; x < 10 ; x++) // Outer loop goes with ROW

count{ for(y=0 ; y<20; y++) // Inner loop goes with

Column count { double_array[x][y] = 0; }

}}

Page 18: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Passing to a function:#include <stdio.h>#include<stdlib.h>void custom_func(int [][2]);// Multi dimensional arrays must have a bound main(){

int double_array[2][2]={{1,2},{3,4}};custom_func (double_array);system(“pause”);

}void custom_func (int temp[][2]){ printf(“Test: %d, %d, %d”, temp[0][0],temp[0][1],temp[1][1]);}OUTPUT: Test: 1, 2, 4

Page 19: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Lect 14 P. 19

Pointers are variables that contain memory addresses as their values.

A variable name directly references a value.

A pointer indirectly references a value. Referencing a value through a pointer is called indirection.

A pointer variable must be declared before it can be used.

ALL Arrays are Pointers!

Page 20: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Lect 14 P. 20

Examples of pointer declarations:FILE *fptr;int *a;float *b;char *c;

The asterisk, when used as above in the declaration, tells the compiler that the variable is to be a pointer, and the type of data that the pointer points to, but NOT the name of the variable pointed to.

Page 21: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Lect 14 P. 21 Winter Quarter

Consider the statements:#include <stdio.h>int main ( ){FILE *fptr1 , *fptr2 ; /* Declare two file pointers

*/int *aptr ; /* Declare a pointer to an int

*/float *bptr ; /* Declare a pointer to a float

*/int a ; /* Declare an int variable */float b ; /* Declare a float variable */return 0;}

Page 22: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Lect 14 P. 22 Winter Quarter

When is & used?

When is * used?

& -- "address operator" which gives or produces the memory address of a data variable

* -- "dereferencing operator" which provides the contents in the memory location specified by a pointer

Page 23: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Lect 14 P. 23 Winter Quarter

If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables.

The following shows a swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returned to main function.

Page 24: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Unary operator (&) – “Address of”int x=10;int *xptr;xptr = &x; //xptr now points to x

Indirection operator (*)int x, y = 10;int *xptr;xptr = &y;x = *xptr //copies contents of y

into x

Page 25: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Lect 14 P. 25

#include <stdio.h>void swap ( int *, int *) ;int main ( ){ int a = 5, b = 6; printf("a=%d b=%d\

n",a,b) ; swap (&a, &b) ; printf("a=%d b=%d\

n",a,b) ; return 0 ;}

void swap( int *a, int *b ){ int temp; temp= *a; *a= *b; *b =

temp ; printf ("a=%d b=%d\n", *a,

*b);}Output:a=5 b=6 //printed from maina=6 b=5 //printed from inside

swapa=6 b=5 // printed from main

after calling swap function

Page 26: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

To avoid accidently changing the value the pointer points to: Use const

void custom(const int *);main(){}void custom(const int *){

}

Page 27: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Strings are character arrays that have a special set of functions for handling their data as complete sentences or “strings” of characters.

Since a string is an array it is also a pointer.

Character literals are expressed with a single quote: char example=‘a’;

String literals are expressed with double quote:char example[10]=“REACH”;

Page 28: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

When determining the maximum length your string variable needs to be it is important to consider a NULL Character: “\0”

char example[10]=“REACH”;example[0] -> Rexample[1] -> Eexample[2] -> Aexample[3] -> Cexample[4] -> Hexample[5] -> \0

Page 29: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Page 30: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h>#include<stdlib.h>#include <string.h>

int main (){ char szInput[256]; printf ("Enter a sentence: "); gets (szInput); printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));System(“pause”); return 0;}

Output:Enter sentence: just testingThe sentence entered is 12 characters long.

Page 31: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h>#include <ctype.h>int main (){ int i=0; char str[]="Test String.\n"; char c; while (str[i]) { c=str[i]; putchar (tolower(c)); i++; } return 0;}

Output:

test string.

For toupper() will be same case, just replace tolower() by toupper()

Page 32: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h>#include <string.h>

int main (){ char str1[]="Sample string"; char str2[40]; char str3[40]; strcpy (str2,str1); strcpy (str3,"copy successful"); printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3); return 0;}

Output:

str1: Sample stringstr2: Sample stringstr3: copy successful

Page 33: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h>#include <string.h>

int main (){ char str[80]; strcpy (str,"these "); strcat (str,"strings "); strcat (str,"are "); strcat (str,"concatenated."); puts (str); return 0;}

Output:these strings are concatenated.

Page 34: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include <stdio.h>#include <string.h>

int main (){ char szKey[] = "apple"; char szInput[80]; do { printf ("Guess my favourite fruit? "); gets (szInput); } while (strcmp (szKey,szInput) != 0); puts ("Correct answer!"); return 0;}

Output:Guess my favourite fruit? orange Guess my favourite fruit? AppleCorrect answer!

Page 35: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

/*This example searches for the "simple" substring in str and replaces that word for "sample".*/#include <stdio.h>#include <string.h>

int main (){ char str[] ="This is a simple string"; char * pch; pch = strstr (str,"simple") ; /* returns a pointer to first occurrence of “simple” in str*/ strncpy (pch,"sample",6); // copies 6 characters from source->pch puts (str); return 0;}

Output:This is a sample string

Page 36: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Can you make a program to sort an array of 10 integers either ascending or descending order?Consider you have the array[10]={7,8,5,3,0,3,2,1,4,10}Write a code to do the sorting.

Page 37: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include<iostream.h>

int main(){int array[10]={7,8,5,3,0,3,2,1,4,10};int temp=0,i,j; for(i=0;i<10;i++) // print the array before sorting cout<<array[i]<<" "; cout<<endl; for(i=0;i<10;i++) for(j=0;j<10;j++) { if(array[i]<array[j]) // This will do Ascending order, if you need Descending order just flip to “>” { temp=array[i]; // This will do the swapping array[i]=array[j]; array[j]=temp; } } for(i=0;i<10;i++) // print the array after sorting cout<<array[i]<<" "; getchar(); return 0;}

Page 38: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Can you modify the previous code to pass the array to a function that will sort the array and print the sorted array in main.

Page 39: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

#include<iostream.h>

void sortArray(int [],int size); int array[10]={7,8,5,3,0,3,2,1,4,10};int temp=0,i,j;

int main(){ for(i=0;i<10;i++) cout<<array[i]<<" "; cout<<endl; sortArray(array,10); for(i=0;i<10;i++) cout<<array[i]<<" "; getchar(); return 0;}

void sortArray(int passedArray[],int arraySize){ for(i=0;i<arraySize;i++) for(j=0;j<arraySize;j++) { if(passedArray[i]<passedArray[j]) { temp=passedArray[i]; passedArray[i]=passedArray[j]; passedArray[j]=temp; } } }

Calling the function sortArray(array,10) is equivalent to sortArray(&array[0],10)

Page 40: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

Good Luck from REACH in your Test.

Page 41: CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.

TEXTBOOK RESOURCE: C Programming for the Absolute Beginner 2nd Edition by Michael Vine

www.cprogramming.com