5/10/2016

Matrix in C programming

1.A C program to add two matrices.

#include<stdio.h>

#include<conio.h>

int main()

{

  int m, n, c, d, first[10][10], second[10][10], sum[10][10];


  printf("Enter the number of rows and columns of matrix\n");


  scanf("%d%d", &m, &n);

  printf("Enter the elements of first matrix(row wise)\n");


  for( c = 0 ; c < m ; c++ )

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

  scanf("%d", &first[c][d]);


  printf("Enter the elements of second matrix(row wise)\n");


  for( c = 0 ; c < m ; c++ )

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

  scanf("%d", &second[c][d]);


  for ( c = 0 ; c < m ; c++ )

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

  sum[c][d] = first[c][d] + second[c][d];

/* Matrix addition */


  printf("Sum of entered matrices:-\n");


  for( c = 0 ; c < m ; c++ )

  {

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

  printf("%d\t", sum[c][d]);


  printf("\n");

  }


  return 0;

}


Output:
Enter the number of rows and columns of matrix
3 3
Enter the elements of first matrix(row wise)
2 3 4 5 6 7 8 9 1
Enter the elements of second matrix(row wise)
4 5 6 7 8 9 1 2 3
Sum of entered matrices:-
5         8         10
12         14         16
9         11         4



2.A C program to subtract matrices.

#include<stdio.h>
int main()
{
  int m, n, c, d, first[10][10], second[10][10], difference[10][10];

  printf("Enter the number of rows and columns of matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix(row wise)\n");

  for (c = 0; c < m; c++)
  for (d = 0 ; d < n; d++)
  scanf("%d", &first[c][d]);

  printf("Enter the elements of second matrix(row wise)\n");

  for (c = 0; c < m; c++)
  for (d = 0; d < n; d++)
  scanf("%d", &second[c][d]);

  for (c = 0; c < m; c++)
  for (d = 0; d < n; d++)
  difference[c][d] = first[c][d] - second[c][d];
/* Subtract Matrices*/

  printf("Difference of entered matrices:-\n");

  for (c = 0; c < m; c++)
{
  for (d = 0; d < n; d++)
  printf("%d\t",difference[c][d]);

  printf("\n");
}
 return 0;
 }

Output:
Enter the number of rows and columns of matrix
3 3
Enter the elements of first matrix(row wise)
2 3 4 5 6 7 8 9 1
Enter the elements of second matrix(row wise)
4 5 6 7 8 9 1 2 3
Difference of entered matrices:-
-2         -2        -2
-2        -2         -2
7         7         -2


3.A C program which gives the transpose matrix of a entered matrix. 

#include<stdio.h>
int main()
{
  int m, n, c, d, matrix[10][10], transpose[10][10];
  printf("Enter the number of rows and columns of matrix\n");
  scanf("%d%d",&m,&n);   printf("Enter the elements of matrix(row wise)\n");
  for( c = 0 ; c < m ; c++ )
{
  for( d = 0 ; d < n ; d++ )
{
  scanf("%d",&matrix[c][d]);
}
}

  for( c = 0 ; c < m ; c++ )
{
  for( d = 0 ; d < n ; d++ )
{
  transpose[d][c] = matrix[c][d];
  /* transpose by interchanging rows and columns */
}
}

  printf("Transpose of entered matrix :-\n");
  for( c = 0 ; c < n ; c++ )
{
  for( d = 0 ; d < m ; d++ )
{
  printf("%d\t",transpose[c][d]);
  printf("\n");
}
  return 0;
}

Output:
Enter the number of rows and columns of matrix
3 3
Enter the elements of matrix(row wise)
2 3 4 
5 6 7 
8 9 1
Transpose of entered matrix :-
2         5         8
3         6         9
4         7         1




4.A C program which accepts two matrix and multiply them.

#include<stdio.h>

int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix(row wise)\n");

  for ( c = 0 ; c < m ; c++ )
  for ( d = 0 ; d < n ; d++ )
  scanf("%d", &first[c][d]);

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if ( n != p )
  printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
{
  printf("Enter the elements of second matrix(row wise)\n");

  for ( c = 0 ; c < p ; c++ )
  for ( d = 0 ; d < q ; d++ )
  scanf("%d", &second[c][d]);

  for( c = 0 ; c < m ; c++ )
{
  for( d = 0 ; d < q ; d++ )
{
  for( k = 0 ; k < p ; k++ )
{
  sum = sum + first[c][k]*second[k][d];
}

  multiply[c][d] = sum;
  sum = 0;
}
}

  printf("Product of entered matrices:-\n");

  for( c = 0 ; c < m ; c++ )
{
  for( d = 0 ; d < q ; d++ )
  printf("%d\t", multiply[c][d]);

  printf("\n");
}
}

  return 0;
}

