Database to Excell Export(C#)


public class ExcelHelper
    {
        public static void ExportListToExcel(List listToExport, string xlsName)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
            HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=" + xlsName + ".xls");

            Int32 success = 0;
            string sep = "";
            try
            {
                PropertyInfo[] fieldInfo = listToExport[0].GetType().GetProperties();
                foreach (PropertyInfo col in fieldInfo)
                {
                    HttpContext.Current.Response.Write(sep + col.Name);
                    sep = "\t";
                }
                HttpContext.Current.Response.Write("\n");

                foreach (Blog dataItem in listToExport)
                {
                    PropertyInfo[] allProperties = dataItem.GetType().GetProperties();
                    sep = "";
                    foreach (PropertyInfo thisProperty in allProperties)
                    {
                        object value = thisProperty.GetValue(dataItem, null);
                        String propetyValue = (value == null ? String.Empty : value.ToString());
                        HttpContext.Current.Response.Write(sep + propetyValue.ToString());
                        sep = "\t";
                    }
                    ++success;
                    HttpContext.Current.Response.Write("\n");
                }
                HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

Export data SQL to Excel

আমরা যদি Dadabase(SQL) থেকে Excel Data export করতে চাই তবে নিচের Method Button Click  এ লিখলে .csv file export হবে।

এখানে নির্দিষ্ট range এর ভিতর data export দেখান হল।

protected void ButtonExport_Click(object sender, EventArgs e)

{

 int startId = 0;

int.TryParse(StartId.Text, out startId);/*Text Box Text Start*/

int endId = 0;

int.TryParse(EndId.Text, out endId);/*Text Box Text End*/

List<string> listString = new List<string>();

MemberCollection memberColl = new MemberController().FetchAll();/*Table এর সব data নিয়ে আসবে*/

listString.Add("Id," + "Email," + "FirstName," + "LastName," + "Country,");

foreach (Member member in memberColl)

 {

int memberId = member.Id;

if (memberId >= startId && memberId <= endId)

{

listString.Add(member.Id.ToString() + "," + member.Email + ","

+ member.FirstName + "," + member.LastName + "," + member.Country);

}

}

ExportTestResult(listString, "report");

}

private void ExportTestExcel(List<string> report, string downloadFileName)

{

downloadFileName = downloadFileName.Replace(" ", "-");

string fileName = Server.MapPath("~/") + Guid.NewGuid().ToString() + ".csv";

using (StreamWriter sw = new StreamWriter(fileName))

 {

foreach (string s in report)

{

sw.WriteLine(s);

}

sw.Flush();

                }

                using (StreamReader sr = new StreamReader(fileName))

                {

Response.Clear();

Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadFileName + ".csv");

Response.Write(sr.ReadToEnd());

Response.ContentType = "text/csv";

Response.Flush();

Response.End();

}

 File.Delete(fileName);

}

সব শেষে server এ তৈরি হওয়া file টি আমরা delete করে ফেলব।

Validation Check @ MVC 2

ASP.NET MVC2 Validation Check ২ ভাবে করা যায়

JavaScript এর মাধ্যমে Client side এ এবং Controller এর মাধ্যমে Server Side . তবে ২ ধরনের Validation রাখা ভালো. কারণ  Client side  এর Browser JavaScript Enable না থাকলে Client side validation show করবে না।

আমরা এখানে Server side Validation দেখাব।

Controller Page [StudentController.cs]
===============================================

StudentRepository studentRepositoryObj = new StudentRepository();

[LINQ to SQL Classes. আমরা এ class er Object এর মাধ্যমে Data Save  করব]

 

//[অনেক ভাবে post Data receive করা যায়. আমরা এখানে FormCollection formValus মাধ্যমে Data receive করব।]

  //

        // POST: /Student/Create

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult Create(FormCollection formValus)

        {

            ViewData["groupName"] = LoadDropDownList();

            Student studentObj = new Student();

            studentObj.Status = true;

            studentObj.Name = Request.Form["Name"].Trim();

            studentObj.Age = Convert.ToInt16(Request.Form["Age"]);

            studentObj.Bio = Request.Form["Bio"];

            studentObj.Type = Request.Form["Type"];

            string status = Request.Form["Status"];

            string status3 = Request.Form.GetValues("Status")[1];

            bool state;

            if (bool.TryParse(status, out state))

            {

                studentObj.Status = state;

            }     

            string status2 = Request.Form["IsActive"];

            if (studentObj.Name.Length== 0)

            {

                ModelState.AddModelError("Name", "Name is required.");

            }

            if (studentObj.Age < 18 || studentObj.Age > 120)

            {

                ModelState.AddModelError("Age", "Age between 18 to 120.");

            }

            if (studentObj.Bio.Trim().Length == 0)

            {

                ModelState.AddModelError("Bio", "Please Post Bio.");

            }

            if (studentObj.Type.Length == 0)

            {

                ModelState.AddModelError("Type", "Group Not Selected.");

            }

 

            if (bool.TryParse(status2, out state))

            {

                studentObj.IsActive = state;

            }

            else

            {

                ModelState.AddModelError("IsActive", "Tick on Activity");

            }

            if (!ModelState.IsValid)

            {               

                return View("Create", studentObj);

            }

            studentRepositoryObj.Add(studentObj);

            studentRepositoryObj.Save();

            return RedirectToAction("Index");

        }

 

Validation Check এর জন্য আমরা প্রত্যকটি data receive করে Check করব যে ঠিক মত আছে কিনা। যদি না থাকে তবে ModelState.AddModelErrorADD  করে রাখব।

ModelState.AddModelError("Age", "Age between 18 to 120.");

 

AddModelError এর টি parameter আছে।

প্রথমটি Object property অথবা name.

২য়টি আমরা যে Message Show ক রতে চাই তা।

 

ModelState যদি Valid না হয় তবে আমরা আবার View Page Object নিয়ে যাবো এবং Error message Show করব Html.ValidationSummary() এবং Html.ValidationMessage("Name","*") এর মাধ্যমে।

আর যদি Valid হয় তবে studentRepositoryObj Object এর মাধ্যমে Save ক রব এবং index page Redirect ক রে চলে যাব।

 

View Page [Create.cs]
===============================================
<% using (Html.BeginForm()) {%>

 

        <%List<string> groups = ViewData["groupName"] as List<string>; %>

        <% var studentObj = ViewData.Model as StudentInfo; %>

        <%= Html.ValidationSummary() %> <%--Display Controller Message--%>

        <fieldset>

            <legend>New Student</legend>

<P>

                      <%= Html.Label("Name") %>:

<%= Html.TextBox("Name")%>

<%= Html.ValidationMessage("Name","*") %>

               </p>

              <p>

<%= Html.Label("Age") %>:

<%= Html.TextBox("Age") %>

<%= Html.ValidationMessage("Age", "*") %>

              </p>

              <p>

                        <%= Html.Label("Bio") %>:

<%= Html.TextArea("Bio","",5,23,null) %>

<%= Html.ValidationMessage("Bio", "*") %>

</p>

              <p>

                        <%= Html.Label("Type") %>:

<%= Html.DropDownList("Type", new SelectList(type), "Select")%>

<%=Html.ValidationMessage("Type", "Select Group")%>

</p>

              <p>

<%= Html.Label("Status") %>:

<%= Html.CheckBox("Status")%>

<%= Html.CheckBox("Status")%>

</p>

              <p>

Is Active:

<%= Html.RadioButton("IsActive","True")%>Yes

<%= Html.RadioButton("IsActive", "False")%>No

<%=Html.ValidationMessage("IsActive", "Activity not Selected") %>

</p>

<p>

                        <input type="submit" value="Create" />

</p>

        </fieldset>

 

    <% } %>



HTML Helper (ASP.NET MVC2)

<%= Html.Label("Name") %> 
<%= Html.TextBox("Name")%>                   
<%= Html.ValidationMessage("Name","*")%>                    
<%= Html.TextArea("Bio","Input Here",5,23,null) %>
<%= Html.DropDownList("Type", new SelectList(type), "Select")%>
<%= Html.CheckBox("Status")%>True
<%= Html.CheckBox("Status")%>False
<%= Html.CheckBox("Status2")%>No Comment
<%= Html.RadioButton("IsActive","True")%>Yes
<%= Html.RadioButton("IsActive", "False")%>No
<%= Html.ActionLink("Back to List", "Index") %>//Applied on Same Controller
<%=Html.ActionLink("Add New Project", "AddProject", "Project")%>//Display Text, Action Link, Controller Name

Validation Check (ASP.NET MVC 2)

Controller Page[StudentController.cs]
===============================================
StudentRepository studentRepositoryObj = new StudentRepository();    //LINQ to SQL Classes
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formValus)
{
    StudentInfo studentObj = new StudentInfo();

    studentObj.Name = Request.Form["Name"].Trim();
    if (studentObj.Name.Length == 0)
    {
        ModelState.AddModelError("Name", "Name is required.");    
    }

    studentObj.Age = Convert.ToInt16(Request.Form["Age"]);
    if (studentObj.Age < 69 || studentObj.Age > 420)
    {
        ModelState.AddModelError("Age", "Age between 69 to 420.");
    }

    studentObj.Type = Request.Form["Type"];
    if (studentObj.Type.Length == 0)
    {
        ModelState.AddModelError("Type", "Type Not Selected.");
    }

    string status = Request.Form["Status"];
    bool state;
    if (bool.TryParse(status, out state))
    {
        studentObj.Status = state;
    }

    string isActive = Request.Form["IsActive"];          
    if (bool.TryParse(isActive, out state))
    {
        studentObj.IsActive = state;
    }
    else
    {
        ModelState.AddModelError("IsActive", "Tick on Activity");
    }

    if (!ModelState.IsValid)
    {    
        return View("Create", studentObj);
    }
    studentRepositoryObj.Add(studentObj);
    studentRepositoryObj.Save();
    return RedirectToAction("Index");
}

View Page [Create.cs]
===============================================
<% using (Html.BeginForm()) {%>
    <%= Html.ValidationSummary("An Error Occured") %><%--Display All Controller Message if Field is Balnk--%>
        
        <p>
            <%= Html.Label("Name") %>:
            <%= Html.TextBox("Name")%>
            <%= Html.ValidationMessage("Name","*") %>
        </p>
        <p>
            <%= Html.Label("Age") %>:              
            <%= Html.TextBox("Age") %>
            <%= Html.ValidationMessage("Age", "*") %>
        </p>    
            <%= Html.Label("Type") %>:
            <%= Html.DropDownList("Type", new SelectList(type), "Select")%>
            <%=Html.ValidationMessage("Type", "Select Type") %>
        <p>
        </p>    
            <%= Html.Label("Status") %>:
            <%= Html.CheckBox("Status")%>
        </p>
        <p>
            Is Active:
            <%= Html.RadioButton("IsActive","True")%>Yes
            <%= Html.RadioButton("IsActive", "False")%>No
            <%=Html.ValidationMessage("IsActive", "Activity not Selected") %>
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
        
<% } %>

SubSonic App.Config File

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" requirePermission="false"/>
  </configSections>
  <connectionStrings>
    <add name="DatabaseConnection" connectionString="Server=.\SQLEXPRESS;Database=WinForm;Trusted_Connection=yes"/>
  </connectionStrings>
  <SubSonicService defaultProvider="CustomerDatabase">
    <providers>
      <clear/>
      <add name="CustomerDatabase" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="DatabaseConnection" generatedNamespace="CustomerDatabase"/>
    </providers>
  </SubSonicService>
</configuration>

For More SubSonic Help

Connection String in ASP.NET

string connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

Where ConnectionString=Name of commection string in web.config file.

Iterative Process

/*................... Iterative process.........................
.......................Author:-SUDIPTA KUMAR PAIK......................
.......................NUMERICAL METHOD.................*/


#include math.h>
#include stdio.h>
main()
{
int j,i,N;
float sum,nsum,t,oeq[21][2],eq[21][2];
start:
clrscr();
sum=0.0;

printf("\n Enter [0] to terminate input\n\n");

for(i=0; i<21; efficient="); scanf(" i="="0)" power="); scanf(" n="i;" i="0;" t="eq[i][0]*(-1);" j="0;" i="0;"> else
printf("+%.2fX%.0f",oeq[i][0],oeq[i][1]);
}
if(oeq[i][0]<0)
{
printf("%.2fX%.0f",oeq[i][0],oeq[i][1]);
}
}
printf("=0");

