Tuesday, January 30, 2024

POLYNOMIALS (Using Linked List)
Implemented using C Language

Polynomial: E.g.:   3x5 - 2x3 + x2 + 4  

The important information about the polynomial is contained in the coefficients and exponents of x; the variable x itself is just a place holder (a dummy variable). We can think of a polynomial as a sum of terms, each of which consists of a coefficient and an exponent. It can be represented as a list of pairs of coefficients and exponents. A linked list is preferable to a contiguous list for representing a polynomial because we may not know the bound on the degree, or only a few non-zero terms may appear.
 

Functionality Implemented in this Program:

  1. typedef struct polynominal
  2. typedef struct polynominallist
  3. poly *createpoly(void)
  4. void addterm(poly *p,float c,int e)
  5. poly *addpoly(poly *p1, poly *p2)
  6. void printpoly(poly *p)
  7. void killpoly(poly *p)

C Language Code:

              POLYNOM.H           

// DSTC -- POLYNOM.H -- polynomial header file ..
// 3x^2 + 3x + 1
// Operations .... create,addition of 2 polynomials...
//1.poly *createpoly(void);

//#include "assert.h"

typedef struct polynominal
{
float coeff ;
int exp;
struct polynominal *next ;
}term ;

typedef struct polynominallist
{
term *first ;
term *last ;
}poly ;

poly *createpoly(void)
{
poly *p;
p = (poly *) malloc (sizeof(poly)) ;
p->first = NULL ;
p->last = NULL ;
return (p) ;
}

void addterm(poly *p,float c,int e)
{
term *nterm,*temp ;
if (c!=0)
{
nterm=(term *)malloc(sizeof(term));
nterm->coeff = c ;
nterm->exp = e ;
nterm->next = NULL ;
}
if (p->first == NULL)
{
p->first = nterm ;
p->last = nterm ;
}
else
{
if (p->last->exp > e)
{
p->last->next = nterm ;
p->last = nterm ;
}
}
}

poly *addpoly(poly *p1, poly *p2)
{
term *t1,*t2 ;
poly *p3;
float sum ;
p3 = createpoly();
t1 = p1->first ;
t2 = p2->first ;

while(t1 && t2)
{
if (t1->exp == t2->exp)
{
// when degree/power of X is same for both then add both and
// store in the next of new poly-list
sum = t1->coeff + t2->coeff ;
addterm(p3,sum,t1->exp) ;
t1 = t1->next ;
t2 = t2->next ;
}
else if(t1->exp > t2->exp)
{
// when, suppose 3x^2 is in poly1 and poly2 is not having power of 2
// then just add the term of p1 into new poly-list
addterm(p3,t1->coeff,t1->exp) ;
t1 = t1->next ;
}
else
{
// when, suppose 3x^2 is in poly2 and poly1 is not having power of 2
// then just add the term of p2 into new poly-list
addterm(p3, t2->coeff,t2->exp) ;
t2=t2->next;
}
}

while (t1)
{
// adding the remaining/extra terms in the p1 into p3
addterm(p3,t1->coeff,t1->exp) ;
t1 = t1->next ;
}

while (t2)
{
// adding the remaining/extra terms in the p2 into p3
addterm(p3,t2->coeff,t2->exp) ;
t2 = t2->next ;
}

return(p3);
}

// prints the polynomial

void printpoly(poly *p)
{
term *temp ;
if (p->first != NULL)
{
temp = p->first;
while(temp)
{
if(temp == p->last)
printf("%fx^%d",temp->coeff,temp->exp) ;
else
printf("%fx^%d + ",temp->coeff,temp->exp) ;

temp = temp->next ;
}
}

}

void killpoly(poly *p)
{
term *save, *temp ;
save = p->first ;
while(save)
{
temp = save->next ;
free(save);
save = temp ;
}
free(p);
}

              POLYNOM.CPP           

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "polynom.h"

