Professional Documents
Culture Documents
Unit 1 Recap
Unit 1 Recap
UNIT 1
RECAP
SYLLABUS CONTENT
Recap of C:
Structures
Pointers
Pointers and Array
Pointers and Structures
Recursion
Signed, Unsigned
Long, short
OVERVIEW OF C
Array
Function
Recursion
Structure
Pointer
OVERVIEW OF C: ARRAYS
Need
An array is a collection of individual data elements that is ordered, fixed in size, and homogeneous.
An array is considered to be a derived data type.
Array enables the storing and manipulation of potentially huge quantities of data.
Basics:
When defining an array in a program three things need to be specified.
the type of data it can hold, i.e., int, char, double, float, etc.
The number of values it can hold, i.e., the maximum number of elements it can hold
A name
data_type array_name [SIZE];
int data[100];
INITIALIZING ARRAYS
MULTIDIMENSIONAL ARRAYS
Arrays with more than one dimension are called multidimensional arrays.
An array of two dimensions can be declared as follows:
data_type array_name[size1][size2];
A three-dimensional array, such as a cube, can be declared as follows:
data_type array_name[size1][size2][size3]
For example, x[i] refers to an element of a one-dimensional array, x. Similarly, y[i][j] refers to
an element of a two-dimensional array, y:
DECLARE: int y[3][5]
INITIALIZE: int y[3][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
OPERATIONS ON AN ARRAY
Insert
Delete
Update
Search
Sort
Display / traverse
Merge
Split
OVERVIEW OF C: FUNCTION
FUNCTION BASICS
Function Prototype:
[return_type] function_name ( arg-type name-1,… , arg-type name-n);
Function Definition:
[return_type] funtion_name ( arg-type name-1,…, arg-type name-n)
{
statements;
}
int func1(…) /* Returns an type int. */
float func2(…) /* Returns a type float */
void finc3(…) /* Returns nothing */
FUNCTION INVOCATION
• Program Stack
• Program Counter
• Stack Frame
• Stack Frame is pushed into stack.
• Sub-routine instructions are executed.
• Stack Frame is popped from the stack.
• Now Program Counter is holding the return
address.
PARAMETER PASSING
RECURSION
RECURSION INVOCATION
RECURSION CONTD.
int factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
RECURSION CONTD.
RECURSION
struct coord
{
int x;
int y;
} first, second;
struct coord
{
int x;
int y;
};
struct coord first, second;
first.x = 50;
first.y = 100;
COPY STRUCTURE
first = second;
is equivalent to,
first.x = second.x;
first.y = second.y;
struct rectangle
{
struct coord topleft;
struct coord bottomrt;
};
struct rectangle mybox;
mybox.topleft.x = 0;
mybox.topleft.y = 10;
mybox.bottomrt.x = 100;
mybox.bottomrt.y = 200;
struct data
{
int x[4];
char y[10];
}record;
record.x[2] = 100;
ARRAY OF STRUCTURES
struct entry
{
char fname[10];
char lname[12];
char phone[8];
};
POINTER
OVERVIEW OF C: POINTER
#include <stdio.h>
void main( )
{
int i = 5 ;
int * j ; // Declaration of Pointer Variable
j = &i ; // Assigning address of variable i to variable j
printf("\n Address of i = %p", &i) ;
printf("\n Address of i = %p", j) ;
printf("\n Value of i = %d", i) ;
printf("\n Value of i = %d", *(&i)) ;
printf("\n Value of i = %d", *j) ;
}
USING POINTERS
int i1;
int i2; 0x1014 … 0x1000
int *ptr1;
0x1010 ptr2:
int *ptr2;
0x100C … 0x1000
i1 = 1; 0x1008 ptr1:
i2 = 2;
0x1004 i2: 2
3
ptr1 = &i1; 0x1000 i1: 3
1
ptr2 = ptr1;
*ptr1 = 3;
i2 = *ptr2;
DR. NEEPA SHAH 29
POINTER ARITHMETIC
pointer + number OR pointer – number
p = &a; p = &a;
p += 1; In each, p now points to b p += 1;
SCALE FACTOR
}
*y = t ;
*x and *y swap
DR. NEEPA SHAH 33
#include <stdio.h>
LAB 1
Write a Program for Inventory Updating using a structure having (char name[20], float rate, int qty, float price).
1. Ask the user to entered one record (keep price blank). Create a function to calculate the price=rate * qty and store it
for the record. Also, ask the user for updating the rate and qty and update the record and recalculate the price. Display
all the details of the record.
2. Rewrite First Program to make a single structure variable and a single pointer to structure. Assign the address of the
structure variable to the pointer and enter one record through the pointer and Update the record using Pointer.
3. Rewrite the First Program for Inventory Updating; this time creating an array of structures so that n records can be
kept. Ask the user to enter number of records and then details of all the records. Finally for updating, ask the user to
enter the record number [array subscript] to be updated with the new rate and qty and recalculate the price and
display either all records or only the updated record.