380 likes | 553 Views
Review of scalar variables. Variable (up to now) have: name type (int, float, double, char) address value. N 28C4 (int) q 28C8 (float) r 28CC (float). 35. 3.14. 8.9. e.g. Name type address value N integer 28C4 35 q float 28C8 3.14 r float 28CC 8.9. Intro to Arrays.
E N D
Review of scalar variables Variable (up to now) have: name type (int, float, double, char) address value N 28C4 (int) q 28C8 (float) r 28CC (float) 35 3.14 8.9 e.g. Name type address value N integer 28C4 35 q float 28C8 3.14 r float 28CC 8.9
Intro to Arrays Any single element of x may be used like any other scalar variable x[0] 45 3044 x[1] 55 3048 x[2] 25 304C x[3] 85 3050 x[4] 75 3054 printf("%d",x[3]); // prints 85 printf("%d",x[7]+x[1]); // prints 115 x[5] 65 3058 x[6] 100 305C x[7] 60 3060
Intro to Arrays Arrays declared by: // Allocate 8 elements for x int x[8]; x[0] 45 3044 x[1] 55 3048 x[2] 25 304C x[3] 85 3050 x[4] 75 3054 x[5] 65 3058 x[6] 100 305C x[7] 60 3060
Declaring Arrays Define an 8 element array: int x[8]; Elements numbered 0 to 7 Arrays in C always start with 0 (zero based) The initial value of each array element is unknown (just like scalar variables) x[0] ? 3044 x[1] ? 3048 x[2] ? 304C x[3] ? 3050 x[4] ? 3054 x[5] ? 3058 x[6] ? 305C x[7] ? 3060
Declaring/defining Arrays double A[]={ 1.23, 3.14159, 2.718, 0.7071 }; A[0] 1.23 4430 You can also define the values to be held in the array and instruct the compiler to figure out how many elements are needed. Not putting a value within the [] tells the compiler to determine how many locations are needed. A[1] 3.14159 4438 A[2] 2.718 4440 A[3] 0.7071 4448
Working with Arrays (input) #include <stdio.h> void main(void){ int x[8]; int i; float sum, avg; // used later // get 8 values into x[] for (i=0; i<8; i++) { printf("Enter test %d:",i+1); scanf("%d",&x[i]); }
Working with Arrays (input) Sample run (user input underlined): Enter test 1:80Enter test 2:75Enter test 3:90Enter test 4:100Enter test 5:65Enter test 6:88Enter test 7:40Enter test 8:90 x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90
Working with Arrays (find biggest) // WAY 1-initialize biggest one so// far to zerobig=0;for (i=0; i<8; i++){ // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } }printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 ??? What happens if there are only negative values in the array ???
Working with Arrays (find biggest) // WAY 2-initialize biggest one so// far to smallest possible valuebig= -2147483648; for (i=0; i<8; i++){ // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } }printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 ???What happens if program is ported to another system?????? (where integers are stored in 2 or 8 bytes)???
Working with Arrays (find biggest) // WAY 3-initialize biggest one so// far to smallest possible valuebig= INT_MIN; // BUT use symbolfor (i=0; i<8; i++){ // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } }printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 The above works, but there is one more (slightly better) way
Working with Arrays (find biggest) // WAY 4-initialize biggest one so// far to the first array elementbig= x[0];for (i=1; i<8; i++) // start at 1{ // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } }printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 -----
Working with Arrays (find smallest) // initialize smallest one so// far to the first array elementsml= x[0];for (i=1; i<8; i++){ // if x[i] < smallest one so far if (x[i]<sml) { // make that the "new" smallest sml=x[i]; } }printf("Smallest is %d\n",sml); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 Ways 1, 2, and 3 for finding biggest could be adapted to find smallest as well; the above is the one usually used.
Working with Arrays (find average) // initialize sum// for reference:// int sum; // float avg; sum = 0; // add each element of x[] to sumfor (i=0; i<8; i++) sum += x[i]; // divide by n (8) to get average// first float needed since result// datatype of division is a doubleavg=(float)((float)sum/8.0); printf("Average is %.2f\n",avg); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90
Passing Arrays to functions (findBig) //*******************************************// function findBig// On Entry:// test[] - array with values// n - number of elements to examine// On Exit:// returns biggest value in the first n // elements of test[]int findBig(int test[], int n){ int i,big; big=test[0]; for (i=1; i<n; i++) if (x[i]>big) big=x[i]; return big;}
Passing Arrays to functions (findSml) //*******************************************// function findSml// On Entry:// test[] - array with values// n - number of elements to examine// On Exit:// returns smallest value in the first n // elements of test[]int findSml(int test[], int n){ int i,sml; sml=test[0]; for (i=1; i<n; i++) if (x[i]<sml) sml=x[i]; return sml;}
Passing Arrays to functions (findAvg) //*******************************************// function findAvg// On Entry:// test[] - array with values to avg// n - number of values to avg// On Exit:// returns avg of first n elements of test[]float findAvg(int test[], int n){ int i,sum=0; float avg; for (i=0; i<n; i++) sum+=test[i]; avg=(float)((float)sum/(float)n); return avg;}
Passing Arrays to functions (SclAry) //*******************************************// function SclAry// On Entry:// test[] - array with values to scale// n - number of values to scale// p - number of points to scale// On Exit:// The first n values of test[] are// scaled by p pointsvoid SclAry(int test[], int n, int p){ int i; for (i=0; i<n; i++) test[i]=test[i]+p; // or use test[i]+=p; }
#include <stdio.h>void SclAry(int test[], int n, int p);void main(void){ int i; int x[]={ 51,62,73,84,95,100,66,57,48,79 }; int N=sizeof(x)/sizeof(int); SclAry(x,N,10); for (i=0; i<N; i++) printf("%4d",x[i]); printf("\n");} void SclAry(int test[], int n, int p) { int i; for (i=0; i<n; i++) test[i]=test[i]+p; // or use test[i]+=p; } Passing Arrays to functions (SclAry)
Passing Arrays to functions (SclAry) Output of program: 61 72 83 94 105 110 76 67 58 89 For reference: int x[]={ 51,62,73,84,95,100,66,57,48,79 }; ??? What's wrong with this picture ???
Passing Arrays to functions (SclAry) Output of program: 61 72 83 94 105 110 76 67 58 89 For reference: int x[]={ 51,62,73,84,95,100,66,57,48,79 }; ??? What's wrong with this picture ??? The array in the main program was UPDATED ... (say "Hmmmm")Does this seem contrary to all we know about functions? (say "Yes")Is this how it really works? (Yep, it is)Is your head getting ready to explode? (say "Almost") ??? SO WHAT IS GOING ON ???
Passing Arrays to functions Before call to SclAry After call to SclAry test[0] 51 3044 test[0] 61 3044 test[1] 62 3048 test[1] 72 3048 test[2] 73 304C test[2] 83 304C test[3] 84 3050 test[3] 94 3050 test[4] 95 3054 test[4] 105 3054 test[5] 100 3058 test[5] 110 3058 test[6] 66 305C test[6] 76 305C test[7] 57 3060 test[7] 67 3060 test[8] 48 3064 test[8] 58 3064 test[9] 79 3068 test[9] 89 3068 Passing the name only (i.e. test vs. test[4]) passes the ADDRESS of element zero of the array. Put another way: myfunc(ary) same as myfunc (&ary[0])
Sorting Arrays (prelim - swapping) Before After int i,j,t; j=2; t=x[j]; // 1 x[j]=x[j+1]; // 2 x[j+1]=t; // 3 x[0] 61 x[0] 61 x[1] 172 x[1] 172 x[2] 83 x[2] 74 x[3] 74 x[3] 83 x[4] 99 x[4] 99 x[5] 11 x[5] 11 t 83 ? t 83 t 83 x[0] 61 x[0] 61 x[0] 61 1 3 x[1] 172 x[1] 172 x[1] 172 x[2] 83 x[2] 74 83 2 x[2] 74 83 x[3] 74 x[3] 74 x[3] 83 74 x[4] 99 x[4] 99 x[4] 99 x[5] 11 x[5] 11 x[5] 11
Sorting Arrays void bubblesort(int x[], int n){ int i,j,t; for (i=0; i<n; i++) { for (j=0; j<(n-1); j++) { if (x[j]>x[j+1]) { t=x[j]; x[j]=x[j+1]; x[j+1]=t; } } }}
void bubblesort(int x[], int n) // say n=6 int i,j,t; for (i=0; i<n; i++) for (j=0; j<(n-1); j++) if (x[j]>x[j+1]) t=x[j]; x[j]=x[j+1]; x[j+1]=t; Sorting ArraysPass 0 i=0,j=0 i=0,j=1 i=0,j=2 i=0,j=3 i=0,j=4 x[0] 61 61 61 61 61 x[1] 172 83 172 83 83 83 x[2] 83 172 83 74 172 74 74 61>172? No, no swap 172>83? Yes, swap 172>74? Yes, swap 172>105? Yes, swap 172>11? Yes, swap x[3] 74 74 172 74 99 172 99 x[4] 99 99 99 172 99 11 172 x[5] 11 11 11 11 172 11
void bubblesort(int x[], int n) // say n=6 int i,j,t; for (i=0; i<n; i++) for (j=0; j<(n-1); j++) if (x[j]>x[j+1]) t=x[j]; x[j]=x[j+1]; x[j+1]=t; Sorting ArraysPass 1 i=1,j=0 i=1,j=1 i=1,j=2 i=1,j=3 i=1,j=4 x[0] 61 61 61 61 61 x[1] 83 74 83 74 83 83 x[2] 74 83 74 83 83 74 61>83? No, no swap 83>74? Yes, swap 83>99? No, no swap 99>11? Yes, swap 99>172? No, no swap x[3] 99 99 99 11 99 11 x[4] 11 11 11 99 11 99 x[5] 172 172 172 172 172
void bubblesort(int x[], int n) // say n=6 int i,j,t; for (i=0; i<n; i++) for (j=0; j<(n-1); j++) if (x[j]>x[j+1]) t=x[j]; x[j]=x[j+1]; x[j+1]=t; Sorting ArraysPass 2 i=2,j=0 i=2,j=1 i=2,j=2 i=2,j=3 i=2,j=4 x[0] 61 61 61 61 61 x[1] 83 74 83 74 74 74 x[2] 74 83 74 11 83 11 11 61>83? No, no swap 83>74? Yes, swap 83>11? Yes, swap 83>99? No, no swap 99>172? No, no swap x[3] 11 11 83 11 83 83 x[4] 99 99 99 99 99 x[5] 172 172 172 172 172
void bubblesort(int x[], int n) // say n=6 int i,j,t; for (i=0; i<n; i++) for (j=0; j<(n-1); j++) if (x[j]>x[j+1]) t=x[j]; x[j]=x[j+1]; x[j+1]=t; Sorting ArraysPass 3 i=3,j=0 i=3,j=1 i=3,j=2 i=3,j=3 i=3,j=4 x[0] 61 61 61 61 61 x[1] 74 11 74 11 11 11 x[2] 11 74 11 74 74 74 61>74? No, no swap 74>11? Yes, swap 74>83? No, no swap 83>99? No, no swap 99>172? No, no swap x[3] 83 83 83 83 83 x[4] 99 99 99 99 99 x[5] 172 172 172 172 172
void bubblesort(int x[], int n) // say n=6 int i,j,t; for (i=0; i<n; i++) for (j=0; j<(n-1); j++) if (x[j]>x[j+1]) t=x[j]; x[j]=x[j+1]; x[j+1]=t; Sorting ArraysPass 4 i=4,j=0 i=4,j=1 i=4,j=2 i=4,j=3 i=4,j=4 x[0] 11 61 11 11 11 11 x[1] 61 11 61 61 61 61 x[2] 74 74 74 74 74 61>74? No, no swap 61>11? Yes, swap 74>83? No, no swap 83>99? No, no swap 99>172? No, no swap x[3] 83 83 83 83 83 x[4] 99 99 99 99 99 x[5] 172 172 172 172 172
void bubblesort(int x[], int n) // say n=6 int i,j,t; for (i=0; i<n; i++) for (j=0; j<(n-1); j++) if (x[j]>x[j+1]) t=x[j]; x[j]=x[j+1]; x[j+1]=t; Sorting ArraysPass 5 i=5,j=0 i=5,j=1 i=5,j=2 i=5,j=3 i=5,j=4 x[0] 11 11 11 11 11 x[1] 61 61 61 61 61 x[2] 74 74 74 74 74 61>74? No, no swap 11>61? No, no swap 74>83? No, no swap 83>99? No, no swap 99>172? No, no swap x[3] 83 83 83 83 83 x[4] 99 99 99 99 99 x[5] 172 172 172 172 172
Sorting Arrays Analysis AfterPass 0 AfterPass 1 AfterPass 2 AfterPass 3 AfterPass 4 AfterPass 5 Before i=0,j=0 i=0,j=4 i=1,j=4 i=2,j=4 i=3,j=4 i=4,j=4 i=5,j=4 x[0] 61 61 61 61 61 11 11 x[1] 172 83 83 74 11 61 61 x[2] 83 74 74 11 74 74 74 x[3] 74 99 11 83 83 83 83 x[4] 99 11 99 99 99 99 99 x[5] 11 172 172 172 172 172 172 Biggest number in correct position Biggest five numbers in correct position (so really all six numbers in correct position). Pass 5 is really a waste. Biggest two numbers in correct position Biggest three numbers in correct position Biggest four numbers in correct position
Analysis (n=100, array in rev order) #exec #cyc time(ns)i=0; 1 1 1while (i<n) 100 1 100 j=0; 100 1 100 while (j<(n-1)) 9900 2 19800 if (x[j]>x[j+1]) 9900 5 49500 t=x[j]; 4950 3 14850 x[j]=x[j+1]; 4950 5 24750 x[j+1]=t; 4950 4 19800 j++; 9900 1 9900 i++; 100 1 100 44851 138901
Analysis (General for n) i=0; 1 1while (i<n) 100 n j=0; 100 n while (j<(n-1)) 9900 n*(n-1) if (x[j]>x[j+1])9900 n*(n-1) t=x[j]; 4950 (n*(n-1))/2 x[j]=x[j+1]; 4950 (n*(n-1))/2 x[j+1]=t; 4950 (n*(n-1))/2 j++; 9900 n*(n-1) i++; 100 n
Analysis (General for n) t = 1 + 3n + 14[n(n-1)] t = 1 + 3n + 14n2 - 14n t = 14n2 - 11n + 1 Check for n=100: t = 14(100)2 - 11(100) + 1 t = 140000 - 1100 + 1 t = 138901 ns = 138.9 us = 0.138 ms
Analysis (General for n) This is a linear function for n
void bubblesort(int x[], int n){ int i,j,t,n1; n1=n-1; for (i=0; i<n1; i++) { for (j=0; j<(n1-i); j++) { if (x[j]>x[j+1]) { t=x[j]; x[j]=x[j+1]; x[j+1]=t; } } }} Sorting Arrays
void bubblesort(int x[], int n){ int i,j,t,n1, F=1; n1=n-1; for (i=0; i<n1; i++) { if (F) { F=0; for (j=0; j<(n1-i); j++) { if (x[j]>x[j+1]) { t=x[j]; x[j]=x[j+1]; x[j+1]=t;F=1; } } }}} Sorting Arrays
void bubblesort(int x[], int n){ int i,j,t,n1, F=1; n1=n-1; for (i=0; (i<n1)&&F; i++) { F=0; for (j=0; j<(n1-i); j++) { if (x[j]>x[j+1]) { t=x[j]; x[j]=x[j+1]; x[j+1]=t;F=1; } } }}} Sorting Arrays