printf("\n\n\n Enter First approximation=");
scanf("%f",&nsum);

while(1)
{ stt:
for(i=0; i sum=sum+oeq[i][0]*pow(nsum,oeq[i][1]);
if(nsum==sum)
{ printf("\n\n Solution=%f",sum);
goto ex;
}
else
{ nsum=sum;
goto stt;
}

}
ex:
nsum=sum;
sum=0.0;
for(i=0; i sum=sum+oeq[i][0]*pow(nsum,oeq[i][1]);
printf("\n\n\n f(%.2f)=%.2f",nsum,sum);

getch();
goto start;
}

M M C , M C & etc Random Number Generator

/* ........Random Number Generator.........*/
/*.........Author: Sudipta Kumar paik......*/
/*.........Date: 16/02/2008Time:11:47 pm...*/

#include stdio.h >
#include stdlib.h >
#include dos.h >
#include math.h >

main()
{
int a,b,k,i,j,m,nn,seed,r[50];
char c;


clrscr();

start:
printf("\n\n\n ==========================================================");
printf("\n ==========================================================");
printf("\n Your Choice : ");
printf("\n ==========================================================");
printf("\n [a]= MIXED MULTIPLICATIVE CONGRUENTIAL (M M C) GENERATOR:");
printf("\n [b]= MULTIPLICATIVE CONGRUENTIAL (M C) GENERATOR:");
printf("\n [c]= ADDITIVE CONGRUENTIAL GENERATOR:");
printf("\n [d]= ARITHMATIC CONGRUENTIAL GENERATOR:");
printf("\n [e]= EXIT:");
printf("\n ==========================================================");
printf("\n Choice: ");
c=getchar();
printf("\n ==========================================================");
switch(c)
{ case 'a':

printf("\n Enter the INTEGER Values of a: ");
scanf("%d",&a);
printf("\n Enter the INTEGER Values of b: ");
scanf("%d",&b);
printf("\n Enter the INTEGER Values of m: ");
scanf("%d",&m);

printf("\n Numbers of Random Numbers to be generated n= ");
scanf("%d",&nn);

printf("\n\n [ %d ] Random Numbers are..........\n\n",nn);

r[0]=1;

for(i=1; i<=nn; ++i)
{
r[i]=(a*r[i-1]+b)%m;
printf(" °%4d° ",r[i]);
}

break;

case 'b':

printf("\n Enter the INTEGER Values of a: ");
scanf("%d",&a);
printf("\n Enter the INTEGER Values of m: ");
scanf("%d",&m);
printf("\n Numbers of Random Numbers to be generated n= ");
scanf("%d",&nn);
printf("\n\n [ %d ] Random Numbers are..........\n\n",nn);

r[0]=1;
b=0;

for(i=1; i<=nn; ++i)
{
r[i]=(a*r[i-1]+b)%m;
printf(" °%4d° ",r[i]);
}

break;

case 'c':

printf("\n Enter the INTEGER Values of b: ");
scanf("%d",&b);
printf("\n Enter the INTEGER Values of m: ");
scanf("%d",&m);

printf("\n Numbers of Random Numbers to be generated n= ");
scanf("%d",&nn);

printf("\n\n [ %d ] Random Numbers are..........\n\n",nn);

r[0]=1;
a=1;

for(i=1; i<=nn; ++i)
{
r[i]=(a*r[i-1]+b)%m;
printf(" °%4d° ",r[i]);
}

break;

case 'd':

printf("\n Enter the INTEGER Values of r1: ");
scanf("%d",&r[1]);
printf("\n Enter the INTEGER Values of r2: ");
scanf("%d",&r[2]);
printf("\n Enter the INTEGER Values of m: ");
scanf("%d",&m);

printf("\n Numbers of Random Numbers to be generated n= ");
scanf("%d",&nn);

printf("\n\n [ %d ] Random Numbers are..........\n\n",nn);

for(i=1; i<=nn-2; ++i)
{
r[i+2]=(r[i]+r[i+1])%m;
printf(" °%4d° ",r[i+2]);
}

break;

case 'e':

printf("\n\n\n\t\t\t\t\t\t ............Thanks");
printf("\n\n\n\t\t\t\t\t\t ............Develop By Sudipta");
delay(1400);
exit(1);

}
printf("\n\n\n\t\t\t\t\t\t ............Press Enter");
getch();
goto start;
}

Mid Square Random Number Generator

/* ........Random Number Generator.........*/
/*.........Author: Sudipta Kumar paik......*/
/*.........Date: 16/02/2008Time:11:47 pm...*/

#include stdio.h>
#include stdlib.h>
#include math.h>

main()
{
long int i,s,x,y,z,nd,seed;
int n;

clrscr();

seed=6785;
printf("\n Numbers of Random Numbers to be generated n= ");
scanf("%d",&n);
printf("\n\n [ %d ] Random Numbers are..........\n\n",n);

for(i=1; i<=n; ++i)
{
y=(seed*seed)/100;
z=y/10000;
x=(y-z*10000);
seed=x;
printf(" °%4d° ",x);
}
printf("\n\t\t\t\t\t\t ............Develop By Sudipta");
getch();
}

Star

/*......++++Create Star+++++++++++.....
.......++++AUTHOR: SUDIPTA KUMAR PAIK++++++.......
.....++++Date 17/02/2008++++++++++.......... */
main()
{
int i,j,n;

clrscr();

printf("\n Enter No of Star: ");
scanf("%d",&n);
printf("\n\n");

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

for(j=n; j>=i; j=j-2)
printf(" ");

for(j=1; j<=i; j++)
printf("*");
printf("\n");
}

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

for(j=n; j>=i; j=j-2)
printf(" ");

for(j=1; j<=i; j++)
printf("*");
printf("\n");
}

