Download presentation

Presentation is loading. Please wait.

Published byKevin Calland Modified over 2 years ago

1
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable called f. In scientific applications, one often needs many variables of the same type. E.g., one needs to store the value of temperatures in ten cities. This is done in C by declaring an array: float temps[10];

2
Single Variable v.s. Array float f; reserves one cell in memory of type float. float temps[10]; reserves 10 cells in memory of type float. All cells are of the same type. temps 0 1 2 3 4 5 6 7 8 9 temps[0] temps[8] &f

3
Array Index The index of a 10-cell array varies from 0 to 9. So float temps[10]; defined 10 cells. We refer to these 10 cells as temps[0], temps[1], temps[2], temps[3],..., temps[7], temps[8], temps[9]. A reference to temps[10] is a programming error (and difficult to detect).

4
Array Initialization We can assign array element values. E.g., int id[4]; id[0] = 45; id[1] = 2; id[2] = 800; id[3] = 81; Or equivalently, an array can be initialized in the definition. E.g., int id[4] = {45, 2, 800, 81}; Uninitialized array elements usually contain arbitrary values. All variables should be initialized before being used.

5
Array and Pointer float f; reserves one cell in memory of type float. float temps[10]; reserves 10 cells in memory of type float. temps 0 1 2 3 4 5 6 7 8 9 temps[0] temps[8] &f &f is the address of the variable f. The address tells you where f is located in computer memory. An array name by itself is the starting address of the array. I.e. address of temps[0].

6
Memory and Address Each memory cell (actually each byte) has a numerical label as its address. A pointer variable can hold an address. Pointer constant is the numerical constant representing an address. An array name is a pointer constant. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17... A memory cell at address 8

7
Character Sequences: String A sequence of characters known as string is stored as an array of characters, with the last character being '\0' (with ASCII code 0), denoting the end of a string. A constant string is specified in double quotes. E.g., The string "Moe" is stored as: Moe \0 Cells of char

8
Strings and Characters #include main() { char stooge1[4]; char stooge2[6]; char stooge3[6] = "Larry"; stooge1[0] = 'M'; stooge1[1] = 'o'; stooge1[2] = 'e'; stooge1[3] = '\0'; printf("\n\n\tPlease enter the" " second Stooge's name: "); scanf("%s", stooge2); printf("\n%s", stooge1); printf("\n%s", stooge2); printf("\n%s", stooge3); }

9
Initialization of a String The declaration char stooge3[6]="Larry"; is equivalent to char stooge3[6]; stooge3[0] = 'L'; stooge3[1] = 'a'; stooge3[2] = 'r'; stooge3[3] = 'r'; stooge3[4] = 'y'; stooge3[5] = '\0'; Try to use stooge3[6] in an expression is a serious programming error.

10
Format Descriptor %s scanf("%s", stooge2); The conversion specification %s read a sequence of characters, with initial white space skipped, up to the next (but not include) white space. A terminating '\0' is added to the sequence. A white space character is either return, or space, or tab, etc. E.g., if input is Curly stooge2 array becomes Curly \0

11
Format Descriptor %s in printf() printf("%s", stooge2); The conversion specification %s prints a sequence of characters stored in the array stooge2 until '\0' is reached. The terminating character '\0' is not printed. If stooge2 array contains The print out is Curly Curl y \0 Omitting the ending '\0' in a string is a serious programming error.

12
Single Quotes v.s. Double Quotes 'M' representing a single character, equivalent to its ASCII code (77 for 'M' ). "M" is a string, i.e., an array of characters. So "M" is the char array of 'M''\0'

13
Real World Application: The Fourier Transform Compute a specified number of terms of the discrete Fourier transform (h1,h2) of the function g. If g(t) is defined for t = 0, 1, …, r-1, the functions h1 and h2 are defined by the formulas h1(s) = g(t) cos(2 st/r) h2(s) = g(t) sin(2 st/r) for s = 0,1, …, r-1. The values h1(s) and h2(s) for s = 0, 1, …, r- 1 are called the Fourier coefficients for g. Print the amplitude spectrum (h1[s] 2 + h2[s] 2 ) 1/2 for the Fourier coefficients that were computed.

14
Solution We use three float arrays: one to store the values of g and two for computing and storing the values of h1 and h2. We define these arrays to be of size 300; thus, our program can accommodate a function g of up to 300 values, and we can compute up to 300 terms of the Fourier series. We cannot first read the number of values of g and then define our arrays to be of that size, because C requires the size of the array to be specified as a constant. After reading the values of g, we use nested for loops to compute the Fourier coefficients and then another for loop to print the amplitude spectrum.

15
The comment paragraph /* This program computes the first max_coeff + 1 terms of the discrete Fourier transform (h1, h2) of the function g, which is represented as the array g[0], g[1],..., g[r-1] h1 is the cosine component and h2 is the sine component of the Fourier transform; h1 and h2 are represented as the arrays: h1[0], h1[1],..., h1[max_coeff] h2[0], h2[1],..., h2[max_coeff] The program prints the amplitude spectrum sqrt( h1[i]^2 + h2[i]^2) for i = 1,..., max_coeff. The input must be of the form r g[0] g[1]... g[r-1] max_coeff */

16
The Program #include #define MAX 300 main() { int r, i, s, t, max_coeff; float scale; float h1[MAX], h2[MAX], g[MAX]; scanf("%d", &r); if (r MAX) { printf("Invalid resolution!\n"); exit(1); } scale = 2 * M_PI/r; for(i = 0; i < r; ++i) scanf("%f", &g[i]); for(s = 0; s <= max_coeff; ++s) { h1[s] = 0; h2[s] = 0; for(t = 0; t < r; ++t) { h1[s] += g[t] * cos(scale*s*t); h2[s] += g[t] * sin(scale*s*t); } printf("\n\nSpectrum:\n"); for(i = 1; i <= max_coeff; ++i) printf("%f\n", sqrt(h1[i]*h1[i] + h2[i]*h2[i]) ); }

17
Reading/Home Working Read Chapter 6, page 230 to 249. Work on Problems –Section 6.2, page 238, exercise 1, 3, 13. –Section 6.4, page 249, exercise 1, 3, 5. Check your answers in the back of the textbook. Do not hand in.

Similar presentations

OK

1 ICS103 Programming in C Lecture 3: Introduction to C (2)

1 ICS103 Programming in C Lecture 3: Introduction to C (2)

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google