Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) Explain data structure with syntax. b) List out the classification of data st

ID: 3842123 • Letter: A

Question

a) Explain data structure with syntax. b) List out the classification of data structure, and sketch a diagram of data structure classification c) Explain array data structure with example, and how to accesses element value of an array. d) Explain malloc() and free() methods. e) Write any three disadvantages about array data structure. f) Define algorithm, and what necessary criteria to be satisfied by every algorithm. g) Write any three characteristic of data structure. h) Set up an array to hold the following values, and in this order: 23, 6, 47, 35, 2, 14. Write a program to get the average of all 6 numbers.

Explanation / Answer

a) A data structure is a means of grouping number of variables or values of same or different types together in a single unit. It provides a logical way of storing and retrieving data. A very simple example of a data structure is an array whose syntax in C programming language is as follows:

int arr[] = {1, 2, 3, 5, 9};

The above array contains 5 elements of integer data type.

b) Data structures can be classified in various ways.

     (i) Primitive and Non-primitive Data structure - The atomic or indivisible structures are called primitive data types such as int, char, float etc. Where as non-atomic structures are called non-primitive data structures such as array, string etc.

    (ii) Linear and Non-linear Data Structure - The sturctures in which the data items are stored in a linear way are called linear data structures such as stacks and queues, where as the structures in which the data items are stored in a non-linear way are called non-linear data structure such as trees and graphs.

   (iii) Homogeneous and Non-homogeneous data structure - The structures in which all the data items are of the same type are called homogeneous data structure such as arrays where as in which the data items can be of different types are called non-homogeneous data structures such as structures.

c) Array is a linear data structure which stores all the data items of same type in a linear sequence each of which can directly be accessed by an index. Array stores all its data items in contiguous memory locations. It can be declared as follows:

   int cheggQuesNum[5] = {2, 4, 5, 8, 6};

The above statement defines an array of 5 elements of integer data type. The value of an array element can be accessed by indexing on the array e.g. cheggQuesNum[3] will fetch the value 8, i.e. 4th element in the array. Note that array indexing starts from 0. So, cheggQuesNum[0] will fetch the first element and so on.

int thirdQues = cheggQuesNum[2];

d) The malloc() function is used to allocate a certain amount of memory dynamically during the program execution. The memory allocation is done from the heap storage area. The malloc() function asks for the required memory from the operating system, which if available returns the address of the memory area, otherwise returns a null pointer on non-availability.

     int *ptr = ( int *) malloc (sizeof (int) * 5);

The above statement asks for memory for storing 5 integer items. If the required memory is available, ptr will hold the address of the allocated memory in heap, otherwise will contain a null pointer value.

When the amount of memory allocated using malloc() function is not needed anymore, it should be freed using the free() function to prevent from memory leak. The below statement will free the allocated memory in ptr for further use.

    free (ptr);

e) Disadvantages of array:

(i) The array size (number of items in the array) is needed to be known at the time of declaration only.

(ii) Array is fixed-size or static structure. Its size can not be changed dynamically.

(iii) Array stores all the elements in contiguous memory locations. So, it is difficult and time-consuming to insert and delete the elements in an array.

f) An algorithm list down all the steps which will work on the inputs received, perform the ncessary operations and calculations to get the desired output. An algorithm must satisfy the following criteria:

   (i) Input - Values received by the algorithm

   (ii) Output - Values produced by the algorithm

   (iii) Definiteness - Clarity and unambiguousness in the steps

   (iv) Finiteness - Termination after a finite number of steps

   (v) Effectiveness - Feasibility of the step and its role in reaching towards the termination

g) Three characteristics of data structures:

   (i) Containment of the data items - individual or grouped or another data structure

   (ii) Set of operations - insertion, deletion, search

   (iii) Rules about the relation of data items with each other

h) Program to get the average in C:

int main() {

int arr[] = {23, 6, 47, 35, 2, 14};

float sum = 0;

for (int i=0; i< 6; i++) {

     sum += i;

   }

float avg = sum/6;

printf ("Average: %f", avg);

return 0;

}