Wednesday, 8 April 2015

Write a C program to solve linear system of equations by using Gauss Seidal Method.

#include<stdio.h>
#include<math.h>

void show(float a[][200],int r);

main()
{
    float a[200][200],err,aerr=0.00001,x[200]={0},maxerr,t;
    int r,i,j,maxitr=10,itr,s;
    printf("\nEnter number of variables=");
    scanf("%d",&r);
    for(i=0;i<r;i++)
    {
        for(j=0;j<r;j++)
        {
            printf("\nEnter coefficient of x%d=",j);
            scanf("%f",&a[i][j]);
        }
        printf("\nEnter constant value=");
        scanf("%f",&a[i][r]);
    }
    printf("\nThe augmented matrix is \n");
    show(a,r);
    printf("Iteration\t");
    for(i=0;i<r;i++)
        printf("     x[%2d]     ",i+1);
    printf("\n");

    for(itr=1;itr<=maxitr;itr++)
    {
        maxerr=0;
        for(i=0;i<r;i++)
        {
            s=0;
            for(j=0;j<r;j++)
            {
                if(j!=i)
                {
                    s+=a[i][j]*x[j];
                }
            }
            t=(a[i][r]-s)/a[i][i];
            err=fabs(x[i]-t);
            if(err>maxerr)
                maxerr=err;
            x[i]=t;
        }
        printf("%5d     ",itr);
        for(i=0;i<r;i++)
        {
            printf("%15f",x[i]);
        }
        printf("\n");
        if(maxerr<aerr)
        {
            printf("Converges in %3d Iteration\n",itr);
            for(i=0;i<r;i++)
            {
                printf("x[%d]=%f",i,x[i]);
            }
            return 0;
        }
    }
    printf("\nSolution does not converge, needs more iteration \n");
}

void show(float a[][200],int r)
{
    int i,j;
    for(i=0;i<r;i++)
    {
        for(j=0;j<r;j++)
        {
            printf("%20f",a[i][j]);
        }
        printf("  %20f",a[i][r]);
        printf("\n");
    }
    printf("\n");
}

Monday, 30 March 2015

Gauss Jordan Method

#include<stdio.h>

void make1( float a[][20],int, int,int );
void make0( float a[][20],int, int,int );
void show(float a[][20],int r,int c);
int rankA(float a[][20],int r);
int rankAB(float a[][20],int r,int c);

main()
{
    float a[ 20 ][ 20 ];
    int r,c,i,j;
    printf("\nEnter number of variables=");
    scanf("%d",&r);
    c=r+1;
    for(i=0;i<r;i++)
    {
        for(j=0;j<c-1;j++)
        {
            printf("\nEnter coefficient of x%d=",j);
            scanf("%f",&a[i][j]);
        }
        for(;j<c;j++)
        {
            printf("\nEnter constant value=");
            scanf("%f",&a[i][j]);
        }
    }
    printf("\nThe augmented matrix is \n");
    show(a,r,c);
    for(i=0;i<r;i++)
    {
            make1(a,i,r,c);
            show(a,r,c);
            make0(a,i,r,c);
            show(a,r,c);
    }
    if(rankA(a,r)==r && rankAB(a,r,c)==r)
    {
        printf("\nSolutions are\n");
        for(i=0;i<r;i++)
            printf("x%d%d=%f\t",i,i,a[i][c-1]);
    }
    else if(rankA(a,r)==rankAB(a,r,c))
        printf("\nInfinite solutions\n");
    else
        printf("\nNo solutions\n");
}

int rankA(float a[][20],int r)
{
    int rnk=0,i,j;
    for(i=0;i<r;i++)
    {
        if(a[i][i]!=0)
            rnk++;
    }
    return rnk;
}

int rankAB(float a[][20],int r,int c)
{
    int rnk=0,i,j;
    for(i=0;i<r;i++)
    {
        if(a[i][i]!=0)
            rnk++;
        else if(a[i][c]!=0)
            rnk++;
    }
    return rnk;
}
void show(float a[][20],int r,int c)
{
    int i,j;
    for(i=0;i<r;i++)
    {
        for(j=0;j<c-1;j++)
        {
            printf("%20f",a[i][j]);
        }
        printf(" | %20f",a[i][j]);
        printf("\n");
    }
    printf("\n");
}

void make1( float a[][20], int i,int r,int c )
{
  
    float t=a[i][i];
    int j=i;
    printf("\nMaking 1\n");
    if(t==0)
        return;
    for(j=i;j<c;j++)
    {
        a[i][j]=a[i][j]/t;
    }
}

void make0( float a[][20], int k,int r,int c )
{
    int i,j;
    float temp;
    printf("\nMaking 0\n");
    for(i=0;i<r;i++)
    {
      
        if(i!=k)
        {
            temp=a[i][k];
            for(j=k;j<c;j++)
            {
                a[i][j]=a[i][j]-a[k][j]*temp;
            }
        }
    }
}

Thursday, 26 February 2015

series

/*
Write a program to determine the sum of the following series:
    S = 1 – 3 + 5 – 7 + ...(n terms)
Read the value of n from the user.

*/
#include<stdio.h>

main()
{
    int n, s=0,i,sign=1;
    printf("\nEnter value of n=");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        s=s+sign*(2*i-1);   
        sign=sign*(-1);
    }
    printf("\nThe sum of the series=%d",s);
}

Write a function that returns 1 if the two matrices passed to it as argument are equal and 0 otherwise

#include<stdio.h>