getch();
}

Least Squaqe Regression (Order 1)

/* ......++++++ Least Squre Regression ....++++
......++++++ Order 1 ....++++
......++++++ AUTHOR: SUDIPTA KUMAR PAIK ....++++
......++++++ Numerical Method __3702 ....++++*/


# define eqn 50

main()
{ int q,n,m;
float xy[eqn+1][eqn+2];
float sumxy=0,sumx=0,sumy=0,sumx2=0,x_bar=0,y_bar=0,a0,a1;

do
{
clrscr();

printf("\n\t\tEnter [ 0 ] for EXIT \n");
printf("\n\t\tMaximum[ %d ] \n",eqn);
printf("\n Enter No. of Observation ");

scanf("%d",&n);
if(n==0)
{ printf("\n\n\n\t\tThank you for use this program\n");
printf("\n\t\t.............Prepared By SUDIPTA.............. ");
getch();
exit(1);
}
m=n+1;
for(q=1; q<=n; q++)
{ printf("\n\n Observation %d\n",q);
xy[q][1]=q;
printf("\n x%d=%.f",q,xy[q][1]);
printf("\ty%d=", q);
scanf("%f",&xy[q][2]);
}

for(q=1; q<=n; q++)
{ sumxy=sumxy+xy[q][1]*xy[q][2];
sumx2=sumx2+xy[q][1]*xy[q][1];
sumx=sumx+xy[q][1];
sumy=sumy+xy[q][2];
}
x_bar=sumx/n;
y_bar=sumy/n;

a1=((n*sumxy)-(sumx*sumy))/((n*sumx2)-(sumx*sumx));
a0=y_bar-a1*x_bar;

printf("\n\n\n The Least Square Fit is ");

if(a1<0)
printf("\n\n y=%f%fx",a0,a1);
else
printf("\n\n y=%f+%fx",a0,a1);

getch();

}while(1);

}

