sess-5

Upload: ashish-srivastava

Post on 05-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Sess-5

    1/59

    NIIT SEM Q/CPR/CR/SESSION 5/1/VER06/95

    REVIEW QUIZ

    #1 Through the following function, swap (a, b),the values of 2 variables passed to it have to

    be swapped :

    swap (a, b)

    int a, b;

    {

    int temp;

    temp = a;

    a = b;

    b = temp;

    return (a);

    return (b);}

  • 8/2/2019 Sess-5

    2/59

    NIIT SEM Q/CPR/CR/SESSION 5/2/VER06/95

    REVIEW QUIZ (Contd.)

    The function is invoked from main () and thevalues of variables x an y are passed to it.

    a. Is the function syntactically correct?

    b. Is the function logically correct?

    c. Will the function interchange the values of

    the variables x and y?

    If not, give the correct code.

  • 8/2/2019 Sess-5

    3/59

    NIIT SEM Q/CPR/CR/SESSION 5/3/VER06/95

    #2 Study the following C statements of 2 programfiles :

    /* Program A * / /* Program B */

    main () int x = 0;

    { count (charac, str)char c, locate [20]; {

    count (&c, locate); int I = 0;

    printf (%d, x); while(str [i] != \0)

    } if (str [i+ + ] = = charac)

    x+ + ;

    }

    What declarations have to be included /modified in each of these programs ?

    REVIEW QUIZ (Contd.)

  • 8/2/2019 Sess-5

    4/59

    NIIT SEM Q/CPR/CR/SESSION 5/4/VER06/95

    #3 A program called find expects one valuefrom the command line which is a string.How are these parameters accepted by theprogram?

    Give all parameter declarations and their

    values when the following command isentered at the $ prompt:

    Find The

    REVIEW QUIZ (Contd.)

  • 8/2/2019 Sess-5

    5/59

    NIIT SEM Q/CPR/CR/SESSION 5/5/VER06/95

    #4 Given the following return statement of afunction called ret_val ():

    return (float) rate * unit_sold;

    which of the following declaration (s) for thefunction ret_val() should be included in themain program?

    a. void ret_val ();b. int ret_val ();

    c. float ret_val ();

    d. No declaration is necessary

    REVIEW QUIZ (Contd.)

  • 8/2/2019 Sess-5

    6/59

    NIIT SEM Q/CPR/CR/SESSION 5/6/VER06/95

    #5 Given that a char array has to be assignedthe string:

    LOOKUPPAGE

    state whether true or false.

    The array may be declared to be either autoor extern or declared globally, but not static.

    REVIEW QUIZ (Contd.)

  • 8/2/2019 Sess-5

    7/59

    NIIT SEM Q/CPR/CR/SESSION 5/7/VER06/95

    #6 What are the values of a and b after the fourth

    time the following function is invoked?

    find_type (c)

    char c;

    { static int a = 0, b; b = 0;

    if ((c > = A) && (c < = Z))

    a+ + ;

    else if (( c > = a) && (c < = z))b+ + ;

    }

    Assume that the values of c passed in the 4

    function calls are :

    A b c D

    REVIEW QUIZ (Contd.)

  • 8/2/2019 Sess-5

    8/59

    NIIT SEM Q/CPR/CR/SESSION 5/8/VER06/95

    #1 a. Yes

    b. No, because a function cannot return 2values.

    c. No, since it is a call by value, the values of

    x and y get copied to memory variables aand b respectively. The changes to theseare not reflected in x and y.

    A call by reference should be made insteadof a call by value.

    So, the correct code is :

    SOLUTIONS TO REVIEW QUIZ

  • 8/2/2019 Sess-5

    9/59

    NIIT SEM Q/CPR/CR/SESSION 5/9/VER06/95

    swap (a, b) /* interchange *a and *b */

    int *a, *b;

    {

    int temp;

    temp = *b;*b = *a;

    *a = temp;

    }

    #2 In program A:

    extern int x; /* within main () */

    In program B:

    char *charac, str []; /* after count (charac,str) */

    SOLUTIONS TO REVIEW QUIZ

    (Contd.)

  • 8/2/2019 Sess-5

    10/59

    NIIT SEM Q/CPR/CR/SESSION 5/10/VER06/95

    #3 The declarations are :main (argc, argv)

    int argc;

    char *argv [];

    Value of argc will be 2.Value of argv [0] will be find.

    Value of argv [1] will be The.

    #4 c.

    #5 False

    #6 The variable a will be 2.

    The variable b will be O (b gets initialized

    each time the function is invoked).

    SOLUTIONS TO REVIEW QUIZ

    (Contd.)

  • 8/2/2019 Sess-5

    11/59

    NIIT SEM Q/CPR/CR/SESSION 5/11/VER06/95

    Objectives

    At the end of this session, you will be able to:

    Write C programs that invoke functionsthrough :

    Call by reference Call by value

    Write C programs using void type functionsand functions that return values of different

    data types Explain the structure of the function main ()

    and write C programs which handle commandline parameters

    SPL SESSION

  • 8/2/2019 Sess-5

    12/59

    NIIT SEM Q/CPR/CR/SESSION 5/12/VER06/95

    Objectives (Contd.)

    State the features of the following datastorage types :

    Auto

    Static

    Extern

    and use these in single and multiple Cprogram files

    Use the following standard string comparisonfunctions of c:

    strcmp ()

    strcpy ()

    strcat ()

    strlen ()

    SPL SESSION (Contd.)

  • 8/2/2019 Sess-5

    13/59

    NIIT SEM Q/CPR/CR/SESSION 5/13/VER06/95

    Objectives (contd.) Use the following string to numeric conversion

    function:

    atoi ()

    atof () Use the following functions for formatting

    strings in memory:

    sscanf ()

    sprintf ()

    SPL SESSION (Contd.)

  • 8/2/2019 Sess-5

    14/59

    NIIT SEM Q/CPR/CR/SESSION 5/14/VER06/95

    Data that the function must receive when calledfrom another function

    A function may/may not have parameters

    Example

    main (){

    disp_head ();

    }

    disp_head () /* no parameters */{

    printf (EMPLOYEE REPORT);

    }

    disp_head () does not have parameters

    PARAMETERS OF A FUNCTION

  • 8/2/2019 Sess-5

    15/59

    NIIT SEM Q/CPR/CR/SESSION 5/15/VER06/95

    Example

    The following function has two parameters, aand b:

    main ()

    {:

    sum (x, y);

    }

    sum (a, b) /*2 parameters */int a, b;

    {

    int value;

    value = a + b;

    }

    PARAMETERS OF A FUNCTION

    (Contd.)

  • 8/2/2019 Sess-5

    16/59

    NIIT SEM Q/CPR/CR/SESSION 5/16/VER06/95

    Parameters are of same data type as that ofvalues received

    Declarations must be made outside opening bracefor function

    Parameters may be passed :

    By value

    By reference

    PARAMETERS OF A FUNCTION

    (Contd.)

  • 8/2/2019 Sess-5

    17/59

    NIIT SEM Q/CPR/CR/SESSION 5/17/VER06/95

    Values of variables passed get copied into the

    memory locations of the parameters

    Example

    Contents of main ()

    {

    num1 num2 operator :

    2 3 * calc (num1,

    num2,operator

    :2 3 * }

    val1 val2 oper calc (val1, val2, oper)

    int val1, val2;

    char oper;

    { : }

    INVOKING FUNCTIONS

    THROUGH CALL BY VALUE

  • 8/2/2019 Sess-5

    18/59

    NIIT SEM Q/CPR/CR/SESSION 5/18/VER06/95

    Addresses of variables passed get copied intothe memory locations of the parameters

    Example

    Contents ofnum1 num2 operator main ()

    105 120 13 {

    2 3 + :

    addresses passed calc ( &num1,

    105 120 130 &num2 ,&oper);

    val1 val2 oper :

    }

    INVOKING FUNCTIONS

    THROUGH CALL BY

    REFERENCE

  • 8/2/2019 Sess-5

    19/59

    NIIT SEM Q/CPR/CR/SESSION 5/19/VER06/95

    2 + 3 calc (val1, val2, oper)

    int *val1, *val2;

    val1 points to num1 char *oper;

    val2 points to num2 {

    :

    :printf (%d,*val1+ *val1);

    }

    INVOKING FUNCTIONS

    THROUGH CALL BY

    REFERENCE (Contd.)

    INVOKING FUNCTIONS

  • 8/2/2019 Sess-5

    20/59

    NIIT SEM Q/CPR/CR/SESSION 5/20/VER06/95

    Arrays are inherently passed through call byreference

    Example

    main ()

    {

    int str [10];

    :

    stringfunc (str);}

    stringfunc (number_list)

    int number_list [ ]; /* no subscript*/

    {: }

    INVOKING FUNCTIONS

    THROUGH CALL BY

    REFERENCE (Contd.)

    INVOKING FUNCTIONS

  • 8/2/2019 Sess-5

    21/59

    NIIT SEM Q/CPR/CR/SESSION 5/21/VER06/95

    ORstringfunc (number_list)

    int number_list [ 10 ]; /* same size asnum_array*/

    {

    :

    }

    ORstringfunc (number_list)

    int *number_list: /* int type pointer because */

    /* number_list is int type array */

    { :}

    INVOKING FUNCTIONS

    THROUGH CALL BY

    REFERENCE (Contd.)

    RETURNING VALUES FROM

  • 8/2/2019 Sess-5

    22/59

    NIIT SEM Q/CPR/CR/SESSION 5/22/VER06/95

    The return Statement

    Sends back one value to the caller

    Example

    sum (a, b)

    int a, b;{

    return a + b; /* value of a + b returned */

    }

    Different values may be returned based onconditions

    RETURNING VALUES FROM

    FUNCTIONS

    RETURNING VALUES FROM

  • 8/2/2019 Sess-5

    23/59

    NIIT SEM Q/CPR/CR/SESSION 5/23/VER06/95

    Example

    diff (a, b)

    int a, b;

    {

    if (a > b)return a - b;

    else if (b > a)

    return b - a;

    else

    return O;

    }

    Default return value is int type

    Also transfers control back to calling function

    RETURNING VALUES FROM

    FUNCTIONS

    RETURNING VALUES FROM

  • 8/2/2019 Sess-5

    24/59

    NIIT SEM Q/CPR/CR/SESSION 5/24/VER06/95

    Other data types may also be returned

    Example

    main ()

    {

    float sum (); /* function declared float */float x, y, value ;

    scanf (%f, &x, &y);

    fflush (stdin);

    value = sum (x, y);

    printf (Total is %f\n, value);

    }

    RETURNING VALUES FROM

    FUNCTIONS (Contd.)

    RETURNING VALUES FROM

  • 8/2/2019 Sess-5

    25/59

    NIIT SEM Q/CPR/CR/SESSION 5/25/VER06/95

    float sum (a, b) /*function declared float */float a, b;

    {

    return a+b;

    }

    Function must be declared as type of value itreturns

    RETURNING VALUES FROM

    FUNCTIONS (Contd.)

    Void TYPE FUNCTIONS

  • 8/2/2019 Sess-5

    26/59

    NIIT SEM Q/CPR/CR/SESSION 5/26/VER06/95

    Functions that simply return control to caller

    without returning a value

    Example

    main ()

    {void sum (); /* sum () declared void */

    float x, y; scanf (%f %f, &x, &y); sum (x, y);

    }

    void sum (a, b)float a, b;

    {

    printf (%f, a + b);

    return; /* sends control back */

    }

    Void TYPE FUNCTIONS

    THE i () FUNCTION

  • 8/2/2019 Sess-5

    27/59

    NIIT SEM Q/CPR/CR/SESSION 5/27/VER06/95

    Standard type of parameters are used toreceive arguments from command line

    main (argc, argv)

    int argc;

    char *argv [ ];{

    :

    }

    argv 100 106 125 135

    argv [0] 100

    argv [1] 106

    : : :Pointer to an Array

    THE main () FUNCTION

    THE main () FUNCTION

  • 8/2/2019 Sess-5

    28/59

    NIIT SEM Q/CPR/CR/SESSION 5/28/VER06/95

    Usage of Command Line Arguments

    Upper Abracadabra (entered on command line to

    execute a program called upper)

    argc =2 (2 words entered at command line)

    argv [0] 100 u p p e r /0

    (first word on command line)

    argv [1] 106 A b r a c a d a b r a /0

    (second word on command

    line, i.e. first argument)

    Pointer to an Array (char type)

    THE main () FUNCTION

    (Contd.)

    DATA STORAGE TYPES

  • 8/2/2019 Sess-5

    29/59

    NIIT SEM Q/CPR/CR/SESSION 5/29/VER06/95

    Storage Created Initialized Can be

    Type accessed

    auto Each time Can be initia- Only in the

    (default) the function lized at time function inis invoked of declara- which

    tion or later declared

    static The first time Initialized at Only in the

    the function time of decl- function in

    is invoked aration so whichthat the value declared

    is not reset;

    static arrays

    can also be initialized

    DATA STORAGE TYPES

    DATA STORAGE TYPES (Contd )

  • 8/2/2019 Sess-5

    30/59

    NIIT SEM Q/CPR/CR/SESSION 5/30/VER06/95

    Storage Created Initialized Can be

    Type accessed

    extern Created at Initialized In any

    the time where decl- function

    where decl- ared global in sameared as global since mem- /different

    but not where ory is reser- program

    declared ext- ved then file

    ern

    DATA STORAGE TYPES (Contd.)

    STANDARD STRING-HANDLING

  • 8/2/2019 Sess-5

    31/59

    NIIT SEM Q/CPR/CR/SESSION 5/31/VER06/95

    STRCMP ()

    Compares 2 strings (its parameters) characterby character (ASCII comparison)

    Returns any of the following integer values :

    Return value Meaning Example

    < 0 ASCII value of y= strcmp (ABC,

    character of first abc);

    string is less than returns-32 (ASCII

    that of correspon- differenceding character of between A and a)

    second string

    STANDARD STRING HANDLING

    FUNCTIONS

    STANDARD STRING-HANDLING

  • 8/2/2019 Sess-5

    32/59

    NIIT SEM Q/CPR/CR/SESSION 5/32/VER06/95

    Return value Meaning Example

    0 Strings are identi- y = strcmp (ABC,

    cal ABC);

    returns 0

    > 0 ASCII value of y = strcmp (abc,character of first ABC);

    string is greater returns 32 (ASCII

    than that of corre- difference

    sponding charac- between a and Ater of second stri-

    ng

    FUNCTIONS (Contd.)

    STANDARD STRING-HANDLING

    FUNCTIONS

  • 8/2/2019 Sess-5

    33/59

    NIIT SEM Q/CPR/CR/SESSION 5/33/VER06/95

    strcpy ()

    Copies second string to the first string namesas the parameter

    Example

    strcpy (str1, ABC);

    copies string ABC to array str1

    strcpy (str1, str2);

    copies contents of array str2 to array str1

    Current contents of first array are lost

    FUNCTIONS

    STANDARD STRING-HANDLING

    FUNCTIONS (C td )

  • 8/2/2019 Sess-5

    34/59

    NIIT SEM Q/CPR/CR/SESSION 5/34/VER06/95

    strcat ()

    appends second string passed to it to end offirst string passed to it

    Example

    strcat (str1, ABC);

    adds string ABC to current contents of str1

    strcat (str1, str2);

    adds contents of array str2 to current contents

    of str1

    FUNCTIONS (Contd.)

    STANDARD STRING-HANDLING

    FUNCTIONS (C td )

  • 8/2/2019 Sess-5

    35/59

    NIIT SEM Q/CPR/CR/SESSION 5/35/VER06/95

    strlen ()

    Returns length of string passed to it

    Length does not include NULL character

    Example

    If str1 contains 1234y = strlen (str1);

    then y = 4

    FUNCTIONS (Contd.)

    STANDARD STRING - TO -

    NUMERIC

  • 8/2/2019 Sess-5

    36/59

    NIIT SEM Q/CPR/CR/SESSION 5/36/VER06/95

    atoi ()

    Returns int type value of string passed to it;returns 0 if string does not begin with digit

    Example

    If str1 contains string 1234

    y = atoi (str1);

    then y = 1234

    If str1 contains string ABCy = atoi (str1);

    then y = 0

    NUMERIC

    CONVERSION FUNCTIONS

    STANDARD STRING - TO -

    NUMERIC

  • 8/2/2019 Sess-5

    37/59

    NIIT SEM Q/CPR/CR/SESSION 5/37/VER06/95

    atof ()

    Returns double type value of string passed to it;returns O if string does not begin with digit for decimal

    pointExample

    If str1 contains string 1234

    y = atof (str1);

    then y = 1234.000000Following declaration must be included in program:

    double atof ();

    since it returns a non-integer value

    NUMERIC

    CONVERSION FUNCTIONS

    (Contd.)

    FORMATTING DATA IN

  • 8/2/2019 Sess-5

    38/59

    NIIT SEM Q/CPR/CR/SESSION 5/38/VER06/95

    sprintf () formats data and can control the

    width of the destination string

    Syntax

    sprintf (string, format-specification, data, );

    where:

    string is a pointer to destination string in

    memory which will contain

    formatted data as per format-

    specificationformat- is specification of how data is to be

    specification formatted

    data, is list of data to be formatted

    MEMORY

    FORMATTING DATA IN

  • 8/2/2019 Sess-5

    39/59

    NIIT SEM Q/CPR/CR/SESSION 5/39/VER06/95

    Example

    sprintf (str, %02s-%02s-%02s, 28, 8, 89);printf (str contains : %s\n, str);

    Output

    str contains : 28-08-89

    MEMORY (Contd.)

    FORMATTING DATA IN

  • 8/2/2019 Sess-5

    40/59

    NIIT SEM Q/CPR/CR/SESSION 5/40/VER06/95

    sscanf() assigns different parts of a string to

    different variablesSyntax

    sscanf (string, format-specification, variable, );

    where :

    string is a pointer to source string in

    memory which contains data to

    be assigned to the list of variables

    according to format-specificationformat- is the specification of how data is

    specification to be formatted

    variable, is the list of variables to which the

    data would be assigned after

    formatting

    MEMORY (Contd.)

    FORMATTING DATA IN

    MEMORY (C td )

  • 8/2/2019 Sess-5

    41/59

    NIIT SEM Q/CPR/CR/SESSION 5/41/VER06/95

    Example

    sscanf (str, %2d%2s%s%d, &day, suffix,

    mnth, & year);

    printf (The day is: %d, suffix is: %s, month is:

    %s, year is: %d\n, day, suffix, mnth, year);

    Example

    If the data in str is 28th August 1989:

    The day is : 28, suffix is:th, month is : August,year is: 1989

    MEMORY (Contd.)

    SPL EXERCISE

  • 8/2/2019 Sess-5

    42/59

    NIIT SEM Q/CPR/CR/SESSION 5/42/VER06/95

    #1 Given below is a C program called remdigit.c

    and a listing of errors in the program indicatedby the compiler. Go through the error listingand correct the program. Since the C compilerdoes not always give very meaningful error

    messages, go through the program carefully.1 /* search for the number of digits in aninput string and

    2 copy all characters except digits into

    another string*/3

    4 main ()

    5 {

    6 char inp , out [];

    SPL EXERCISE (Contd.)

  • 8/2/2019 Sess-5

    43/59

    NIIT SEM Q/CPR/CR/SESSION 5/43/VER06/95

    7 int numb = I = j = 0;

    8

    9 printf (Enter string :);

    10 gets (inp);

    11 fflush (stdin);12

    13 while (inp [i] != \0)

    14 {

    15 if (inp [i] > = 0 && inp [i] < = 9)16 number+ + ;

    17 else

    18 out [j+ + ] = inp [i];

    19

    SPL EXERCISE (Contd.)

  • 8/2/2019 Sess-5

    44/59

    NIIT SEM Q/CPR/CR/SESSION 5/44/VER06/95

    20 }

    21

    22 printf (Number of digits in string were :%c numb);

    23 printf (String without digits is %s, out);

    24

    25

    Error listing :

    remdigit.c, line 7: i undefined

    remdigit.c, line 7: j undefined

    remdigit.c, line 9: Enter undefined

    SPL EXERCISE (Contd.)

  • 8/2/2019 Sess-5

    45/59

    NIIT SEM Q/CPR/CR/SESSION 5/45/VER06/95

    Error listing (Contd.):

    remdigit.c, line 9: syntax error

    remdigit.c, line 11: stdin undefined

    remdigit.c, line 13: illegal indirection

    remdigit.c, line 15: illegal indirection

    remdigit.c, line 15: illegal indirection

    remdigit.c, line 16: number undefined

    remdigit.c, line 18: illegal indirectionremdigit.c, line 22: syntax error

    remdigit.c, line 25: syntax error

    SOLUTION TO SPL EXERCISE

  • 8/2/2019 Sess-5

    46/59

    NIIT SEM Q/CPR/CR/SESSION 5/46/VER06/95

    #1 /* search for the number of digits in an input

    string and copycopy all characters except except digits into

    another string */

    # include < stdio.h>

    main ()char inp [80] , out [80];

    int I, j, number;

    number = i = j = O;

    printf (Enter string :);

    gets (inp);

    fflush (stdin);

    SOLUTION TO SPL EXERCISE

    (Contd )

  • 8/2/2019 Sess-5

    47/59

    NIIT SEM Q/CPR/CR/SESSION 5/47/VER06/95

    while (inp [i] != \O)

    {if (inp [i] > = O && inp [i] < = 9

    number+ + ;

    else

    out [j+ + ] = inp [i];

    i+ + ;

    }

    printf (Number of digits in string were : %d,number);

    printf (String without digits is %s, out);

    }

    (Contd.)

    SPL EXERCISE

  • 8/2/2019 Sess-5

    48/59

    NIIT SEM Q/CPR/CR/SESSION 5/48/VER06/95

    #2 Write a program to display all the positions atwhich a character occurs in a string. Both thecharacter to be located and the string to besearched should be passed to a functioncalled nextpos (findchar, searchstr). Each time

    the function locates the character, it shouldpass back the position.

    After searching the entire string, the functionshould return the value -1.

    SOLUTION TO SPL EXERCISE

  • 8/2/2019 Sess-5

    49/59

    NIIT SEM Q/CPR/CR/SESSION 5/49/VER06/95

    #2 #include < stdio.h>

    main ()

    {

    int occur = O;

    char str [100], whichchar;

    printf (Enter a string of upto 99 characters \n);

    scanf (%99s, str);

    fflush (stdin);

    printf (Enter character to be located : );scanf (%c, &whichchar);

    fflush (stdin);

    SOLUTION TO SPL EXERCISE

    (Contd )

  • 8/2/2019 Sess-5

    50/59

    NIIT SEM Q/CPR/CR/SESSION 5/50/VER06/95

    while ((occur = nextpos (whichchar, str +

    occur )) != -1{

    printf (The character %c occurs in the

    string at position %d \n, whichchar, occur);

    }

    }

    nextpos (findchar, searchstr)

    char findchar, *searchstr;{

    static int i=0;

    (Contd.)

    SOLUTION TO SPL EXERCISE

    (Contd.)

  • 8/2/2019 Sess-5

    51/59

    NIIT SEM Q/CPR/CR/SESSION 5/51/VER06/95

    while (searchstr [i] != \0

    {

    if (searchstr [i+ + ] = = findchar)

    return i;}

    return -1;}

    (Contd.)

    CLASSROOM EXERCISE

  • 8/2/2019 Sess-5

    52/59

    NIIT SEM Q/CPR/CR/SESSION 5/52/VER06/95

    #1 Alcatel Automatics is a company known for its

    marketing success. This success has beenlargely due to its superb data analysisprograms.

    The Product Manager wants some

    modifications to the existing programs.When running the program, he should be ableto specify any of the following with the runcommand itself :

    %s for displaying the product sales by eachsalesman as a percentage of the totalsalesman sales

    %p for displaying the product sales by each

    salesman as a percentage of the total productsales

    CLASSROOM EXERCISE

    (Contd.)

  • 8/2/2019 Sess-5

    53/59

    NIIT SEM Q/CPR/CR/SESSION 5/53/VER06/95

    %i for displaying the product sales as an index

    of total sales of all products

    He should also be shown some help messageto assist him in case he forgets what to specifywith the command and should then be able togive the command again.

    Since the calculations done in this program areto be used in some other data analysisprograms also, it has been decided that thecalculations will be coded in separate functionswhich are stored together in one file called

    func. C, separate from the main () function.

    (Contd.)

    CLASSROOM EXERCISE

    (Contd.)

  • 8/2/2019 Sess-5

    54/59

    NIIT SEM Q/CPR/CR/SESSION 5/54/VER06/95

    The functions available in this file are :

    inddat () this displays the data as an index oftotal sales

    proddat () this displays the data as apercentage of total product sales

    calcprodtot () this calculates the product-wise totals

    calcsaltot () this calculates thesalesman-wise totals

    (The code of the file func.c is provided in yourLearner Guide).

    Write the code for the function main (). In casethe %s option is chosen, print the message

    Module not ready yet.

    ( )

    CLASSROOM EXERCISE

    (Contd.)

  • 8/2/2019 Sess-5

    55/59

    NIIT SEM Q/CPR/CR/SESSION 5/55/VER06/95

    as the function to display product sales as a

    percentage of the total salesman sales has notbeen coded in func.c as yet.

    Hints for Classroom Exercise

    a. Check first argument. If invalid, display

    message.b. Input data into 2-d array. Array is declared

    global so that is available to all other functions.

    c. Declare array as extern in other functions.

    d. Depending on first argument, call appropriatefunction.

    e. exit () function terminates program execution.

    ( )

    SOLUTION TO CLASSROOM

    EXERCISE

  • 8/2/2019 Sess-5

    56/59

    NIIT SEM Q/CPR/CR/SESSION 5/56/VER06/95

    #1 # include < stdio.h>

    main (argc, argv)int argc;

    char *argv [ ];

    {

    extern float salesdat [4] [4], prodtot [4],salestot [4];

    extern int ind, row, col;

    /* check parameter count and first argument*/

    SOLUTION TO CLASSROOM

    EXERCISE (Contd.)

  • 8/2/2019 Sess-5

    57/59

    NIIT SEM Q/CPR/CR/SESSION 5/57/VER06/95

    if ((argc != 2) && (strcmp (argv [1], %I) !=

    0) && (strcmp (argv [1], %p) != 0) &&(strcmp (argv [1], %s) != 0))

    {

    printf ( Usage : sales [%s] [%p] [%i]\n);

    exit ();

    }

    if (argv [1] [1] = = s)

    }printf (\nModule not ready yet.);

    exit ();}

    ( )

    SOLUTION TO CLASSROOM

    EXERCISE (Contd.)

  • 8/2/2019 Sess-5

    58/59

    NIIT SEM Q/CPR/CR/SESSION 5/58/VER06/95

    /* input sales data into 2-d array */

    for (row = O; row < 4; row+ +)

    {

    printf (Enter sales figures for salesman %d

    :\n, row + 1);

    for (col = O; col < 4; row+ +)

    {

    scanf (%f, &salesdat [row] [col]);

    fflush (stdin);

    }

    }

    ( )

    SOLUTION TO CLASSROOM

    EXERCISE (Contd.)

  • 8/2/2019 Sess-5

    59/59

    NIIT SEM Q/CPR/CR/SESSION 5/59/VER06/95

    /* execute appropriate function depending on

    first argument */

    if (strcmp (argv [1], %p) = = O)

    proddat ();

    else if (strcmp (argv [1], %i) = = O)

    inddat ();

    }