SlideShare a Scribd company logo
In this session, you will learn to: Work with arrays Appreciate preprocessor directives Objectives
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 arrays Multidimensional arrays Working with 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] . One-Dimensional Arrays
Write the array definition statements for storing the following data: Ten amounts with paisa. Five ages to be stored in years and six non-fractional quantities. A thirty character long name. Practice: 3.1
Solution: a. float fnum[10]; b. int age[5], qty[6]; c. char name[31]; Practice: 3.1 (Contd.)
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.)
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. 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. Practice: 3.2
Solution: Practice: 3.2 (Contd.)
Array elements: Are referenced by using subscripts. Are integers, therefore array manipulation is possible through the use of variables. One-Dimensional Arrays (Contd.)
Write a function to convert a character string into  lower-case. 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. Practice: 3.3
Solution: Practice: 3.3 (Contd.)
Write a function to accept up to 25 numbers and to display the highest and lowest numbers along with all the numbers. Practice: 3.4
Solution: Practice: 3.4 (Contd.)
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.)
Write a program in C to extract a substring from a specified position containing a specified number of characters from an input string. Practice: 3.5
Solution: Practice: 3.5 (Contd.)
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. Multidimensional Arrays
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}, }; Practice: 3.6
Solution: 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.)
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.)
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]); Practice: 3.7
Solution: a. M b. Birds, Beasts, and Relatives c. 97 Practice: 3.7 (Contd.)
Write the program segment to calculate the class average across students in each subject. 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: Display Student Names and Average Marks. Display Subjects and Class Averages. Practice: 3.8
Display Marks of a specific Student in a specific Subject. Display Subject wise marks of all students whose average is above 80. For each option the following action needs to be taken. Practice: 3.8 (Contd.) 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.
Solution: Practice: 3.8 (Contd.)
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. Appreciating Preprocessor Directives
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 1 No 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. Appreciating Preprocessor Directives (Contd.)
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 arrays Multidimensional 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. Summary
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. Summary (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. Summary (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. Summary (Contd.)

More Related Content

What's hot (20)

PPT
Data type in c
thirumalaikumar3
 
PPS
C programming session 05
Vivek Singh
 
PDF
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
PPTX
Computer programming(CP)
nmahi96
 
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PDF
C programming & data structure [character strings & string functions]
MomenMostafa
 
PPTX
C Programming Unit-2
Vikram Nandini
 
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PDF
Pointers-Computer programming
nmahi96
 
PDF
Functions-Computer programming
nmahi96
 
PDF
Structures-2
arshpreetkaur07
 
PDF
Computer
Hanaa Ahmed
 
PDF
Tutorial on c language programming
Sudheer Kiran
 
DOCX
Theory1&2
Dr.M.Karthika parthasarathy
 
PDF
C++
Shyam Khant
 
PDF
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
PDF
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
Gautham Rajesh
 
PPTX
C language basics
Milind Deshkar
 
PDF
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
DOC
Assignment c programming
Icaii Infotech
 
Data type in c
thirumalaikumar3
 
C programming session 05
Vivek Singh
 
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
Computer programming(CP)
nmahi96
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
C programming & data structure [character strings & string functions]
MomenMostafa
 
C Programming Unit-2
Vikram Nandini
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointers-Computer programming
nmahi96
 
Functions-Computer programming
nmahi96
 
Structures-2
arshpreetkaur07
 
Computer
Hanaa Ahmed
 
Tutorial on c language programming
Sudheer Kiran
 
C programming_MSBTE_Diploma_Pranoti Doke
Pranoti Doke
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
Gautham Rajesh
 
C language basics
Milind Deshkar
 
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
Assignment c programming
Icaii Infotech
 

Viewers also liked (14)

PPS
C programming session 03
Dushmanta Nath
 
PPSX
C language (Collected By Dushmanta)
Dushmanta Nath
 
PPS
Dacj 1-2 c
Niit Care
 
PPS
C programming session 11
Dushmanta Nath
 
PPTX
Session no 4
Saif Ullah Dar
 
PPTX
Session no 2
Saif Ullah Dar
 
PPTX
Java script session 3
Saif Ullah Dar
 
PPS
C programming session 07
Dushmanta Nath
 
PPT
Session No1
Saif Ullah Dar
 
PPS
C programming session 08
Dushmanta Nath
 
PPTX
Niit
Mahima Narang
 
PPTX
Java script Session No 1
Saif Ullah Dar
 
PPTX
An Overview of HTML, CSS & Java Script
Fahim Abdullah
 
PPTX
Session 3 Java Script
Muhammad Hesham
 
C programming session 03
Dushmanta Nath
 
C language (Collected By Dushmanta)
Dushmanta Nath
 
Dacj 1-2 c
Niit Care
 
C programming session 11
Dushmanta Nath
 
Session no 4
Saif Ullah Dar
 
Session no 2
Saif Ullah Dar
 
Java script session 3
Saif Ullah Dar
 
C programming session 07
Dushmanta Nath
 
Session No1
Saif Ullah Dar
 
C programming session 08
Dushmanta Nath
 
Java script Session No 1
Saif Ullah Dar
 
An Overview of HTML, CSS & Java Script
Fahim Abdullah
 
Session 3 Java Script
Muhammad Hesham
 
Ad

Similar to C programming session 04 (20)

DOC
Arrays and Strings
Dr.Subha Krishna
 
PPTX
Module 4- Arrays and Strings
nikshaikh786
 
PPTX
MODUL new hlgjg thaybkhvnghgpv7E_02.pptx
lomic31750
 
PPTX
Array.pptx
Govindraj Chittapur
 
PPTX
Chapter 13.pptx
AnisZahirahAzman
 
PPTX
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
yatakumar84
 
PDF
Array in C.pdf
georgejustymirobi1
 
PDF
Array.pdf
mounikanarra3
 
PDF
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
PPTX
Ch8 Arrays
SzeChingChen
 
PPTX
Array ppt you can learn in very few slides.
dwivedyp
 
PPS
C programming session 05
AjayBahoriya
 
PPTX
Arrays & Strings
Munazza-Mah-Jabeen
 
PPT
Arrays in c
vampugani
 
PPTX
3.ArraysandPointers.pptx
FolkAdonis
 
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
AntareepMajumder
 
DOCX
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
Sitamarhi Institute of Technology
 
PDF
Array&string
chanchal ghosh
 
PPTX
c unit programming arrays in detail 3.pptx
anithaviyyapu237
 
DOCX
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 
Arrays and Strings
Dr.Subha Krishna
 
Module 4- Arrays and Strings
nikshaikh786
 
MODUL new hlgjg thaybkhvnghgpv7E_02.pptx
lomic31750
 
Chapter 13.pptx
AnisZahirahAzman
 
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
yatakumar84
 
Array in C.pdf
georgejustymirobi1
 
Array.pdf
mounikanarra3
 
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
Ch8 Arrays
SzeChingChen
 
Array ppt you can learn in very few slides.
dwivedyp
 
C programming session 05
AjayBahoriya
 
Arrays & Strings
Munazza-Mah-Jabeen
 
Arrays in c
vampugani
 
3.ArraysandPointers.pptx
FolkAdonis
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
AntareepMajumder
 
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
Sitamarhi Institute of Technology
 
Array&string
chanchal ghosh
 
c unit programming arrays in detail 3.pptx
anithaviyyapu237
 
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 
Ad

More from Dushmanta Nath (6)

DOC
Global Warming Project
Dushmanta Nath
 
DOC
DBMS Practical File
Dushmanta Nath
 
DOC
Manufacturing Practice (MP) Training Project
Dushmanta Nath
 
DOC
BSNL Training Project
Dushmanta Nath
 
DOC
IT- 328 Web Administration (Practicals)
Dushmanta Nath
 
DOC
IT-314 MIS (Practicals)
Dushmanta Nath
 
Global Warming Project
Dushmanta Nath
 
DBMS Practical File
Dushmanta Nath
 
Manufacturing Practice (MP) Training Project
Dushmanta Nath
 
BSNL Training Project
Dushmanta Nath
 
IT- 328 Web Administration (Practicals)
Dushmanta Nath
 
IT-314 MIS (Practicals)
Dushmanta Nath
 

Recently uploaded (20)

PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Horarios de distribución de agua en julio
pegazohn1978
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Difference between write and update in odoo 18
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
Introduction to Indian Writing in English
Trushali Dodiya
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 

C programming session 04

  • 1. In this session, you will learn to: Work with arrays Appreciate preprocessor directives Objectives
  • 2. 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 arrays Multidimensional arrays Working with Arrays
  • 3. 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] . One-Dimensional Arrays
  • 4. Write the array definition statements for storing the following data: Ten amounts with paisa. Five ages to be stored in years and six non-fractional quantities. A thirty character long name. Practice: 3.1
  • 5. Solution: a. float fnum[10]; b. int age[5], qty[6]; c. char name[31]; Practice: 3.1 (Contd.)
  • 6. 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.)
  • 7. 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. 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. Practice: 3.2
  • 9. Array elements: Are referenced by using subscripts. Are integers, therefore array manipulation is possible through the use of variables. One-Dimensional Arrays (Contd.)
  • 10. Write a function to convert a character string into lower-case. 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. Practice: 3.3
  • 12. Write a function to accept up to 25 numbers and to display the highest and lowest numbers along with all the numbers. Practice: 3.4
  • 14. 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.)
  • 15. Write a program in C to extract a substring from a specified position containing a specified number of characters from an input string. Practice: 3.5
  • 17. 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. Multidimensional Arrays
  • 18. 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}, }; Practice: 3.6
  • 19. Solution: 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.)
  • 20. 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.)
  • 21. 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]); Practice: 3.7
  • 22. Solution: a. M b. Birds, Beasts, and Relatives c. 97 Practice: 3.7 (Contd.)
  • 23. Write the program segment to calculate the class average across students in each subject. 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: Display Student Names and Average Marks. Display Subjects and Class Averages. Practice: 3.8
  • 24. Display Marks of a specific Student in a specific Subject. Display Subject wise marks of all students whose average is above 80. For each option the following action needs to be taken. Practice: 3.8 (Contd.) 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.
  • 26. 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. Appreciating Preprocessor Directives
  • 27. 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 1 No 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. Appreciating Preprocessor Directives (Contd.)
  • 28. 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 arrays Multidimensional 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. Summary
  • 29. 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. Summary (Contd.)
  • 30. 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. Summary (Contd.)
  • 31. 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. Summary (Contd.)

