Fundamentals of Programming Session 14 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology
Outlines Some Examples of Recursive Functions Arrays Array Examples 2
Q1 Write a program to find sum of first n natural numbers using recursion. 3
A1 int sum(int n); int main() { int num, add; printf ("Enter a positive integer:\n"); scanf ("%d", &num); printf ("sum=%d", sum(num)); } 4 int sum (int n) { if(n==0) return n; else return (n + sum(n-1)); }
Q2 Write a program to take two integer numbers and find their addition using a recursive function. 5
A2 int add(int, int); main() { int a,b; scanf ("%d", &a); scanf ("%d", &b); printf("the value of addition is %d\n", add(a,b)); return 0; } 6 int add(int i, int j) { if(j==0) return(i); else return (1 + add (i, j-1)); }
Q3 Write a program to take the integer number n and calculate using recursion. You cannot use the function sqrt. Moreover, you should compute and show two digits after decimal point. 7
A3 void f1(int,int); int main(){ int i=1; int n; scanf("%d",&n); f1(n,i); getch(); return 0; } 8 void f1(int n,int i) { if((n*10000)-(i*i)<=0) printf("%f", (double)i/100); else f1(n,i+1); }
Arrays 9 Arrays are data structures consisting of related data items of the same type. An array is a group of memory locations related by the fact that they all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. The first element in every array is the zeroth element. The position number contained within square brackets is more formally called a subscript (or index). If a program uses an expression as a subscript, then the expression is evaluated to determine the subscript. For example, consider the following statement for a = 5 and b = 6 c[ a + b ] += 2;
Arrays 10 Arrays occupy space in memory. To tell the computer to reserve 12 elements for integer array c, use the definition int c[ 12 ]; The following definition int b[ 100 ], x[ 27 ]; reserves 100 elements for integer array b and 27 elements for integer array x. Arrays may contain other data types. Figure 6.3 uses for statements to initialize the elements of a 10-element integer array n to zeros and print the array in tabular format.
11 Array Examples
12 Array Examples
Array Examples The elements of an array can also be initialized when the array is defined by following the definition with an equals sign and braces, {}, containing a commaseparated list of initializers. Figure 6.4 initializes an integer array with 10 values (line 9) and prints the array in tabular format. 13
14 Array Examples
15 Array Examples
Array Examples 16 If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. For example, the elements of the array n in Fig. 6.3 could have been initialized to zero as follows: int n[ 10 ] = { 0 }; It s important to remember that arrays are not automatically initialized to zero. The array definition int n[ 5 ] = { 32, 27, 64, 18, 95, 14 }; causes a syntax error because there are six initializers and only five array elements. If the array size is omitted from a definition with an initializer list, the number of elements in the array will be the number of elements in the initializer list. For example, int n[] = { 1, 2, 3, 4, 5 }; would create a five-element array.
17 Array Examples
18 Array Examples
Array Examples 19 The #define preprocessor directive is introduced in this program. Line 4 #define SIZE 10 defines a symbolic constant SIZE whose value is 10. If the #define preprocessor directive in line 4 is terminated with a semicolon, all occurrences of the symbolic constant SIZE in the program are replaced with the text 10; by the preprocessor. This may lead to syntax errors at compile time, or logic errors at execution time. Figure 6.6 sums the values contained in the 12-element integer array a.
20 Array Examples
Array Examples Our next example uses arrays to summarize the results of data collected in a survey. Consider the problem statement. Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means awful and 10 means excellent). Place the 40 responses in an integer array and summarize the results of the poll. This is a typical array application (see Fig. 6.7). 21
22 Array Examples
23 Array Examples
24 Array Examples
Array Examples 25 C has no array bounds checking to prevent the program from referring to an element that does not exist. Thus, an executing program can walk off the end of an array without warning. You should ensure that all array references remain within the bounds of the array. Our next example (Fig. 6.8) reads numbers from an array and graphs the information in the form of a bar chart or histogram each number is printed, then a bar consisting of that many asterisks is printed beside the number.
26 Array Examples
27 Array Examples
Array Examples Roll a single six-sided die 6000 times to test whether the random number generator actually produces random numbers. An array version of this program is shown in Fig. 6.9. 28
29 Array Examples
30 Array Examples
Array Examples 31 We now discuss storing strings in character arrays. So far, the only string-processing capability we have is outputting a string with printf. A string such as "hello" is really a static array of individual characters in C. A character array can be initialized using a string literal. For example, char string1[] = "first"; initializes the elements of array string1 to the individual characters in the string literal "first". In this case, the size of array string1 is determined by the compiler based on the length of the string.
Array Examples 32 The string "first" contains five characters plus a special string-termination character called the null character. Thus, array string1 actually contains six elements. The character constant representing the null character is '\0'. All strings in C end with this character. The preceding definition is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; Figure 6.10 demonstrates initializing a character array with a string literal, reading a string into a character array, printing a character array as a string and accessing individual characters of a string.
33 Array Examples
34 Array Examples