Saturday, December 9, 2023

c language question set 3

c language question set 3


What are enumerations?

What do the functions atoi(), itoa()?







introduction 

types of type conversions in C

Describe the different types of constants in C with example? 

what are C tokens?

What are C identifiers?

Difference between syntax vs logical error? 

Explain formatted input and output statement with examples.

return value of printf() and scanf().

What is dangling else problem? Explain how to handle tis in C programming

variable declaration vs variable definition

What is the difference between „a‟ and “a” in C?






operator

Explain the hierarchy (priority) and associativity(clubbing)of operators in „C‟ with example?

explain sizeof operator



LOOP

entry controlled loop vs exit controlled

difference between break and continue

use of goto


STRING 

1.Difference between strdup and strcpy?

2.What is the difference between Strings anD character Arrays?

3. getchar(),getch(),getche(),getc,gets()

4.puts(),putchar()

5.strlen(),strcpy(),strcmp(),strcat()

isalnum(c) Is c an alphanumeric character? 

isalpha(c) Is c an alphabetic character

 isdigit(c) Is c a digit? 

islower(c) Is c alower case letter?

 isprint(c) Is c a character?

 ispunct(c) Is c a punctuation mark?

 isspace(c) Is c a white space character?

 isupper(c) Is c an upper case letter?

 tolower(c) Convert ch to lower case

 toupper(c) Convert ch to upper case


ARRAY 

What is an array? How to declare and initialize arrays? Explain with examples

How can we declare and initialize 2D arrays? Explain with examples

IF ELSE SWITCH



pointer

What is a far pointer? Where we use it?

why is the void pointer useful? When would you use it?

What is a NULL Pointer? Whether it is same as an uninitialized pointer?

What is static memory allocation?

What is dynamic memory allocation?

. What is pointer to a pointer?

What is an array of pointers?

Difference between an array of pointers and a pointer to an array?

Discuss on pointer arithmetic?

What is the invalid pointer arithmetic?

Are the expressions *ptr ++ and ++ *ptr same?

Explain in detail how to access a one dimensional array using pointers with an example program?

Explain in detail how to access a two dimensional array using pointers with an example program?

Write in detail about pointers to functions? Explain with example program.



STRUCTURE

1. What are the differences between structures and union?

2.What are the differences between structures and arrays?

3.What is the use of typedef?

4.What the advantages of using Unions?

5. Explain array of structure and structure within a structure with an example




FILE

How would you use the functions fseek(), freed(), fwrite() and ftell()?

fopen,fclose

ftell,fseek,rewind


DYNAMIC MEMORY ALLOCATION 

1.What are the differences between malloc () and calloc ()?

What is the purpose of realloc(),free()?


PREPROCESSOR

1. What are macros? What are its advantages and disadvantages?

What is a preprocessor, what are the advantages of preprocessor?

What are the two forms of #include directive?

List and explain compiler control directives

difference  between object macro and funtion macro



FUNCTION

Difference between pass by reference and pass by value?

What is recursion?

Difference between formal argument and actual argument? 

Distinguish between Library functions and User defined functions in C and Explain with examples.

Function Declaration vs Function Definition

Explain the Parameter Passing Mechanisms in C-Language with examples.

How can we pass the Whole Array to Functions? Explain with example 

Explain function call, function definition and function prototype with examples


command line argument 

What do the ‘c’ and ‘v’ in argc and argv stand for?




storage class

What does static variable mean?

What are different types of storage classes in ‗C‘




 

Tuesday, October 10, 2023

Stack using linked list

 Stack using linked list (pranjali)



#include<stdio.h>

#include<malloc.h>

struct node

  {

    int data;

    struct node *link;

  };

 struct node * top= NULL;  //Initially stack is empty

  void push()

   {

       struct node*temp;

       temp=(struct node *)malloc(sizeof(struct node));

       printf("Enter a value");

       scanf("%d",&temp->data);

       temp->link=top;

       top=temp;

   void pop()

    {

    if(top==NULL)

    {

    printf("Stack underflow");

}

else

{

struct node*temp=top;

top=top->link;

printf("poped value=%d",temp->data);

free(temp);

}

}

    void display()

  {

  struct node *temp=top;

  if(top==NULL)

  {

  printf("Empty stack");

   }

  else

   {

 while(temp!=NULL)

    {

    printf("%d",temp->data);

    temp=temp->link;

}

  }

  

int main()

    {

    while(1)

    {

    printf("enter 1 for push\n");

    printf("enter 2 for pop\n");

    printf("enter 3 for display\n");

    printf("enter 4 for exit");

   

int choice;

    scanf("%d",&choice);

    switch(choice)

    {

    case 1:

    push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

printf("end of the program");

break;

default:

printf("wrong choice\n"); 

  }

  if (choice==4)

  break;

}

}

sparse matrix representation in 2d array

sparse matrix representation in 2d array(arik mukherjee)



 # include<iostream>

using namespace std;


int main()

{

int a[10][10],row,col,count=0,i,j,k=1;

int s[10][3];

cout<<"total number of row";

cout<<"total number of column";

cin>>row;

cin>>col;

cout<<"enter value";

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

{

cin>>a[i][j];

}

}

cout<<"matrix element\n";

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

{

cout<<a[i][j]<<" ";

}

cout<<"\n";

}

//count total number of nonzero elements

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

{

if(a[i][j]!=0)

{

count++;

}

}

}

