0% found this document useful (0 votes)
43 views15 pages

C Programming: Recursion & Structures

The document discusses lecture 7 of the MA 511: Computer Programming course, covering recursion, structures, and unions. Recursion is defined as a process where a function calls itself repeatedly until a condition is satisfied. An example factorial function is provided. Structures allow different data types for individual elements, while unions allocate the same memory for all members but only one member can be active at a time. Accessing structure members and using typedef, nested structures, and unions are also demonstrated.

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views15 pages

C Programming: Recursion & Structures

The document discusses lecture 7 of the MA 511: Computer Programming course, covering recursion, structures, and unions. Recursion is defined as a process where a function calls itself repeatedly until a condition is satisfied. An example factorial function is provided. Structures allow different data types for individual elements, while unions allocate the same memory for all members but only one member can be active at a time. Accessing structure members and using typedef, nested structures, and unions are also demonstrated.

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MA 511: Computer Programming

Lecture 7: Recursion, Structure, Union

[Link]

Partha Sarathi Mandal


psm@[Link]
Dept. of Mathematics, IIT Guwahati
Semester 1, 2010-11
Recursion
• Is a process by which a function calls itself repeatedly until some
specific condition has been satisfied.
Example:
long int factorial(int n){
If(n<=1)
return(1);
else
return(n*factorial(n-1));
}
main(){
int n;
print(“Type n = ”);
scanf(“%d”, &n);
printf(“n! = %ld ”, factorial(n));
}

MA511: Computer Programming


Partha S Mandal, IITG
The Towers of Hanoi
• It consists of three rods, and a number of
disks of different sizes which can slide onto
any rod.
• The puzzle starts with the disks neatly
stacked in order of size on one rod, the
smallest at the top, thus making a conical
shape.
• The objective of the puzzle is to move the
entire stack to another rod, obeying the
following rules:
– Only one disk may be moved at a time.
– Each move consists of taking the upper disk
from one of the pegs and sliding it onto
another rod, on top of the other disks that
may already be present on that rod.
– No disk may be placed on top of a smaller
disk.

MA511: Computer Programming


Partha S Mandal, IITG
The Towers of Hanoi
• Can you write a c program for the Towers of
Hanoi using recursion where number of disks
is input ?

MA511: Computer Programming


Partha S Mandal, IITG
Assignments
1. Write a C-program for the function f(x) defined
below:
f(x) = 2x2+ 3x + 4 for x < 2
= 0 for x = 2
= -2x2+ 3x -4 for x > 2
2. Write function to validate the elements of a
matrix of size nxn. The validation rules are:
a) All diagonal entries should be positive.
b) The matrix should be symmetric.
c) All the non-diagonal elements should be negative or
zero.
MA511: Computer Programming
Partha S Mandal, IITG
structures
array: Data Structure whose elements are all of the same data type.
Example:
int A[10]; float B[10];
structure: individual elements can differ in type:
Example:
struct account {
int acct_no;
char acct_type;
char name[80];
float balance;
};
where acct_no, acct_type, name[80], balance are the member of
the structure account.

MA511: Computer Programming


Partha S Mandal, IITG
Contd.. structures
Example:
struct account {
int acct_no;
char acct_type;
char name[80];
float balance;
} oldcustomer, newcustomer;
• oldcustomer, newcustomer are the structure
variable of type account.
MA511: Computer Programming
Partha S Mandal, IITG
Contd.. structures
Example:
struct account {
int acct_no;
char acct_type;
char name[80];
float balance;
};
• We also can declare the structure variable follows:
• struct account oldcustomer, newcustomer;
• oldcustomer, newcustomerare the structure variable of
type account.

MA511: Computer Programming


Partha S Mandal, IITG
Contd.. structures
Example:
struct account {
int acct_no;
char acct_type;
char name[80];
float balance;
}customer[100];
• This declarations implies that customer is a 100
element array of the structures of the type
account.

MA511: Computer Programming


Partha S Mandal, IITG
Example
how to access variables acct_no, acct_type, name, balance of the struct account ?
struct account {
int acct_no;
char acct_type;
char name[80];
float balance;
};
static/external struct account customer[ ] = {12, ‘S’, “abcd”, 123.0,
13, ‘C’, “wert”, 234.0,
14, ‘S’, “rsdef”, 1234.0};
or
static/external struct account customer[ ] = {{12, ‘S’, “abcd”, 123.0}, {13, ‘C’, “wert”,
234.0},{14, ‘S’, “rsdef”, 1234.0}};

static/external : storage class

MA511: Computer Programming


Partha S Mandal, IITG
Example
how to access variables acct_no, acct_type, name, balance of the struct account ?
#include<stdio.h>
void input_data(int i){
void input_data(int i); printf("Name of the customer:");
void output_data(int i); scanf(" %[^\n]", customer[i].name);
printf("Acct_no of the customer :");
struct account {
scanf("%d", &customer[i].acct_no);
int acct_no; printf("Balance of the customer :");
char acct_type; scanf("%f", &customer[i].balance);
printf("Type of the customer ");
char name[80]; scanf(" %c", &customer[i].acct_type);
float balance; }
} customer[100];
void output_data(int i){
main(){ int i, n;
printf("\n Name of the customer: %s", customer[i].name);
printf(“No of Customers? ”); printf("\n Acct_no of the customer : %d", customer[i].acct_no);
scanf(“%d”, &n); printf("\n Balance of the customer : %f", customer[i].balance);
for(i = 0; i < n; i = i + 1) printf("\n Acct_type of the customer : %c", customer[i].acct_type);
input_data(i); }
for(i = 0; i < n; i = i + 1)
output_data(i);
} //end of the main MA511: Computer Programming
Partha S Mandal, IITG
Exercise
• Write a c-programming using struct for
detecting a set of given n randomly generated
points are belonging to a given circle or not.
struct coordinates {
int x;
int y;
} points[100];

MA511: Computer Programming


Partha S Mandal, IITG
typedef
int i, j; equivalent to typedef int mydef;
mydef i, j;
typedef struct {
int acct_no;
char acct_type;
char name[80];
float balance;
} account ;
account oldcustomer, newcustomer;

MA511: Computer Programming


Partha S Mandal, IITG
Member of a struct may be a struct
Equivalent to:
typedef struct { struct date {
int day; int day;
int month;
int month;
int year;
int year; };
} date; struct account {
typedef struct { int acct_no;
int acct_no; char acct_type;
char acct_type; char name[80];
char name[80]; float balance;
float balance; struct date update;
date update; };
struct account customer[100];
} customer[100] ;
customer[i].acct_no: variable of the structure account
customer[i].[Link]: variable of the structure date

MA511: Computer Programming


Partha S Mandal, IITG
Union
Union tag { Union account {
int acct_no;
member 1;
char acct_type;
… char name[80];
member m; float balance;
}; };
• Like structures, contain members whose individual data types may
differ from one another.
• union allocates the memory equal to the maximum memory
required by the member of the union but structure allocates the
memory equal to the total memory required by the members.
• In union, one block is used by all the member of the union but in
case of structure, each member have their own memory space
• Union is useful for application where values need not be assigned
to all of the members simultaneously.

MA511: Computer Programming


Partha S Mandal, IITG

You might also like