CECS 121 Final Test. Function Prototype Syntax return-type function_name ( arg_type arg1,...,...

download CECS 121 Final Test.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data.

If you can't read please download the document

description

 Function Prototypes are placed after preprocessor directives and before the main(){} function.  Function Definitions are self contained outside of the main(){} function

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

CECS 121 Final Test 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 are placed after preprocessor directives and before the main(){} function. Function Definitions are self contained outside of the main(){} function #include int mult ( int, int ); main() { int x,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(); // To pause the screen, same like system(pause) } int mult (int x, int y) // Make sure not to end this with a semicolon { return(x * y); } #include void print_char( char *, int); main() { char name[20]=REACH CRC; print_char(name, 3); // The name of the array is a pointer to that array getchar(); } void print_char (char *nameStringArray, int x) { printf(The character specified is: %c, nameStringArray[x-1]); } #include void printReportHeader(); main() { printReportHeader; } void printReportHeader() { printf(\n Column1\tColumn2\tColumn3\tColumn4 \n); } #include void printReportHeader(); main() { printReportHeader; // Should be corrected to printReportHeader() } void printReportHeader() { printf(\n Column1\tColumn2\tColumn3\tColumn4 \n); } 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 #include void printNumbers(); int iNumber; // This is a global variable reachable from any place in code main() { int x; for(x=0, x A example[3] -> C example[4] -> H example[5] -> \0 #include 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 testing The sentence entered is 12 characters long. #include 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() #include 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 string str2: Sample string str3: copy successful #include 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. #include 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? Apple Correct answer! /*This example searches for the "simple" substring in str and replaces that word for "sample".*/ #include 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 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. #include int main(){ int array[10]={7,8,5,3,0,3,2,1,4,10}; int temp=0,i,j; for(i=0;i