Latest News
|
|
UNIT 3
TOPICS TO BE COVERED IN THIS UNIT:
- Arrays- concepts, declaration, definition, accessing elements, storing elements
- Arrays and Functions
- Two-dimensional and multi-dimensional arrays
- Applications of arrays
- Pointers- concepts, initialization of pointer variables
- Pointers and function arguments
- Address arithmetic
- Character pointers and functions
- Pointers to Pointers
- Pointers and multidimensional arrays
- Dynamic memory managements functions
- Command line arguments
TEACHING MATERIAL
Unit 3 Teaching Aids - Click Here
TUTORIAL MATERIAL
Tutorial 1
- Consider the array definition
int num[10]={3,3,3};
Pick the correct answer:
- num[9] is the last element of the array num
- the value of num[8] is 3
- the value of num[3] is 3
- all the above
- The following program fragment
int n[5][5],i,j,X[5][5];
for(i=0;i<5;++i)
for(j=0;j<5;j++)
X[i][j]=n[j][i];
What is resultant matrix of X.?
- transposes the matrix
- symmetric matrix
- does not alter the matrix
- none
- Consider the statement
int val[2][4]={1,2,3,4,5,6,7,8};
4 will be the value of ?
- val[1][4]
- val[0][3]
- val [1][1]
- none
- If two dimensional array is used as a formal parameter, then
- both the subscripts may be left empty
- the first(row) subscript may be left empty
- the first subscript must be left empty
- both subscripts must be left empty
- The default parameter passing mechanism of an array is
- call by value
- call by reff
- call by value result
- none
int main()
{
static int x[]={1,2,3,4,5,6,7,8};
int i, X[ 8];
for(i=2;i<6;++i)
X[x[i]]=x[i];
for(i=0;i<8;++i)
printf("%d",X[i]);
}
The output of the above program is
- 1 2 3 4 5 6 5 7 8
- 1 2 3 4 5 6 7 8
- 8 7 6 5 4 3 2 1
- some garbage values
Tutorial 2
- If m and n have been declared as integers and p1 and p2 as pointers to integers then state if any errors in the following statements :
- p1=&m;
- p2=n;
- *p1=&n;
- p2=&*&m;
- m=p2-p1;
- p1=&p2;
- m=*p1+*p1++;
- State whether each of the following statements is true (or) false (T/F)Give reasons.
- An integer can be added to a pointer [ ]
- A pointer can never be subtracted from another pointer [ ]
- When an array is passed as an argument to a function, a pointer is passed [ ]
- Pointers can not be used as formal parameters in headers to function definition [ ]
- value of a local variable in a function can be changed by another function [ ]
-
int a, b,*ptr;
ptr=&a;
a=9;
b=*ptr;
a++;
what is the value of a______b_______*ptr________?
int a,b,*ptr;
a=9;
ptr=&a;
b=*ptr;
a++;
what is the value of a______b_______*ptr________?
int a,b,*ptr;
a=9;
ptr=&a;
b=*ptr;
*ptr+=2;
what is the value of a______b_______*ptr________?
int a,b,*ptr;
a=9;
ptr=&a;
b=*ptr;
ptr=&b;
++b;
what is the value of a______b_______*ptr________?
Tutorial 3
int main()
{
int a,*b=&a, **c=&b;
a=5;
**c=15;
*b=**c;
clrscr();
printf("A=%d,B=%d",a,*b);
}
- A=15,B=15
- A=15,B=5
- A=15,B=16
- none
main()
{
int a1,a2,c=3,*pt;
pt=&c;
a1=3*(c+5);
a2=3*(*pt+5);
printf("A=%d,B=%d",a1,*pt);
}
- A=24,B=24
- A=12,B=24
- A=12,B=12
- none
- Which of these are reasons for using pointers?
- To manipulate parts of an array
- To refer to keywords such as for and if
- To return more than one value from a function
- To refer to particular programs more conveniently
- Which of the following is the correct way of declaring a float pointer?
- float ptr ;
- float *ptr ;
- *float ptr
- None of the above
- By the following declaration int a[10];
Which of the following is correct?
- &a == a
- &a == a[0]
- a == &a[0]
- a != &a[0]
Tutorial 4
- int s[5] is a one-dimensional array of integers, which of the following refers to the third element in the array?
- *( s + 2 )
- *( s + 3 )
- s + 3
- s + 2
- If an array arr contains n elements, then write a program to check if arr[0] = arr[n-1], arr[1] = arr[n-2] and so on.
- Find the smallest number in an array using pointers.
- Which of the following must be used to implement pass by reference (pass by address)?
- structure
- union
- array
- pointer
- ____ operator is used to access a member of a structure using a pointer to structure ?
- *
- .
- >
- ->
- What is the output of the following program?
main()
{
int a[10];
scanf("%d"',a);
printf("%d",*a);
}
- Compilation error as array name is used in scanf()
- Takes a value from user and displays it. The value is stored in 0th element of the array
- Takes a value from user and displays the address of that value but not the value
- None of the above
- Which function can be used to read a single character from user?
- getchar()
- fgetc()
- get()
- All there above
PRACTICE PROBLEMS
Beginner Problems
- What is the result of the following code segment?
main()
{
int a[5]={10,20,30};
printf("%d",a[3]+a[4]);
}
- write a program to reverse the given string.
- Write a program to find smallest and largest values in the array.
- What is the output of the following segment ?
main()
{
int i=0,a[5];
a[i]=i++;
printf("%d",a[i]);
}
- What is the output of the following segment?
main()
{
int a[5]={1,2,3,4,5},i;
for(i=0;i<5;i++);
printf("%d",a[i]);
}
- Write a program to swap two numbers using pointers.
- Write a program to read a string and print it in alphabetical order.
Intermediate Problems
- Write a program to generate the Fibonacci series.
- Write a program to add two matrices.
- Write a program to multiply two matrices.
- Write a program to transpose a given matrix.
- write a program to reverse the given string without using string library functions.
- Write a program to find the sum of elements in the array using pointers.
- Write a program to compare two strings without using string library functions.
- Write a program to print the sum of the upper triangle and lower triangle.
Advanced Problems
- Write a program to print the given number in words.
(eg: if n=123 then the output will be one hundred and twenty three).
- Write a program to generate the first n terms of Fibonacci series using command line arguments.
- Write a program to find whether the given string is palindrome or not without using string library functions.
- Write a program to extract a substring from a given string.
- Write a program to create a matrix of size m*n. Find the sum of each row and each column and if it is a square matrix then the sum of the diagonal elements also.
This Page Is Designed By Akash Jain
|