string csc
TRANSCRIPT
8/7/2019 string csc
http://slidepdf.com/reader/full/string-csc 1/4
LOVELY PROFESSIONAL UNIVERSITY
MODEL HOME WORK: #3
Course Code: CSE151 Course Title: COMPUTING 1
School: Department:
Name of the faculty member:
Class: Term: Section: Bat
Max. Marks: Date of Allotment: Date of Submission:
PART-A
Q1: A programmer must specify the prototype of a function if the calling statement is encountered before the definition of
the function. Can we write a program without specifying the prototype of any function? Give your answer with a
suitable example.
Q2: “Call by value parameter passing scheme does not reflect the changes done in the called program into the calling prog
Do you support the statement? Give an example to explain the phenomena .
Q3: “If a problem is solvable by using both recursion and loops then it’s better to use loops from system performance poin
of view.” Explain the fact with a suitable example.
Q4. “Preprocessing is the phase just before the compilation.” Is this statement true? Give your answer with definition of
preprocessing directives.
PART-B
Q5. Suppose you have to write a program in which you have to declare 100 variables of the same type. Take an example
of same kind of problem and write the code.
Q6. Write a program to initialize an array of strings and display the strings using pointer to strings.
Q7. Can we change the base address of an array at run time? Give your answer with proper justification.
Ans: If the compiler allocated the array at compile time, or if the array was automatically allocated as a local variab
then no, we cannot change its base address at run time. If we allocated the array at run time from the heap, we can
change its base address by allocating a new array, copying the old elements from old to new and deleting the old arra
Q8: Write a program to explain the concept of passing array elements to a function.
Ans:- Array elements can be passed to a function by calling the function by value, or by reference. In the callby value we pass values of array elements to the function, whereas in the call by reference wePass addresses of array elements to the function. These two calls are illustrated below:
/* Demonstration of call by value */
#include<stdio.h>Void display (int);
Void main ( )
8/7/2019 string csc
http://slidepdf.com/reader/full/string-csc 2/4
for ( a = 0 ; a <= 6 ; a++ )
void display ( marks[a] ) ;
}
void display ( int m )
{
printf ( "The marks of students are as follows=%d ", m ) ;
}
The output of the following code fragment is as follows:-
The marks of the students are as follows= 45 55 65 75 56 78 90
In the above program, we are passing an individual array element at a time to the
Function display ( ) and getting it printed in the function display ( ).
And now, to illustrate the call by reference
/* Demonstration of call by reference */
#include<stdio.h>void display(int);
void main ( )
{
int a;
int marks[ ] = { 45,55, 65, 75, 56,78, 90 } ;for ( a = 0 ; a <= 6 ; a++ )
{
void display ( &marks[a] ) ;
}
display ( int *n )
{printf ( "%d ", *n ) ;
}
And here’s the output...
55 65 75 56 78 78 90
Here, we are passing addresses of individual array elements to the
function display( ). Hence, the variable in which this address is
collected (n) is declared as a pointer variable. And since n contains
the address of array element, to print out the array element we are
using the ‘value at address’ operator (*).