/* * programming_arrays_fns.c * * Created on: Apr 14, 2023 * Author: johnsontimoj */ ////////////////////////////////// // // practice with arrays and functions // // inputs - user // outputs - prints // ////////////////////////// #include void splash(void); void print_1(const int the_val); void print_ary(int n, const int the_ary[]); void print_ary_ptrs(int n, const int * the_ary); void load_ary(int n, int an_ary[]); void load_ary_ptrs(int n, int * an_ary); void copy_ary(int n, const int src_ary[], int dest_ary[]); void copy_ary_ptrs(int n, const int * src_ary, int * dest_ary); void swap_ary(int n, int the_ary[]); void swap_ary_ptrs(int n, int * the_ary); int sum_ary(int n, int the_ary[]); int sum_ary_ptrs(int n, int * the_ary); int main(void){ setbuf(stdout, NULL); // fix buffer problem // splash splash(); // declare and declare/initialize an array int ary1[10] = {5,7,9,1,3,6,8,2,4,0}; int ary2[10]; int ary_sum; // print 1 value printf("Print 1 value from array\n"); print_1(ary1[3]); print_1(*(ary1 + 3)); printf("also 1 value from array: %i\n", *(ary1 + 3)); // print initialized array printf("print the initialized array\n"); print_ary(10, ary1); printf("\n\n"); // print initialized array - ptrs printf("print the initialized array - ptrs\n"); print_ary_ptrs(10, ary1); printf("\n\n"); // load values into an array printf("Loading values into an array and printing\n"); load_ary(10, ary1); print_ary(10, ary1); printf("\n\n"); // load values into an array - ptrs printf("Loading values into an array and printing - ptrs\n"); load_ary_ptrs(10, ary1); print_ary_ptrs(10, ary1); printf("\n\n"); // copy the array printf("copy one array into another, then print both\n"); copy_ary(10, ary1, ary2); print_ary(10, ary1); print_ary(10, ary2); printf("\n\n"); // copy the array - ptrs printf("copy one array into another, then print both - ptrs\n"); copy_ary_ptrs(10, ary1, ary2); print_ary_ptrs(10, ary1); print_ary_ptrs(10, ary2); printf("\n\n"); // do swap on an array printf("swap the order of values in an array\n"); swap_ary(10, ary1); print_ary(10, ary1); // do swap on an array - ptrs printf("swap the order of values in an array - ptrs\n"); swap_ary_ptrs(10, ary1); print_ary_ptrs(10, ary1); // sum the ary ary_sum = sum_ary(10, ary1); printf("The sum of the array elements is: %i\n", ary_sum); // sum the ary - ptrs ary_sum = sum_ary_ptrs(10, ary1); printf("The sum of the array elements is: %i\n", ary_sum); return 0; }// end main ///////////////////////// // splash() // // display a message // // inputs - none // outputs - printed message // ///////////////////////// void splash(void){ printf("/////////////////////\n"); printf("//\tprogramming_arrays_fns.c\n"); printf("//\tDr. Johnson\n"); printf("//\t 1-d Arrays in functions\n"); printf("/////////////////////\n"); printf("\n"); return; }// end splash ///////////////////////// // print_1() // // print 1 value in an array // // inputs - 1 value from an array // outputs - printed value // ///////////////////////// void print_1(const int the_val){ // use const to indicate it is not your intention to modify the array printf("%i ", the_val); printf("\n"); return; }// end print_1 ///////////////////////// // print_ary() // // print values in an array // // inputs - array passed by reference (pointer), number of elements // outputs - printed values // ///////////////////////// void print_ary(int n, const int the_ary[]){ // use const to indicate it is not your intention to modify the array int i; for(i = 0; i < n; i++) printf("%i ", the_ary[i]); printf("\n"); return; }// end print_ary ///////////////////////// // print_ary_ptrs() // // print values in an array // // inputs - array passed by reference (pointer), number of elements // outputs - printed values // ///////////////////////// void print_ary_ptrs(int n, const int * the_ary){ // use const to indicate it is not your intention to modify the array int i; for(i = 0; i < n; i++) printf("%i ", *(the_ary + i)); printf("\n"); return; }// end print_ary_ptrs ///////////////////////// // load_ary() // // load values into an array - asking for each // // inputs - array passed by reference (pointer), number of elements // outputs - none - array is modified by reference (pointer) // ///////////////////////// void load_ary(int n, int an_ary[]){ int i; for(i = 0; i < n; i++){ printf("enter value %i for the array: ", i); scanf("%i", &an_ary[i]); } return; }// end load_ary ///////////////////////// // load_ary_ptrs() // // load values into an array - asking for each // // inputs - array passed by reference (pointer), number of elements // outputs - none - array is modified by reference (pointer) // ///////////////////////// void load_ary_ptrs(int n, int * an_ary){ int i; for(i = 0; i < n; i++){ printf("enter value %i for the array: ", i); scanf("%i", (an_ary + i)); } return; }// end load_ary_ptrs ///////////////////////// // copy_ary() // // copy elements of one array to another array // // inputs - source and destination arrays passed by reference (pointer), number of elements // outputs - none - array is modified by reference (pointer) // ///////////////////////// void copy_ary(int n, const int src_ary[], int dest_ary[]){ int i; for(i = 0; i < n; i++){ dest_ary[i] = src_ary[i]; } return; }// end copy_ary ///////////////////////// // copy_ary_ptrs() // // copy elements of one array to another array // // inputs - source and destination arrays passed by reference (pointer), number of elements // outputs - none - array is modified by reference (pointer) // ///////////////////////// void copy_ary_ptrs(int n, const int * src_ary, int * dest_ary){ int i; for(i = 0; i < n; i++){ *(dest_ary + i) = *(src_ary + i); } return; }// end copy_ary_ptrs ///////////////////////// // swap_ary() // // reverse the order of elements in an array // // inputs - array passed by reference (pointer), number of elements // outputs - none - array is modified by reference (pointer) // ///////////////////////// void swap_ary(int n, int the_ary[]){ int i; int tmp_ary[n]; copy_ary(n, the_ary, tmp_ary); for(i = 0; i < n; i++) the_ary[i] = tmp_ary[(n - 1) - i]; return; }// end swap_ary ///////////////////////// // swap_ary_ptrs() // // reverse the order of elements in an array // // inputs - array passed by reference (pointer), number of elements // outputs - none - array is modified by reference (pointer) // ///////////////////////// void swap_ary_ptrs(int n, int * the_ary){ int i; int tmp_ary[n]; copy_ary(n, the_ary, tmp_ary); for(i = 0; i < n; i++) *(the_ary + i) = *(tmp_ary + (n - 1) - i); return; }// end swap_ary_ptrs ///////////////////////// // sum_ary() // // add the values in an array // // inputs - array passed by reference (pointer), number of elements // outputs - sum of the values // ///////////////////////// int sum_ary(int n, int the_ary[]){ int i; int sum; sum = 0; for(i = 0; i < n; i++) sum = sum + the_ary[i]; return sum; }// end sum_ary ///////////////////////// // sum_ary_ptrs() // // add the values in an array - ptr notation // // inputs - array passed by reference (pointer), number of elements // outputs - sum of the values // ///////////////////////// int sum_ary_ptrs(int n, int * the_ary){ int i; int sum; sum = 0; for(i = 0; i < n; i++) sum = sum + *(the_ary + i); return sum; }// end sum_ary_ptrs