ECE 1322: Section 9 and 10 Lab Assessment 9 Part 1: Preprocessor Directives, time.h, rand(), srand() A simple header code is shown below. Type it out and save it as “header.h”. 1 2 3 4 5 6 7 8 9 10 11 12 13 //header.h #ifndef HEADER_H #define HEADER_H int ThisReturnsOne() { return 1; } float ThisReturnsTwo() { return 3.35; } #endif //HEADER_H Preprocessor wrapper (lines 2, 3, and 13) prevents the code between #ifndef (which means “if not defined”) and #endif from being included if the name HEADER_H has been previously defined. Now try using the header in a program. Compile and run the following program: #include "header.h" int main() { int x; float y,z; x = ThisReturnsOne(); y = ThisReturnsTwo(); z = (float) x + y; printf("x = %d\n",x); printf("y = %.2f\n",y); printf("z = %.2f\n",z); system("pause"); return 0; } 1 time.h Read about this header at http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.15.html Try out the following examples as provided in the website: Example 1: #include<time.h> #include<stdio.h> int main(void) { time_t timer; timer=time(NULL); printf("The current time is %s.\n",asctime(localtime(&timer))); system("pause"); return 0; } Example 2: #include<time.h> #include<stdio.h> int main(void) { clock_t ticks1, ticks2; ticks1=clock(); ticks2=ticks1; while((ticks2/CLOCKS_PER_SEC-ticks1/CLOCKS_PER_SEC)<1) ticks2=clock(); printf("Took %ld ticks to wait one second.\n",ticks2-ticks1); printf("This value should be the same as CLOCKS_PER_SEC %ld.\n",CLOCKS_PER_SEC); system("pause"); return 0; } 2 which is Random function The code below uses rand( ) to generate random numbers. Compile and run the program. What can you comment about the output of this program? #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int i; for (i=1; i<=20; i++) { printf("%10d", 1 + (rand()%6)); if (i % 5 == 0) { printf("\n"); } } system("pause"); return 0; } Add the following code after the declaration of the integer i. How is the output different now? Comment. srand(time(NULL)) ; END OF PART 1 3 Part 2: Multi-dimensional Arrays, Strings The following program maintains two orderings of a list of applicants i.e.: (i) the original ordering of the data, and (ii) an alphabetical ordering accessed through an array of pointers. Modify the program such that it will store the two orderings in two separate text files. Name the files as “original_order.txt” and “alphabetical order.txt”. #include <stdio.h> #define STRSIZ 30 #define MAXAPP 50 int alpha_first(char *list[], int min_sub, int max_sub); void select_sort_str(char *list[], int n); int main(void) { char applicants[MAXAPP][STRSIZ]; char *alpha[MAXAPP]; int num_app,i; char one_char; printf("Enter number of applicants (0...%d)\n>", MAXAPP); scanf("%d", &num_app); do scanf("%c", &one_char); while (one_char != '\n'); printf("Enter number of applicants on separate lines\n"); printf("in the order in which they applied\n"); for(i=0;i<num_app;++i) gets(applicants[i]); // fills array of pointers and sorts for(i=0;i<num_app;++i) alpha[i]=applicants[i]; //copies only address select_sort_str(alpha,num_app); printf("\n\n%-30s%5c%-30s\n\n","Application Order",' Order"); for(i=0;i<num_app;++i) printf("%-30s%5c%-30s\n",applicants[i],' ',alpha[i]); ',"Alphabetical system("pause"); return 0; } /* Finds the index of the string that comes first alphabetically in elements min_sub..max_sub of list */ int alpha_first(char *list[], int min_sub, int max_sub) 4 { int first, i; first = min_sub; for(i = min_sub+1; i <= max_sub; ++i) if(strcmp(list[i], list[first]) < 0) first = i; return(first); } /* Orders the pointers in array list so they access strings in alphabetical order */ void select_sort_str(char *list[], int n) { int fill, index_of_min; char *temp; for(fill = 0; fill < n-1; ++fill) { index_of_min = alpha_first(list,fill,n-1); if(index_of_min != fill) { temp = list[index_of_min]; list[index_of_min] = list[fill]; list[fill] = temp; } } } 5 Lab Assignment A resistor is a circuit device designed to have a specific resistance value between its ends. Resistance values are expressed in ohms (Ω) or kilo-ohms (kΩ). Resistors are frequently marked with colored bands that encode their resistance values, as shown in Figure 1. The first two bands are digits, and the third is a power-of-ten multiplier. Figure 1. Bands encoding the resistance value of a resistor The table below shows the meanings of each band color, which can be referred from Figure 1 as well. For example, if the first band is green, the second is black, and the third is orange, the resistor has a value of 50 x 103 Ω or 50kΩ. Color Black Brown Red Orange Yellow Green Blue Violet Gray White Value as Digit 0 1 2 3 4 5 6 7 8 9 Value as Multiplier 100 101 102 103 104 105 106 107 108 109 The information in the table can be stored in a C program as an array of strings. 6 char COLOR_CODES[10][7] = {“black”, “brown”, “green”, “blue”, “violet”, “gray”, “white”}; “red”, “orange”, “yellow”, Notice that “red” is COLOR_CODES[2] and has a digit value of 2 and a multiplier value of 102. In general, COLOR_CODES[n] has digit value n and multiplier value of 10n. Write a program that prompts for the colors of Band 1, Band 2, and Band 3, and then displays the resistance in kilo-ohms. Include a helper function ‘search’ that takes three parameters- the list of strings, the size of the list, and a target string, and returns the subscript of the list element that matches the target or returns -1 if the target is not in the list. Here is a short sample run of what your output should look like: Enter the colors of the resistor’s three bands, beginning with nearest the end. Type the colors in lowercase letters only, NO CAPS. Band 1 => green Band 2 => black Band 3 => yellow Resistance value: 500 kilo-ohms the Do you want to decode another resistor (Type ‘y’ for yes, or ‘n’ for no)? => y Enter the colors of the resistor’s three bands, beginning with the nearest the end. Type the colors in lowercase letters only, NO CAPS. Band 1 => green Band 2 => vilet Band 3 => gray Invalid color: vilet Do you want to decode another resistor (Type ‘y’ for yes, or ‘n’ for no)? => n 7 band band