Download presentation
Presentation is loading. Please wait.
Published byJanel Craig Modified over 8 years ago
1
CS 108 Notes for March 1, 2016
2
#include //My file 1.c int main (void) { char c = 'z' ; char c_array [10] ; int i = 666 ; int i_array [10] ; float f = 3.1415 ; float f_array [10] ; double d = 3.1415 ; double d_array [10] ; printf ("\n Size of char variable c = %d byte.", sizeof(c) ) ; printf ("\n Size of int variable i = %d bytes. ", sizeof(i) ) ; printf ("\n Size of float variable f = %d bytes.", sizeof(f) ) ; printf ("\n Size of double variable d = %d bytes.", sizeof(d) ) ; printf ("\n Size of c_array (10 char elements) = %d bytes.", sizeof(c_array) ) ; printf ("\n Size of i_array (10 int elements) = %d bytes.", sizeof(i_array) ) ; printf ("\n Size of f_array (10 float elements) = %d bytes.", sizeof(f_array) ) ; printf ("\n Size of d_array (10 double elements) = %d bytes.", sizeof(d_array) ) ; printf ("\n\n\n" ) ; return (0) ; } Size of Data Types and Arrays
3
#include // My file 2.c int main (void) { int index = 0 ; char c = 'z' ; char c_array [10] ; int i = 666 ; int i_array [10] ; float f = 3.1415 ; float f_array [10] ; double d = 3.1415 ; double d_array [10] ; printf ("\nMemory location of char variable c = %p ", &c ) ; printf ("\nMemory location of int variable i = %p ", &i ) ; printf ("\nMemory location of float variable f = %p ", &f ) ; printf ("\nMemory location of double variable d = %p \n\n\n", &d ) ; for (index = 0 ; index < 10 ; index++) printf ("\nMemory location of c_array [ %d ] = %p ", index, &c_array[index] ) ; printf ("\n\n") ; for (index = 0 ; index < 10 ; index++) printf ("\nMemory location of i_array [ %d ] = %p ", index, &i_array[index] ) ; printf ("\n\n\n") ; return (0) ; } Size and Addresses of Data Types and Arrays
4
The next slide is a graphic representation of the locations in memory of the single variables and some of the arrays found in program 2.c on the previous slide Each "slot" on the next slide represents a byte of memory (bytes are addressable) The addresses listed are just the last four hexadecimal values of the 12 hexadecimal values used to more easily present the 48-bit addressing used on Fang Notice the "space" required by different data types Size and Addresses of Data Types and Arrays
5
eafbVariable c eafa eaf9 eaf8 eaf7 eaf6 eaf5 eaf4Variable i eaf3 eaf2 eaf1 eaf0Variable f eaef eaee eaed eaec eaeb eaea eae9 eae8Variable d eae7c_array[9] eae6c_array[8] eae5c_array[7] eae4c_array[6] eae3c_array[5] eae2c_array[4] eae1c_array[3] eae0c_array[2] eadfc_array[1] eadec_array[0] eadd eadc eadb eada ead9 ead8i_array[9] ead7 ead6 ead5 ead4i_array[8] ead3 ead2 ead1 ead0i_array[7] eacf eace eacd eacci_array[6] eacb eaca eac9 eac8i_array[5] eac7 eac6 eac5 eac4i_array[4] eac3 eac2 eac1 eac0i_array[3] eabf eabe eabd
6
Sorting Arrays Many times we will want all the info in an array sorted (which mean ordered) A variety of ways to do this… let's look at accomplishing this as a series of simple tasks. "Chalk talk"
7
Bubble Sort Adjacent pairs of elements in an array are compared and put into proper order. The first two elements are compared and exchanged if the second is smaller of the two (they are put in order). The value of the next adjacent pair of elements compared and exchanged if necessary (again, put in order). The sequence is repeated until the last two elements of the array have been compared. This sequence of comparisons happens n-1 times for n elements… each sequence of comparisons is called a pass.
8
Bubble Sort This: 8 4 7 2 3 (1 st Pass) First pair-wise comparison: 8 4 7 2 3 Result of first pair-wise comparison: 4 8 7 2 3 Second pair-wise comparison: 4 8 7 2 3 Result of second pair-wise comparison: 4 7 8 2 3 Third pair-wise comparison: 4 7 8 2 3 Result of third pair-wise comparison: 4 7 2 8 3 Fourth pair-wise comparison: 4 7 2 8 3 Result of fourth pair-wise comparison: 4 7 2 3 8 First pass is now complete… on to pass #2 Question: how would we accomplish this swap?
9
Bubble Sort This: 8 4 7 2 3 (2 nd Pass) First pair-wise comparison: 4 7 2 3 8 Result of first pair-wise comparison: 4 7 2 3 8 Second pair-wise comparison: 4 7 2 3 8 Result of second pair-wise comparison: 4 2 7 3 8 Third pair-wise comparison: 4 2 7 3 8 Result of third pair-wise comparison: 4 2 3 7 8 Fourth pair-wise comparison: 4 2 3 7 8 Result of fourth pair-wise comparison: 4 2 3 7 8 Second pass is now complete… on to pass #3
10
Bubble Sort This: 8 4 7 2 3 (3 rd Pass) First pair-wise comparison: 4 2 3 7 8 Result of first pair-wise comparison: 2 4 3 7 8 Second pair-wise comparison: 2 4 3 7 8 Result of second pair-wise comparison: 2 3 4 7 8 Third pair-wise comparison: 2 3 4 7 8 Result of third pair-wise comparison: 2 3 4 7 8 Fourth pair-wise comparison: 2 3 4 7 8 Result of fourth pair-wise comparison: 2 3 4 7 8 Third pass is now complete… on to pass #4
11
Bubble Sort This: 8 4 7 2 3 (4 th Pass) First pair-wise comparison: 2 3 4 7 8 Result of first pair-wise comparison: 2 3 4 7 8 Second pair-wise comparison: 2 3 4 7 8 Result of second pair-wise comparison: 2 3 4 7 8 Third pair-wise comparison: 2 3 4 7 8 Result of third pair-wise comparison: 2 3 4 7 8 Fourth pair-wise comparison: 2 3 4 7 8 Result of fourth pair-wise comparison: 2 3 4 7 8 Fourth pass is now complete… FINISHED!!
12
Bubble Sort: Completed - Observations First pair-wise comparison: 2 3 4 7 8 Result of first pair-wise comparison: 2 3 4 7 8 Second pair-wise comparison: 2 3 4 7 8 Result of second pair-wise comparison: 2 3 4 7 8 Third pair-wise comparison: 2 3 4 7 8 Result of third pair-wise comparison: 2 3 4 7 8 Fourth pair-wise comparison: 2 3 4 7 8 Result of fourth pair-wise comparison: 2 3 4 7 8 Notice: 5 elements required 4 passes… in general, there must be “size – 1” passes
13
Bubble Sort: Completed - Observations First pair-wise comparison: 2 3 4 7 8 Result of first pair-wise comparison: 2 3 4 7 8 Second pair-wise comparison: 2 3 4 7 8 Result of second pair-wise comparison: 2 3 4 7 8 Third pair-wise comparison: 2 3 4 7 8 Result of third pair-wise comparison: 2 3 4 7 8 Fourth pair-wise comparison: 2 3 4 7 8 Result of fourth pair-wise comparison: 2 3 4 7 8 Elements 0 and 1 Elements 1 and 2 Elements 2 and 3 Elements 3 and 4
14
Bubble Sort: Completed - Observations First pair-wise comparison: 2 3 4 7 8 Result of first pair-wise comparison: 2 3 4 7 8 Second pair-wise comparison: 2 3 4 7 8 Result of second pair-wise comparison: 2 3 4 7 8 Third pair-wise comparison: 2 3 4 7 8 Result of third pair-wise comparison: 2 3 4 7 8 Fourth pair-wise comparison: 2 3 4 7 8 Result of fourth pair-wise comparison: 2 3 4 7 8 Pairwise comparisons: “left element” compared to “right element” or “first element” compared to “first + 1 element” Elements 0 and 1 Elements 1 and 2 Elements 2 and 3 Elements 3 and 4
15
Bubble Sort: Completed - Observations First pair-wise comparison: 2 3 4 7 8 Result of first pair-wise comparison: 2 3 4 7 8 Second pair-wise comparison: 2 3 4 7 8 Result of second pair-wise comparison: 2 3 4 7 8 Third pair-wise comparison: 2 3 4 7 8 Result of third pair-wise comparison: 2 3 4 7 8 Fourth pair-wise comparison: 2 3 4 7 8 Result of fourth pair-wise comparison: 2 3 4 7 8 Pairwise comparisons: “first element” incremented by 1 therefore “first element + 1” also incremented by 1 Elements 0 and 1 Elements 1 and 2 Elements 2 and 3 Elements 3 and 4
16
Bubble Sort: Completed - Observations First pair-wise comparison: 2 3 4 7 8 Result of first pair-wise comparison: 2 3 4 7 8 Second pair-wise comparison: 2 3 4 7 8 Result of second pair-wise comparison: 2 3 4 7 8 Third pair-wise comparison: 2 3 4 7 8 Result of third pair-wise comparison: 2 3 4 7 8 Fourth pair-wise comparison: 2 3 4 7 8 Result of fourth pair-wise comparison: 2 3 4 7 8 Pairwise comparisons: 5 elements and 4 comparisons… in general, there must be “size – 1” comprisons Elements 0 and 1 Elements 1 and 2 Elements 2 and 3 Elements 3 and 4
17
We need to keep track of two things to accomplish a bubble sort: –We need to keep track of the “passes” via an index starting with 1 and ending at index < size ) –We need to keep track of the “pairwise comparisons” Via another index starting with 0 and ending at index < size – 1) Therefore, we need to know the size of the array (number of elements) to accomplish “size – 1” passes and “size – 1” pairwise comparisons Bubble Sort: Completed - Observations
18
We’ll need to incorporate the 3 way switch –We’ll need a local variable to as a temporary holding space To track passes we need a pass counter that doesn’t increment until all the pairwise comparisons have been accomplished To track pairwise comparisons we’ll need a pairwise comparison index Pass counter is the outer loop, pairwise comparison index is the inner loop Bubble Sort: Completed - Observations
19
#include #define SIZE 5 int main ( void ) //3.c { //p_c: pass_counter… left_ele: left element int index = 0 ; int qwerty [ SIZE ] = { 8, 4, 7, 2, 3 } ; int temp = 0 ; //temporary variable used in 3-way exchange int p_c = 0; //counts the passes through the array (need n - 1) int left_ele = 0 ; // facilitates pairwise comparisons (need n – 1) for ( p_c = 1 ; p_c < SIZE ; p_c++ ) // need n - 1 passes { for ( left_ele = 0 ; left_ele < SIZE - 1 ; left_ele++ ) { // need n - 1 pairwise comparisons… starting at 0 if ( qwerty[ left_ele ] > qwerty [ left_ele + 1 ] ) { // 3-way exchange is accomplished, if necessary temp = qwerty[ left_ele ] ; qwerty[ left_ele ] = qwerty[ left_ele + 1 ] ; qwerty[ left_ele + 1 ] = temp ; } for ( index = 0 ; index < SIZE ; index++ ) printf( "\n%d\n", qwerty[ index ] ) ; return ; }
20
//p_c: pass_counter… left_ele: left element Initial element values/positions in qwerty[ ] : 8 4 7 2 3 SIZE : temp : p_c : left_ele : qwerty[ left_ele ] : qwerty[ left_ele + 1 ] : Bubble Sort Example
21
Selection Sort The selection sort process is pretty straightforward: find the unsorted position in a list with the smallest value and swap it with the value found in the first unsorted position in the list. Example, suppose we have the following list of integer values: 8 4 7 2 3 Step 1 Since 2 is the smallest unsorted item, and since there hasn’t been a swap yet, swap 2 with 8 because 8 is the first item on the list. This leaves: 2 4 7 8 3
22
Selection Sort Step 2 2 4 7 8 3 We now consider 2 to be “sorted.” Since 3 is the smallest unsorted item, and because 4 is the first unsorted item on the list, swap 3 with 4. This leaves: 2 3 7 8 4 Step 3 2 3 7 8 4 Now we consider 2 and 3 to be sorted. Since 4 is the smallest unsorted item, and because 7 is the first unsorted item on the list, swap 4 with 7. This leaves: 2 3 4 8 7
23
Selection Sort Step 4 2 3 4 8 7 We now consider 2, 3, and 4 to be sorted. Since 7 is the smallest unsorted item, and because 8 is the first unsorted item on the list, swap 8 with 7. This leaves: 2 3 4 7 8 (a sorted list) Replace “process” with “algorithm”, replace “list” with “array” and replace “item” with “array element” and you have the selection sort algorithm for an array The selection sort algorithm is pretty straightforward: find the unsorted array element with the smallest value and swap it with the value found in the first unsorted array element.
24
Selection Sort The following slides appear “busy” but that “busy- ness” is due to long identifier names and copious comments To truly understand the selection sort and bubble sort (or any algorithm), you must really study it and trace it until you master it Use the following slides on sorting to help you master these two sorting algorithms
25
void selection_sort (int passed_array[ ], int n) // n: size of the passed_array { int qwerty [ SIZE ] = { 8, 4, 7, 2, 3 } ; int pos_first; //position of the first unsorted element int pos_other ; //position of other unsorted elements int pos_min_value; //position of the minimum unsorted element int min_value; //value of minimum unsorted element int temp; //temporary variable necessary for 3-way exchange for (pos_first = 0; pos_first < SIZE ; pos_first++) { // increment of outer loop moves pos_first_unsorted_ele forward 1 element in array min_value = qwerty[ pos_first ]; pos_min_value = pos_first ; for ( pos_other = pos_first + 1; pos_other < SIZE ; pos_other++) { // inner for loop finds the lowest value (and it’s element position) to swap if qwerty[ pos_other ] < min_value ) { //testing to find the lowest value and it’s postion in the array min_value = qwerty[ pos_other ] ; pos_of_min_value = pos_other ; } temp = qwerty[ pos_first ] ; qwerty[ pos_first ] = min_value; qwerty[ pos_min_value] = temp; } // once the lowest value and it’s postion are found, it’s swapped (3-way } // exchange) with the value in the first unsorted position (three lines of code above)
26
#include #define SIZE 5 int main ( void ) //4.c { int qwerty [ SIZE ] = { 8, 4, 7, 2, 3 }, index = 0; int pos_first; //position of the first unsorted element int pos_other ; //position of other unsorted elements int pos_min_value; //position of the minimum unsorted element int min_value; //value of minimum unsorted element int temp; //temporary variable necessary for 3-way exchange for (pos_first = 0; pos_first < SIZE ; pos_first++) { // increment of outer loop moves pos_first_unsorted_ele forward 1 element in array min_value = qwerty[ pos_first ]; pos_min_value = pos_first ; for ( pos_other = pos_first + 1; pos_other < SIZE ; pos_other++) { // inner for loop finds the lowest value (and it’s element position) to swap if ( qwerty[ pos_other ] < min_value ) { //testing to find the lowest value and it’s postion in the array min_value = qwerty[ pos_other ] ; pos_min_value = pos_other ; } temp = qwerty[ pos_first ] ; qwerty[ pos_first ] = min_value; qwerty[ pos_min_value] = temp; } for ( index = 0 ; index < SIZE ; index++ ) printf( "\n%d\n", qwerty[ index ] ) ; return ; }
27
Initial element positions in passed_array[ ] : 8 4 7 2 3 SIZE: pos_first : min_value : pos_min_value : pos_other : qwerty [ pos_other] : temp : qwerty[ pos_first ] : qwerty[ pos_min ] : Selection Sort Example
28
Start at one end of the array and traverse each element until you get to the other end –While traversing each element, compare your “target value” (the value for which you are searching) with the value stored in each element of the array Searching an Array – Sequential Search
29
Enumerated Data Types An enumerated data type is a data type with values of your own choosing… different from programmer-defined data types Two way to accomplish this –Use an enum statement –Use typedef and enum statements together
30
Enumerated Data Types Use an enum statement enum days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } ; … enum days today ; today = Tuesday ; Enumeration name names
31
Enumerated Data Types Use a typedef and enum together typedef enum days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } DAYS ; … DAYS today ; today = Tuesday ; tag names Enumeration name
32
Enumerated Data Types Regardless of the method used to create the enumerated data type, if we have declare the variable named today as of type days: DAYS today ; today = Tuesday ; today = Wednesday + 1 ; Note: enumerated values are int values in disguise, so integer arithmetic is allowed Note: order of items in an enumerated list determines their underlying int value beginning with 0 for the first item in the list
33
Back to Arrays Let’s build a program that determines and displays the "percentage enrollment" by major of each major located in the CS Dept. (In other words, we want to determine and display the breakdown of CS, CIS, NCS, TEL, and AC majors) What do we need to know?
34
Back to Arrays What do we need to know? –The number of students per major. –Here’s some imaginary data: CS: 200 CIS: 50 NCS: 60 TEL: 30 AC: 5 What’s the best way to store this info?
35
#include // My file 5.c int main (void) { int cs_enrollment [ 5 ] = {200, 50, 60, 30, 5 } ; float cs_pct [ 5 ] ; int index = 0, total = 0; for ( index = 0 ; index <= 4 ; index = index + 1 ) total = total + cs_enrollment [ index ] ; for ( index = 0 ; index <= 4 ; index = index + 1 ) cs_pct [ index ] = (float) cs_enrollment [ index ] / total ; printf("\n\n\n CS majors comprise %4.2f percent of the CS Dept. ", cs_pct [ 0 ] * 100 ); printf("\n CIS majors comprise %4.2f percent of the CS Dept. ", cs_pct [ 1 ] * 100 ); printf("\n NCS majors comprise %4.2f percent of the CS Dept. ", cs_pct [ 2 ] * 100 ); printf("\n TEL majors comprise %4.2f percent of the CS Dept. ", cs_pct [ 3 ] * 100 ); printf("\n AC majors comprise %4.2f percent of the CS Dept. \n\n\n", cs_pct [ 4 ] * 100 ); return (0) ; }
36
The hardest part of writing an using this program is keep track in our own minds of which elements in the two arrays correspond to which majors. We can use enumerated data types to help us reduce that confusion typedef enum cs_majors { cs, cis, ncs, tel, ac } CS_MAJORS ; … CS_MAJORS majors ;
37
#include // My file 6.c typedef enum cs_majors { cs, cis, ncs, tel, ac } CS_MAJORS ; int main (void) { int cs_enrollment [ 5 ] = {200, 50, 60, 30, 5 } ; float cs_pct [ 5 ] ; int total = 0 ; CS_MAJORS major ; for ( major = cs ; major <= ac ; major = major + 1 ) total = total + cs_enrollment [ major ] ; for ( major = cs ; major <= ac ; major = major + 1 ) cs_pct [ major ] = (float) cs_enrollment [ major ] / total ; printf(" \n\n CS majors comprise %f percent of the CS Dept majors. ", cs_pct [ cs ] * 100 ); printf(" \n CIS majors comprise %f percent of the CS Dept majors. ", cs_pct [ cis ] * 100 ); printf("\n NCS majors comprise %f percent of the CS Dept majors. ", cs_pct [ ncs ] * 100 ); printf("\n TEL majors comprise %f percent of the CS Dept majors. ", cs_pct [ tel ] * 100 ); printf("\n AC majors comprise %f percent of the CS Dept Hall majors. ",cs_pct [ ac ] * 100 ); printf("\n\n\n"); return (0) ; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.