main()
{
poly *p,*p1, *res ;
float c ;
int e ;
p = createpoly() ;
p1 = createpoly() ;

clrscr() ;

printf("\n Enter the 1st Term of Poly in Coeff,Exponent form:" );
scanf("%f,%d", &c,&e) ;

addterm(p,c,e) ;

printf("\n Enter the 2nd Term of Poly in Coeff,Exponent form:" );
scanf("%f,%d", &c,&e) ;

addterm(p,c,e) ;

printf("\n Enter the 3rd Term of Poly in Coeff,Exponent form:") ;
scanf("%f,%d", &c,&e) ;

addterm(p,c,e) ;

printf("\n Print Polynomial ...\n") ;

printpoly(p);
/////////////////

printf("\n Enter the 1st Term of Poly in Coeff,Exponent form:" );
scanf("%f,%d", &c,&e) ;

addterm(p1,c,e) ;

printf("\n Enter the 2nd Term of Poly in Coeff,Exponent form:" );
scanf("%f,%d", &c,&e) ;
addterm(p1,c,e) ;
printf("\n Enter the 3rd Term of Poly in Coeff,Exponent form:") ;
scanf("%f,%d", &c,&e) ;
addterm(p1,c,e) ;
printf("\n Print Polynomial ...\n") ;
printpoly(p1);

res=addpoly(p,p1) ;
printf("\n res \n") ;
printpoly(res) ;

}

Arrays - 2D Array/Matrix - DSTC using C Language (Source Code Implemented)

Arrays - 2D Array/Matrix
Implemented using C Language


Two-dimensional arrays are a little more complicated to use than one-dimensional ones. Here data is stored in the form of ROWS and COLUMNS which are accessed with the help of SUBSCRIPT number.
For e.g int arr[5][4] ;

  • Conventionally, the 1st subscript is used for rows and the 2nd for columns.
  • The 2d array can hold up to (rows * columns) values.
  • As is usual in C, subscripts start with zero, so the subscripts range from 0 to 4 for the rows and 0 to 3 for the columns.
  • If the system uses 2 bytes for an int, this array would use 40 bytes of RAM. All of the bytes are in consecutive memory locations, the first row occupying the first 8 bytes, the second the next 8, and so on.

The following table illustrates how the subscripts are specified for this array, which has five (5) rows and four (4) columns.

   
0123
0[0][0][0][1][0][2][0][3]
1[1][0][1][1][1][2][1][3]
2[2][0][2][1][2][2][2][3]
3[3][0][3][1][3][2][3][3]
4[4][0][4][1][4][2][4][3]

 

C Language Code:

        MULTIARR.H      

// DSTC ------- MULTIARR.H
// This program is creating MATRIX Dynamically
// MATRIX.. add, mulitpication, subtration, creation,

#include "assert.h"

typedef struct MultiDimentionArray
{
int maxr;
int maxc;
int **bfr;
}matrix;

matrix *makemat(int nr,int nc)
{
matrix *m;
int i,j;
m = (matrix *) malloc(sizeof(matrix));
m->maxr= nr;
m->maxc= nc;
m->bfr = (int **) malloc(sizeof(int*)*nr);
for(i=0;i<nr;i++)
{
m->bfr[i]=(int *)malloc(sizeof(int)*nc);
}
// m->bfr = (int *) malloc(sizeof(int **)*nc);
return (m);
}

void inputmat(matrix *m,int val, int r, int c)
{
m->bfr[r][c] = val ;
}

int inputmat(matrix *m, int r, int c)
{
return(m->bfr[r][c]);
}

void printmat(matrix *m)
{
int i,j ;
printf("\n");
for(i=0;i < m->maxr;i++)
{
printf("\n |");


Get Paid by Reading Ads on your Mobiles

for(j=0;j < m->maxc ; j++)
{
printf("\t%d",m->bfr[i][j]);
}
printf(" |");
}
} void add2mat(matrix *m1,matrix *m2,matrix *res)
{
int i,j;
assert((m1->maxr == m2->maxr) && (m1->maxc == m2->maxc)) ;
for(i=0;i<m1->maxr;i++)
{
for(j=0;j < m1->maxc ; j++)
{
res->bfr[i][j] = m1->bfr[i][j] + m2->bfr[i][j] ;
}
}
}

