Wednesday 22 March 2023

Make more efficient SQL queries using subqueries.

 Subqueries | SQL

In SQL a subquery can be simply defined as a query within another query. In other words we can say that a Subquery is a query that is embedded in the WHERE clause of another SQL query.

Important rules for Subqueries,

You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING clause, FROM clause. Subqueries can be used with SELECT, INSERT, UPDATE, DELETE statements along with expression operators. It could be equality operator or comparison operators such as =, >= , <= and LIKE operator.

A subquery is a query within a query. The outer query is called the main query and inner query is called Subquery.

The subquery generally executes first when the subquery does not have any co-relation with the main query, when there is a co-relation the parser takes the decision on the fly on which query to execute on the precedence and uses the output of subquery accordingly.

Subqueries must be enclosed in parenthesis.

Subqueries are on the right side of the comparison operator.

ORDER-BY command cannot be used in the subquery, GROUP-BY command can be used to perform same function as ORDER-BY command.

Use single-row operators with single row subqueries, multi-row operators with multiple row subqueries.


Syntax :

There is not any general syntax for subqueries. However, subqueries are seem to be used most frequently with the SELECT statement as shown below :


Example :                                DATABASE


Query : 

To display NAME, LOCATION and PHONE_NUMBER of the students from DATABASE table whose SECTION is 'A'.

Action :

SELECT NAME, LOCATION, PHONE_NUMBER from DATABASE

WHERE ROLL_NO IN

(SELECT ROLL_NO FROM STUDENT WHERE SECTION = 'A' );

Explanation :

The subquery executes first, SELECT ROLL_NO FROM STUDENT WHERE SECTION = 'A' , it returns the ROLL-NO from STUDENT table who's SECTION is 'A'. Then the main query executes SELECT NAME, LOCATION, PHONE_NUMBER from DATABASE, it returns the NAME, LOCATION and PHONE_NUMBER of the student who's ROLL_NO returned by the subquery.

OUTPUT : 




Sunday 20 November 2022

A simple JAVA Pattern written in java language | #java #patterns #javaProgram

 A Simple JAVA Pattern

CODE


public class demo{
  public static void main(String argv[]) {


        System.out.println("     J    a   v     v  a     ");
        System.out.println("     J   a a   v   v  a a    ");
        System.out.println("  J  J  aaaaa   V V  aaaaa   ");
        System.out.println("   JJ  a     a   V  a     a  ");
  }
}





Come Again, Learn Again !

<iCodeBuzz/>









LinkedIn
LinkedIn

Sunday 20 March 2022

NPTEL - WEEK 5 - ASSIGNMENT 5 [ QUESTION 02 ] SOLUTION

WEEK 5 ASSIGNMENT 5 QUESTION 02 SOLUTION 

QUESTION : 

Consider a matrix M of integers. Divide M into 4 sub-matrices. These sub-matrices are called as Quadrants. Report the Quadrant number which has the smallest minimum-element. If two or more quadrants have same smallest minimum, report the smallest quadrant index.
The matrix M is divided into four quadrants by halving the rows and columns. If row/column is an odd number, divide them in such a way that the first half of the row/column should be one smaller than the second half.

The four quadrants are numbered from 1 to 4 in the structure shown below: Q1 | Q2
---+---
 Q3 | Q4
INPUT FORMAT: M is a matrix of integers. You would be given two numbers m and n specifying the number of rows and columns. This would be followed by m lines of n integers each specifying the data of the m*n matrix M.
n and m will be greater than 1 and less than 12. OUTPUT FORMAT: Print in a separate line, the quadrant number with the smallest minimum-element.

Tuesday 15 March 2022

NPTEL - WEEK 4 - ASSIGNMENT 4 - QUESTION 02

 WEEK 4 - ASSIGNMENT 4 - QUESTION 02 SOLUTION

QUESTION : 

Write a program that replaces the occurence of a given character (say c)
in a primary string (say PS) with another string (say s).

Input:
The first line contains the primary string (PS)
The next line contains a character (c)
The next line contains a string (s)

