+ All Categories
Home > Documents > TA ZC142-L10

TA ZC142-L10

Date post: 03-Apr-2018
Category:
Upload: rajpd28
View: 223 times
Download: 0 times
Share this document with a friend

of 30

Transcript
  • 7/28/2019 TA ZC142-L10

    1/30

    TA ZC142 Computer Programming

    Lecture 10Date: 08/02/2013

  • 7/28/2019 TA ZC142-L10

    2/30

    Last Lecture

    Problem Solving Technique

    Top Down Design

    Functions

  • 7/28/2019 TA ZC142-L10

    3/30

    Todays Agenda

    Passing Array as an argument to a

    function

    One dimensional array

    Strings

    Two dimensional array

  • 7/28/2019 TA ZC142-L10

    4/30

    Passing Arrays To Functions (Pass by reference)

    One- dimensional Array:

    int Maximum(int a[], int size);

    main()

    { int val[5]={3,5,2,7,4};

    printf(%d\n,Maximum(val,5));

    }

    int Maximum(int a[], int size)

    { int i, max = a[0];

    for(i=0; i

  • 7/28/2019 TA ZC142-L10

    5/30

    Write a program using functions to sort

    elements in an 1D array. Write a function

    to read an array, print an array and sort an

    array.

    Exercise

  • 7/28/2019 TA ZC142-L10

    6/30

    void readArr(int a[],int size);

    void printArr(int a[],int size);

    void sortArr(int a[],int size);

    int main()

    {

    int a[5];

    printf("Enter elements in an array\n");

    readArr(a,5);

    printf("Display elements in Array beforesorting\n");

    printArr(a,5);

    sortArr(a,5);

    printf("Display elements in Array aftersorting\n");

    printArr(a,5);

    }

  • 7/28/2019 TA ZC142-L10

    7/30

    voidreadArr(int a[],int size)

    {

    int i;

    for(i=0;i

  • 7/28/2019 TA ZC142-L10

    8/30

    voidsortArr(int a[],int size){ int i,j,t;

    for(i=1;i

  • 7/28/2019 TA ZC142-L10

    9/30

    Exercise

    Extend the above program by adding few

    more functions to search an element in an

    array, find maximum in an array, find sum

    of elements in an array and return thesum. return the results found in each

    function to the calling function.

  • 7/28/2019 TA ZC142-L10

    10/30

    int lsearch(int a[],int size,int x);

    int max(int a[],int size);

    int sum(int a[],int size);

  • 7/28/2019 TA ZC142-L10

    11/30

    int main()

    {

    int a[5],index,x=10;

    printf("Enter elements in an array\n");readArr(a,5);

    printf("Display elements in Array before sorting\n");

    printArr(a,5);

    sortArr(a,5);

    printf("Display elements in Array after sorting\n");printArr(a,5);

    index = lsearch(a,5,x);

    if(index != -1)

    printf("Element %d is found at %d\n",x,index+1);else

    printf("Element not found\n");

    printf("Maximum element in array is %d\n",max(a,5));

    }

  • 7/28/2019 TA ZC142-L10

    12/30

    int lsearch(int a[],int size,int x)

    {

    int i,flag = 0;printf("x = %d\n",x);

    for(i=0;i

  • 7/28/2019 TA ZC142-L10

    13/30

    intmax(int a[],int size)

    {

    int i;sortArr(a,size);

    return(a[size-1]);

    }

  • 7/28/2019 TA ZC142-L10

    14/30

    Exercise

    Extend the previous program to add two

    more functions.

    (1)Search an element in an array using

    Binary search and return the position if it

    is present else display the message

    Element Not Found.

    (2)Insert an element in a sorted array if the

    element is not present in the array. And

    print the updated list.

  • 7/28/2019 TA ZC142-L10

    15/30

    Passing Strings to functions

    Function Definition:

    void fun_name(char arr_name[]){

    statements;

    }

    Function prototype:

    void fun_name(char arr_name[]);

    Calling a function:

    fun_name(arr_name);

  • 7/28/2019 TA ZC142-L10

    16/30

    Example

    voidstring_copy(char source[],chartarget[]){

    int i=0;

    printf(Source String : %s,source);for(i = 0; source[i]!=\0; i++)

    target[i] = source[i];

    target[i] = \0;

    }

  • 7/28/2019 TA ZC142-L10

    17/30

    Calling string_copy(a,b)

    int main()

    { char a[100], char b[100];

    printf(Input Source string:\n);

    scanf(%[^\n],a);

    string_copy(a,b);/* Function call */printf(\n Target String:\n %s,b);

    return 0;

    }

  • 7/28/2019 TA ZC142-L10

    18/30

    Exercise

    Write a function which takes a sentence

    and a word as input argument and finds

    the number of occurrence of the word in

    the sentence.

  • 7/28/2019 TA ZC142-L10

    19/30

    int findWord(char s[],char w[]);

    int main()

    { char sentence[MAX],word[MAX];int occ;gets(sentence);gets(word);

    occ = findWord(sentence,word);if(occ != 0)

    printf("no. of occurrences of %sare %d\n",word,occ);

    elseprintf("%s is not found \n",word);

    }

  • 7/28/2019 TA ZC142-L10

    20/30

    int findWord(char s[],char w[]){

    int i=0,j,count=0; char temp[MAX];while(s[i] != '\0')

    {j=0;

    while(s[i] != '\0' && s[i] != ' '){

    temp[j++] = s[i];

    i++;}temp[j] = '\0';if(strcmp(w,temp) == 0)

    count++;while(s[i] == ' ')

    i++;}return(count);

    }

  • 7/28/2019 TA ZC142-L10

    21/30

    Example

    Write a C program which will read a line of

    text and rewrite it in the alphabetical order.

  • 7/28/2019 TA ZC142-L10

    22/30

    void sort(char s[]);

    int main()

    {char sentence[MAX];

    gets(sentence);

    sort(sentence);

    printf("sentence after sort is \n%s \n",sentence);

    return(0);

    }

  • 7/28/2019 TA ZC142-L10

    23/30

    voidsort(char s[]){

    int i,j;

    char t;for(i=1;i

  • 7/28/2019 TA ZC142-L10

    24/30

    Write a C program to replace a particularword by another word in a given string.

    Both the words are provided by the user at

    run time.

    Exercise

  • 7/28/2019 TA ZC142-L10

    25/30

    Two Dimensional Array as a function Argument

    float fun_name(int A[][N], int M, int N)

    {statements;

    return;

    }

    main()

    {

    .

    fun_name(A,M,N);..

    }

  • 7/28/2019 TA ZC142-L10

    26/30

    Example

    Write a program using functions to find

    transpose of a matrix. Write a function to

    read a matrix, print a matrix and to find

    transpose of a matrix.

  • 7/28/2019 TA ZC142-L10

    27/30

    #define M 10

    #define N 10

    void readArr(int a[][N],int ,int );void printArr(int a[][N],int ,int );

    void transpose(int a[][N],int ,int );

    int main()

    {int i,j;

    int a[M][N];

    readArr(a,3,3);

    printArr(a,3,3);transpose(a,3,3);

    printArr(a,3,3);

    }

  • 7/28/2019 TA ZC142-L10

    28/30

    voidreadArr(int a[][N],int m,int n){

    int i,j;for(i=0;i

  • 7/28/2019 TA ZC142-L10

    29/30

    voidtranspose(int a[][N],int m,int n)

    {

    int i,j,t;

    for(i=0;i

  • 7/28/2019 TA ZC142-L10

    30/30

    Any Doubts?


Recommended