1 / 18

16.216 ECE Application Programming

16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2012 Lecture 19: Math library One dimensional arrays. Lecture outline. Announcements/reminders Program 5 due 10/26 Today’s class Math library One dimensional arrays. ANSI library functions. Example libraries

maia
Download Presentation

16.216 ECE Application Programming

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. 16.216ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 19: Math library One dimensional arrays

  2. Lecture outline • Announcements/reminders • Program 5 due 10/26 • Today’s class • Math library • One dimensional arrays ECE Application Programming: Lecture 19

  3. ANSI library functions • Example libraries • <stdio.h>: already using frequently • <stdlib.h>: various standard operations—conversion, memory allocation, etc. • <ctype.h> <string.h>: functions for manipulating characters and strings • We’ll cover these in a later lecture • <math.h>: Built-in math functions ECE Application Programming: Lecture 19

  4. <math.h> functions • Trigonometric functions • All arguments of type double • Single argument in radians: • cos(r), sin(r), tan(r), acos(r), asin(r), atan(r) • Two arguments (x,y coordinates): atan2(x,y) • Remember: # in degrees = (180/pi) * (# in radians) • Exponential functions • exp(x): ex • log(x): natural log of x • log10(x): base 10 log of x • Power functions • pow(x, y): xy • sqrt(x): square root of x • Absolute value of a double: fabs() ECE Application Programming: Lecture 19

  5. Example: Using math functions • Write a function to: • Given the other two sides, calculate and return the length of the hypotenuse of a right triangle • Remember, c2 = a2 + b2 • Given a number in degrees, calculate and return its sine • Remember: • Sine function takes an argument in radians • (# in degrees) = (180 / π) * (# in radians) • π = 4.0 * arctan(1.0) ECE Application Programming: Lecture 19

  6. Example solutions • Write a function to: given the other two sides, calculate and return the length of the hypotenuse of a right triangle double calcHyp(double a, double b) { return sqrt(a * a + b * b); } ECE Application Programming: Lecture 19

  7. Example solutions (cont.) • Write a function to: given a number in degrees, calculate and return its sine double calcSin(double deg) { double rad; rad = deg * (4.0 * atan(1.0)) / 180; return sin(rad); } ECE Application Programming: Lecture 19

  8. Review of scalar variables Variables (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 ECE Application Programming: Lecture 19

  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 ECE Application Programming: Lecture 19

  10. Declaring Arrays Define an 8 element array: int x[8]; Elements numbered 0 to 7 Arrays in C always start with location 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 ECE Application Programming: Lecture 19

  11. 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 ECE Application Programming: Lecture 19

  12. Working with Arrays (input) #include <stdio.h> void main(void){ int x[8]; int i; // get 8 values into x[] for (i=0; i<8; i++) { printf("Enter test %d:",i+1); scanf("%d",&x[i]); } } ECE Application Programming: Lecture 19

  13. 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 ECE Application Programming: Lecture 19

  14. Pitfalls • What happens if we change previous code to: #include <stdio.h> void main(void){int x[8];inti; 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]); } } ECE Application Programming: Lecture 19

  15. Pitfalls (cont.) • Although x has 8 elements, x[8] is not one of those elements! • Compiler will not stop you from accessing elements outside the array • Must make sure you know the size of the array ECE Application Programming: Lecture 19

  16. Example • What does the following program print? int main() { intarr[10]; inti; printf("First loop:\n"); for (i = 0; i < 10; i++) { arr[i] = i * 2; printf("arr[%d] = %d\n", i, arr[i]); } printf("\nSecond loop:\n"); for (i = 0; i < 9; i++) { arr[i] = arr[i] + arr[i + 1]; printf("arr[%d] = %d\n", i, arr[i]); } return 0; } ECE Application Programming: Lecture 19

  17. Example solution First loop: arr[0] = 0 arr[1] = 2 arr[2] = 4 arr[3] = 6 arr[4] = 8 arr[5] = 10 arr[6] = 12 arr[7] = 14 arr[8] = 16 arr[9] = 18 Output continued: Second loop: arr[0] = 2 arr[1] = 6 arr[2] = 10 arr[3] = 14 arr[4] = 18 arr[5] = 22 arr[6] = 26 arr[7] = 30 arr[8] = 34 ECE Application Programming: Lecture 19

  18. Next time • Arrays and functions • Two dimensional arrays • Reminders: • Program 5 due 10/26 ECE Application Programming: Lecture 19

More Related