Output:
Print the string PS with every occurence of c replaced by s.

NOTE:
- There are no whitespaces in PS or s. 
- Maximum length of PS is 100.
- Maximum length of s is 10.


SOLUTION : 

#include<stdio.h>
#include<string.h>

int main()
{
    char ps[100], s[10], c, buff;
    int i=0,k=0, j=0, occ=0, len_ps, len_s, tlen;

    scanf("%s", ps);
    scanf("%c", &buff);
    scanf("%c", &c);
    scanf("%s", s);

    len_ps = strlen(ps);
    len_s = strlen(s);
    occ = 0;
    for(i=0; i<len_ps; i++)
    {
        if(ps[i] == c)
        {
            occ++;
        }
    }
    tlen = len_ps + ((len_s * occ) - occ);
   
    j = tlen;
   
    for(i=len_ps; i>0; i--)
    {
        if(ps[i-1] == c)
        {
            for(k=len_s; k>0; k--)
            {
                ps[j-1] = s[k-1];
                j--;
            }
        }
        else
        {
            ps[j - 1] = ps[i-1];
             j--;
        }
      
    }
    ps[tlen] = '\0';
    puts(ps);
    return 0;
}

Sunday 13 March 2022

BCS F.Y. [SEM. 2] NOTES - INTRO. TO. PROG. LANG. USING C PART - 2 (BCS - 202)

 1. FUNCTIONS IN C

 What is a Function ?

       In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.

Types of Functions :

There are two types of functions in C programming:

  1. Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  2. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.

Function Aspects :

  • Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type.
  • Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.
  • Function definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.

Function Aspects declaration syntax

Recursions :

Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem. Any function which calls itself is called recursive function, and such function calls are called recursive calls. Recursion involves several numbers of recursive calls. However, it is important to impose a termination condition of recursion. Recursion code is shorter than iterative code however it is difficult to understand.

Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be defined in terms of similar subtasks. For Example, recursion may be applied to sorting, searching, and traversal problems.

Generally, iterative solutions are more efficient than recursion since function call is always overhead. Any problem that can be solved recursively, can also be solved iteratively. However, some problems are best suited to be solved by the recursion, for example, tower of Hanoi, Fibonacci series, factorial finding, etc.

In the following example, recursion is used to calculate the factorial of a number.

  1. #include <stdio.h>  
  2. int fact (int);  
  3. int main()  
  4. {  
  5.     int n,f;  
  6.     printf("Enter the number whose factorial you want to calculate?");  
  7.     scanf("%d",&n);  
  8.     f = fact(n);  
  9.     printf("factorial = %d",f);  
  10. }  
  11. int fact(int n)  
  12. {  
  13.     if (n==0)  
  14.     {  
  15.         return 0;  
  16.     }  
  17.     else if ( n == 1)  
  18.     {  
  19.         return 1;  
  20.     }  
  21.     else   
  22.     {  
  23.         return n*fact(n-1);  
  24.     }  
  25. }  

OUTPUT :

Enter the number whose factorial you want to calculate?5
factorial = 120                                         
  

We can understand the above program of the recursive method call by the figure given below:

loading_image...



Standard String Library Functions:

There are many important string functions defined in "string.h" library.

No.FunctionDescription
1)strlen(string_name)returns the length of string name.
2)strcpy(destination, source)copies the contents of source string to destination string.
3)strcat(first_string, second_string)concats or joins first string with second string. The result of the string is stored in first string.
4)strcmp(first_string, second_string)compares the first string with second string. If both strings are same, it returns 0.
Returns Negative value if first string's mismatching character's ASCII value is less than second string's corresponding mismatching character's ASCII value.
Returns Positive value if first string's mismatching character's ASCII value is greater than second string's corresponding mismatching character's ASCII value.
5)strrev(string)returns reverse string.
6)strlwr(string)returns string characters in lowercase.
7)strupr(string)returns string characters in uppercase.


Storage Classes In C :

Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a variable. There are four types of storage classes in C

  • Automatic
  • External
  • Static
  • Register
