Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

data_structures_module_1_qb_and_solutions, Exams of Data Representation and Algorithm Design

This document covers the basics of arrays and pointers in C programming language. It explains the syntax of declaring and initializing arrays, types of arrays, accessing matrix elements, and pointer arithmetic. It also discusses common bugs related to pointers and memory management, such as dangling and null pointers. sample code for recursive functions to display factorial and generate Fibonacci series numbers.

Typology: Exams

2021/2022

Available from 12/31/2022

diya-misra
diya-misra 🇮🇳

2 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Department of Computer Science and Engineering
Course: 21CSDS004-Data Structures
Module 1 – Question Bank
Q1. An array is a fixed size sequenced collection of elements of the same data type .
syntax--
Type array-name[size] = {list of values}. e.g—int number[3] = {1,2,3};
Declaration and definition only reserve space for the elements in the array. No values are stored. If
we want to store values in the array, we must either initialize the elements like the above, read
values from the keyboard by scanf function using a loop construct like for loop, or assign values to
each individual element.
Q2 Thre types--One-dimensional array,two dimensional arrays and multi-dimensional arrays.
Syntax—2-dim—type array-name[row-size][col-size];
3-dim—type array-name[s1][s2][s3] where si is the size of ith dimension.
Example—int table[2][3] = {0,0,0,1,1,1} and int stat[3][5][12] .
Second example will have 180 elements.
Q3. (a) #include <stdio.h>
int main()
{ int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n); printf("Enter %d elements\n",
n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an
element\n"); scanf("%d",
&position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c]; array[position-1] =
value; printf("Resultant array is\n");
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download data_structures_module_1_qb_and_solutions and more Exams Data Representation and Algorithm Design in PDF only on Docsity!

Department of Computer Science and Engineering

Course: 21CSDS004-Data Structures

Module 1 – Question Bank

Q 1. An array is a fixed size sequenced collection of elements of the same data type. syntax-- Type array-name[size] = {list of values}. e.g—int number[3] = {1,2,3}; Declaration and definition only reserve space for the elements in the array. No values are stored. If we want to store values in the array, we must either initialize the elements like the above, read values from the keyboard by scanf function using a loop construct like for loop, or assign values to each individual element. Q2 Thre types--One-dimensional array,two dimensional arrays and multi-dimensional arrays. Syntax—2-dim— type array-name[row-size][col-size]; 3-dim—type array-name[s1][s2][s3] where si is the size of ith dimension. Example— int table[2][3] = {0,0,0,1,1,1} and int stat[3][5][12]. Second example will have 180 elements. Q 3. (a) #include <stdio.h> int main() { int array[100], position, c, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for (c = n - 1; c >= position - 1; c--) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array is\n");

for (c = 0; c <= n; c++) printf("%d\n", array[c]); return 0; } (b) #include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if (position >= n+1) printf("Deletion not possible.\n"); else { for (c = position - 1; c < n - 1; c++) array[c] = array[c+1]; printf("Resultant array:\n"); for (c = 0; c < n - 1; c++) printf("%d\n", array[c]); } return 0; } Q4.(a) //accessing a matrix elements #include<stdio.h> int main() { int i, j, a[3][3]; // i : For Counting Rows // j : For

/* Before accepting the Elements Check if number of rows and columns of both matrices is equal */ if (r1! = r2 || c1! = c2) { printf("\nOrder of two matrices is not same "); exit (0); } //Accept the Elements in Matrix 1 printf(― Enter first matrix elements :\n‖); for (i = 0; i < r1; i++) { for (j = 0; j < c1; j++) { scanf("%d", &a[i][j]); } } //Accept the Elements in Matrix 2 printf(― Enter second matrix elements :\n‖); for (i = 0; i < r1; i++){ for (j = 0; j < c1; j++) { scanf("%d", &b[i][j]); } } //Addition of two matrices printf(―Addition of two matrices is :\n‖); for (i = 0; i < r1; i++) { for (j = 0; j < c1; j++) { c[i][j] = a[i][j] + b[i][j]; printf("%d\t",c[i] [j]); } printf("
n"); } } (c) //Transpose of a Matrix #include <stdio.h> int main()

