SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
Programming in C
Objectives


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




     Ver. 1.0                                           Slide 1 of 31
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




     Ver. 1.0                                                              Slide 2 of 31
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].



     Ver. 1.0                                                            Slide 3 of 31
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.




     Ver. 1.0                                                                   Slide 4 of 31
Programming in C
Practice: 3.1 (Contd.)


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




     Ver. 1.0                             Slide 5 of 31
Programming in C
One-Dimensional Arrays (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.




     Ver. 1.0                                                                  Slide 6 of 31
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.




     Ver. 1.0                                                              Slide 7 of 31
Programming in C
Practice: 3.2 (Contd.)


                Solution:




                            Microsoft Word
                               Document




     Ver. 1.0                                Slide 8 of 31
Programming in C
One-Dimensional Arrays (Contd.)


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




     Ver. 1.0                                                             Slide 9 of 31
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.




     Ver. 1.0                                                        Slide 10 of 31
Programming in C
Practice: 3.3 (Contd.)


                Solution:




                            Microsoft Word
                               Document




     Ver. 1.0                                Slide 11 of 31
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.




     Ver. 1.0                                                         Slide 12 of 31
Programming in C
Practice: 3.4 (Contd.)


                Solution:




                            Microsoft Word
                               Document




     Ver. 1.0                                Slide 13 of 31
Programming in C
One-Dimensional Arrays (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.




     Ver. 1.0                                                                 Slide 14 of 31
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.




     Ver. 1.0                                                           Slide 15 of 31
Programming in C
Practice: 3.5 (Contd.)


                Solution:




                            Microsoft Word
                               Document




     Ver. 1.0                                Slide 16 of 31
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.



     Ver. 1.0                                                          Slide 17 of 31
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},
                          };




     Ver. 1.0                                                            Slide 18 of 31
Programming in C
Practice: 3.6 (Contd.)


                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},
                                            };




     Ver. 1.0                                                              Slide 19 of 31
Programming in C
Multidimensional Arrays (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.




     Ver. 1.0                                                            Slide 20 of 31
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]);




     Ver. 1.0                                                           Slide 21 of 31
Programming in C
Practice: 3.7 (Contd.)


                Solution:
                 a. M
                 b. Birds, Beasts, and Relatives
                 c. 97




     Ver. 1.0                                      Slide 22 of 31
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.




     Ver. 1.0                                                        Slide 23 of 31
Programming in C
Practice: 3.8 (Contd.)


                a. Display Marks of a specific Student in a specific Subject.
                b. 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.




     Ver. 1.0                                                                                  Slide 24 of 31
Programming in C
Practice: 3.8 (Contd.)


                Solution:




                            Microsoft Word
                               Document




     Ver. 1.0                                Slide 25 of 31
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.


     Ver. 1.0                                                                Slide 26 of 31
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.




     Ver. 1.0                                                           Slide 27 of 31
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.

    Ver. 1.0                                                              Slide 28 of 31
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.
    Ver. 1.0                                                            Slide 29 of 31
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.



    Ver. 1.0                                                                   Slide 30 of 31
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.




    Ver. 1.0                                                                  Slide 31 of 31

Mais conteúdo relacionado

Mais procurados

Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
Detecting Bugs in Binaries Using Decompilation and Data Flow Analysis
Detecting Bugs in Binaries Using Decompilation and Data Flow AnalysisDetecting Bugs in Binaries Using Decompilation and Data Flow Analysis
Detecting Bugs in Binaries Using Decompilation and Data Flow AnalysisSilvio Cesare
 
Matlab intro
Matlab introMatlab intro
Matlab introfvijayami
 
MatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMOHDRAFIQ22
 
An ElGamal Encryption Scheme of Adjacency Matrix and Finite Machines
An ElGamal Encryption Scheme of Adjacency Matrix and Finite MachinesAn ElGamal Encryption Scheme of Adjacency Matrix and Finite Machines
An ElGamal Encryption Scheme of Adjacency Matrix and Finite MachinesComputer Science Journals
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notesInfinity Tech Solutions
 
Self Stabilizing Depth-first Search
Self Stabilizing Depth-first SearchSelf Stabilizing Depth-first Search
Self Stabilizing Depth-first SearchIntan Dzikria
 
02 ds and algorithm session_02
02 ds and algorithm session_0202 ds and algorithm session_02
02 ds and algorithm session_02yogeshjoshi362
 
02 ds and algorithm session_02
02 ds and algorithm session_0202 ds and algorithm session_02
02 ds and algorithm session_02Niit Care
 
Chapter3 hundred page machine learning
Chapter3 hundred page machine learningChapter3 hundred page machine learning
Chapter3 hundred page machine learningmustafa sarac
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3Vince Vo
 