Storage ClassesStorage PlaceDefault ValueScopeLifetime
autoRAMGarbage ValueLocalWithin function
externRAMZeroGlobalTill the end of the main program Maybe declared anywhere in the program
staticRAMZeroLocalTill the end of the main program, Retains value between multiple functions call
registerRegisterGarbage ValueLocalWithin the function



2.  POINTERS IN C

What is Pointer ? Pointer declaration and initialization :
 
The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.

Consider the following example to define a pointer which stores the address of an integer.

  1. int n = 10;   
  2. int* p = &n; // The variable ' p ' will store the memory address of  variable ' n '


Declaring a pointer :

The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.


Dereferencing Pointers :

As we already know that "what is a pointer", a pointer is a variable that stores the address of another variable. The dereference operator is also known as an indirection operator, which is represented by (*). When indirection operator (*) is used with the pointer variable, then it is known as dereferencing a pointer. When we dereference a pointer, then the value of the variable pointed by this pointer will be returned.


Pointer to a Pointer :

As we know that, a pointer is used to store the address of a variable in C. Pointer reduces the access time of a variable. However, In C, we can also define a pointer to store the address of another pointer. Such pointer is known as a double pointer (pointer to pointer). The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the first pointer. Let's understand it by the diagram given below.



The syntax of declaring a double pointer is given below.


int **p; // pointer to a pointer which is pointing to an integer.


Arrays and Pointers :

Generally, in most cases, users assume that the pointer and array are both the same thing. However, it is not that simple because we will find some distinct differences if we look deeply at both. Therefore in this article, we will see whether the pointer and array are really the same or not.

Definition of Arrays 

Arrays are generally used for storing the same types of data items or values that are same in the nature. Although arrays cannot be used to store the data items or values that are not same in the nature, it is also considered a significant disadvantage of the arrays.

Syntax:

int mark[5] = {19, 10, 8, 17, 9}; 

Definition of Pointers

Apart from the arrays, if we talk about the pointer, they are generally used for storing the memory address of the other variable instead of storing the actual value of the variable. However, we can also use the pointer to access the whole array by using the pointer arithmetic. In addition, it also even makes the accessing array process much faster.

Syntax:

The pointer variable can be as declared as shown below:

  1. int n = 10;   
  2. int* p = &n; // The variable ' p ' will store the memory address of  variable ' n '


What are the main differences between a Array and a Pointer ?

Apart from what we have discussed above, the key differences can be found while implementing the pointer and array. For example, when the arrays are implemented, the fixed size of the memory block is allocated. On the other hand, where the pointers are implemented, the memory is dynamically allocated. Thus, allocating the memory in both the pointers and arrays can be considered the key difference. However, it is not the only difference that lies between the arrays and pointer because some other differences also do exist that are as follows:

  1. An array usually stores the variables ofsimilar data types, and the data types of the variables must match the type of array. However, the pointer variable stores the address of a variable of a type similar to a type of pointer variable type.
  2. We can generate an array of pointers, i.e. array whose variables are the pointer variables. On the other hand, we can also create a pointer that points to an array.
  3. In general, arrays are static, which means once the size of the array is declared, it cannot be resized according to users requirements. While on the other hand, pointers are dynamic, which means the memory allocated can be resized later at any point in time.
  4. Arrays are allocated at compile-time, while pointers are allocated at runtime
  5. If we talk about the size of an array, it usually depends on the number of variables are stored in it. While in the case of the pointer variable, it stores the address of the variable only. To understand it more clearly, you can consider the following given examples:
  6. The "sizeof" operator
If we use the "sizeof(array)", it will return the amount of memory used by all elements stored in the array. However, if we use the it for the pointer, e.g. "sizeof(pointer)", it only returns the amount of memory used by the pointer variable itself.


Functions and Pointers :

As we know that we can create a pointer of any data type such as int, char, float, we can also create a pointer pointing to a function. The code of a function always resides in memory, which means that the function has some address. We can get the address of memory by using the function pointer.

