0% found this document useful (0 votes)
24 views

Practice3

Uploaded by

Tanim Ahmed
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)
24 views

Practice3

Uploaded by

Tanim Ahmed
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
You are on page 1/ 8

Assignment 3

[Topics: Recursion, Pointers, Two-dimensional array, Structures, File processing]

1) What is recursion? How does a recursive function work? What is the advantage
and disadvantage of recursion?
2) A 5-digit positive integer is entered through the keyboard, write -
a) A recursive function and
b) A non recursive function to calculate the sum of the digits.
3) Answer the following-
a) A positive integer is entered through the keyboard, write a program to
obtain the prime factors of the number.
b) Modify the function suitably to obtain the prime factors recursively.
4) Complete the following -
a) int power_raiser(int base, int power)
{
int ans;
if (power == ______)
ans = ______;
else
ans = ______ * ____________;
return (ans);
}
b) What is the output of the following program? What does function
strange compute when called with a positive integer?
c) #include <stdio.h>
int strange(int n);
int
main(void)
{
printf("%d\n", strange(7));
}
int strange(int n)
{
int ans;
if (n == 1)
ans = 0;
else
ans = 1 + strange(n / 2);
return (ans);
}
5) Answer the following -
a) Write a C program with a function named fibonacci that computes the
n- th fibonacci number.
b) Modify the function to compute the n-th fibonacci number recursively.
c) Explain what would happen if the terminating condition for function
fibonacci were just (n == 1).
6) Write a C program with a recursive function to extract capital letters from a
string.
7) Write a C program with a recursive function to take n words as input and print
them in reverse order on separate lines
8) Write a recursive functions for the following problems
a) Calculate GCD of two numbers.
b) Calculate LCM of two numbers.
9) What will be the output of the following programs:
a) # include <stdio.h>
int main( )
{
float a = 13.5 ;
float *b, *c ;
b = &a ; /* suppose address of a is 1006 */
c = b ;
printf ( "%u %u %u\n", &a, b, c ) ;
printf ( "%f %f %f %f %f\n", a, *(&a), *&a, *b, *c ) ;
return 0 ;
}
b) # include <stdio.h>
void function ( int * ) ;
int main( )
{
int i = 35, *z ;
z = function ( &i ) ;
printf ( "%d\n", z ) ;
return 0 ;
}
void function ( int *m )
{
return ( m + 2 ) ;
}

10) Answer the following:


a) Write a C program to add two numbers using pointers.
b) Write a C program to swap two numbers using pointers.
11) Write a function that receives 5 integers and returns the sum, average and
standard deviation of these numbers. Call this function from main( ) and print
the results in main( ). You have to use pointers.
12) What is a pointer to an array? Explain the relationship between array and
pointers using examples.
13) Answer the following:
a) Write a C program to copy one array to another array using pointers
and then print the new array.
b) Write a C program to reverse an array using pointers.

14) What will be the output of the following programs:


a) # include <stdio.h>
int main( )
{
int n[ 3 ][ 3 ] = {
2, 4, 3,
6, 8, 5,
3, 5, 1
} ;
printf ( "%d %d %d\n", *n, n[ 3 ][ 3 ], n[ 2 ][ 2 ] ) ;
return 0 ;
}
b)
# include <stdio.h>
int main( )
{
int n[ 3 ][ 3 ] = {
2, 4, 3,
6, 8, 5,
3, 5, 1
} ;
int i, *ptr ;
ptr = n ;
for ( i = 0 ; i <= 8 ; i++ )
printf ( "%d\n", *( ptr + i ) ) ;
return 0 ;
}
15) Write a C program to find the upper triangular matrix.
16) What is structure? Can we declare function inside a structure? Is it
necessary that all elements of structure should be different in size?
17) Answer the following:
a)
# include <stdio.h>
# include <string.h>
int main( )
{
struct part
{
char partname[50] ;
int partnumber ;
} ;
struct part p, *ptrp ;
ptrp = &p ;
strcpy ( p.partname, "CrankShaft" ) ;
p.partnumber = 102133 ;
printf ( "%s %d\n", p.partname, p.partnumber ) ;
printf ( "%s %d\n", (*ptrp).partname, (*ptrp).partnumber ) ;
printf ( "%s %d\n", ptrp->partname, ptrp->partnumber ) ;
return 0 ;
}
b)
# include <stdio.h>
struct gospel
{
int num ;
char mess1[50] ;
char mess2[50] ;
} m1 = { 2, "If you are driven by success",
"make sure that it is a quality drive"
} ;
int main( )
{
struct gospel m2, m3 ;
m2 = m1 ;
m3 = m2 ;
printf ( "%d %s %s\n", m1.num, m2.mess1, m3.mess2 ) ;
return 0 ;
}
18) Point out the errors [if any] from the following problem:
a) # include <stdio.h>
# include <string.h>
int main( )
{
struct employee
{
char name[ 25 ] ;
int age ;
float salary ;
} ;
struct employee e ;
strcpy ( e.name, "Shailesh" ) ;
age = 25 ;
salary = 15500.00 ;
printf ( "%s %d %f\n", e.name, age, salary ) ;
return 0 ;
}
b) # include <stdio.h>
struct virus
{
char signature[ 25 ] ;
char status[ 20 ] ;
int size ;
} v[ 2 ] = {
"Yankee Doodle", "Deadly", 1813,
"Dark Avenger", "Killer", 1795
} ;
int main( )
{
int i ;
for ( i = 0 ; i <=1 ; i++ )
printf ( "%s %s\n", v.signature, v.status ) ;
return 0 ;
}
19) Write a code for the following:
Create a structure named Account_info to store the name, account number
and balance of 20
customers. All the inputs will be provided by the user.
Write a function low_balance_customer() to print the names of all the
customers having balances less than $200.

20) In the previous code(19) write a function special_increament() to add $100


in the balance of all the customers having more than $1000 in their balance
and then print the incremented value of their balance.

Now, open a text file named Bank_info and write all the customers’ name and
their balance into them.

21) Write a C program to read a file and display its contents along with line
numbers before each line.
22) In C file handling Is it necessary that a file created in text mode must
always be opened in text mode for subsequent operations? What is the
purpose of the library function fflush( )?
23) Answer the following:
a) While using the following statements in C file handling-
fp = fopen ( "myfile.txt", "r" ) ;
what happens if ‘myfile.txt’ does not exist on the disk and if ‘myfile.txt’
exists on the disk.
b) While using following the statement,
fp = fopen ( "myfile.c", "wb" ) ;
what happens if ‘myfile.c’ does not exist on the disk and if ‘myfile.c’
exists on the disk.
24) Write a C program to print the elements of the following 2D array using a
pointer.
{11,22,33,44},
{55,66,77,88},
{11,66,77,44}

25) Point out the errors [if any] from the following problem:
a) # include <stdio.h>
int main( )
{
int twod[ ][ ] = {
2, 4,
6, 8
} ;
printf ( "%d\n", twod ) ;
return 0 ;
}
b) # include <stdio.h>
int main( )
{
int three[ 3 ][ ] = {
2, 4, 3,
6, 8, 2,
2, 3 ,1
} ;
printf ( "%d\n", three[ 1 ][ 1 ] ) ;
return 0 ;
}

You might also like