Module 4 Arithmetic Coding
Module 4 Arithmetic CodingModule 4 Arithmetic Coding
Module 4 Arithmetic Codinganithabalaprabhu
 

Mais procurados (20)

Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Pcg2012 presentation
Pcg2012 presentationPcg2012 presentation
Pcg2012 presentation
 
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
 
Detecting Bugs in Binaries Using Decompilation and Data Flow Analysis
Detecting Bugs in Binaries Using Decompilation and Data Flow AnalysisDetecting Bugs in Binaries Using Decompilation and Data Flow Analysis
Detecting Bugs in Binaries Using Decompilation and Data Flow Analysis
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
C++ basics
C++ basicsC++ basics
C++ basics
 
Mmc manual
Mmc manualMmc manual
Mmc manual
 
MatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On PlottingMatLab Basic Tutorial On Plotting
MatLab Basic Tutorial On Plotting
 
An ElGamal Encryption Scheme of Adjacency Matrix and Finite Machines
An ElGamal Encryption Scheme of Adjacency Matrix and Finite MachinesAn ElGamal Encryption Scheme of Adjacency Matrix and Finite Machines
An ElGamal Encryption Scheme of Adjacency Matrix and Finite Machines
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Mm2521542158
Mm2521542158Mm2521542158
Mm2521542158
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
 
Structures
StructuresStructures
Structures
 
Ch7 structures
Ch7 structuresCh7 structures
Ch7 structures
 
Self Stabilizing Depth-first Search
Self Stabilizing Depth-first SearchSelf Stabilizing Depth-first Search
Self Stabilizing Depth-first Search
 
02 ds and algorithm session_02
02 ds and algorithm session_0202 ds and algorithm session_02
02 ds and algorithm session_02
 
02 ds and algorithm session_02
02 ds and algorithm session_0202 ds and algorithm session_02
02 ds and algorithm session_02
 
Chapter3 hundred page machine learning
Chapter3 hundred page machine learningChapter3 hundred page machine learning
Chapter3 hundred page machine learning
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 
Module 4 Arithmetic Coding
Module 4 Arithmetic CodingModule 4 Arithmetic Coding
Module 4 Arithmetic Coding
 

Destaque

C programming session 04
C programming session 04C programming session 04
C programming session 04AjayBahoriya
 
C programming session 14
C programming session 14C programming session 14
C programming session 14AjayBahoriya
 
C programming session 02
C programming session 02C programming session 02
C programming session 02AjayBahoriya
 
C programming session 10
C programming session 10C programming session 10
C programming session 10AjayBahoriya
 
C programming session 08
C programming session 08C programming session 08
C programming session 08AjayBahoriya
 
C programming session 11
C programming session 11C programming session 11
C programming session 11AjayBahoriya
 
C programming session 16
C programming session 16C programming session 16
C programming session 16AjayBahoriya
 
C programming session 07
C programming session 07C programming session 07
C programming session 07AjayBahoriya
 

Destaque (8)

C programming session 04
C programming session 04C programming session 04
C programming session 04
 
C programming session 14
C programming session 14C programming session 14
C programming session 14
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C programming session 10
C programming session 10C programming session 10
C programming session 10
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
C programming session 16
C programming session 16C programming session 16
C programming session 16
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 

Semelhante a C programming session 05

C programming session 05
C programming session 05C programming session 05
C programming session 05Vivek Singh
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Dushmanta Nath
 
C programming session 01
C programming session 01C programming session 01
C programming session 01AjayBahoriya
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotesSowri Rajan
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)Dushmanta Nath
 
Question 7 Algorithms and Their Complexitya) Write a simple progr.pdf
Question 7 Algorithms and Their Complexitya) Write a simple progr.pdfQuestion 7 Algorithms and Their Complexitya) Write a simple progr.pdf
Question 7 Algorithms and Their Complexitya) Write a simple progr.pdf4babies2010
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Kumar Boro
 
An ANSI C Superset for Vector and Superscalar Computers and Its Retargetable ...
An ANSI C Superset for Vector and Superscalar Computers and Its Retargetable ...An ANSI C Superset for Vector and Superscalar Computers and Its Retargetable ...
An ANSI C Superset for Vector and Superscalar Computers and Its Retargetable ...Ann Wera
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxKorbanMaheshwari
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptxKorbanMaheshwari
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 
C programming session 08
C programming session 08C programming session 08
C programming session 08Vivek Singh
 
05 iec t1_s1_oo_ps_session_07
05 iec t1_s1_oo_ps_session_0705 iec t1_s1_oo_ps_session_07
05 iec t1_s1_oo_ps_session_07Niit Care
 

