You are on page 1of 11

Linear equation solving by Jacobi method.

#include<iostream>

#include<cmath>

#define error 0.001

using namespace std;

int main(){

cout<<"Enter your array row size :";

int a,b,f=0,c=0,k=1;

cin>>a;

cout<<"Enter your array column size :";

cin>>b;

double array[a][b+1];

cout<<"Input please : \n";

for(int i=0;i<a;i++){

for(int j=0;j<b+1;j++){

cin>>array[i][j];

cout<<"output : \n";

double a0[a]={0},an[a];

while(1){

f=0;

for(int i=0;i<a;i++){

double sum=0.0;

for(int j=0;j<b;j++){

if(j!=i){

sum+=array[i][j]*a0[j];
}

an[i]=(array[i][a]-sum)/array[i][i];

for(int i=0;i<a;i++){

if(fabs(((an[i]-a0[i]))/a0[i])>error){

cout<<k <<" iteration "<<" x1 = "<<a0[0]<<" x2 = "<<a0[1]<<" x3 = "<<a0[2]<<endl;

a0[i]=an[i];

f++;

c++;

k++;

if(c==10)

break;

}
Linear equation solving by Gauss-Seidel method
#include<iostream>

#include<cmath>

#define error 0.001

using namespace std;

int main(){

cout<<"Enter your array row size :";

int a,b,f=0,c=0,k=1;

cin>>a;

cout<<"Enter your array column size :";

cin>>b;

double array[a][b+1];

cout<<"Input please : \n";

for(int i=0;i<a;i++){

for(int j=0;j<b+1;j++){

cin>>array[i][j];

cout<<"output : \n";

double a0[a]={0},store;

while(1){

f=0;

for(int i=0;i<a;i++){

double sum=0.0;
for(int j=0;j<b;j++){

if(j!=i){

sum+=array[i][j]*a0[j];

store=(array[i][a]-sum)/array[i][i];

if(fabs(((store-a0[i]))/a0[i])>error){

cout<<k <<" iteration "<<" x1 = "<<a0[0]<<" x2 = "<<a0[1]<<" x3 =


"<<a0[2]<<endl;

a0[i]=store;

f++;

c++;

k++;

if(c==10)

break;

}}
write a program to implement the following task using linked list
structure
1. create a list in ascending order
2. display the item from the list
3. delete a specific item the list

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void insert(int value) {


Node* newNode = new Node;
newNode->data = value;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
return;
}

if (head->data > value) {


newNode->next = head;
head = newNode;
return;
}

Node* temp = head;

while (temp->next != NULL && temp->next->data < value) {


temp = temp->next;
}

newNode->next = temp->next;
temp->next = newNode;
}

void display() {
Node* temp = head;

while (temp != NULL) {


cout << temp->data << " ";
temp = temp->next;
}

cout << endl;


}

void deleteItem(int value) {


Node* temp = head;

if (head == NULL) {
return;
}

if (head->data == value) {
head = head->next;
delete temp;
return;
}

while (temp->next != NULL && temp->next->data != value) {


temp = temp->next;
}

if (temp->next == NULL) {
return;
}

Node* deleteNode = temp->next;


temp->next = temp->next->next;
delete deleteNode;
}

int main() {

while(1){
cout<<"1. Create List()."<<endl;
cout<<"2. Display List()."<<endl;
cout<<"3. Delete List()."<<endl;
cout<<"4. Exit()"<<endl;
int i;
cout<<"Enter your choice : ";
cin>>i;
if(i==1){
int j;
cout<<"\n Insert item into the linked list : ";
cin>>j;
insert(j);
}
else if(i==2){
int j;
cout<<"\n Display item into the linked list : ";
display();
}
else if(i==3){
int j;
cout<<"\n Delete item position from the linked list : ";
cin>>j;
deleteItem(j);

}
else{
break;
}

}
return 0;
}

Output:
1. Create List().
2. Display List().
3. Delete List().
4. Exit()
Enter your choice : 1

Insert item into the linked list : 5


1. Create List().
2. Display List().
3. Delete List().
4. Exit()
Enter your choice : 2

Display item into the linked list : 5


1. Create List().
2. Display List().
3. Delete List().
4. Exit()
Enter your choice : 1

Insert item into the linked list : 2


1. Create List().
2. Display List().
3. Delete List().
4. Exit()
Enter your choice : 2

Display item into the linked list : 2 5


1. Create List().
2. Display List().
3. Delete List().
4. Exit()
Enter your choice : 1

Insert item into the linked list : 1


1. Create List().
2. Display List().
3. Delete List().
4. Exit()
Enter your choice : 2

Display item into the linked list : 1 2 5


1. Create List().
2. Display List().
3. Delete List().
4. Exit()
Enter your choice : 3

Delete item position from the linked list : 2


1. Create List().
2. Display List().
3. Delete List().
4. Exit()
Enter your choice : 2

Display item into the linked list : 1 5


1. Create List().
2. Display List().
3. Delete List().
4. Exit()
Enter your choice : 4
Process returned 0 (0x0) execution time : 123.589 s
Press any key to continue.

Conclusion:
In conclusion, a linked list is a dynamic data structure that can be used to efficiently store a sequence of
elements. It offers efficient insertion and deletion of elements, but does not provide constant-time access
to individual elements. Despite its limitations, linked lists are widely used in computer science and are an
important concept to understand for anyone interested in programming.

You might also like