Assignment 7:  Functions                                               Dated : 5th  March 2007


Tutorial 7A: Introduction

                    Write a C program to compute the length of a string using functions.

#include<stdio.h>

void main()

    {

        int  n;

        char string[50];

        printf("Enter the String.:");

        scanf("%[^\n]s",string);

         /* call the function */

        n = string_length(string);

        printf("Length of string = %d\n", n);

    }

 

/* definition of local function string_length */

int string_length (char string[])

    {

    /* local variable in this function   */

    int n;

    for(n=0;string[n]!='\0';n++);

 

    return n;

    }

Input: This is the first tutorial on functions.

Output: 40



Assign. 7A:

          Write a C program using functions to do the following on a given string:

1.    Display the input string in uppercase

2.    Compute the length of the string(with spaces)

3.    Compute the length of the string(without spaces)

4.    Compute the word count of the string

            Represent each as a different function.

Note: If the expression "%[^\n]s" does not work on your terminal then you can use “gets(<string variable>)”.

 

Input                                :  This is the first assignment on functions

Output

Uppercase                        :  This is the first assignment on functions

Length(with spaces)        :  41

Length(without spaces)   :  35               

Word Count                     : 7

 


Tutorial 7B: Recursion

                    Write a C program to compute the value of a given integer raised to a given positive power using a recursive function.

#include<stdio.h>

void main()

     {

     int a,b,c;

     printf("\n Enter the numbers :");

     scanf("%d %d" ,&a,&b);

     if(b<0)

          {

          printf("Sorry only positive powers are allowed.");

          exit(0);

          }

     c=power(a,b); //A Function Call//

     printf("\n The value of %d raised to the power %d is %d",a,b,c);

     }

int power(int a,int b)

     {

     int result=1;

     if(b==0)

          return result;

     else

          {

          result=a*power(a,--b);

          }

     return result;

                                     }

INPUT

a=-5,b=-8

OUTPUT

Invalid

a=2,b=8

256

a=-3,b=3

-27



Assignment 7B:

                    Write a C program to find the factorial of a positive integer using both non-recursive & recursive functions.

INPUT

4

OUTPUT

24

8

40320

-5

Invalid

20

2432902008176640000



Tutorial 7C: Pattern Generation

          Write a C program to generate the following pattern.

 

9

88

777

55555

444444

3333333

22222222

111111111

0000000000

111111111

22222222

3333333

444444

55555

6666

777

88

9

 

#include<stdio.h>

void main()

   {

   int i;

   int pattern_generator(int var1,int var2);

   for(i=9;i>=0;i--)

        {

         pattern_generator(i,i);

         printf("\n");

         }

   for(i=1;i<=9;i++)

        {

         pattern_generator(i,i);

         printf("\n");

         }

   }

 

int pattern_generator(int var1,int var2)

   {

   if(var1>9)

        return 0;

    else

        {

        pattern_generator(var1+1,var2);

        printf("%d",var2);

        }

    return 0;

    }

     



Assign 7C:

                    Write a C program using recursive functions to print the following pattern. The function once called should print a single line of the given pattern. (Maintain a macro named SIZE so the pattern can be scaled up or down in size).

*

***

*****

*******

*********

*******

*****

***

*

 



Assign. 7D: Banking System

Write a C program using functions to implement the following banking system.

1.   The program should take input details of at least two consumers (like Name, password, account number, initial balance.)

2.   Any consumer should be able to make the following transactions: Deposit, Withdraw, Balance enquiry, Money transfer, provided the password is correct.     Each of these operations should be implemented as a separate function.

3.   All sorts of error checking like insufficient balance, wrong password, and wrong account number must be notified.

4.   The whole setup can be presented to the user in the form of a menu. The user should able to choose from the set of options what he needs to do.

5.   The program should loop unless user wants to exit.

 

Hint: the functions can be of the form:

                    new_account();    

                    balance_enquiry(argument account_number);

                    deposit(argument account_number);

                    withdraw(argument account_number);

                    money_transfer(argument source_account_number destination account_number);