int IsEqual(int [][100],int,int, int[][100],int,int);
main()
{
    int m1[100][100],m2[100][100],r1,r2,c1,c2,i,j;
    printf("\nEnter row and column number of the first matrix=");
    scanf("%d%d",&r1,&c1);
    printf("\nEnter the elements of the first matrix\n");
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            scanf("%d",&m1[i][j]);
        }
    }
    printf("\nThe first matrix\n");
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            printf("%6d",m1[i][j]);
        }
        printf("\n");
    }
    printf("\nEnter row and column number of the second matrix=");
    scanf("%d%d",&r2,&c2);
    printf("\nEnter the elements of the first matrix\n");
    for(i=0;i<r2;i++)
    {
        for(j=0;j<c2;j++)
        {
            scanf("%d",&m2[i][j]);
        }
    }
    printf("\nThe second matrix\n");
    for(i=0;i<r2;i++)
    {
        for(j=0;j<c2;j++)
        {
            printf("%6d",m2[i][j]);
        }
        printf("\n");
    }
    if(IsEqual(m1,r1,c1,m2,r2,c2)==1)
        printf("\nThe matrices are equal");
    else
        printf("\nThe matrices are not equal");
}

int IsEqual(int a[][100],int r1,int c1, int b[][100],int r2,int c2)
{
    int i,j;
    if(r1!=r2 || c1!=c2)
        return 0;
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            if(a[i][j]!=b[i][j])
            {
                return 0;
            }
        }
    }
    return 1;
   
}

Write a function which accepts an array of size n containing integer values and returns average of all values. Call the function from main program.

#include<stdio.h>

int avg(int [],int);

main()
{
    int a[100], n,i,avrg;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    avrg=avg(a,n);
    printf("\nThe average=%d",avrg);
}
int avg(int a[],int n)
{
    int i,s=0;
    for(i=0;i<n;i++)
    {
        s=s+a[i];
    }
    return s/n;
}

Tuesday, 24 February 2015

DOEACC A3-R3 January, 2009 Question paper

5.
a)    What are the commonly used input functions in ‘C’? Write their syntax and explain the purpose of each.
b)    Develop a flowchart and then write a program to compute the roots of a quadratic equation A*X^2 + B*X + C = 0. Allow the possibility that (B^2 – 4*A*C) <= 0.
(6+9)
6.
a)    What are logical, syntactic and execution errors? Give examples of each. Which is most difficult to find and why?
b)    Enumerate features of a good ‘C’ program. Describe the commonly used techniques as to how ‘C’ programs can be made highly readable and modifiable.
c)    What is an algorithm? Develop an algorithm to test whether a given number is a prime number.
(5+5+5)
7.
a)    Develop loops using
    i)    While statement
    ii)    Do-while statement
    iii)    For statement
that will calculate the sum of every third integer, beginning with k=2 for all values of k <= 100.
b)    Write a function that will compute
        Y = X^n
    Where Y and X are floating point numbers and n is an integer number. Use this function and print the output
     X         n        Y
    ...         ...        ...

Check for possible exceptions that may occur during computations with regard to the magnitude of computed values.

(6+9)
8.
a)    How does an array differ from a structure? Give and explain the syntax of array and structure as defined in ‘C’.
b)    How are one-dimensional and two-dimensional arrays stored in computer memory? Illustrate with an example.
c)    Develop a program to multiply two matrices with sizes 3x4 and 4x5. Your program should take care of the fact that no element of either matrix can be negative. Include appropriate documentation.

(6+2+7)
9.
a)    Give the main advantage of storing data as a file. Describe various ways in which data files can be categorized in ‘C’. Illustrate by examples.
b)    What is an indirection operator? Explain its usage to access a multidimensional array element. Illustrate your answer by an example.
c)    ‘C’ compiler supports many pre-processor commands. Write their names only.

(6+6+3)
 

DOEACC A3-R3 July, 2008 Question paper

5.
a)    What is structured programming? Explain and give examples of relevant constructs using pseudo-code. Highlight the advantages and disadvantages of structured programming.
b)    What is an execution error? Differentiate it from syntactic error. Give examples.
c)    It is said that ‘C’ is a middle level assembly language. Mention those features of ‘C’ which enable this description.

(8+3+4)
6.
a)    Write and explain the action of WHILE statement. Develop a program in ‘C’ language to compute the average of every third integer number lying between 1 and 100. Include appropriate documentation.
b)    Develop a function to calculate sum of n even integers starting from a given even integer.
c)    Identify all the compound statements which appear in the following program segment:

{
    sum=0;
    do
    {
        scanf(“%d”, &i);
        if (i < 0)
        {
            i=-i;
            ++flag;
        }
        sum += i;
    } while (i != 0);
}

(7+5+3)
7.
a)    Define an array. How are arrays processed in ‘C’? Illustrate by taking two-dimensional arrays as examples.
b)    What are subscripts? How are they specified? What restrictions apply to the values that can be assigned to subscripts in ‘C’ language?
c)    Write a ‘C’ program that will enter a line of text, store in an array and then display backwards. The length of the line should be undefined, (being terminated by ENTER key), but less than 80 characters.

(4+4+7)
8.
a)    What is a pointer in ‘C’? How is a pointer variable declared? Give examples and explain. Enumerate the utility of pointer variables.
b)    A program in ‘C’ language contains the following declaration:
    static int x[8] = {1,2,3,4,5,6,7,8};
    i)    What is the meaning of x?
    ii)    What is the meaning of (x + 2)?
    iii)    What is the meaning of *x?
    iv)    What is the meaning of (*x + 2)?
    v)    What is the meaning of *(x + 2)?
c)    What is a structure? How does a structure differ from a union? Give examples. For what kind of applications, union data structure is useful? How are arrays different from structure?
(4+5+6)
9.
a)    How can a procedure be defined in ‘C’? Give an example. Bring out the differences between function and procedure.
b)    Draw a flowchart and then develop an interactive ‘C’ program which finds whether a given integer number is prime or not. Make use of a function subprogram.
(6+9)