11}12printf( "sum of all elements in a is %d\n",13sumElements ( a, size ) );14printf( "sum of first 5 elements in a is %d\n",15sumElements ( a, 5 ) );16printf( "sum of last 5 elements in a is %d\n",17sumElements ( a+5, 5 ) );18return0;19}2021// addssizemanyelementsofarraya22// assumption :thesizeofthearrayisatleastequaltosize23intsumElements (int*a,intsize ){24intsum = 0;25inti;26for(i = 0; i < size; i++ ) {27sum = sum + a[i];28}29returnsum;30}Seesum.candsum2.cfor this code and its version that passes int a[] as a parameter to the function.Something to Think About:DNHI: Modify the code above to check your answers to the following questions:1) What happens ifsumElements()function modifies the content of arraya? Do changes affect the array declared in2) What happens if thesizeparamter passed to thesumElements()function exceeds the size of the declared arraya? Try it by
CSCI-UA 201Lecture 2: Introduction to C Programming LanguageJoanna Klukowska[email protected]chartmp;//1byteofstorageinti;for(i = 0; i < size; i++) {//foreverybyte :swapthebytetmp = x[i];x[i] = y[i];y[i] = tmp;}}Seeswapgeneric.cfor an example of use of this function.5Strings in CTwo strings walk into a bar and sit down. The bartender says, ”So what’ll it be?”The first string says, ”I think I’ll have a beer quag fulk boorg jdkˆCjfdLk jk3s d#f67howe˜owmc63ˆDz x.xvcu””Please excuse my friend,” the second string says, ”He isn’t null-terminated.”A string in C is an array of characters terminated by a null character (\0). Every function that performs operations on strings usesthat null character. A missing null character in a string is also the source of most errors (and jokes) related to strings in C.Whenever we have a double quoted string in a program it is stored as a string constant terminated by a null character. For example:printf("Hello World!\n");Writingcharh[] = "hello";creates and array of 6 characters (yes, 5 letters + a null character). It is equivalent to writingcharh[6]= "hello";Note that the following lines will compile as well and sometimes even run without obvious problems (at first)char