void subtract2mat(matrix *m1,matrix *m2,matrix *res)
{
int i,j;
assert((m1->maxr == m2->maxr) && (m1->maxc == m2->maxc)) ;
for(i=0;i<m1->maxr;i++)
{
for(j=0;j < m1->maxc ; j++)
{
res->bfr[i][j] = m1->bfr[i][j] - m2->bfr[i][j] ;
}
}
}

// precondion for matrix Multiplication is
// cols of first matrix = rows of second matrix
// and new matrix will be if
// m1(m*n) * m2(n*p) == res(m*p)

matrix *multiplymat(matrix *m1, matrix *m2)
{
assert(m1->maxc == m2->maxr) ;
matrix *r;
int i,j,m,n,s ;
i = m1->maxr ;
j = m2->maxc ;
r = makemat(i,j);

for(i=0;i < r->maxr;i++)
{
for(j=0;j < r->maxc;j++)
{
printf("\n");
s=0;
for(m=0,n=0;m< m1->maxc;m++,n++)
{
s = s + (m1->bfr[i][m] * m2->bfr[m][j]);
r->bfr[i][j] = s;
printf("m1[%d][%d] * m2[%d][%d]\t",i,m,m,j);
}
}
}
return (r) ;
}

          MULTIARR.CPP        

// DSTC ------- MULTIARR.CPP (including MULTIARR.H header file)
// this program is creating Dynamically
//

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "multiarr.h"

main()
{
clrscr();

int i,j,v ,r,c;
matrix *m,*m1,*res;

printf("\n Enter the 1 Matrix's Dimentions r,c");
scanf("%d,%d", &r,&c);

m = makemat(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\nEnter m[%d][%d]:",i,j);
scanf("%d",&v);
inputmat(m,v,i,j);
}
}

printf("\n Enter the 2 Matrix's Dimentions r,c");
scanf("%d,%d", &r,&c);
m1 = makemat(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\nEnter m1[%d][%d]:",i,j);
scanf("%d",&v);
inputmat(m1,v,i,j);
}
}

Get Paid by Reading Ads on your Mobiles

res = makemat(r,c) ;
printmat(m);
printf("\n + ");
printmat(m1);
//add2mat(m,m1,res);

printf(" = \n" );
//printmat(res);
//subtract2mat(m,m1,res);
//printf(" - \n" );
//printmat(res);
res = multiplymat(m,m1) ;
printf("\nMatrix Multiplication:");
printmat(res);
}

Arrays - One Dimensional - DSTC using C Language (Source Code Implemented)

Arrays - One Dimensional 
Implemented using C Language

 

Arrays have 2 basic operations i.e. extraction and storing values.

  • Extraction operation is a function that accepts an array (arr) and an index or subscript number ( i ) , and returns a values stored at index nuber. It is denoted as arr[i].

  • Storing operation accepts an array (arr), and index or subscript ( i ), and a value to be stored ( X ). It is denoted as arr[i] = X. Before a value has been assigned to an element of the array, its value is undefined and a reference to it in an expression is illegal.

C Language Code:

       ARRAY1D.H      

#include "assert.h"
typedef struct integer_array
{
int *bfr ;
int size;
}intarr ;

intarr *makearray(int n)
{
intarr *a1;
a1 = (intarr *) malloc(sizeof(intarr)) ;
a1->size = n ;
a1->bfr = (int *) malloc(sizeof(int)*n);
return (a1);
}

void input(intarr *a1,int val, int pos)
{
assert(pos>=0 && pos<a1->size);
assert(val != 0) ;
a1->bfr[pos] = val;
// or *(a1->bfr+pos) = v ;
}

int get(intarr *a1,int pos)
{
assert(pos>=0 && pos<a1->size);
return(a1->bfr[pos]);
// or return(*(a->bfr+pos));
}

