1 / 22

Computer Programming for Engineering Applications

Computer Programming for Engineering Applications . ECE 175. Intro to Programming . Lecture Set Overview. Modular Programming – Functions. Functions – Modular Programming. Functions hide unnecessary details and hence, reduce complexity of the program Example:

ctheresa
Download Presentation

Computer Programming for Engineering Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programming forEngineering Applications ECE 175 Intro to Programming

  2. Lecture Set Overview Modular Programming – Functions ECE 175

  3. Functions – Modular Programming Functions hide unnecessary details and hence, reduce complexity of the program Example: So far we have used the standardized functions of C language such as printf, scanf, fprintf, fscanf Functions make our program modular. Easier to debug and isolate errors Re-use: We can reuse function we have written in other programs Simplicity: simplify our code when a block of commands has to be repeated multiple times within the main function Input parameters F Output result ECE 175

  4. Function Declaration Placement in your program Before the main function Usually at the top of your program right after the declarations for the pre-processor Or can be placed at the bottom of your code, with a name declaration at the top Input arguments – Data passed to the function Output arguments – Data returned from the function Argument list correspondence Number of arguments must be the same Ordering of arguments determines correspondence Each argument must be of a data type that can be assigned to the formal parameter Syntax: data type function_name(data type var1, data type var2) { variable declarations; statements; return statement; } ECE 175

  5. Example – Check if a 3-tuple is a Pythagorean triple Problem Analysis Input: three integer numbers a, b, c Output: A binary decision (yes/no) Algorithm design Initial condition: Read three numbers from keyboard Checking condition: a^2 + b^2 = c^2 or b^2 + c^2 = a^2 or a^2 + c^2 = b^2  outcome is a Pythagorean triple Modular design Function that computes x^y. Inputs: x, y Output: x^y ECE 175

  6. Functions with multiple inputs, one output Function name Input arguments Data type returned by the function longint power(intb, int n) { inti; longintp=1 ; for (i = 1; i <= n; i++) p=p*b; return p; } Internal variable declaration Returned value ECE 175

  7. Function Calling #include<stdio.h> longint power(int base , int n); int main(void) { int i, base=2; longint res; printf("Base\tPower\tResult\n"); for (i=1; i<=10; i++) { res=power(base, i); printf("%d\t%d\t%ld\n", base, i, res); } return (0); } ECE 175

  8. Function Re-use #include<stdio.h> longint power(intb, int n); int main(void) { int a, b, c; printf("Enter the values of a, b,c:"); scanf("%d%d%d", &a, &b, &c); if (power(a,2)+power(b,2)==power(c,2) || power(a,2)+power(b,2)==power(c,2) || power(a,2)+power(b,2)==power(c,2)) { printf("Values (%d, %d, %d) form a Pythagorean triple", a,b,c); } else { printf("Values (%d, %d, %d) do not form a Pythagorean triple", a,b,c); } return(0); } ECE 175

  9. Functions without Return Types (and statement…) #include<stdio.h> voidprint_rboxed(float r); int main(void) { floatreal_n; printf("Give me any real number:"); scanf("%f", &real_n); // scanning for a real print_rboxed(real_n); // calling the print_rboxed function return(0); } voidprint_rboxed(float r) { printf("***************\n"); printf("* *\n"); printf("* %7.2f *\n", r); printf("* *\n"); printf("***************\n"); } ECE 175

  10. Function data Each time a function is executed, an area of memory is allocated Local variables declared within the scope of the function The data area is lost when the function terminates It is recreated empty when the function is called again All the local variables have undefined (random) value ECE 175

  11. Functions with no arguments void title(void) { printf("Power calculation tool\n\n"); printf("Base\tPower\tResult\n"); return; } intmain(void) { inti, base=2; longint res; title(); for (i=1; i<=10; i++) { res=power(base,i); printf("%d\t%d\t%ld\n", base, i, res); } return (0); } ECE 175

  12. Conversion Binary to Decimal, Decimal to Binary Problem statement: Write a C program that converts binary numbers to decimals. ECE 175

  13. Breaking down to elementary tasks Program analysis Input: A binary number Output: the converted number Algorithm design Initial condition: Read the number from keyboard Conversion from binary to decimal: compute number length. Divide by 10^(i-1) to isolate the leftmost digit. Keep a sum of the leftmost digit times 2^(i-1). Subtract 10^i from binary number and continue until number is equal to zero. Modular design Function for binary to decimal and length of the number. User Interface Ask user for binary number or quit the conversion tool ECE 175

  14. Function for the Computation of the Number Length intnumb_len(longint number) { int count=0; // digit counter initialized to zero while(number!=0) { count++; // increase digit counter number=number/10; // divide by ten } return count; // return the length of the number } ECE 175

  15. Binary-to-decimal intbin_dec(longint number) // binary-to-decimal conversion { int digit=0, i=0, dec=0; for (i=numb_len(number); number!=0; i--) { digit=number/pow(10,i-1); dec+=digit*pow(2,i-1); //2^i number=number-(digit*pow(10,i-1)); // number = number - 10^i } returndec; } ECE 175

  16. Interface #include<stdio.h> #include<math.h> intnumb_len(longint number); intbin_dec(longint number); int main(void) { longint binary; charans='y'; while (ans!='n' && ans!='N') { printf("Enter the binary number you want to convert>"); scanf("%ld",&binary); printf("(%ld)_2 equals (%d)_10\n", binary, bin_dec(binary)); printf("Do you want to continue y/n?:"); fflush(stdin); scanf("%c", &ans); } return (0); } ECE 175

  17. Another Version of the bin_dec Function intbin_dec(longint bin) // binary-to-decimal conversion { int digit=0, i=0, dec=0; while(bin!=0) { digit=bin%10; // compute the remainder with 10 // to isolate the rightmost digit dec+=digit*pow(2,i); //2^i bin=bin/10; // integerdivision with 10 i++; } returndec; } ECE 175

  18. Computing Grade Statistics - Function Style Write a program that reads file “grades.txt”and prints the following on file “stats.txt” average, minimum, and maximum grade Input: grades.txt Output: stats.txt Constraints: Number of students is unknown Algorithm: AVE = SUM/# of students MIN = 100, if current # < MIN  MIN = current # MAX = 0, if current # > MAX  MAX = current # ECE 175

  19. Simplified main function #include <stdio.h> intmax_grade(void); intmin_grade(void); float average(void); int main(void) { int low, high; floatave; ave=average(); low=min_grade(); high=max_grade(); if (ave!=-1) printf("The average grade in the class is:%.1f\n", ave); if (low!=-1) printf("The minimum grade in the class is:%d\n", low); if (high!=-1) printf("The maximum grade in the class is:%d\n", high); return(0); } ECE 175

  20. Max Grade function intmax_grade(void) { intn_st=0, max=0, grade; FILE*inp= fopen("grades.txt", "r"); // open grades.txt if (inp == NULL) { printf("The input file does not exist\n"); max = -1; } else { while (fscanf(inp, "%d", &grade)!=EOF ) // while have not reached the end of file { n_st++; if (grade>max || n_st==1) max=grade; } fclose(inp); // close the file that inp points at } return max; } ECE 175

  21. Min Grade function intmin_grade(void) { intn_st=0, min=0, grade; FILE *inp = fopen("grades.txt", "r"); // open grades.txt if (inp == NULL) { printf("The input file does not exist\n"); min = -1; } else { while (fscanf(inp, "%d", &grade)!=EOF ) // while have not reached the end of file { n_st++; if (grade<min || n_st==1) min=grade; } fclose(inp); // close the file that inp points at } return min; } ECE 175

  22. Average Grade function float average(void) { intn_st=0, grade; float sum=0, ave; FILE *inp = fopen("grades.txt", "r"); // open grades.txt if (inp == NULL) { printf("The input file does not exist\n"); ave = -1; } else { while (fscanf(inp, "%d", &grade)!=EOF ) { n_st++; sum+=grade; } ave = sum/n_st; fclose(inp); // close the file thatinp points at } return ave; } ECE 175

More Related