Unit 4 18CSS101J Programming for Problem Solving, Slides of Programming for Engineers

Unit 4 18CSS101J Programming for Problem Solving Detailed lecture slides covering all topics as prescribed by the curriculum for the academic year. Important and Necessary Exam Material

Typology: Slides

2017/2018

Uploaded on 02/20/2022

vasundhara-jhobta
vasundhara-jhobta 🇺🇸

4.6

(8)

8 documents

1 / 97

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
18CSS101J Programming for Problem Solving
Unit IV
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61

Partial preview of the text

Download Unit 4 18CSS101J Programming for Problem Solving and more Slides Programming for Engineers in PDF only on Docsity!

18CSS101J – Programming for Problem Solving

Unit IV

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI.

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI UNIT IV INTRODUCTION Passing Array Element to Function-Formal and Actual Parameters- Advantages of using Functions-Processor Directives and #define Directives-Nested Preprocessor Macro-Advantages of using Functions- Functions-Pointers and address operator-Size of Pointer Variable and Pointer-Operator-Pointer Declaration and dereferencing-pointers-Void Pointers and size of Void Pointers-Arithmetic Operations-Incrementing Pointers-Pointers-Constant Pointers-Pointers to array elements and strings-Function Pointers-Array of Function Pointers-Accessing Array of Function Pointers-Null Pointers-Pointers

Introduction

 Multidimensional arrays are also allowed to be passed as arguments to functions.  The first dimension is omitted when a multidimensional array is used as a formal parameter in a function.

Formal and Actual Parameters One-Dimensional Array  Actual Parameters: int a[10]; Function call: add(a); here, a is the base address of the array add is the function

Formal and Actual Parameters Two-Dimensional Array  Actual Parameters: int a[10][10]; Function call: add(a); here, a is the base address of the array ‘a’ b is the base address of the array ‘b’ add is the function

Formal and Actual Parameters One-Dimensional Array  Formal Parameters: within the function body void add(int a[][10]) { .... } int a[][10] - is a formal parameter, the first dimension is omitted only column value is taken into the account.

Adding Constant five to the given array using functions(One-Dimensional)  Main Function int main() { int a[50],i,n; scanf(“%d”,&n); for(i=1;i<=n;i++) scanf(“%d”,&a[i]); add(a,n); for(i=1;i<=n;i++) printf(“%d\n”,a[i]); return 0; }

Adding Constant five to the given array using functions(One-Dimensional)  Add Function: void add(int a[],int n) { int r; for(r=1;r<=n;r++) { a[r]=a[r]+5; } }

Main Function

int main() { int num[2][2], i, j; printf("Enter 4 numbers:\n"); for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { scanf("%d", &num[i][j]); } } // passing multi-dimensional array to displayNumbers function displayNumbers(num); return 0; }

Display Numbers Function

void displayNumbers(int num[2][2]) { // Instead of the above line, // void displayNumbers(int num[][2]) is also valid int i, j; printf("Displaying:\n"); for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { printf("%d\n", num[i][j]); } } }

Preprocessor Directives MACROS

 The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation.  A macro is a segment of code which is replaced by the value of macro.  Macro is defined by #define directive.  Preprocessing directives are lines in your program that start with #.  The # is followed by an identifier that is the directive name.  For example, #define is the directive that defines a macro.  Whitespace is also allowed before and after the #.

List of preprocessor directives

#include

#define

#undef

#ifdef

#ifndef

#if

#else

#elif

#endif

#error

#pragma

#include "file"  This variant is used for header files of your own program.  It searches for a file named file first in the current directory, then in the same directories used for system header files.  The current directory is the directory of the current input file. #include anything else  This variant is called a computed #include.  Any #include directive whose argument does not fit the above two forms is a computed include.

Macro's (#define) #define token value There are two types of macros: 1.Object-like Macros

  1. Function-like Macros Object-like Macros  The object-like macro is an identifier that is replaced by value.  It is widely used to represent numeric constants. For example: #include <stdio.h> #define PI 3. main() { printf("%f",PI); } Output: