c programming session 04

31
Slide 1 of 31 Ver. 1.0 Programming in C In this session, you will learn to: Work with arrays Appreciate preprocessor directives Objectives

Upload: dushmanta-nath

Post on 16-Apr-2017

1.562 views

Category:

Education


1 download

TRANSCRIPT

Page 1: C programming session 04

Slide 1 of 31Ver. 1.0

Programming in C

In this session, you will learn to:Work with arraysAppreciate preprocessor directives

Objectives

Page 2: C programming session 04

Slide 2 of 31Ver. 1.0

Programming in CWorking with Arrays

Arrays:Are a group of similar objects.Can be defined as a contiguous area in memory.Can be referred to by a common name.Has a unique identifier for each element, called as a subscript or an index.Can be categorized as:

One-dimensional/Single-dimensional arraysMultidimensional arrays

Page 3: C programming session 04

Slide 3 of 31Ver. 1.0

Programming in COne-Dimensional Arrays

The syntax for declaring a one-dimensional array is:type arrayname[n];

For Example:char string [11];

Defines a character array named string to store 10 characters.string[0] to string[9] are for valid characters.string[10] for the string-terminator character, \0 (NULL).

Similarly:int numbers [11];

Defines an integer type array called numbers to store 11 integers.Integers are stored in numbers[0] to numbers[10].

Page 4: C programming session 04

Slide 4 of 31Ver. 1.0

Programming in CPractice: 3.1

1. Write the array definition statements for storing the following data:a. Ten amounts with paisa.b. Five ages to be stored in years and six non-fractional

quantities.c. A thirty character long name.

Page 5: C programming session 04

Slide 5 of 31Ver. 1.0

Programming in CPractice: 3.1 (Contd.)

Solution:a. float fnum[10];b. int age[5], qty[6];c. char name[31];

Page 6: C programming session 04

Slide 6 of 31Ver. 1.0

Programming in C

Initializing Arrays:An array can be initialized, when declared. Initialization at the time of declaration is possible only outside a function unless it is declared static.Direct initialization is possible in the following ways:char strl[7]={‘E’,’X’,’O’,’D’,’U’,’S’,’\0’};char str2[7]={"EXODUS"};

In the first case, individual elements have been moved into the array. Therefore, it is essential that the string terminator be specified explicitly. While in the second case, the string terminator gets attached automatically because a string has been assigned.

One-Dimensional Arrays (Contd.)

Page 7: C programming session 04

Slide 7 of 31Ver. 1.0

Programming in CPractice: 3.2

1. Write a function that stores the alphabets A to Z in an array by applying arithmetic on ASCII codes, given that the ASCII code of character A is 65. The function should display the string also.

2. In a file-delete utility program for 256 files the user response to whether a file is to be deleted or not is to be stored in an array as Y for yes and N for no for all files. By default the user response should be N for all files. Write a function that sets up an array and accepts user-responses.

Page 8: C programming session 04

Slide 8 of 31Ver. 1.0

Programming in C

Solution:

Microsoft Word Document

Practice: 3.2 (Contd.)

Page 9: C programming session 04

Slide 9 of 31Ver. 1.0

Programming in C

Array elements:Are referenced by using subscripts.Are integers, therefore array manipulation is possible through the use of variables.

One-Dimensional Arrays (Contd.)

Page 10: C programming session 04

Slide 10 of 31Ver. 1.0

Programming in CPractice: 3.3

1. Write a function to convert a character string into lower-case.

2. Write a function to compare two strings and to print the larger one. The strings may be compared in terms of ASCII code of each character in the strings.

Page 11: C programming session 04

Slide 11 of 31Ver. 1.0

Programming in C

Solution:

Microsoft Word Document

Practice: 3.3 (Contd.)

Page 12: C programming session 04

Slide 12 of 31Ver. 1.0

Programming in CPractice: 3.4

1. Write a function to accept up to 25 numbers and to display the highest and lowest numbers along with all the numbers.

Page 13: C programming session 04

Slide 13 of 31Ver. 1.0

Programming in C

Solution:

Microsoft Word Document

Practice: 3.4 (Contd.)

Page 14: C programming session 04

Slide 14 of 31Ver. 1.0

Programming in C

Array Addressing:To arrive at a particular element, the following formula is applied:Starting address + ( Offset number * Scaling factor) of the array

Here,Scaling factor is the number of bytes of storage space required by the specific data type.Offset number * Scaling factor gives the number of bytes to be added to the starting address of the array to get to a desired element.

One-Dimensional Arrays (Contd.)

Page 15: C programming session 04

Slide 15 of 31Ver. 1.0

Programming in CPractice: 3.5

1. Write a program in C to extract a substring from a specified position containing a specified number of characters from an input string.

Page 16: C programming session 04

Slide 16 of 31Ver. 1.0

Programming in C

Solution:

Microsoft Word Document

Practice: 3.5 (Contd.)

Page 17: C programming session 04

Slide 17 of 31Ver. 1.0

Programming in CMultidimensional Arrays

C also supports multidimensional arrays.A two-dimensional array is the simplest form of the multidimensional array.A two-dimensional array is an array of one-dimensional arrays.A two-dimensional array is also known as a two-d array.A two-d array is declared as follows:type arrayname[Row][Col];

Rules for initializing a two-d array are same as that of a one-dimensional array.The row subscript may be left blank for a more flexible declaration.

Page 18: C programming session 04

Slide 18 of 31Ver. 1.0