Output:
Enter the number of rows and columns of first matrix
3 3
Enter the elements of first matrix(row wise)
2 3 4 5 6 7 8 9 1
Enter the number of rows and columns of second matrix
3 2
Enter the elements of second matrix(row wise)
4 5 6 7 8 9
product of entered matrices:-
58         67
112        130
94         112

Other Output:
Enter the number of rows and columns of first matrix
3 3
Enter the elements of first matrix(row wise)
2 3 4 5 6 7 8 9 1
Enter the number of rows and columns of second matrix
2 3
Matrices with entered orders can't be multiplied with each other.

_________________________




Please post comment.Also you can post programming related problems.




Sorting in C

Bubble sort
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
1.
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;

printf("Enter the value of num \n");
scanf("%d",&num);

printf("Enter the elements one by one \n");
for(i=0;i<num;i++)
{
scanf("%d",&array[i]);
}
/* Bubble sorting begins */
for(i=0;i<num;i++)
{
for(j=0;j<(num-i-1);j++)
{
if(array[j]>array[j+1]
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}

printf("Sorted array is...\n");
for (i=0;i<num;i++)
{
printf("%d\n", array[i]);
}
getch();
}

Output:
Enter the value of num
6
Enter the elements one by one
20
95
32
12
86
60
Sorted array is…
12
20
32
60
86
95


Insertion sort
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
2.
#include<stdio.h>
#include<conio.h>
#define MAX 10
void insertion_sort(int *);
void main()
{
int a[MAX], i;

printf("Enter 10 elements to be sorted:\n");
for (i = 0;i < MAX;i++)
{
  scanf("%d", &a[i]);
}
insertion_sort(a);
printf("Sorted elements:\n");
for (i=0;i<MAX; i++)
{
printf("%d ", a[i]);
}
getch();
}

/* sorts the input */
void insertion_sort(int *x)
{
int temp, i, j;
for(i = 1;i < MAX;i++)
{
temp = x[i];
j = i - 1;
while(temp < x[j] && j >= 0)
{
x[j + 1] = x[j];
j = j - 1;
}
x[j + 1] = temp;
}
}


Output:
Enter 10 elements to be sorted:
2 4 7 8  3 6 12 24 13 1
Sorted elements:
1 2 3 4 6 7 8 12 13 24

Selection sort
Selection sort divides the input list into two parts: the sublist of items already sorted and the sublist of items remaining to be sorted. It proceeds by finding the smallest (or largest) element in the unsorted sublist, exchanging it with the leftmost unsorted element and moving the sublist boundaries one element to the right.
3.
#include<conio.h>
#include<stdio.h>
#define max 50

void selection_sort(int, int[]);
void main()
{
int i, size, data[max];
printf("\nEnter no of elements:");
scanf("%d",&size);
printf("\nEnter elements:\n");
for(i=1;i<=size;i++)
scanf("%d",&data[i]);

selection_sort(size, data);
getch();
}
void selection_sort(int n, int data[])
{
int i, j, min, temp;
printf("Sorted List is:\n");
for(i=1;i<=n-1;i++)
{
min = i;
for(j=i+1;j<=n;j++)
{
if(data[j]<data[min])
min = j;
}
temp=data[i];
data[i]=data[min];
data[min]=temp;
}
for(i=1;i<=n;i++)
printf("%d\n",data[i]);
          }

Output:
Enter no of Elements:5
Enter Elements:
-5
-40
-12
24
10
Sorted list is:
-40
-12
-5
10
24


Alphabetical sort
Alphabetical sort is a program whereby strings of characters are placed in order based on the position of the characters in the conventional ordering of an alphabet.
4.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char name[10][8], tname[10][8], temp[8];
int i, j, n;
printf("Enter the value of n:
\n");
scanf("%d", &n);
printf("Enter %d names:\n", n);
for(i = 0; i < n; i++)
  {
scanf("%s", name[i]);
strcpy(tname[i], name[i]);
}
for(i = 0; i < n - 1 ; i++)
{
for(j = i + 1; j < n; j++)
{
if (strcmp(name[i],name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
    }
}
printf("Sorted names:\n");

for (i = 0; i < n; i++)
{
printf("%s\n",name[i]);
}
getch();
}


Output:
Enter the value of n:
5
Enter 5 names:
int
float
double
char
string
Sorted names:
char
double
float
int
string

Quicksort
Quicksort is a very efficient sorting algorithm.It has two phases:
-the partition phase and
-the sort phase.
Most of the work is done in the partition phase - it works out where to divide the work. The sort phase simply sorts the two smaller problems that are generated in the partition phase.

5.
#include<stdio.h>
#include<conio.h>
void quicksort(int[],int,int);
int main()
{
int list[50];
int size, i;
clrscr();
printf("Enter the number of elements: ");
scanf("%d",&size); 
printf("Enter the elements to be sorted:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);

quicksort(list, 0, size - 1);
printf("Sorted elements:\n");
for (i = 0; i < size; i++)
{
printf("%d\n", list[i]);
}
printf("\n");  
getch();
return 0;
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j) 
{
while (list[i] <= list[pivot] && i <= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low)
{
j--;
}
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
  }

temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
}


Output:
Enter the number of elements: 4
Enter the elements to be sorted:
12 24 36 -12
Sorted elements:
-12
12
24
36

Merge sort
Merge sort is a divide and conquer comparison-based sorting algorithm. It works as follows:-
1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
2. Repeatedly merge sublists to produce new sublists until there is only 1 sublist remaining. This will be the sorted list.

6.
#include <stdio.h>
#include<conio.h>
void mergeSort(int [], int, int, int);
void partition(int [],int, int);
int main()
{
int list[50];
int i, size;
clrscr();

printf("Enter total number of elements:");
scanf("%d", &size);

printf("Enter the elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}

partition(list, 0, size - 1);

printf("Sorted elements:\n");
for(i = 0;i < size; i++)
{
printf("%d\n",list[i]);
}

return 0;

}

void partition(int list[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
partition(list, low, mid);
partition(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}

void mergeSort(int list[],int low,int mid,int high)
{
int i, mi, k, lo, temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
{
if (list[lo] <= list[mi])
{
temp[i] = list[lo];
lo++;
}
else
{
temp[i] = list[mi];
mi++;
}
i++;
}
if (lo > mid)
{
for(k = mi; k <= high; k++)
{
temp[i] = list[k];
i++;
}
}
else
{
for(k = lo; k <= mid; k++)
{
temp[i] = list[k];
i++;
}
}
for(k = low; k <= high; k++)
{
list[k] = temp[k];
}
}


Output:
Enter total number of elements:5
Enter the elements:
5
4
3
2
1
Sorted elements:
1
2
3
4
5

Heapsort
The heapsort algorithm can be divided into two parts.
-Step 1: a heap is built out of the data.
-Step 2: a sorted array is created by repeatedly removing the largest element from the heap, and inserting it into the array.
The heap is reconstructed after each removal. Once all objects have been removed from the heap, we have a sorted array. The direction of the sorted elements can be varied by choosing a min-heap or max-heap in step one.
Complexity:
Best case = Avg case = Worst case = 0(nlogn)

7.
#include<stdio.h>
#include<conio.h>
void main()
{
int heap[10], no, i, j, c, root, temp;
clrscr();
printf("\n Enter no of elements:");
scanf("%d", &no);
printf("\n Enter the elements:\n");
for(i = 0; i < no; i++)
scanf("%d", &heap[i]);
for(i = 1; i < no; i++)
{
c = i;
do
{
     root = (c - 1) / 2;             
     if(heap[root] < heap[c])   /* to create MAX heap array */
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
c = root;
} while(c != 0);
}
printf("Heap array:\n");
for(i = 0; i < no; i++)
printf("%d\t ", heap[i]);
for(j = no - 1; j >= 0; j--)
{
temp = heap[0];
            heap[0] = heap[j]; 
   /* swap max element with rightmost leaf element */
heap[j] = temp;
root = 0;
do 
{
         c = 2 * root + 1;    
/* left node of root element */
if((heap[c] < heap[c + 1]) && c < j-1)
c++;
   if(heap[root]<heap[c] && c<j) 
   /* again rearrange to max heap array */
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
root = c;
} while(c < j);
printf("\nThe sorted elements:\n");
for(i = 0; i < no; i++)
printf("%d\n", heap[i]);
getch();
}


Output:
Enter no of elements :3
Enter the elements : 
8
13
-7
Heap array:
13         8         -7
The sorted elements:
-7
8
13

Please post comment.Also you can post programming related problems.




Arrays in C

Single Dimension Arrays

Syntax :  type  var_name[ size ] ;


For Example :- 

  int array[ 5 ] ;

Here 5 indicates size of the array.Thus total number of elements in the array is 5

which are represented as follows:

array[0]

array[1]

array[2]

array[3]

array[4]

1.Now understand the following program:-

#include<stdio.h>

#include<conio.h>

int main()

{

/*declaration of an array*/

int array[4];

    array[0]=6; /*first element*/

    array[1]=5; /*2nd element*/

    array[2]=8; /*3rd element*/

    array[3]=1; /*4th element*/

    clrscr();

   printf("The 1st element of array = %d\n",array[0]); 

   printf("The 2nd element of array = %d\n",array[1]);

   printf("The 3rd element of array = %d\n",array[2]);

   printf("The 4th element of array = %d",array[3]);

    getch();

}

Output:

The 1st element of array =6

The 2nd element of array =5 

The 3rd element of array =8 

The 4th element of array =1

We can also write this program using for loop.

2.A C program to accept N numbers and print them in an ascending order.

 

 #include<stdio.h>

#include<conio.h>

void main() 

{

  int i, j, a, N, number[30]; 

/*30 is the size of the array*/

 

  printf("Enter the value of N: \n");

  scanf("%d", &N);


  printf("Enter the numbers: \n");

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

{

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

 }

 

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

{

  for (j = i + 1; j < N; ++j)

 {

     if (number[i] > number[j]) 

       {

        a = number[i];

        number[i] = number[j];

        number[j] = a;

        }

 

    }

 

  }

 

  printf("The numbers in ascending order: \n");

 

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

{

     printf("%d\n", number[i]);

 }

 getch();

}

Output:

Enter the value of N:

3

Enter the numbers:

9 6 7

The numbers in ascending order:

6

7

9


3.A C program to reverse  the digits of number using array.

#include<stdio.h> 
#include<conio.h>
void main()
{
int num;
int n,i;
int temp[100];
printf("Enter no. of digits in your number:");
scanf("%d",&n);

printf("Enter number:");
scanf("%d",&num);
for(i=0;i<n;i++)
{
temp[i]=num%10;
num=num/10;
}
/*storing elements in array*/
printf("Required reverse number:");
for(i=0;i<n;i++)
{
printf("%d",temp[i]);
}
/*print the elements of array*/
getch();
}

Output:
Enter no. of digits in your number:4
Enter number:4563
Required reverse number:3654

Multidimensional Arrays

Syntax : type   name [ rows ] [ columns ]  ;

For Example :- 2D array of dimension 2 X 3.

  int d[ 2 ] [ 3 ] ;

3.Now understand the following program:

#include<stdio.h>

#include<conio.h>

int main()

{

    int array[2][4]={{1,3,3,5,},{3,4,6,7}};

    

    clrscr();

   printf("array[0][1] = %d\n",array[0][1]); 

   printf("array [1][1] = %d\n",array[1][1]);

   printf("array[0][0] = %d\n",array[0][0]);

   printf("array[1][0] = %d",array[1][0]);

    getch();

}

Output:

array[0][1] = 3

array[1][1] = 4

array[0][0] = 1

array[1][0] = 3

4.Following is the C program of the summation of two 3×3 matrix.

#include<stdio.h>

#include<conio.h>

int main()

{

    int a[10][10],b[10][10],c[10][10];

    printf("Enter first 3×3 matrix:\n");

    for(int i=1;i<=3;i++)

        {

        for(int j=1;j<=3;j++)

        {

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

            }

    }

    

    

       printf("Enter second 3×3 matrix:\n");

    for(int i=1;i<=3;i++)

        {

        for(int j=1;j<=3;j++)

        {

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

            }

    }

   printf("Required matrix after summation:\n");

    for(int i=1;i<=3;i++)

    {

        for(int j=1;j<=3;j++)

        {

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

   }

        printf("\n");

        }

    getch();

}

Output:
Enter first 3×3 matrix:
1 2 3
4 3 5
3 2 1
Enter second 3×3 matrix:
2 2 2 
3 3 3
4 1 1
Required matrix after summation:
3  4  5
7  6  8
7  3  2


More programs of array is available in for loop.

Please post comment.Also you can post programming related problems.




Question bank of C programming

Question bank(Magadh University)

2015
1.What is the function of 'if-statement'?Explain with example.
2.Write a program in C to determine whether n given number is odd or even and print message.
NUMBER IS EVEN
       OR
NUMBER IS ODD
Without using else option.
3.What are pointers?Why are they required? How do you declare and initialize them? Write a program to read two integers x and y and swap the contents of rhe two variables x and y using pointer.
4.What do you mean by strings?State some library functions for string manipulation.Also create a function C to find the length of the string.
5.Write a program to find the number of vowels from a given text line. 
6.Differeciate between an array and a structure.Explain with an example.
7.Differetiate between:
(a) rational operator and logical operator
(b)++x and x++
(c) logical AND and OR 
(d) >> and << operators
(e) while and do-while loops

Question bank  (AKU)
2014
1.What are the variables and constent in C programming language? What are the ranges of different variables in C programming langauge ?Given one example for each variable type usage with proper jestifiction in favour of your decision for that using that type of variable.
2. Write a C program for currence conversion to INR based on following table :
         Dollar             70 INR
         Pound            100 INR
         AUD                65 INR
         Ruble              20 INR
3.Write a C program to input string "ARAY KUMAR" and print it on the screen.
4.What are different types of loop in C program ? Discuss and compare between each type of loop with proper code smippts.
5.write a C program to print all prime number till 1000.
6.Write a C program to implement a simple calculator program which is capable of doing the basic arithmetic opration over two numbers.
7. What are difference types of decision-making options avalable in C programing langauge? Discuss each type with proper code illustration.
8. Write a C program witch will input a string in lower case and will print the output in upper case .
9.Difine one-  and two-dimentional arrays of C programming language. Write a C program to store the following information in a two-dimensional array:
        1    2    3    4
        2    4    6    8
        3    6    9    12
        4    8    12  16
Please post comment.Also you can post programming related problems.




5/07/2016

Decimal to binary conversion with c++ programming

Decimal to binary conversion

(only for integer number)
-----------------------------------------------

#include<iostream>
using namespace std;
int main()
{
int no,i=1,a,b[1000],cons;
cout<<"Enter an integer number no.:";
cin>>no;
do
{
a=no/2;
b[i]=no%2;
no=a;
i++;
}
while(no>0);
cons=i-1;
cout<<"Equivalent binary no.:\n";
for(i=cons:i>=1;i--)
{
cout<<b[i];
}
return 0;
}


Output:
Enter an integer no.:30
Equivalent binary no.:
11110


--------------------------

SEARCH ALL TOPICS AND PROGRAMMINGS


Please just write a comment.






NMCT question bank

 NMCT question bank

Numerical method and computational technique (AKU UNIVERSITY)

Note:- The answers of various questions are available on their ralated topic link on this website.
---------------------------------------------

      
1.Find the real root of the equation f(X)=X^3-X-1=0 by bisection method upto 5th approximation.
2.Find the real root of the equation 
X^6-X^4-X^3-1 using the method of false position upto four decimal places.
3.Find the real root of the equation logx-cosx-0 by Newton-Raphson method.
4.Write a program in C++/C to convert a decimal number to binary number.
5.Find the inverse of the matrix using Gauss elimination method
       4    1    2
       2    3   -1
       1   -2   2     .
6.Write a algorithm of Jacobi iteration method.
7.Write a program to demonstrate Gauss elimination method for four unknows.
8.Write a program in C++/C to generate a series of Armstrong numbers from 100-500(a number is Armstrong if sum of the cube of the digit is equal to the number).
9.What is flow chart?Discuss this with symbols and give one example.
10.Compute the value of f(x) for x=2.5
from the table
x:       1     2     3     4
f(x):   1     8    27    64
using Lagrange's interpolation method.
11.Find a root of the equation X^2-3×X-5=0 by bisection method.
12.Solve Xlog10X=1.2 by regula falsi method.
13.Solve the following equations by Gauss-Seidel method
20x+y-2z=17
3x+20y-z=-18
2x-3y+20z=25  .
14.Find the root of the equation xtanx=1.28 that lies between 0 and 1, correct to two places of decimals, using bisection method.
Write a computer program using C++ for the above question using bisection method.
15.Solve the equation dy/dx=1/(x+y),y(0)=1 for y(0.1) and y(0.2), using Runge-Kutta method of the fourth order.


5/06/2016

Gauss elimination method,False position method,Newton-Raphson method

TOPICS:-
Gauss Elimination Method
False Position Method
Newton-Raphson Method


A program with C++ for finding the solution of the set of equations of four unknown by "Gauss Elimination Method".

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
float a[100][100];
float x[10];
float cons,sum;
int i,j;
clrscr();
cout<<"Enter augmented 4×5 matrix:\n";
for(i=1;i<=4;i++)
{
for(j=1;j<=5;j++)
{
cin>>a[i][j];
}
}

for(int k=1;k<=3;k++)
{
for(i=k+1;i<=4;i++)
{
cons=a[i][k]/a[k][k];
for(j=1;j<=5;j++)
{
a[i][j]=(a[i][j])-(cons*a[k][i]);
}
}
}

cout<<"Upper traingular matrix\n";
for(i=1;i<=4;i++)
{
for(j=1;j<=5;j++)
{
cout<<a[i][j]<<"     ";
}
cout<<endl;
}

for(i=1;i<=4;i++)
{
x[i]=0;
}

for(i=4;i>=1;i--)
{
sum=0;
for(j=i+1;j<5;j++)
{
sum=sum+a[i][j]*x[j];
}
x[i]=((a[i][5]-sum)/(float)a[i][i]);
}

cout<<"Solution:\n";
for(i=1;i<=4;i++)
{
cout<<x[i]<<endl;
}
return 0;
}


Output:
Enter augmented 4×5 matrix:
1 1 1 4 -6
1 7 1 1 12
1 1 6 1 -5
5 1 1 1 4
Upper traingular matrix
1     1     1     4     -6
0     6     0     -3     18
0     0     5     -3     1
0     0     0     -23.4     46.8
Solution:
1
2
-1
-2


………………………………………
FALSE POSOTION METHOD


#include<iostream>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<iomanip>
#include<stdlib.h>
#define f(x) ((x*x*x*x*x*x)-(x*x*x*x)-(x*x*x)-1)
using namespace std;
int main()
{
   
    int i=1;
float approx,x0,x1,x2;
    float temp;
cout<<"Enter initial approx. value of x0:";
cin>>x0;
cout<<"Enter initial approx. value of x1:";
cin>>x1;

cout<<"Enter prescribed lower bound of slope:";
cin>>approx;
if(f(x1)*f(x0)>0)
{
cout<<"Value of x0 and x1 are not suitable";
exit(1);
}

do
{
if(f(x1)-f(x0)<approx)
{
cout<<"Slope too small";
exit(1);
}

x2=x0-(((x1*f(x0))-(x0*f(x0)))/(f(x1)-f(x0)));


if(f(x0)*f(x2)<0)
{
x1=x2;
}
else
{
x0=x2;
}
cout<<"\nAfter iteration "<<i<<" value of x2:"<<x2;
     
        if(fabs(temp-x2)<0.00001)
        {
  cout<<"\n\nAfter iteration "<<i<<" solution:"<<x2;
            exit(1);
         
    }
        temp=x2;
       
i++;
}
while(i<=50);
   

cout<<"\nApprox. solution after iteration 50 :";
cout<<x2;
return 0;
}

Output:

After iteration 1 value of x2:1.04878
After iteration 2 value of x2:1.0959
After iteration 3 value of x2:1.14055
After iteration 4 value of x2:1.18197
After iteration 5 value of x2:1.21952
After iteration 6 value of x2:1.25278
After iteration 7 value of x2:1.28158
After iteration 8 value of x2:1.30599
After iteration 9 value of x2:1.32629
After iteration 10 value of x2:1.34287
After iteration 11 value of x2:1.35623
After iteration 12 value of x2:1.36686
After iteration 13 value of x2:1.37524
After iteration 14 value of x2:1.38178
After iteration 15 value of x2:1.38687
After iteration 16 value of x2:1.39079
After iteration 17 value of x2:1.39382
After iteration 18 value of x2:1.39614
After iteration 19 value of x2:1.39791
After iteration 20 value of x2:1.39927
After iteration 21 value of x2:1.4003
After iteration 22 value of x2:1.40109
After iteration 23 value of x2:1.40169
After iteration 24 value of x2:1.40215
After iteration 25 value of x2:1.4025
After iteration 26 value of x2:1.40276
After iteration 27 value of x2:1.40296
After iteration 28 value of x2:1.40312
After iteration 29 value of x2:1.40323
After iteration 30 value of x2:1.40332
After iteration 31 value of x2:1.40339
After iteration 32 value of x2:1.40344
After iteration 33 value of x2:1.40348
After iteration 34 value of x2:1.40351
After iteration 35 value of x2:1.40353
After iteration 36 value of x2:1.40355
After iteration 37value of x2:1.40356
After iteration 38 value of x2:1.40357

After iteration 38 solution=1.40357



………………………………………………………

NEWTON-RAPHSON METHOD
………………………………………………………

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define f(x) (log(x)-cos(x)-0)
#define dr(x) ((1/x)+sin(x))
int main()
{
float x0,x1,epsilon;
    float fi,fj;
float i,j;

cout<<"Enter tolerance value:";
cin>>epsilon;
    for(i=1;;i++)
    {
        fi=f(i);
        cout<<"f("<<i<<")="<<fi<<endl;
    if(fi>0)
        {
         
            break;
        }
        }
    for(j=i-1;;j--)
    {
        fj=f(j);
     cout<<"f("<<j<<")="<<fj<<endl;  
    if(fj<0)
            {
       
    break;
}
}
    x0=((i+j)/2);
    cout<<"Initial approx. value of root x0="<<x0<<endl;

for(i=1;i<=10;i++)
{
x1=x0-(f(x0)/dr(x0));
cout<<"\nValue of x1="<<x1<<" after itertion number="<<i;
       
if((fabs(x0/x1-1)<=epsilon) || f(x1)==0)
{
cout<<"\nSolution="<<x1;
exit(1);
}
x0=x1;
}
cout<<"\nSolution doesn't converge after"<<i<<"iteration";
return 0;
}


Output:

Enter tolerance value:0.001
f(1)=-0.540302
f(2)=1.10929
f(1)=-0.540302
Initial approx. value of root x0=0.5

Value of x1=1.29886 after iteration number=1
Value of x1=1.30296 after iteration number=2
Value of x1=1.30296 after iteration number=3
Solution=1.30296


………………………………………………………

NEWTON-RAPHSON METHOD


#include<iostream>
#include<math.h>
using namespace std;
#define f1(x,y) (x*x-y*y-4)
#define f2(x,y) (x*x+y*y-16)
#define df1x(x,y) (2*x)
#define df1y(x,y) (-2*y)
#define df2x(x,y) (2*x)
#define df2y(x,y) (2*y)


int main()
{
   
double x0=2.828,y0=2.828;
    double a,b,c,d,e,f,g,h;
    for(int i=1;i<=5;i++)
    {
    a=f1(x0,y0);
    b=df1x(x0,y0);
    c=df1y(x0,y0);
    d=f2(x0,y0);
    e=df2x(x0,y0);
    f=df2y(x0,y0);
    h=(-d+(e*a/b))/(f-(e*c/b));
    g=(-a-(h*c))/b;
        cout<<"\nAfter iteration "<<i<<endl;
    cout<<g<<"   "<<h<<endl;
   
    x0=x0+g;
        y0=y0+h;
        cout<<"Value of x0="<<x0<<"\nvalue of y0="<<y0<<endl;
        }
cout<<"\nHence final solution after 5th iteration= "<<"\nX="<<x0<<"  Y="<<y0;  
   
return 0;}


Output:

After iteration 1
0.344034  -35318
Value of x0=3.18203
Value of y0=2.47482

After iteration 2
-0.019695  -0.025201
Value of x0=3.16234
Value of y0=2.44962

After iteration 3
-6.13298e-05  -0.00012963
Value of x0=3.16228
Value of y0=2.44949

After iteration 4
-5.94721e-10  -3.43011e-09
Value of x0=3.16228
Value of y0=2.44949

After iteration 5
3.51083e-16  2.71948e-16
Value of x0=3.16228
Value of y0=2.44949

Hence final solution after 5th iteration=
X=3.16228  Y=2.44949



SEARCH ALL PROGRAMMINGS AND TOPICS


------------------------------------------------
Please just write a comment.