Professional Documents
Culture Documents
#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); }
NIIT SEM Q/CPR/CR/SESSION 5/1/VER06/95
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
SPL SESSION
Objectives At the end of this session, you will be able to: Write C programs that invoke functions through : Call by reference Call by value Write C programs using void type functions and functions that return values of different data types Explain the structure of the function main () and write C programs which handle command line parameters
NIIT
NIIT
PARAMETERS OF A FUNCTION
Data that the function must receive when called from 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
NIIT SEM Q/CPR/CR/SESSION 5/14/VER06/95
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
u p
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)
NIIT SEM Q/CPR/CR/SESSION 5/28/VER06/95
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
NIIT
SPL EXERCISE
#1 Given below is a C program called remdigit.c and a listing of errors in the program indicated by the compiler. Go through the error listing and correct the program. Since the C compiler does not always give very meaningful error messages, go through the program carefully. 1 /* search for the number of digits in an input string and 2 copy all characters except digits into another string*/ 3 4 main () 5 { 6 char inp , out [];
NIIT SEM Q/CPR/CR/SESSION 5/42/VER06/95
int numb = I = j = 0;
NIIT
NIIT
NIIT
SPL EXERCISE
#2 Write a program to display all the positions at which a character occurs in a string. Both the character to be located and the string to be searched should be passed to a function called nextpos (findchar, searchstr). Each time the function locates the character, it should pass back the position. After searching the entire string, the function should return the value -1.
NIIT
NIIT
NIIT
return -1;
NIIT
CLASSROOM EXERCISE
#1 Alcatel Automatics is a company known for its marketing success. This success has been largely due to its superb data analysis programs. The Product Manager wants some modifications to the existing programs. When running the program, he should be able to specify any of the following with the run command itself : %s for displaying the product sales by each salesman as a percentage of the total salesman sales %p for displaying the product sales by each salesman as a percentage of the total product sales
NIIT SEM Q/CPR/CR/SESSION 5/52/VER06/95
He should also be shown some help message to assist him in case he forgets what to specify with the command and should then be able to give the command again.
Since the calculations done in this program are to be used in some other data analysis programs also, it has been decided that the calculations will be coded in separate functions which are stored together in one file called func. C, separate from the main () function.
NIIT SEM Q/CPR/CR/SESSION 5/53/VER06/95
NIIT
NIIT
NIIT
fflush (stdin);
} }
NIIT SEM Q/CPR/CR/SESSION 5/58/VER06/95
inddat ();
NIIT