Programming in CPractice: 3.6

1. State whether True or False:If the number of salesmen is 5, then the declaration of the two-dimensional array and its initialization to zero would be:int s_p_array [5][4] = {

{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},

};

Page 19: C programming session 04

Slide 19 of 31Ver. 1.0

Programming in C

Solution:1. False. Note that since there are 5 salesman with 4 products,

there should be 5 sets of {}, each containing four zeroes. The correct initialization is:int s_p_array [5][4] = {

{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},

};

Practice: 3.6 (Contd.)

Page 20: C programming session 04

Slide 20 of 31Ver. 1.0

Programming in C

Two-Dimensional Character Arrays:Are typically used to create an array of strings.Use two subscripts, one for row and the other for column.Are declared as:char err_msg [row] [col];

Can be initialized at the time of declaration.

Multidimensional Arrays (Contd.)

Page 21: C programming session 04

Slide 21 of 31Ver. 1.0

Programming in CPractice: 3.7

1. Based on the books array, predict the output of the following commands:a. printf(“%c”, books[2][5]);b. printf(“%s”,books[3]);c. printf(“%d”,books[4][7]);

Page 22: C programming session 04

Slide 22 of 31Ver. 1.0

Programming in C

Solution:a. Mb. Birds, Beasts, and Relativesc. 97

Practice: 3.7 (Contd.)

Page 23: C programming session 04

Slide 23 of 31Ver. 1.0

Programming in CPractice: 3.8

1. Write the program segment to calculate the class average across students in each subject.

2. Assume that the data exists in the arrays and averages have been calculated. Write program segments, which will allow the user to query on the arrays to:a. Display Student Names and Average Marks.b. Display Subjects and Class Averages.

Page 24: C programming session 04

Slide 24 of 31Ver. 1.0

Programming in CPractice: 3.8 (Contd.)

c. Display Marks of a specific Student in a specific Subject.d. Display Subject wise marks of all students whose average is

above 80.For each option the following action needs to be taken.

OPTION ACTION

a. Display each student's name along with his average marks.

b. Display each subject along with class average on the subject.

c. Display list of students and list of subjects. Allow the user to choose one from each. Based on the numbers chosen, display the appropriate marks. Remember, subscripting in C starts from zero.

d. Check the average of each student. Wherever average exceeds 80, display the student name and the subject name and marks in each subject.

Page 25: C programming session 04

Slide 25 of 31Ver. 1.0

Programming in C

Solution:

Microsoft Word Document

Practice: 3.8 (Contd.)

Page 26: C programming session 04

Slide 26 of 31Ver. 1.0

Programming in CAppreciating Preprocessor Directives

Preprocessor directives:Are instructions to the compiler in the source code of a C program.Are not actually a part of the C language.Expand the scope of the C programming environment.Begin with a #.

#include is also a preprocessor directive.#include instructs the compiler to include the specified source file into the one which contains the #include directive.#define defines an identifier and a string constant that will be substituted for the identifier each time it is encountered in the file.

Page 27: C programming session 04

Slide 27 of 31Ver. 1.0

Programming in CAppreciating Preprocessor Directives (Contd.)

It helps in reducing the chances of inconsistency within the program and also makes the program easy to modify.Consider the following macro definition:#define TRUE 1No semicolon is required after the statement.Every time the compiler encounters the string TRUE in the program, it substitutes it with the value 1.No text substitution will occur if the identifier is enclosed within quotes.

Page 28: C programming session 04

Slide 28 of 31Ver. 1.0

Programming in CSummary

In this session, you learned that:An array can be defined as a contiguous area in memory, which can be referred to by a common name. In C, arrays can be categorized as:

One-dimensional/Single-dimensional arraysMultidimensional arrays

The syntax for declaring a one-dimensional array is as follows:type arrayname[n];

Array elements are referenced by using subscripts.The last element in a character array is reserved to store the string terminator character \0 (NULL).An array can be initialized, when declared, by specifying the values of some or all of its elements.Initialization can also be done inside the function, after the array has been declared, by accepting the values.

Page 29: C programming session 04

Slide 29 of 31Ver. 1.0

Programming in CSummary (Contd.)

To arrive at the particular element, the following formula is applied:Starting address + ( Offset number * Scaling factor) of the array

C supports multidimensional arrays.The simplest form of the multidimensional array is the two-dimensional (two-d) array.The general form of declaration of the two-d array would be:type arrayname[x][y];

Two-dimensional integer arrays are very much like one-dimensional integer arrays, the only difference being that two-dimensional arrays have two indices.Initialization of two-dimensional arrays can be done at the time of declaration itself.A better way to initialize a two-dimensional array is using the for statement.

Page 30: C programming session 04

Slide 30 of 31Ver. 1.0

Programming in CSummary (Contd.)

Two-dimensional character arrays are declared in the same way as two-dimensional integer arrays:

The first index specifies the number of strings.The second index specifies the length of the longest string plus one.

Initialization can be done along with declaration, if done outside main().Preprocessor directives are not actually a part of the C language; they expand the scope of the C programming environment.The preprocessor directives normally begin with a #.#include is also a preprocessor directive. It instructs the compiler to include the specified source file into the one which contains the #include directive.

Page 31: C programming session 04

Slide 31 of 31Ver. 1.0

Programming in CSummary (Contd.)

The #define statement can be used to declare a variable with a constant value throughout the program. It helps in:

Reducing the chances of inconsistency within the program.Making modification easier, as the value has to be changed only at one place.