Semelhante a C programming session 05 (20)

C programming session 05
C programming session 05C programming session 05
C programming session 05
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
Pcd201516
Pcd201516Pcd201516
Pcd201516
 
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
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
 
Arrays
ArraysArrays
Arrays
 
Question 7 Algorithms and Their Complexitya) Write a simple progr.pdf
Question 7 Algorithms and Their Complexitya) Write a simple progr.pdfQuestion 7 Algorithms and Their Complexitya) Write a simple progr.pdf
Question 7 Algorithms and Their Complexitya) Write a simple progr.pdf
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
An ANSI C Superset for Vector and Superscalar Computers and Its Retargetable ...
An ANSI C Superset for Vector and Superscalar Computers and Its Retargetable ...An ANSI C Superset for Vector and Superscalar Computers and Its Retargetable ...
An ANSI C Superset for Vector and Superscalar Computers and Its Retargetable ...
 
Visual c sharp
Visual c sharpVisual c sharp
Visual c sharp
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptx
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptx
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Arrays
ArraysArrays
Arrays
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
05 iec t1_s1_oo_ps_session_07
05 iec t1_s1_oo_ps_session_0705 iec t1_s1_oo_ps_session_07
05 iec t1_s1_oo_ps_session_07
 

Último

Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 

Último (20)

Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 

C programming session 05

  • 1. Programming in C Objectives In this session, you will learn to: Work with arrays Appreciate preprocessor directives Ver. 1.0 Slide 1 of 31
  • 2. 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 Ver. 1.0 Slide 2 of 31
  • 3. 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]. Ver. 1.0 Slide 3 of 31
  • 4. 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. Ver. 1.0 Slide 4 of 31
  • 5. Programming in C Practice: 3.1 (Contd.) Solution: a. float fnum[10]; b. int age[5], qty[6]; c. char name[31]; Ver. 1.0 Slide 5 of 31
  • 6. Programming in C One-Dimensional Arrays (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. Ver. 1.0 Slide 6 of 31
  • 7. 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. Ver. 1.0 Slide 7 of 31
  • 8. Programming in C Practice: 3.2 (Contd.) Solution: Microsoft Word Document Ver. 1.0 Slide 8 of 31
  • 9. Programming in C One-Dimensional Arrays (Contd.) Array elements: Are referenced by using subscripts. Are integers, therefore array manipulation is possible through the use of variables. Ver. 1.0 Slide 9 of 31
  • 10. 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. Ver. 1.0 Slide 10 of 31
  • 11. Programming in C Practice: 3.3 (Contd.) Solution: Microsoft Word Document Ver. 1.0 Slide 11 of 31
  • 12. 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. Ver. 1.0 Slide 12 of 31
  • 13. Programming in C Practice: 3.4 (Contd.) Solution: Microsoft Word Document Ver. 1.0 Slide 13 of 31
  • 14. Programming in C One-Dimensional Arrays (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. Ver. 1.0 Slide 14 of 31
  • 15. 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. Ver. 1.0 Slide 15 of 31
  • 16. Programming in C Practice: 3.5 (Contd.) Solution: Microsoft Word Document Ver. 1.0 Slide 16 of 31
  • 17. 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. Ver. 1.0 Slide 17 of 31
  • 18. 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}, }; Ver. 1.0 Slide 18 of 31
  • 19. Programming in C Practice: 3.6 (Contd.) 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}, }; Ver. 1.0 Slide 19 of 31
  • 20. Programming in C Multidimensional Arrays (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. Ver. 1.0 Slide 20 of 31
  • 21. 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]); Ver. 1.0 Slide 21 of 31
  • 22. Programming in C Practice: 3.7 (Contd.) Solution: a. M b. Birds, Beasts, and Relatives c. 97 Ver. 1.0 Slide 22 of 31
  • 23. 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. Ver. 1.0 Slide 23 of 31
  • 24. Programming in C Practice: 3.8 (Contd.) a. Display Marks of a specific Student in a specific Subject. b. 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. Ver. 1.0 Slide 24 of 31
  • 25. Programming in C Practice: 3.8 (Contd.) Solution: Microsoft Word Document Ver. 1.0 Slide 25 of 31
  • 26. 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. Ver. 1.0 Slide 26 of 31
  • 27. 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. Ver. 1.0 Slide 27 of 31
  • 28. 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. Ver. 1.0 Slide 28 of 31
  • 29. 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. Ver. 1.0 Slide 29 of 31
  • 30. 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. Ver. 1.0 Slide 30 of 31
  • 31. 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. Ver. 1.0 Slide 31 of 31

Notas do Editor

  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.