Least Square Regression (Order 2)

/*......++++++ Least Squre Regression ++++++......
......++++++ Order 2(Graphics)++++++......
......++++++ AUTHOR: SUDIPTA KUMAR PAIK ++++++......
......++++++ Numerical Method __3721 ++++++......*/

#include graphics.h>
#include stdlib.h>
#include stdio.h>
#include conio.h>
#include math.h>

# define eqn 50

main()
{
int i,xx,yy,qw,q,n,k,j,h,f,d,N=3,xmax, ymax,xlow,ylow;
int gdriver = DETECT, gmode, errorcode;
float xy[eqn+1][3],r[4][5],a[4][5],hh[1000][3];
float sumxi,sumxi2,sumyi,sumxi3,sumxiyi,sumxi4,sumxi2yi,ar[4],x;
char msg[80];

sumxi=sumxi2=sumyi=sumxi3=sumxiyi=sumxi4=sumxi2yi=0;

/* xmax = 639; ymax = 479; */

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

do
{ sumxi=sumxi2=sumyi=sumxi3=sumxiyi=sumxi4=sumxi2yi=0;
xmax = 639;
ymax = 479;
xlow=169;
ylow=7;

clrscr();
setbkcolor(9);

printf("\n\t\tEnter [ 0 ] for EXIT \n");
printf("\n\t\tMaximum[ %d ] \n",eqn);

printf("\n Enter No. of Observation= ");
scanf("%d",&n);

if(n==0)
{ printf("\n\n\n\t\t\t<<<<===Thank you for use this program===>>>>\n");
printf("\n\t\t\t\t.............Prepared By SUDIPTA.............. ");
getch();
//delay(1200);
exit(1);
}

def1:
setbkcolor(i+1);
printf("\n\n Excute ONE........\n");
printf("\n [1]-INPUT MANUALY");
printf("\n [2]-INPUT RANDOMLY");
printf("\n\n CHOICE= ");
scanf("%d",&q);
printf("\n");
switch(q)
{ case 1:
for(i=1; i<=n; i++)
{ printf("\n Observation [ %d ]\n",i);
printf("\n x%d=",i);
scanf("%f",&xy[i][1]);
printf("\ty%d=", i);
scanf("%f",&xy[i][2]);
}

break;
case 2:
setbkcolor(i+4);
for(i=1; i<=n; i++)
{
xy[i][1]=(rand() % 24);
xy[i][2]=(rand() % 24);
}
break;
default:
setbkcolor(i+5);
printf("\n WRONG SELECTION");
printf("\n Press Enter");
getch();
goto def1;
}



for(i=1; i<=n; i++)
{ sumxi=sumxi+xy[i][1];
sumyi=sumyi+xy[i][2];
sumxiyi=sumxiyi+xy[i][1]*xy[i][2];
sumxi2yi=sumxi2yi+xy[i][1]*xy[i][1]*xy[i][2];
sumxi2=sumxi2+xy[i][1]*xy[i][1];
sumxi3=sumxi3+xy[i][1]*xy[i][1]*xy[i][1];
sumxi4=sumxi4+xy[i][1]*xy[i][1]*xy[i][1]*xy[i][1];
}

r[1][1]=n; r[1][2]=sumxi; r[1][3]=sumxi2; r[1][4]=sumyi;
r[2][1]=sumxi; r[2][2]=sumxi2; r[2][3]=sumxi3; r[2][4]=sumxiyi;
r[3][1]=sumxi2; r[3][2]=sumxi3; r[3][3]=sumxi4; r[3][4]=sumxi2yi;


for(k=1,j=1; j<=N; j++,k++)
{
for(f=1; f<=N; f++)
{ for(h=1; h<=N+1; h++)
a[f][h]=r[f][h];
}

for(i=1; i<=N+1; i++)
r[j][i]=r[j][i]/a[k][k];

for(f=1; f<=N; f++)
{ for(h=1; h<=N+1; h++)
a[f][h]=r[f][h];
}

for(i=1; i<=N; i++)
{ if(i!=j)
{ for(d=1; d<=N+1; d++)
r[i][d]=r[i][d]-a[i][j]*r[j][d];
}
}

}

ar[0]=r[1][4];
ar[1]=r[2][4];
ar[2]=r[3][4];

clrscr();
for(i=1; i<=n; i++)
{ printf(" ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n Observation [ %2d ]\n",i);
printf(" x%2d=%4.2f ±",i,xy[i][1]);
printf(" y%2d=%4.2f\n",i,xy[i][2]);
}

if(r[2][4]<0 && r[3][4]<0)
{
sprintf(msg," y=%f%fx%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);
}
else if(r[2][4]<0 && r[3][4]>0)
{
sprintf(msg," y=%f%fx+%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);
}
else if(r[2][4]>0 && r[3][4]<0)
{
sprintf(msg," y=%f+%fx%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);
}
else if(r[2][4]>0 && r[3][4]>0)
sprintf(msg," y=%f+%fx+%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);


setbkcolor(6);
setcolor(3);
line(xlow, ymax/2, xmax, ymax/2);
line(xlow-1, ymax/2+1, xmax+1, ymax/2+1);
line(xmax/2+xlow/2, ylow, xmax/2+xlow/2, ymax);
line(xmax/2+xlow/2+1,ylow-1, xmax/2+xlow/2+1, ymax+1);
setcolor(GREEN);
rectangle(xlow,ylow, xmax, ymax);
rectangle(xlow-1,ylow-1, xmax-1, ymax-1);
rectangle(xlow-2,ylow-2, xmax-2, ymax-2);


for(i=1; i<=n; i++)
{ setcolor(10);
xx = ceil(xy[i][1])+235;
yy = ceil(xy[i][2])+235;
circle(xx+xlow,yy,2);
delay(24);
}


x=-125.0;

for(qw=1; qw<=1000 ; qw++)
{
hh[qw][1]=x;
hh[qw][2]=ar[0]+ar[1]*x+ar[2]*x*x;

xx = ceil(hh[qw][1])+235;
yy = ceil(hh[qw][2])+235;

putpixel(xx+xlow,yy,14);

delay(11);

x=x+.25;
}

getch();

}while(1);

}

Least Square Regression

/* ......++++++ Least Squre Regression ++++++......
......++++++ Order 2(Graphics) ++++++......
......++++++ AUTHOR: SUDIPTA KUMAR PAIK ++++++......
......++++++ Numerical Method __3721 ++++++......*/

#include graphics.h>
#include stdlib.h>
#include stdio.h>
#include conio.h>
#include math.h>

# define eqn 50

main()
{
int i,xx,yy,qw,q,n,k,j,h,f,d,N=3,xmax, ymax,xlow,ylow;
int gdriver = DETECT, gmode, errorcode;
float xy[eqn+1][3],r[4][5],a[4][5],hh[1000][3];
float sumxi,sumxi2,sumyi,sumxi3,sumxiyi,sumxi4,sumxi2yi,ar[4],x;
char msg[80];

sumxi=sumxi2=sumyi=sumxi3=sumxiyi=sumxi4=sumxi2yi=0;

/* xmax = 639; ymax = 479; */

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

do
{ sumxi=sumxi2=sumyi=sumxi3=sumxiyi=sumxi4=sumxi2yi=0;
xmax = 639;
ymax = 479;
xlow=169;
ylow=7;

clrscr();
setbkcolor(9);

printf("\n\t\tEnter [ 0 ] for EXIT \n");
printf("\n\t\tMaximum[ %d ] \n",eqn);

printf("\n Enter No. of Observation= ");
scanf("%d",&n);

if(n==0)
{ printf("\n\n\n\t\t\t<<<<===Thank you for use this program===>>>>\n");
printf("\n\t\t\t\t.............Prepared By SUDIPTA.............. ");
getch();
//delay(1200);
exit(1);
}

def1:
setbkcolor(i+1);
printf("\n\n Excute ONE........\n");
printf("\n [1]-INPUT MANUALY");
printf("\n [2]-INPUT RANDOMLY");
printf("\n\n CHOICE= ");
scanf("%d",&q);
printf("\n");
switch(q)
{ case 1:
for(i=1; i<=n; i++)
{ printf("\n Observation [ %d ]\n",i);
printf("\n x%d=",i);
scanf("%f",&xy[i][1]);
printf("\ty%d=", i);
scanf("%f",&xy[i][2]);
}

break;
case 2:
setbkcolor(i+4);
for(i=1; i<=n; i++)
{
xy[i][1]=(rand() % 24);
xy[i][2]=(rand() % 24);
}
break;
default:
setbkcolor(i+5);
printf("\n WRONG SELECTION");
printf("\n Press Enter");
getch();
goto def1;
}



for(i=1; i<=n; i++)
{ sumxi=sumxi+xy[i][1];
sumyi=sumyi+xy[i][2];
sumxiyi=sumxiyi+xy[i][1]*xy[i][2];
sumxi2yi=sumxi2yi+xy[i][1]*xy[i][1]*xy[i][2];
sumxi2=sumxi2+xy[i][1]*xy[i][1];
sumxi3=sumxi3+xy[i][1]*xy[i][1]*xy[i][1];
sumxi4=sumxi4+xy[i][1]*xy[i][1]*xy[i][1]*xy[i][1];
}

r[1][1]=n; r[1][2]=sumxi; r[1][3]=sumxi2; r[1][4]=sumyi;
r[2][1]=sumxi; r[2][2]=sumxi2; r[2][3]=sumxi3; r[2][4]=sumxiyi;
r[3][1]=sumxi2; r[3][2]=sumxi3; r[3][3]=sumxi4; r[3][4]=sumxi2yi;


for(k=1,j=1; j<=N; j++,k++)
{
for(f=1; f<=N; f++)
{ for(h=1; h<=N+1; h++)
a[f][h]=r[f][h];
}

for(i=1; i<=N+1; i++)
r[j][i]=r[j][i]/a[k][k];

for(f=1; f<=N; f++)
{ for(h=1; h<=N+1; h++)
a[f][h]=r[f][h];
}

for(i=1; i<=N; i++)
{ if(i!=j)
{ for(d=1; d<=N+1; d++)
r[i][d]=r[i][d]-a[i][j]*r[j][d];
}
}

}

ar[0]=r[1][4];
ar[1]=r[2][4];
ar[2]=r[3][4];

clrscr();
for(i=1; i<=n; i++)
{ printf(" ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n Observation [ %2d ]\n",i);
printf(" x%2d=%4.2f ±",i,xy[i][1]);
printf(" y%2d=%4.2f\n",i,xy[i][2]);
}

if(r[2][4]<0 && r[3][4]<0)
{
sprintf(msg," y=%f%fx%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);
}
else if(r[2][4]<0 && r[3][4]>0)
{
sprintf(msg," y=%f%fx+%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);
}
else if(r[2][4]>0 && r[3][4]<0)
{
sprintf(msg," y=%f+%fx%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);
}
else if(r[2][4]>0 && r[3][4]>0)
sprintf(msg," y=%f+%fx+%fx*x",r[1][4],r[2][4],r[3][4]);
outtextxy(180,420, msg);


setbkcolor(6);
setcolor(3);
line(xlow, ymax/2, xmax, ymax/2);
line(xlow-1, ymax/2+1, xmax+1, ymax/2+1);
line(xmax/2+xlow/2, ylow, xmax/2+xlow/2, ymax);
line(xmax/2+xlow/2+1,ylow-1, xmax/2+xlow/2+1, ymax+1);
setcolor(GREEN);
rectangle(xlow,ylow, xmax, ymax);
rectangle(xlow-1,ylow-1, xmax-1, ymax-1);
rectangle(xlow-2,ylow-2, xmax-2, ymax-2);


for(i=1; i<=n; i++)
{ setcolor(10);
xx = ceil(xy[i][1])+235;
yy = ceil(xy[i][2])+235;
circle(xx+xlow,yy,2);
delay(24);
}


x=-125.0;

for(qw=1; qw<=1000 ; qw++)
{
hh[qw][1]=x;
hh[qw][2]=ar[0]+ar[1]*x+ar[2]*x*x;

xx = ceil(hh[qw][1])+235;
yy = ceil(hh[qw][2])+235;

putpixel(xx+xlow,yy,14);

delay(11);

x=x+.25;
}

getch();

}while(1);

}

GAUSS Jordan

/*..................GAUSS JORDAN.............
...........AUTHOR:-SUDIPTA KUMAR PAIK......
......................N4_11................*/
# define eqn 20

main()
{ int q,w,m,i,j,kkl,ll,n,h,f,k,d,alp=97;
float r[eqn][eqn+1],a[eqn][eqn+1];

do
{
clrscr();

printf("\n Enter [ 0 ] for EXIT \n");
printf("\n Enter No. of Equation ");

scanf("%d",&n);
if(n==0)
{ printf("\n\n\n\t\tThank you for use this program\n");
printf("\n\t\t.............Prepared By SUDIPTA.............. ");
getch();
exit(1);
}
m=n+1;

input:
printf("\n\n [1]-Manual Input ");
printf("\n\n [2]-Random Input ");
printf("\n\n Choice ");
scanf("%d",&kkl);

switch(kkl)
{
case 1:

for(q=1; q<=n; q++)
{ printf("\n\n Eqution %d\n",q);
for(w=1; w<=n; w++)
{ printf("\n Enter %c%d=",alp,w);
scanf("%f",&r[q][w]);
}
printf("\nEnter Constant K%d=", q);
scanf("%f",&r[q][w]);
alp++;
}
break;

case 2:

for(q=1; q<=n; q++)
{ for(w=1; w<=m; w++)
r[q][w]=rand()%(9);
}
break;

default:
printf("\n Wrong Selection");
getch();
goto input;
}

printf("\n\n\n");
for(q=1; q<=n; q++)
{ printf("\n\n");
for(w=1; w<=n; w++)
{ if(r[q][w]<0)
printf("%4.2fX%d",r[q][w],w );
else if(w==1)
printf("%4.2fX%d",r[q][w],w );
else
printf("+%4.2fX%d",r[q][w],w );
}
printf("=%4.2f",r[q][w]);
}

printf("\n\n\n Ú");
for(q=1; q<=8*m; q++)
printf(" ");
printf(" ¿");
printf("\n ³");
for(q=1; q<=8*m; q++)
printf(" ");
printf(" ³");

for(q=1; q<=n; q++)
{ printf("\n ³");
for(w=1; w<=n; w++)
{ printf("%8.4f",r[q][w]);
}
printf("%8.4f ",r[q][w] );
printf("³\n ³");

for(ll=1; ll<=8*m; ll++)
printf(" ");
printf(" ³");

}
printf("\n À");
for(ll=1; ll<=8*m; ll++)
printf(" ");
printf(" Ù");
getch();

for(k=1,j=1; j<=n; j++,k++)
{
for(f=1; f<=n; f++)
{ for(h=1; h<=m; h++)
a[f][h]=r[f][h];
}

for(i=1; i<=m; i++)
r[j][i]=r[j][i]/a[k][k];

for(f=1; f<=n; f++)
{ for(h=1; h<=m; h++)
a[f][h]=r[f][h];
}

for(i=1; i<=n; i++)
{ if(i!=j)
{ for(d=1; d<=m; d++)
r[i][d]=r[i][d]-a[i][j]*r[j][d];
}
}

}

printf("\n\n\n\n\tResult Determinant.........\n");
printf("\n\n Ú");
for(q=1; q<=8*m; q++)
printf(" ");
printf(" ¿");
printf("\n ³");
for(q=1; q<=8*m; q++)
printf(" ");
printf(" ³");

for(q=1; q<=n; q++)
{ printf("\n ³");
for(w=1; w<=n; w++)
printf("%8.4f",r[q][w]);
printf("%8.4f ",r[q][w] );
printf("³\n ³");

for(ll=1; ll<=8*m; ll++)
printf(" ");
printf(" ³");

}
printf("\n À");
for(ll=1; ll<=8*m; ll++)
printf(" ");
printf(" Ù");

printf("\n\n Result.........\n");
for(q=1; q<=n; q++)
printf("\n\n X%d=%.5f",q,r[q][m]);

getch();
}while(1);

}

False Position Method

/*....................... False Position Method.........................

.......................Author:-SUDIPTA KUMAR PAIK......................

.......................NUMERICAL METHOD.................*/



#include

#include

#include

main()

{ int i,N,x;

float fxlr,xr_old,xu,xl,xr,fxl,fxr,fxu,eq[21][2];

clrscr();



printf("\n Enter [0] to terminate input\n\n");



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

{

printf("\n Enter Co-efficient=");

scanf("%f",&eq[i][0]);

if(eq[i][0]==0)

break;

printf("\n Enter Power=");

scanf("%f",&eq[i][1]);

}





N=i;

printf("\n\n\nf(X)=");

for(i=0; i<0)

printf("%.2fX%.0f",eq[i][0],eq[i][1]);

if(eq[i][0]>0)

printf("+%.2fX%.0f",eq[i][0],eq[i][1]);

}



ss:

printf("\n\n\n Enter Lower value xl=");

scanf("%f",&xl);

printf("\n Enter Upper value xu=");

scanf("%f",&xu);



fxl=fxu=fxr=xr=0.0;



for(i=0; i
{ fxl=fxl+eq[i][0]*pow(xl,eq[i][1]);

fxu=fxu+eq[i][0]*pow(xu,eq[i][1]);

}



fxlr=fxl*fxu;



if(fxlr>-1)

{ printf("\n Upper Limit & Lower Limit is Not Correct");

goto ss;

}

else if(fxl==0)

goto end;

else if(fxu==0)

goto end;





xrl:

xr_old=xr;

xr=xu-((fxu*(xl-xu))/(fxl-fxu));

fxl=fxu=fxr=0.0;



for(i=0; i
{ fxl=fxl+eq[i][0]*pow(xl,eq[i][1]);

fxr=fxr+eq[i][0]*pow(xr,eq[i][1]);

fxu=fxu+eq[i][0]*pow(xu,eq[i][1]);

}



fxlr=fxl*fxr;



if((sqrt(pow(xr_old-xr,2)))<0.00005)

goto end;



else if(fxlr<0)

{ xu=xr;

goto xrl;

}

else if(fxlr>0)

{ xl=xr;

goto xrl;

}





end:

xr=xl;

xr=xu;

fxr=0;

printf("\n\n\n ROOT=%f",xr);

for(i=0; i
fxr=fxr+eq[i][0]*pow(xr,eq[i][1]);

printf("\n\n f(%f)=%f",xr,fxr);



getch();

}

Bisection Method

/*....................... Bisection Method.........................

.......................Author:-SUDIPTA KUMAR PAIK......................

.......................NUMERICAL METHOD.................*/



#include < iostream.h >

#include < math.h >

#include < stdio.h >

#include < conio.h >

main()

{ int i,N;

float fxlr,xu,xl,xr,fxl,fxr,fxu,eq[21][2];

clrscr();



fxl=0.0;

fxu=0.0;

fxr=0.0;



printf("\n Enter [0] to terminate input\n\n");



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

{

printf("\n Enter Co-efficient=");

scanf("%f",&eq[i][0]);

if(eq[i][0]==0)

break;

printf("\n Enter Power=");

scanf("%f",&eq[i][1]);

}





N=i;

printf("\n\n\nf(X)=");

for(i=0; i
printf("+%.2fX%.0f",eq[i][0],eq[i][1]);



ss:

printf("\n\n\n Enter Lower value xl=");

scanf("%f",&xl);

printf("\n Enter Upper value xu=");

scanf("%f",&xu);





for(i=0; i
{ fxl=fxl+eq[i][0]*pow(xl,eq[i][1]);

fxu=fxu+eq[i][0]*pow(xu,eq[i][1]);

}



// fxlr=fxl*fxu;



/*if(fxlr>0)

{ printf("\n Upper Limit & Lower Limit is Not Correct");

goto ss;

} */

if(fxl==0)

goto endxl;

else if(fxu==0)

goto endxu;



xrl:

xr=(xl+xu)/2;



for(i=0; i
{ fxl=fxl+eq[i][0]*pow(xl,eq[i][1]);

fxr=fxr+eq[i][0]*pow(xr,eq[i][1]);

}



fxlr=fxl*fxr;



if(fxlr<0)

{ xu=xr;

fxl=0.0;

fxu=0.0;

fxr=0.0;



goto xrl;

}

else if(fxlr>0)

{ xl=xr;

fxl=0.0;

fxu=0.0;

fxr=0.0;



goto xrl;

}

else if (fxlr<0.0005 && fxlr>-0.0005 )

printf("\n\n\n ROOT=%.2f",xr);

goto end;





endxl:

xr=xl;

printf("\n\n\n ROOT=%.2f",xl);

goto end;

endxu:

xr=xu;

printf("\n\n\n ROOT=%.2f",xu);



/* printf("\n\n\nf(%.2f)=%.2f",xl,fxl);

printf("\n\n\nf(%.2f)=%.2f",xr,fxr); */



end:

for(i=0; i
fxr=fxr+eq[i][0]*pow(xr,eq[i][1]);

printf("\n\nf(%.2f)=%.2f",xr,fxr);



getch();

}