cout<<count;

s[0][0]=row;

s[0][1]=col;

s[0][2]=count;

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

{

if(a[i][j]!=0)

{

s[k][0]=i;

s[k][1]=j;

s[k][2]=a[i][j];

k++;

}

}

}

cout<<"\n output array \n";

for(i=0;i<count+1;i++)

{

for(j=0;j<3;j++)

{

  cout<<s[i][j]<<" ";

}

cout<<"\n";

}

}

Saturday, September 30, 2023

Array insertion Deletion program


Array insertion Deletion program(edited by swapnil)


 #include<iostream>

using namespace std;


class array

{

int a[30], n;

public:

void input()

{

cout<<"Enter the length: ";

cin>>n;

cout<<"Enter a value for the array "<<endl;

for(int i=0; i<n; i++)

cin>>a[i];

}

void insert_begin()

{

int value;

cout<<"Enter a value for insertion at the front "<<endl;

cin>>value;

n=n+1;

for(int i=n-1; i>=1; i--)

{

a[i]=a[i-1];

}

a[0]=value;

}

void insert_end()

{

int value;

cout<<"Enter a value for insertion at the end "<<endl;

cin>>value;

n=n+1;

a[n-1]=value;

}

void insert_position()

{

int pos;

cout<<"Enter the position "<<endl;

cin>>pos;

int value;

cout<<"Enter the value "<<endl;

cin>>value;

for(int i=n-1; i>=pos; i--)

{

a[i]=a[i-1];

}

a[pos-1]=value;

}

void display()

{

cout<<"The value of array "<<endl;

for(int i=0; i<n; i++)

cout<<a[i]<<" ";

cout<<endl;

}

void delete_begin()

{

int value;

value=a[0];

cout<<"The deleted value is "<<value<<endl;

for(int i=1; i<=n-1; i++)

{

a[i-1]=a[i];

}

n=n-1;

}

void delete_end()

{

int value;

value=a[n-1];

cout<<"The deleted value is "<<value<<endl;

n=n-1;

}

void delete_position()

{

int value, pos;

cout<<"Enter position "<<endl;

cin>>pos;

value=a[pos-1];

cout<<"The deleted value is "<<value<<endl;

for(int i=pos; i<=n-1; i++)

{

a[i-1]=a[i];

}

n=n-1;

}

};


int main()

{

array ob;

ob.input();

while(1)

{

cout<<"Enter 1 for displaying array "<<endl;

cout<<"Enter 2 for inserting element at the front "<<endl;

cout<<"Enter 3 for inserting element at the end "<<endl;

cout<<"Enter 4 for inserting element at the given position "<<endl;

cout<<"Enter 5 for deleting element from the front "<<endl;

cout<<"Enter 6 for deleting element from the end "<<endl;

cout<<"Enter 7 for deleting element from a given position "<<endl;

cout<<"Enter 8 for exit "<<endl;

int choice;

cin>>choice;

switch(choice)

{

case 1:

ob.display();

break;

case 2:

cout<<"\nValue before insertion\n";

ob.display();

ob.insert_begin();

cout<<"\nValue after insertion\n";

ob.display();

break;

case 3:

cout<<"\nValue before insertion\n";

ob.display();

ob.insert_end();

cout<<"\nValue after insertion\n";

ob.display();

break;

case 4:

cout<<"\nValue before insertion\n";

ob.display();

ob.insert_position();

cout<<"\nValue after insertion\n";

ob.display();

break;

case 5:

cout<<"\nValue before deletion\n";

ob.display();

ob.delete_begin();

cout<<"\nValue after deletion\n";

ob.display();

break;

case 6:

cout<<"\nValue before deletion\n";

ob.display();

ob.delete_end();

cout<<"\nValue after deletion\n";

ob.display();

break;

case 7:

cout<<"\nValue before deletion\n";

ob.display();

ob.delete_position();

cout<<"\nValue after deletion\n";

ob.display();

break;

case 8:

cout<<"Exit from program";

break;

default:

cout<<"Enter a valid choice\n";

}

if(choice==8)

break;

}

return 0;

}