void printarray(intarr *a1,int posupto)
{
// if posupto is zero(0) then it will all array elements otherwise
// only the speicified no. of elements.
int i=0,k ;
if (posupto==0)
i=a1->size ;
else
i = posupto ;

//DISPLAYS THE ARRAY BEFORE SORTING
printf("\n");
for(k=0;k<i;k++)
printf("a[%d]\t",k);

printf("\n");
for(k=0;k<i;k++)
printf("%d\t",a1->bfr[k]);
}

void sortarray(intarr *a1)
{
int i=0,j=0,t ;

for(i=0;i<a1->size-1;+i++)
{
for(j=i+1;j<a1->size;j++)
{
if ( a1->bfr[i] > a1->bfr[j] )
{
t = a1->bfr[i];
a1->bfr[i] = a1->bfr[j] ;
a1->bfr[j] = t ;
}
}
}

}

             ARRAY1D.CPP         

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <assert.h>
#include "array1d.h"

void main()
{
intarr *a1;
int i,val,n ;
printf("\n Enter the no.of elements in the array: ");
scanf("%d",&n);
printf("\nvalue entered wwas %d",n);
a1 = makearray(n);
printf("\nsize of array %d",a1->size);
for(i=0;i<n;i++)
{
printf("\n Enter values of a[%d]=",i);
scanf("%d",&val);
// a1->bfr[i] = val ;
input(a1,val,i);
}
printarray(a1,0) ;

sortarray(a1) ;
printarray(a1,0);
}


Rational Numbers (Abstract Data Type) - DSTC using C Language

Rational Numbers (Abstract Data Type) 
Implemented using C Language

 

Consider the ADT Rational, which is a Mathematical concept of a rational number. A rational number is a number that can be expressed as the quotient of two integers.

Operations to be performed for implemented Rational numbers are:

  • Creation of a rational number from two integers (rat *makerat(int a,int b))
  • Addition
  • Multiplication
  • Testing for Equality

C Language Code:

RATIONAL.H

typedef struct rational
{
int n;
int d;
}rat ;

rat *makerat(int a,int b)
{
rat *r ;
if (b==0)
{
printf("\n Den Not allowed as Zero ") ;
exit(1);
}
r = (rat *)malloc(sizeof(rat)) ;
r->n = a ;
r->d = b ;
return (r) ;
}

rat *sumrat(rat *x, rat *y)
{
rat *r ;
r = (rat *)malloc(sizeof(rat)) ;
r->n = (x->n * y->d) + (x->d * y->n) ;
r->d = x->d * y->d ;
return(r) ;
}

void killrat(rat *n)
{
free(n);
}

void printrat(rat *n)
{
printf("%d/%d",n->n,n->d);
}

rat *reducerat(rat *r)
{
rat *t ;
t = (rat *)malloc(sizeof(rat));
t = r ;
int i ;
if (t->n > t->d)
i = t->d;
else
i = t->n;

for(;i>1;i--)
{
if((t->n%i==0) && (t->d%i==0))
{
t->n = t->n/i ;
t->d = t->d/i ;
}
}

return(t);
}

 

rat *subtractrat(rat *x, rat *y)
{
rat *r ;
r = (rat *)malloc(sizeof(rat)) ;
r->n = (x->n * y->d) - (x->d * y->n);
r->d = x->d * y->d ;
return(r);
}

Get Paid by Reading Ads on your Mobiles

rat *multiplyrat(rat *x, rat *y)
{
rat *r ;
r = (rat *)malloc(sizeof(rat)) ;
r->n = x->n * y->n ;
r->d = x->d * y->d ;
return(r);
}

rat *dividerat(rat *x, rat *y)
{
rat *r ;
r = (rat *)malloc(sizeof(rat)) ;
r->n = (x->n * y->d) / (x->d * y->n);
r->d = 0 ;
return(r);
}

void ratequal(rat *x, rat *y)
{
if((x->n * y->d)== (x->d * y->n) )
printf("\n\t\tNumbers are Equal") ;
else
printf("\n\t\tNumbers are NOT Equal") ;

}

RATIONAL.CPP

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <rational.h>