Editor's Notes

  • #2: Begin the session by explaining the objectives of the session.
  • #5: Use this slide to test the student’s understanding on one-dimensional arrays.
  • #7: Tell the students that if the array is initialized at the time of declaration, then the dimension of the array need not to be given.
  • #8: Use this slide to test the student’s understanding on one-dimensional arrays.
  • #10: Explain bound checking and also tell the students that in C language, there is no bound checking for the arrays. The programmer has to take care of the array bounds. Also, discuss the hazards if the array bound is not taken care of in a program.
  • #11: Use this slide to test the student’s understanding on referencing the array elements.
  • #13: Use this slide to test the student’s understanding on referencing the array elements.
  • #16: Use this slide to test the student’s understanding on array addressing.
  • #18: Tell the students that there can be n-dimensional arrays. Multidimensional arrays can also termed as array of arrays. Explain the concept of array of arrays by taking an example of a 2-d array, which can be viewed as an array of one-dimensional arrays.
  • #19: Use this slide to test the student’s understanding on 2-D arrays.
  • #22: Use this slide to test the student’s understanding on 2-D arrays.
  • #24: Use this slide to test the student’s understanding on 2-D array.
  • #27: Only after the necessary files have been logically included and the # defined tokens are replaced with their values, the actual compilation process starts. When we enclose the file to be included within <> as in: #include <stdio.h> the preprocessor looks for the file in the directory /usr/include. If file does not exist there, an error is reported. If we include the file name within quotes instead, as in: #include “globe.h” the preprocessor looks for the file in the current directory. If the file does not exist there , it looks for it in the directory /usr/include . If it does not exist there either , an error is reported.
  • #29: Use this and the next 3 slides to summarize the session.