Wednesday, August 30, 2023

WEST BENGAL STATE UNIVERSITY NEP Syllabus for B.Sc. Computer Science Major, Minor and Interdisciplinary

 SEMESTER 1. 

CMSACOR01T: Computer Fundamentals and programming with C


Overview of C (5 Lectures) 

History, Basic Structure, 

Algorithms, 

Structured programming constructs. 

Character sets, 

Tokens, 

Keywords, 

Constants, 

Variables, Data Types, 

Declaration of storage classes. 


Operators, Expressions and Preprocessor (8 Lectures) 

Arithmetic, 

Relational, 

Logical and Assignment;

 Increment and Decrement 

and Conditional, 

Bitwise, 

Special operator, 

Operator Precedence and Associativity; 

Arithmetic Expressions,

 Evaluation of expression, 

type casting. 

Comments,

 Input and output operations. 

Understanding the Preprocessor Directives 

(#include, #define, #error, #if, #else, #elif, #endif, #ifdef, #ifndef and #undef), 

Macros 


Decision and Loop Control Structure (7 Lectures)

 If-else statements, 

Nested if-else, switch, 

Conditional operator. 

While, 

do-While, 

for loop, 

break statements, 

continue statements, 

goto statements. 


Functions and Arrays (7 Lectures) 

Utility of functions, 

Call by Value, Call by Reference, 

Functions returning value, 

Void functions,

 Inline Functions, 

Return data type of functions, 

Functions parameters, 

Differentiating between Declaration and Definition of Functions, 

Command Line Arguments/Parameters in Functions,

 Functions with variable number of Arguments. 

Creating and Using One Dimensional Arrays (Declaring and Defining an Array, 

Initializing an Array, Accessing individual elements in an Array, 

Manipulating array elements using loops), 

Use Various types of arrays (integer, float and character arrays / Strings)

 Two-dimensional Arrays (Declaring, Defining and Initializing Two Dimensional Array, Working with Rows and Columns), 

Introduction to Multi-dimensional arrays, 

return statement,

 return values and their types, 

String handling with arrays, 

String handling functions, 

recursion 


Pointers (6 Lectures) 

Definition and initialization, 

Pointer arithmetic, 

Pointers and arrays, 

String functions and manipulation, 

Dynamic storage allocation. 



User defined Datatypes and Memory Allocation (6 Lectures)

 Enumerated datatypes, 

Structures.

 Structure arrays, 

Pointers to Functions and Structures, 

Unions.

 Differentiating between static and dynamic memory allocation, 

use of malloc, calloc and free functions, 

use of new and delete operators, 

storage of variables in static and dynamic memory allocation



 File Access (6 Lectures)

 Opening and closing a file

 (use of fstream header file, ifstream, ofstream), 

Reading and writing Text Files, Using put(), get(), read() and write() functions, 

Random access in files

Monday, August 7, 2023

C program for insertion sort


#include <math.h>

#include <stdio.h>

 

/* Function to sort an array

   using insertion sort*/

void insertionSort(int arr[], int n)

{

    int i, key, j;

    for (i = 1; i < n; i++)

    {

        key = arr[i];

        j = i - 1;

 

        /* Move elements of arr[0..i-1],

           that are greater than key,

           to one position ahead of

           their current position */

        while (j >= 0 && arr[j] > key)

        {

            arr[j + 1] = arr[j];

            j = j - 1;

        }

        arr[j + 1] = key;

    }

}

 

// A utility function to print

// an array of size n

void printArray(int arr[], int n)

{

    int i;

    for (i = 0; i < n; i++)

        printf("%d ", arr[i]);

    printf("\n");

}

 

// Driver code

int main()

{

    int arr[] = {12, 11, 13, 5, 6};

    int n = sizeof(arr) / sizeof(arr[0]);

 

    insertionSort(arr, n);

    printArray(arr, n);

 

    return 0;

}

Merge Sort

 #include <stdio.h>



#define max 10


int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };

int b[10];


