SlideShare une entreprise Scribd logo
1  sur  31
Slide 1 of 31Ver. 1.0
Programming in C
In this session, you will learn to:
Work with arrays
Appreciate preprocessor directives
Objectives
Slide 2 of 31Ver. 1.0
Programming in C
Working 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 arrays
Multidimensional arrays
Slide 3 of 31Ver. 1.0
Programming in C
One-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].
Slide 4 of 31Ver. 1.0
Programming in C
Practice: 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.
Slide 5 of 31Ver. 1.0
Programming in C
Practice: 3.1 (Contd.)
Solution:
a. float fnum[10];
b. int age[5], qty[6];
c. char name[31];
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.)
Slide 7 of 31Ver. 1.0
Programming in C
Practice: 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.
Slide 8 of 31Ver. 1.0
Programming in C
Solution:
Microsoft Word
Document
Practice: 3.2 (Contd.)
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.)
Slide 10 of 31Ver. 1.0
Programming in C
Practice: 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.
Slide 11 of 31Ver. 1.0
Programming in C
Solution:
Microsoft Word
Document
Practice: 3.3 (Contd.)
Slide 12 of 31Ver. 1.0
Programming in C
Practice: 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.
Slide 13 of 31Ver. 1.0
Programming in C
Solution:
Microsoft Word
Document
Practice: 3.4 (Contd.)
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.)
Slide 15 of 31Ver. 1.0
Programming in C
Practice: 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.
Slide 16 of 31Ver. 1.0
Programming in C
Solution:
Microsoft Word
Document
Practice: 3.5 (Contd.)
Slide 17 of 31Ver. 1.0
Programming in C
Multidimensional 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.
Slide 18 of 31Ver. 1.0
Programming in C
Practice: 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},
};
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.)
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.)
Slide 21 of 31Ver. 1.0
Programming in C
Practice: 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]);
Slide 22 of 31Ver. 1.0
Programming in C
Solution:
a. M
b. Birds, Beasts, and Relatives
c. 97
Practice: 3.7 (Contd.)
Slide 23 of 31Ver. 1.0
Programming in C
Practice: 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.
Slide 24 of 31Ver. 1.0
Programming in C
Practice: 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.
Slide 25 of 31Ver. 1.0
Programming in C
Solution:
Microsoft Word
Document
Practice: 3.8 (Contd.)
Slide 26 of 31Ver. 1.0
Programming in C
Appreciating 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.
Slide 27 of 31Ver. 1.0
Programming in C
Appreciating 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 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.
Slide 28 of 31Ver. 1.0
Programming in C
Summary
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.
Slide 29 of 31Ver. 1.0
Programming in C
Summary (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.
Slide 30 of 31Ver. 1.0
Programming in C
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.
Slide 31 of 31Ver. 1.0
Programming in C
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.

Contenu connexe

Tendances (20)

Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
 
Memory management of datatypes
Memory management of datatypesMemory management of datatypes
Memory management of datatypes
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Array
ArrayArray
Array
 
important C questions and_answers praveensomesh
important C questions and_answers praveensomeshimportant C questions and_answers praveensomesh
important C questions and_answers praveensomesh
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Midterm
MidtermMidterm
Midterm
 
Ch5 array nota
Ch5 array notaCh5 array nota
Ch5 array nota
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Aae oop xp_05
Aae oop xp_05Aae oop xp_05
Aae oop xp_05
 
Arrays
ArraysArrays
Arrays
 
Mmt 001
Mmt 001Mmt 001
Mmt 001
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
Computer
ComputerComputer
Computer
 
14 arrays
14 arrays14 arrays
14 arrays
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Arrays
ArraysArrays
Arrays
 
C# String
C# StringC# String
C# String
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 

En vedette

C programming session 04
C programming session 04C programming session 04
C programming session 04Vivek Singh
 
C programming session 16
C programming session 16C programming session 16
C programming session 16Vivek Singh
 
C programming session 10
C programming session 10C programming session 10
C programming session 10Vivek Singh
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Vivek Singh
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Vivek Singh
 
Sql create table statement
Sql create table statementSql create table statement
Sql create table statementVivek Singh
 

En vedette (6)

C programming session 04
C programming session 04C programming session 04
C programming session 04
 
C programming session 16
C programming session 16C programming session 16
C programming session 16
 
C programming session 10
C programming session 10C programming session 10
C programming session 10
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Sql create table statement
Sql create table statementSql create table statement
Sql create table statement
 

Similaire à C programming session 05

Similaire à C programming session 05 (20)

C programming session 05
C programming session 05C programming session 05
C programming session 05
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Pcd201516
Pcd201516Pcd201516
Pcd201516
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
10 array
10 array10 array
10 array
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
Programming Exam Help
 Programming Exam Help Programming Exam Help
Programming Exam Help
 
Break and continue in C
Break and continue in C Break and continue in C
Break and continue in C
 
Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 

Plus de Vivek Singh

C programming session 14
C programming session 14C programming session 14
C programming session 14Vivek Singh
 
C programming session 13
C programming session 13C programming session 13
C programming session 13Vivek Singh
 
C programming session 11
C programming session 11C programming session 11
C programming session 11Vivek Singh
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paperVivek Singh
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tipsVivek Singh
 
Sql where clause
Sql where clauseSql where clause
Sql where clauseVivek Singh
 
Sql update statement
Sql update statementSql update statement
Sql update statementVivek Singh
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sqlVivek Singh
 
Sql select statement
Sql select statementSql select statement
Sql select statementVivek Singh
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimizationVivek Singh
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimizationVivek Singh
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clauseVivek Singh
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operatorsVivek Singh
 
Sql logical operators
Sql logical operatorsSql logical operators
Sql logical operatorsVivek Singh
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraintsVivek Singh
 
Sql insert statement
Sql insert statementSql insert statement
Sql insert statementVivek Singh
 

Plus de Vivek Singh (20)

C programming session 14
C programming session 14C programming session 14
C programming session 14
 
C programming session 13
C programming session 13C programming session 13
C programming session 13
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paper
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tips
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 
Sql update statement
Sql update statementSql update statement
Sql update statement
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sql
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
Sql rename
Sql renameSql rename
Sql rename
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimization
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clause
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operators
 
Sql logical operators
Sql logical operatorsSql logical operators
Sql logical operators
 
Sql joins
Sql joinsSql joins
Sql joins
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
 
Sql insert statement
Sql insert statementSql insert statement
Sql insert statement
 
Sql index
Sql indexSql index
Sql index
 

Dernier

Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 

Dernier (20)

Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 

C programming session 05

  • 1. Slide 1 of 31Ver. 1.0 Programming in C In this session, you will learn to: Work with arrays Appreciate preprocessor directives Objectives
  • 2. Slide 2 of 31Ver. 1.0 Programming in C Working 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 arrays Multidimensional arrays
  • 3. Slide 3 of 31Ver. 1.0 Programming in C One-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].
  • 4. Slide 4 of 31Ver. 1.0 Programming in C Practice: 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.
  • 5. Slide 5 of 31Ver. 1.0 Programming in C Practice: 3.1 (Contd.) Solution: a. float fnum[10]; b. int age[5], qty[6]; c. char name[31];
  • 6. 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.)
  • 7. Slide 7 of 31Ver. 1.0 Programming in C Practice: 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.
  • 8. Slide 8 of 31Ver. 1.0 Programming in C Solution: Microsoft Word Document Practice: 3.2 (Contd.)
  • 9. 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.)
  • 10. Slide 10 of 31Ver. 1.0 Programming in C Practice: 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.
  • 11. Slide 11 of 31Ver. 1.0 Programming in C Solution: Microsoft Word Document Practice: 3.3 (Contd.)
  • 12. Slide 12 of 31Ver. 1.0 Programming in C Practice: 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.
  • 13. Slide 13 of 31Ver. 1.0 Programming in C Solution: Microsoft Word Document Practice: 3.4 (Contd.)
  • 14. 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.)
  • 15. Slide 15 of 31Ver. 1.0 Programming in C Practice: 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.
  • 16. Slide 16 of 31Ver. 1.0 Programming in C Solution: Microsoft Word Document Practice: 3.5 (Contd.)
  • 17. Slide 17 of 31Ver. 1.0 Programming in C Multidimensional 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.
  • 18. Slide 18 of 31Ver. 1.0 Programming in C Practice: 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}, };
  • 19. 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.)
  • 20. 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.)
  • 21. Slide 21 of 31Ver. 1.0 Programming in C Practice: 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]);
  • 22. Slide 22 of 31Ver. 1.0 Programming in C Solution: a. M b. Birds, Beasts, and Relatives c. 97 Practice: 3.7 (Contd.)
  • 23. Slide 23 of 31Ver. 1.0 Programming in C Practice: 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.
  • 24. Slide 24 of 31Ver. 1.0 Programming in C Practice: 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.
  • 25. Slide 25 of 31Ver. 1.0 Programming in C Solution: Microsoft Word Document Practice: 3.8 (Contd.)
  • 26. Slide 26 of 31Ver. 1.0 Programming in C Appreciating 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.
  • 27. Slide 27 of 31Ver. 1.0 Programming in C Appreciating 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 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.
  • 28. Slide 28 of 31Ver. 1.0 Programming in C Summary 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.
  • 29. Slide 29 of 31Ver. 1.0 Programming in C Summary (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.
  • 30. Slide 30 of 31Ver. 1.0 Programming in C 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.
  • 31. Slide 31 of 31Ver. 1.0 Programming in C 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.

Notes de l'éditeur

  1. Begin the session by explaining the objectives of the session.
  2. Use this slide to test the student’s understanding on one-dimensional arrays.
  3. 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.
  4. Use this slide to test the student’s understanding on one-dimensional arrays.
  5. 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.
  6. Use this slide to test the student’s understanding on referencing the array elements.
  7. Use this slide to test the student’s understanding on referencing the array elements.
  8. Use this slide to test the student’s understanding on array addressing.
  9. 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.
  10. Use this slide to test the student’s understanding on 2-D arrays.
  11. Use this slide to test the student’s understanding on 2-D arrays.
  12. Use this slide to test the student’s understanding on 2-D array.
  13. 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.
  14. Use this and the next 3 slides to summarize the session.