Assignment10 July 2024
Assignment10 July 2024
int main()
{
int y = 30;
func(y);
printf("%d", y);
return 0;
}
a) 40
b) 30
c) Compilation error
d) Runtime error
Solution: (b) 30
Parameters are always passed by value in C. Therefore, in the above code, value of y is not
modified using the function func().
Note that everything is passed by value in C. We only get the effect of pass by reference using
pointers.
Solution : (d) In Binary search, the elements in the list should be sorted. It is applicable only
for the ordered list. Hence Binary search in an unordered list is not an application.
Week 10: Assignment 10 Solutions
5. If for a real continuous function 𝑓(𝑥), 𝑓(𝑎) × 𝑓(𝑏) > 0, then in the range of [a,b]
for 𝑓(𝑥) = 0, there is (are)
Solution: (b). If, 𝑓(𝑎) × 𝑓(𝑏) < 0 then they have opposite signs; only then root(s) exists. Since
𝑓(𝑥) is continuous between a and b, the function needs to cross the x-axis to have 𝑓(𝑥)
solvable. Here, as 𝑓(𝑎) × 𝑓(𝑏) > 0, so the function does not cross x-axis. Hence, no root
exists.
6. Assuming an initial range [1,5], the second (at the end of 2 iterations) iterative value of
the root of 𝑡𝑒 −𝑡 − 0.3 = 0 using the bisection method is (Note: you need to find the
root, not the function value)
7. What will be output when you will execute the following C code?
#include<stdio.h>
int main()
{
short num[3][2]={2,5,11,17,23,28};
printf("%d,%d",*(num+2)[0],**(num+1));
return 0;
}
a) 23,11
b) 23,23
c) 11,17
d) 17,17
*(num+2)[0]=*(*((num+2)+0))=*(*(num+2))=*(num[2])=num[2][0]=23
And **(num+1)=*(num[1]+0)=num[1][0]=11
Week 10: Assignment 10 Solutions
#include <stdio.h>
#define A 5
#define B 8
#define C 2
int main()
{
int (*x)[A][B][C];
printf("%d", sizeof(*x));
return 0;
}
Solution: (Numeric answer) 320. Output is 5*8*2*sizeof(int) which is “320” assuming integer
size as 4 bytes.
a) o,o
b) p,g
c) g,g
d) g,r
Solution: (c) g,g
Week 10: Assignment 10 Solutions
p points to the base address of ‘programming’ i.e. p. so *(p+3)= the fourth character i.e. g.
Similarly s[3] is also g. This is a simple example of pointer arithmetic on strings.