void merging(int low, int mid, int high) {

   int l1, l2, i;


   for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {

      if(a[l1] <= a[l2])

         b[i] = a[l1++];

      else

         b[i] = a[l2++];

   }

   

   while(l1 <= mid)    

      b[i++] = a[l1++];


   while(l2 <= high)   

      b[i++] = a[l2++];


   for(i = low; i <= high; i++)

      a[i] = b[i];

}


void sort(int low, int high) {

   int mid;

   

   if(low < high) {

      mid = (low + high) / 2;

      sort(low, mid);

      sort(mid+1, high);

      merging(low, mid, high);

   } else { 

      return;

   }   

}


int main() { 

   int i;


   printf("List before sorting\n");

   

   for(i = 0; i <= max; i++)

      printf("%d ", a[i]);


   sort(0, max);


   printf("\nList after sorting\n");

   

   for(i = 0; i <= max; i++)

      printf("%d ", a[i]);

}

NON RECURSIVE MERGE SORT

 #include <stdio.h>

#define MAX 30


int main()

{

int arr[MAX],temp[MAX],i,j,k,n,size,l1,h1,l2,h2;


printf("Enter the number of elements : ");

scanf("%d",&n);


for(i=0;i<n;i++)

{

printf("Enter element %d : ",i+1);

scanf("%d",&arr[i]);

}


printf("Unsorted list is : ");

for( i = 0 ; i<n ; i++)

printf("%d ", arr[i]);


/*l1 lower bound of first pair and so on*/

for(size=1; size < n; size=size*2 )

{

l1=0;

k=0;  /*Index for temp array*/

while( l1+size < n)

{

h1=l1+size-1;

l2=h1+1;

h2=l2+size-1;

/* h2 exceeds the limlt of arr */

if( h2>=n ) 

h2=n-1;

/*Merge the two pairs with lower limits l1 and l2*/

i=l1;

j=l2;

while(i<=h1 && j<=h2 )

{

if( arr[i] <= arr[j] )

temp[k++]=arr[i++];

else

temp[k++]=arr[j++];

}

while(i<=h1)

temp[k++]=arr[i++];

while(j<=h2)

temp[k++]=arr[j++];

/**Merging completed**/

/*Take the next two pairs for merging */

l1=h2+1; 

}/*End of while*/


/*any pair left */

for(i=l1; k<n; i++) 

temp[k++]=arr[i];


for(i=0;i<n;i++)

arr[i]=temp[i];


printf("\nSize=%d \nElements are : ",size);

for( i = 0 ; i<n ; i++)

printf("%d ", arr[i]);

}/*End of for loop */

printf("Sorted list is :\n");

for( i = 0 ; i<n ; i++)

printf("%d ", arr[i]);

printf("\n");

return 0;

}/*End of main()*/

Heap Sort

 // C++ program for implementation of Heap Sort

#include <iostream>

using namespace std;


// To heapify a subtree rooted with node i which is

// an index in arr[]. n is size of heap

void heapify(int arr[], int n, int i)

{

int largest = i; // Initialize largest as root Since we are using 0 based indexing

int l = 2 * i + 1; // left = 2*i + 1

int r = 2 * i + 2; // right = 2*i + 2


// If left child is larger than root

if (l < n && arr[l] > arr[largest])

largest = l;


// If right child is larger than largest so far

if (r < n && arr[r] > arr[largest])

largest = r;


// If largest is not root

if (largest != i) {

swap(arr[i], arr[largest]);


// Recursively heapify the affected sub-tree

heapify(arr, n, largest);

}

}


// main function to do heap sort

void heapSort(int arr[], int n)

{

// Build heap (rearrange array)

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);


// One by one extract an element from heap

for (int i = n - 1; i >= 0; i--) {

// Move current root to end

swap(arr[0], arr[i]);


// call max heapify on the reduced heap

heapify(arr, i, 0);

}

}


/* A utility function to print array of size n */

void printArray(int arr[], int n)

{

for (int i = 0; i < n; ++i)

cout << arr[i] << " ";

cout << "\n";

}


// Driver program

int main()

{

int arr[] = { 60 ,20 ,40 ,70, 30, 10};

int n = sizeof(arr) / sizeof(arr[0]);

//heapify algorithm

// the loop must go reverse you will get after analyzing manually

// (i=n/2 -1) because other nodes/ ele's are leaf nodes

// (i=n/2 -1) for 0 based indexing

// (i=n/2) for 1 based indexing

for(int i=n/2 -1;i>=0;i--){

heapify(arr,n,i);

}


cout << "After heapifying array is \n";

printArray(arr, n);



heapSort(arr, n);


cout << "Sorted array is \n";

printArray(arr, n);

return 0;

}

//code by Prajwal Chougale