{ int m, n, c, d, matrix[10][10], transpose[10][10]; printf("Enter the number of rows and columns of a matrix\n"); scanf("%d%d", &m, &n); printf("Enter elements of the matrix\n"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &matrix[c][d]); for (c = 0; c < m; c++) for (d = 0; d < n; d++) transpose[d][c] = matrix[c][d]; printf("Transpose of the matrix:\n"); for (c = 0; c < n; c++) { for (d = 0; d < m; d++) printf("%d\t", transpose[c][d]); printf("\n"); } return 0; } Q5 —A POINTER is a variable which contains the address/location of another variable in memory. Syntax of declaration:-- datatype *pt_name; Example-- int *x; // Declaration n initialisation; Int quantity; Int *p; P = &quantity; //Ways of assigning a pointer

  1. Int x, *p = &x;
  2. Int *p = 0; int *p = NULL;
  3. Int x,y,z, *p; P = &x; p = &y; p = &z; Q6 a) The most common bugs related to pointers and memory management is dangling/wild pointers. Sometimes the programmer fails to initialize the pointer with a valid address, then this type of initialized pointer is known as a dangling pointer in C. Dangling pointer occurs at the time of the object destruction when the object is deleted or deallocated from memory without modifying the value of the pointer. In this case, the pointer is pointing to the memory, which is de-allocated. The dangling pointer can point to the memory, which contains either the program code or the code of the operating system. If we assign the value to this pointer, then it overwrites the value of the program code or operating system instructions; in such

scanf("%d", &number); fact = factorial(number); printf("Factorial of %d is %ld\n", number, fact); return 0; } long factorial( int n) { if (n == 0) return 1; else return (n * factorial(n-1)); } B) To generate first twenty Fibonacci series numbers. #include<stdio.h

int Fibonacci(int); int main() { int n=20, i = 0, c; printf("Fibonacci series\n"); for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else

return ( Fibonacci(n-1) + Fibonacci(n-2) ); } C) To generate sum of N natural numbers #include <stdio.h> int Sum(int n); int main(){ int num; printf("Enter a number: "); scanf("%d", &num); printf("Sum of %d Natural Numbers is %d.", num, Sum(num)); return 0; } int Sum(int n){ if (n != 0) return n + Sum(n - 1); else return n; } D) To find prime numbers #include <stdio.h> int primeno(int, int); int main() { int num, check; printf("Enter a number: "); scanf("%d", &num); check = primeno(num, num / 2); if (check == 1) { printf("%d is a prime number \n ", num); } else { printf("%d is not a prime number \n ", num); } return 0;

b=temp; printf("After swapping values in function a = %d, b = %d\n",a,b); } Swapping of two numbers using call by reference: #include <stdio.h> void swap( int *, int *); int main() { int a = 10; int b = 20; printf("Before swapping the values in main a = %d, b = %d
n",a,b); swap(&a,&b); printf("After swapping values in main a = %d, b = %d\n",a,b); } void swap ( int *a, int *b) { int temp; temp = a; a=b; b=temp; printf("After swapping values in function a = %d, b = %d
n",
a,
b); } Q8) With suitable example , Explain storage classes in c? There are four typesof storage classes in C 1)Automatic 2)External 3)Static 4)Register Automatic example #include <stdio.h> int main() {

int a; char b; float c; printf("%d %c %f",a,b,c); return 0; } Extern example #include <stdio.h> int a; int main() { extern int a; printf("%d",a); } Static example #include<stdio. h> static char c; static int i; static float f; static char s[100]; void main () { printf("%d %d %f %s",c,i,f); } Register example #include <stdio.h> int main() { register int a; printf("%d",a); } Q9) Explain the scope rules of a function in c with example? There are basically 4 scope rules

  1. File scope

int Sub(int num1, int num2); int num1; int Sub(int num1, int num2) { return (num1-num2); } int main(void) { printf("%d\n", Sub(10,5)); return 0; } Function Scope example: void func1() { { goto label_exec; label_exec:; } goto label_exec; } void funct2() { goto label_exec; } Q. C. Functions without Parameters n without return value: Calling function does not send any data and it does not receive any data from the called function. #include<stdio.h> void fact(); void main() { fact(); } void fact() { int i,fact=1,n; printf("Enter a Number to Find Factorial: "); scanf("%d",&n); for(i=1; i<=n; i++) { fact=fact*i; } printf("\n Factorial of %d is: %d”,n,fact); } Output: Enter a Number to Find Factorial: 5 Factorial of 5 is: 120 B. Functions with Arguments without return value:

Calling function send data to the called function and it does not receive any data from the called function. It is Single ( One-way) Type Communication #include<stdio.h> void fact(int); void main() { int num; printf("Enter a Number: "); scanf(“%d”,&num); fact(num); } void fact(int n) { int i,fact=1; for(i=1; i<=n; i+ +) { fact=facti; } printf("\nFactorial of %d is: %d",n, fact); } Output : Enter a Number : 5 Factorial of 5 is: 120 D. Functions without Parameters with return value : Calling function does not send any data but it receives data from the called function. It is Single ( One-way) Type Communication #include<stdio.h> Int fact(void); void main() { int f; f=fact() ; printf("\nFactorial of %dis:%d ",fact); } void fact() { int i,fact=1,n; printf("Enter a Number:"); scanf("%d",&n); for(i=1; i<=n; i++) { fact=facti;