Let's see a simple example.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     printf("Address of main() function is %p",main);  
  5.     return 0;  
  6. }  

The above code prints the address of main() function.

Output


In the above output, we observe that the main() function has some address. Therefore, we conclude that every function has some address.


Dynamic Memory Allocation in C

The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()
Before learning above functions, let's understand the difference between static memory allocation and dynamic memory allocation.

static memory allocationdynamic memory allocation
memory is allocated at compile time.memory is allocated at run time.
memory can't be increased while executing program.memory can be increased while executing program.
used in array.used in linked list.

Now let's have a quick look at the methods used for dynamic memory allocation.

malloc()allocates single block of requested memory.
calloc()allocates multiple block of requested memory.
realloc()reallocates the memory occupied by malloc() or calloc() functions.
free()frees the dynamically allocated memory.


Command Line Arguments :

The arguments passed from command line are called command line arguments. These arguments are handled by main() function.

To support command line argument, you need to change the structure of main() function as given below.

  1. int main(int argc, char *argv[] )  

Here, argc counts the number of arguments. It counts the file name as the first argument.

The argv[] contains the total number of arguments. The first argument is the file name always.

Example

Let's see the example of command line arguments where we are passing one argument with file name.

  1. #include <stdio.h>  
  2. void main(int argc, char *argv[] )  {  
  3.   
  4.    printf("Program name is: %s\n", argv[0]);  
  5.    
  6.    if(argc < 2){  
  7.       printf("No argument passed through command line.\n");  
  8.    }  
  9.    else{  
  10.       printf("First argument is: %s\n", argv[1]);  
  11.    }  
  12. }  

Run this program as follows in Linux:

  1. ./program hello  

Run this program as follows in Windows from command line:

  1. program.exe hello  

Output:

Program name is: program                
First argument is: hello                
If you pass many arguments, it will print only one.

  1. ./program hello c how r u  

Output:

Program name is: program
First argument is: hello

But if you pass many arguments within double quote, all arguments will be treated as a single argument only.

  1. ./program "hello c how r u"  

Output:

Program name is: program
First argument is: hello c how r u


You can write your program to print all the arguments. In this program, we are printing only argv[1], that is why it is printing only one argument.


3. STRUCTURES IN C

What is a Structure ?

Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member. Structures ca; simulate the use of classes and templates as it can store various information

The ,struct keyword is used to define the structure. Let's see the syntax to define the structure in c.

  1. struct structure_name   
  2. {  
  3.     data_type member1;  
  4.     data_type member2;  
  5.     .  
  6.     .  
  7.     data_type memeberN;  
  8. };  

Let's see the example to define a structure for an entity employee in c.

  1. struct employee  
  2. {   int id;  
  3.     char name[20];  
  4.     float salary;  
  5. };  

The following image shows the memory allocation of the structure employee that is defined in the above example.


Here, struct is the keyword; employee is the name of the structure; idname, and salary are the members or fields of the structure. Let's understand it by the diagram given below:


Creating Structures :

We can declare a variable for the structure so that we can access the member of the structure easily. There are two ways to declare structure variable:

  1. By struct keyword within main() function
  2. By declaring a variable at the time of defining the structure.
1st way:

Let's see the example to declare the structure variable by struct keyword. It should be declared within the main function.

  1. struct employee  
  2. {   int id;  
  3.     char name[50];  
  4.     float salary;  
  5. };  

Now write given code inside the main() function.

  1. struct employee e1, e2;  

The variables e1 and e2 can be used to access the values stored in the structure. Here, e1 and e2 can be treated in the same way as the objects in C++ and Java.

2nd way:

Let's see another way to declare variable at the time of defining the structure.

  1. struct employee  
  2. {   int id;  
  3.     char name[50];  
  4.     float salary;  
  5. }e1,e2;  


Accessing Members of Structures :

There are two ways to access structure members:

  1. By . (member or dot operator)
  2. By -> (structure pointer operator)
Let's see the code to access the id member of p1 variable by. (member) operator.
    
     p1.id