void main()
{
rat *m, *n, *p, *l, *q, *t ;
int i,j,k ;
clrscr();
printf("\n Enter 1st num and denomentor(n/d):");
scanf("%d/%d",&i,&j);
m = makerat(i,j);

printf("\n Enter 2st num and denomentor(n/d):");
scanf("%d/%d",&i,&j);
n = makerat(i,j);

// SUM OF Rational nos.
p = sumrat(m,n);

printf("\n Display Two Rational No.s sum ");
printrat(m) ;
printf(" + ");
printrat(n) ;
printf(" = ");
printrat(p) ;
t = reducerat(p) ;
printf(" = ");
printrat(t);

//********* SUBSTRATION OF 2 RATIONAL NOS.

q = subtractrat(m,n);

printf("\n Display Two Rational No.s Subtraction ");
printrat(m) ;
printf(" - ");
printrat(n) ;
printf(" = ");

printrat(q) ;
t = reducerat(q) ;
printf(" = ");
printrat(t);

killrat(m) ;
killrat(n) ;
killrat(p) ;
killrat(l) ;
killrat(q) ;
killrat(t) ;

}

Tuesday, January 24, 2023

How do I Insert data in SQL Server using ASP .Net

To insert data into a SQL Server database using ASP.NET, you can use the ADO.NET library to establish a connection to the database and execute an INSERT statement. Here is an example of how to do this:

1.    Create a new ASP.NET project and add a reference to the System.Data.SqlClient namespace.

2.    Create a new method that will handle the insertion of data.

3.    In this method, create a new SqlConnection object and set the connection string to the appropriate value for your SQL Server database.

4.    Open the connection using the Open() method.

5.    Create a new SqlCommand object and set the CommandText property to the INSERT statement you want to execute.

6.    Add any necessary parameters to the SqlCommand object using the Parameters.Add() method.

7.    Execute the command using the ExecuteNonQuery() method.

8.    Close the connection using the Close() method.

Here is an example of how your code might look:

using (SqlConnection con = new SqlConnection("Data Source=myServer;Initial Catalog=myDB;User ID=myUsername;Password=myPassword"))

{

    con.Open();

    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (col1, col2) VALUES (@val1, @val2)", con))

    {

        cmd.Parameters.AddWithValue("@val1", "some value");

        cmd.Parameters.AddWithValue("@val2", 123);

        cmd.ExecuteNonQuery();

    }

    con.Close();

} 

 

 

How do I retrieve data from SQL Server using ASP .Net

In order to retrieve data from a SQL Server database in an ASP.NET application, you can use ADO.NET, which is a set of classes that provide data access services for the .NET Framework. The most commonly used classes for this purpose are the SqlConnection, SqlCommand, and SqlDataReader classes.

Here is an example of how you can use these classes to retrieve data from a SQL Server database and display it in an ASP.NET page:

Create a new SqlConnection object and set its connection string to the appropriate value for your SQL Server database.

Create a new SqlCommand object and set its CommandText property to the SQL query you want to execute.

Open the SqlConnection.

Execute the query by calling the ExecuteReader method of the SqlCommand object and store the result in a SqlDataReader object.

Iterate through the SqlDataReader object and retrieve the data you need.

Close the SqlConnection and SqlDataReader.

Here is an example of the above steps

Sample Code:


using (SqlConnection con = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
    {
        con.Open();
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                // Retrieve data from the reader
                string name = reader["Name"].ToString();
                string city = reader["City"].ToString();
                // Do something with the data
            }
        }
        con.Close();
    }
}


It's also important to consider that this is just a simple example, and in production applications, you should handle exceptions, use parameters to avoid SQL injection, and dispose objects properly.

Thursday, December 3, 2020

How to Shrink SQL Server Database Log File Size using DBCC Command

 

 

How to Shrink SQL Server Database Log File Size using DBCC Command

you must take a full backup of your database before executing the following: 

 

ALTER DATABASE <YOUR DATABASE NAME>
SET RECOVERY SIMPLE
GO
DBCC SHRINKFILE (<YOUR DATABASE LOG FILE NAME>, TARGET SIZE IN MB)
GO
ALTER DATABASE <YOUR DATABASE NAME>
SET RECOVERY FULL