I am trying to implement the Gauss seidel Iterative method in C++. i have a very messy code because i am still learning. for some reason my while loop seems to run without implementing the loop at all. I cannot get do while to work either.
The test matrix im using is
number of equations=2
x[0][0]=4
x[0][1]=2
x[1][0]=1
x[1][1]=3
b[0][0]=1
b[1][0]=-1
accuracy=0.2
the loop should continue until K has become less than accuracy.
The results should be;
x1=0.4583
x2= 0.4681
(i printed the F matrix just to make sure that part was working correct)
k=0.136
iterations performed=2
Again sorry for the messy code, like i said i am still learning.
Also i've tested the maths on separate codes and it works perfectly.
int main()
{
int n,i,j,p=0,l=0;
cout<<"Enter number of Equations = ";
cin>>n;
double a[n][n],b[n-1][1],F[n-1][1],x[n-1][1],T[n-1][1],e,k,B,C;
cout<<"[a].[x]=[b]"<<endl;
cout<<"Enter Matrix a:"<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cout<<"a["<<i<<","<<j<<"] = ";
cin>>a[i][j];
}
cout<<"Enter Matrix b:"<<endl;
for(j=0;j<n;j++)
{
cout<<"b[0,"<<j<<"] = ";
cin>>b[0][j];
}
cout<<"Enter the Accuracy = ";
cin>>e;
for (i=0;i<n;i++){
T[i][0]=0;
}
for(i=0;i<n;i++){
x[i][0]=0;
}
for(i=0;i<n;i++){
F[i][0]=0;
}
while (k>=e){
C=0;
p=p+1;
k=0;
for(i=0;i<n;i++){
B=0;
T[i][0]=(b[i][0]/a[i][i]);
C=a[i][i];
for (j = 0; j < n; j++) {
if (j!=i)
B=B+(a[i][j])*(x[j][0]);
}
x[i][0]=T[i][0]-(B/C);
}
cout<<x[0][0]<<endl;
cout<<x[1][0]<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++){
F[i][0]=F[i][0]+(a[i][j]*x[j][0]);
}
}
for(i=0;i<n;i++){
F[i][0]=(F[i][0]-b[i][0]);
}
for(i=0;i<n;i++){
k=k+((F[i][0])*F[i][0]);
}
k=sqrt(k);
}
for (i=0;i<n;i++){
cout<<"x"<<i+1<<"="<<x[i][0]<<endl;}
cout<<"number of iterations used: "<<p<<endl;
for (i=0;i<n;i++){
cout<<"F"<<i+1<<"="<<F[i][0]<<endl;}
cout<<"k="<<k<<endl;
return 0;
}
Edit: i tried giving k an initial value and that goes infinitely and the pro gram crashes.
Heres are a run down.
The 3 important Matrixes are a,x and b.
ax=b
each row is changed into an equation and solved for x(i) with initial value for all the position in matrix x to be 0.
after completing this for all values in matrix x the accuracy "k" is checked by function: ax-b=matrix F, All values of F are then squared, added and under root to get k which is checked against e.
the results are:
x1=0
x2=0
number of iterations used: 0
F1=0
F2=0
k=2.96439e-323
First please note that the method you implement is the Jacobi iterative method, not Gauss-Seidel method.
There were several issues in your code:
k was not initialized: it must me set to a large value before the while loop, and set to 0 prior the calculation inside this loop
Some vectors were not initialized at the good size: n-1 instead of n
The F[.] vector should be reset to 0 at each iteration, not only at the start of the program
Moreover, I made some other modifications
I replaced variable length arrays (not C++) by std::vector
I replace [n][1] arrays by [n] vectors
I tried to avoid some i, j global variables
Here is a working code:
#include <iostream>
#include <cmath>
#include <vector>
using std::cin, std::cout;
int main() {
int n, p = 0;
cout << "Enter number of equations = ";
cin >> n;
double e , k, C;
std::vector<double> b(n), F(n, 0), x(n, 0), T(n, 0);
std::vector<std::vector<double>> a(n, std::vector<double> (n));
cout << "[a].[x]=[b]" << "\n";
cout << "Enter Matrix a:" << "\n";
for (int i = 0;i < n; i++) {
for(int j = 0; j < n; j++) {
cout << "a[" << i << ","<< j << "] = ";
cin >> a[i][j];
}
}
cout << "Enter Vector b:" << "\n";
for(int j = 0; j < n; j++)
{
cout << "b[" << j << "] = ";
cin >> b[j];
}
cout << "Enter the Accuracy = ";
cin >> e;
k = 1e10;
while (k >= e){
p = p + 1;
for (int i = 0; i < n; i++) {
double B = 0;
T[i] = b[i]/a[i][i];
C = a[i][i];
for (int j = 0; j < n; j++) {
if (j!=i)
B += a[i][j] * x[j];
}
x[i] = T[i]-B/C;
}
cout << x[0] << "\n";
cout << x[1] << "\n";
for (int i = 0; i < n; i++)
{
F[i] = 0.0;
for (int j = 0; j < n; j++){
F[i] = F[i] + a[i][j]*x[j];
}
}
for (int i = 0; i < n; i++){
F[i] = F[i]-b[i];
}
k = 0.0;
for (int i = 0;i < n;i++) {
k += F[i] * F[i];
}
k = std::sqrt(k);
}
cout << "Solution :\n";
for (int i = 0; i < n; i++){
cout << "x" << i << "=" << x[i] << "\n";
}
cout << "number of iterations used: " << p <<"\n";
for (int i = 0; i< n; i++){
cout <<"F" << i << "=" << F[i] << "\n";
}
cout << "Final error = " << k << "\n";
return 0;
}
Related
Im trying to create a program that creates two 2d dynamic arrays and multiply them and give an output and calculate the time taken for the multiplication process based on the input size n. This code works when it comes to outputs lesser than 7 rows and 7 cols but gives an error when the number goes above 8.
using namespace std;
int m1c , m1r , m2c , m2r , i , j , k , l;
int** arr1 = new int*[m1c];
int** arr2 = new int*[m2c];
int** multArr = new int*[m1c];
int main(){
cout << "Enter the Number of rows for matrix 1 :";
cin >> m1c;
cout << "Enter the Number of columns for matrix 1 :";
cin >> m1r;
cout << "Enter the Number of rows for matrix 2 :";
cin >> m2c;
cout << "Enter the Number of columns for matrix 2 :";
cin >> m2r;
for (i = 0; i < m1r; i++) {
arr1[i] = new int[m1c];
multArr[i] = new int[m1c];
}
for (i = 0; i < m2r; i++) {
arr2[i] = new int[m2c];
}
if (m1r != m2c) {
cout << "Number of rows in the first matrix must be equal to the numbr of columns in the second matrix ";
return -1;
}
for (i = 0; i < m1r; i++) {
for (j = 0; j < m2c; j++) {
arr1[i][j] = rand() % 100;
}
}
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
arr2[i][j] = rand() % 100;
}
}
//Displaying the two arrays
for (i = 0; i < m1r; i++) {
for (j = 0; j < m1c; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
delete[] arr1;
delete[] arr2;
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Looks like an error initializing arr1. Your using m2c as the column count. You probably meant m1c.
Consider two sets retained in two arrays. Find the union, intersection and difference (relative complement) of the two sets.
I managed to solve the union and the intersection, but the difference is giving me a hard time. Any hints? And if possible, keep it as simple as possible, without functions or more complex aspects, because I'm a beginner and I still have a lot to learn.
Thank you in advance!
#include <iostream>
using namespace std;
int main()
{
int v1[100], v2[100], u[200], intersection[100], d[100];
unsigned int v1_length, v2_length, i, j, OK = 0, union_length;
cout << "Enter the number of elements of the first array:" << " ";
cin >> v1_length;
cout << "Enter the elements of the first array:" << '\n';
for (i = 0; i < v1_length; i++)
cin >> v1[i];
cout << "Enter the number of elements of the second array:" << " ";
cin >> v2_length;
cout << "Enter the elements of the second array:" << '\n';
for (i = 0; i < v2_length; i++)
cin >> v2[i];
//Union
union_length = v1_length;
for (i = 0; i < v1_length; i++)
u[i] = v1[i];
for (i = 0; i < v2_length; i++)
{
int ok = 0;
for (j = 0; !ok && j < v1_length; j++)
if (v1[j] == v2[i])
ok = 1;
if (!ok)
{
u[union_length] = v2[i];
union_length++;
}
}
cout << "The union of the two sets contained in the arrays is: ";
for (i = 0; i < union_length; i++)
cout << u[i] << " ";
cout << '\n';
//Intersection
unsigned int k = 0;
cout << "The intersection of the two sets contained in the arrays is: ";
for (i = 0; i < v1_length; i++)
for (j = 0; j < v2_length; j++)
if (v1[i] == v2[j])
{
intersection[k] = v1[i];
k++;
}
for (i = 0; i < k; i++)
cout << intersection[i] << " ";
cout << '\n';
//Difference
unsigned int l = 0, OK2 = 0;
cout << "The difference of the two sets contained in the arrays is: ";
for (i = 0; i < v1_length; i++)
{
for (j = 0; j < v2_length; j++)
{
if (v1[i] == v2[j])
OK2 = 1;
if (!OK2)
{
d[l] = v1[i];
l++;
}
}
}
for (i = 0; i < l; i++)
cout << d[i] << " ";
cout << '\n';
return 0;
}
It seems that the intersection is the best place to start. You want the items that only in appear in one of the two arrays, right?
So, for the inner loop, you need to compare all the elements. Then, if no match was found, you have the a unique element.
You need to add the curly braces {} to the for loop. I know that curly braces are distracting at times, but over time, you will probably find it safer to almost always include them to avoid confusion.
for (i = 0; i < v1_length; i++)
for (j = 0; j < v2_length; j++) {
if (v1[i] == v2[j]){
break; // this item is not unique
} else if(j == v2_length - 1){
d[l] = v1[i]; // This is the unique one, add it to the answer array
l++;
}
}
for (i = 0; i < l; i++)
cout << intersection[l] << " ";
cout << '\n';
You're on the right track!
You're doing a few things wrong. Here are some fixes you can try:
Only set OK2 to 0 once per inner-loop
Reset OK2 to 0 at the end of the inner-loop
Only do the insertion into d after the inner-loop has completed
As an optimization, consider breaking after you set OK2 to 1, as you know at that point it can never be set to 0 for the current value pointed to by the outer-loop.
After running this code , it will generate a random array of desired rows and columns . Then a loop will divide the array by its diagonal into an upper side and lower side.In the upper side the loop will look for a max number and in the lower side the loop will look for a min number . Then in the final stage I need to change the positions of min and max . Max in place of min and vice versa.The code runs and finds the min and max.Don't know how to change their places.
Code
#include <iostream>
#include <time.h>
#include <limits.h>
using namespace std;
int main(){
int rows, columns;
int max = INT_MAX;
int min = INT_MIN;
int XindexOfMax, YindexOfMax;
int XindexOfMin, YindexOfMin;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter columns: ";
cin >> columns;
int **array = new int *[rows]; //generating random array
for(int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int)time(NULL)); //generating randoms
for(int i = 0; i < rows; i++){ //loop for the main array
for(int j = 0; j < columns; j++){
array[i][j] = rand() % 10;
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Max: " << endl;
for(int i = 0; i < rows; i++){ //upper half of the diagonal
for(int j = 0; j < columns - i; j++){
cout << array[i][j] << " ";
if(array[i][j] > max){
max = array[i][j];
XindexOfMax = i; //find x and y coordinates if max
YindexOfMax = j;
}
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for (int i = 0; i < rows; i++){ // lower half of the diagonal
for (int j = 0; j < columns; j++){
if (j < columns - i - 1){
cout << " ";
}
else{
cout << array[i][j] << " ";
if(array[i][j] < min){
min = array[i][j];
XindexOfMin = i; //find x and y coordinates if min
YindexOfMin = j;
}
}
}
cout << "\n";
}
cout << "Result" << endl;
//swapping positions of min and max
std::swap(array[XindexOfMax][YindexOfMax], array[XindexOfMin][YindexOfMin]);
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
cout << array[i][j] << " "; //Printing the final array
}
cout << "\n";
}
return 0;
}
In addition to the min and max values, you need to remember the indizes where you found min and max, respectively. Then you can exchange the values (either manually or by using std::swap).
BTW: you need to initialize max and min with INT_MIN and INT_MAX, respectively, and not the other way around.
So it needs to be
int max = INT_MIN;
int min = INT_MAX;
Otherwise, if you write int max = INT_MAX, then no comparison like if(array[i][j] > max) will ever evaluate to true, since there is no integral value greater than INT_MAX.
The swapping is done at the end of main() using std::swap().
I broke a couple of routines into functions. The answer would be improved by breaking out more functions so that each function performs a single task. Still, I added a class, yet tried to stay true to your original design for printing the triangle halves. Hope you can handle some classes at this point.
#include <iostream>
#include <time.h>
#include <limits.h>
#include <cmath>
using namespace std;
class Point {
public:
Point(int x, int y, int value) : x(x), y(y), value(value) {}
int X() { return x; }
int Y() { return y; }
int Value() { return value; }
void SetValue(int valueArg) { value = valueArg; }
void SetPoint(int xArg, int yArg, int valueArg) {
x = xArg;
y = yArg;
value = valueArg;
}
string to_string() {
return std::to_string(value);
}
private:
int x;
int y;
int value;
};
void PrintArray(int **array, int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << array[i][j] << " ";
}
cout << "\n";
}
}
int main() {
int rows, columns;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter columns: ";
cin >> columns;
int **array = new int *[rows]; //generating random array
for (int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int) time(NULL));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = rand() % 10; //generating randoms
}
}
PrintArray(array, rows, columns);
Point maxPoint = Point(0, 0, INT_MIN); // initialize max
Point minPoint = Point(0, 0, INT_MAX);;
cout << "For finding Max: " << endl;
for (int i = 0; i < rows; i++) { //separating the upper half
for (int j = 0; j < columns - i; j++) {
if (j > columns - i) {
cout << array[i][j] << " ";
} else {
cout << array[i][j] << " ";
if (array[i][j] > maxPoint.Value()) {
maxPoint.SetPoint(i, j, array[i][j]);
}
}
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for (int i = 0; i < rows; i++) { //separating the lower half
for (int j = 0; j < columns; j++) {
if (j < columns - i - 1) {
cout << " ";
} else {
cout << array[i][j] << " ";
if (array[i][j] < minPoint.Value()) {
minPoint.SetPoint(i, j, array[i][j]);
}
}
}
cout << "\n";
}
cout << "array before: " << endl;
PrintArray(array, rows, columns);
cout << "Swapping " << "maxPoint(" << maxPoint.X() << ", " <<
maxPoint.Y() << ") with minPoint("
<< minPoint.X() << ", " << minPoint.Y() << ")" << endl;
std::swap(
minPoint.GetCellReference(array),
maxPoint.GetCellReference(array));
PrintArray(array, rows, columns);
return 0;
}
As mentioned in one of the comments, std::swap() is one method of performing a swap. I have modified the example to use std:swap() at the end of main();
So I have 2 arrays. Let's say the first one it's called a and the second one b. The first one uses "i" for it's elements and the second one uses "j".
For example we have a[ 1 2 3 4] and b[3 4 5] it should show c[1 2]. In the array c I want to show the elements that are in a and aren't in b.
This is what I've tried, but without succes:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
int a[50], b[50], c[50], i, j, k, n, m;
cout << "n= "; cin >> n;
//Read arrays
for (i = 0; i < n; i++) {
cout << "a[" << i << "]: "; cin >> a[i];
}
cout << "\nm= "; cin >> m;
for (j = 0; j < m; j++) {
cout << "b[" << j << "]: "; cin >> b[j];
}
//Show the arrays
cout << endl;
cout << "\na[ ";
for (i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << "]";
cout << endl;
cout << "\nb[ ";
for (j = 0; j < m; j++) {
cout << b[j] << " ";
}
cout << "]";
//Calculate the difference
k = 0; i = 0;
for (j = 0; j < m; j++) {
if (a[i] != b[j])
c[k] = a[i];
k++;
while (j == m && i < n)
i++;
}
//Show the difference array
cout << endl;
cout << "\nc[ ";
for (i = 0; i < k; i++) {
cout << c[i] << " ";
}
cout << "]";
return 0;
}
If the items are sorted, use std::set_difference:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
int a[] = { 1, 2, 3, 4 };
int b[] = { 3, 4, 5 };
std::vector<int> cv;
std::set_difference(std::begin(a), std::end(a),
std::begin(b), std::end(b),
std::back_inserter(cv));
for (auto& s : cv)
std::cout << s << "\n";
}
Output:
1
2
The advantage of using the STL algorithms is that the purpose of the code is known immediately just by looking at the name of the function, and that they work every time (if you give them the correct parameters). Note the lack of comments -- any competent C++ programmer understands right away what's being done.
On the other hand, if you didn't mention what your original code was trying to do (including removing the comments), it would take much more effort to figure out what it's supposed to be doing, and as you've seen, it contains bugs.
Your logic is wrong.
Explanation
So the thing that we will do
For each element in a we will have to check if it is there in array b or not.
If we see any element of a[i] in b[1..m] then we can't add it to c.
So in code we just mark it by f=1
When I get out of that second for loop I want to check if that a[i] is eqaul to any of the element in b[1..m] in which case f will be 1. But if it is 0 then add it to array c[].
Correct one
int k=0;
for(int i=0;i<n;i++)
{
int f=0;
for(int j=0;j<m;j++)
if(a[i]==b[j])
f=1;
if(!f)
c[k++]=a[i];
}
Where OP went wrong?
Being not equal to one element of b[] doesn't guarantee that the element is not appearing b[0..m-1] . This is where op went wrong.
In the for loop
for(j=0;j<m;j++) you are checking if particular a[i] is equal to b[j] or not. If that is the case then it is added to c[] . It is wrong. Also i is not incremented in the loop unless j==m and as in the for loop the condition is j<m so i is never incremented. And k is incremented every time so not every element in c is valid they may contain garbage value even after processing.
k = 0; i = 0;
for (j = 0; j < m; j++) {
if (a[i] != b[j]) // this doesn't mean that it is not appearing in `b`
c[k] = a[i];
k++; // k is incremented in every iteration which is wrong. It should be only when we are sure that `a[i]` is not in `b[0..m-1] `
while (j == m && i < n)
i++; // OP is not using it anywhere...this is redundant.
}
what op did?
Compared first element of a[0] with every element of b[0..m-1] and array c[] contains m elements irrespective of what a[] and b[] is, out of which
c[i]={ a[0] if b[j]==a[0]
{ garbage value if b[j] not equal to a[0]
Dry Run of OP's code
k = 0; i = 0;
for (j = 0; j < m; j++) {
if (a[i] != b[j])
c[k] = a[i];
k++;
while (j == m && i < n)
i++;
}
Input
Case: 1 2 3 4 :a[]
2 3 4 1 :b[]
Step 1: i=0 a[0]!=b[0] is true so c[0]=a[0]. the `while loop` not entered.
j++
Step-2: i is still 0. a[0]!=b[1] so it is added c[1]=a[0]. While loop not entered.
j++
Step-3: i is still 0. a[0]!=b[2]. So c[2]=a[0]. While loop skipped.
j++
Step-4: i is still 0. a[0]==b[3] is true so no assignment done. But k is incremented. so c[3]=garbage. j=3 so while loop skipped
j++
Out of for loop.
Output: [here x is garbage value]
a[]: 1 2 3 4
b[]: 2 3 4 1
c[]: 1 2 3 x
Example test case
1 2 3 4 :=a
2 3 4 1 :=b
Corrected Code
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
int a[50], b[50], c[50], i, j, k, n, m;
cout << "n= "; cin >> n;
//Read arrays
for (i = 0; i < n; i++) {
cout << "a[" << i << "]: "; cin >> a[i];
}
cout << "\nm= "; cin >> m;
for (j = 0; j < m; j++) {
cout << "b[" << j << "]: "; cin >> b[j];
}
//Show the arrays
cout << endl;
cout << "\na[ ";
for (i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << "]";
cout << endl;
cout << "\nb[ ";
for (j = 0; j < m; j++) {
cout << b[j] << " ";
}
cout << "]";
//Calculate the difference
k = 0; i = 0;
int k=0;
for(int i=0;i<n;i++)
{
int f=0;
for(int j=0;j<m;j++)
{
if(a[i]==b[j])
f=1;
if(!f)
c[k++]=a[i];
}
}
//Show the difference array
cout << endl;
cout << "\nc[ ";
for (i = 0; i < k; i++) {
cout << c[i] << " ";
}
cout << "]";
return 0;
}
Your code seems to check if the elements in a are equal to all elements of b. If you just want to check the elements in a if they are equal to at least one element of b, you can do
for (int i=0; i<n; i++) {
bool found = false;
for (int j=0; j<m; j++) {
if (a[i] == b[j]) {
found = true;
break;
}
}
if (!found) {
std::cout << "a["<<i<<"] is not in b"<<std::endl;
}
}
Or add the element to c, but I would recommend to use std::vector<int> c for that.
In the array c I want to show the elements that are in a and aren't in b
It seems like you are looking for std::set_difference
int a[4] = {1, 2, 3, 4}, b[3] = {3, 4, 5};
int c[2] = {}; // declare c with enough space to hold all the elements in result
std::set_difference(a, a + 4, b, b + 3, c); // now c contains the element that are in a but not in b
You can do this very easily using 'set'.
#include<iostream>
#include<set>
int main(){
std::set<int> a = {1,2,3,4} , b = {3,4,5};
for(int const inB : b)
a.erase(inB);
for(int const inA : a)
std::cout << inA << " ";
std::cout << std::endl;
return 0;
}
I need to write a program that receives 2 arrays and checks how many times 1 is included in the other...
But I cant find what is wrong with my program! tx!!
#include <iostream>
using namespace std;
int main()
{
int vector1[500];
int vector2[100];
int a = 0, b = 0, count = 0, k = 0;
cout << "enter size of first array:" << endl;
cin >> a;
cout << " enter first array values:" << endl;
for (int i = 0; i < a; i++)
cin >> vector1[i];
cout << "enter size of second array:" << endl;
cin >> b;
cout << "enter secound array values:" << endl;
for (int i = 0; i < b; i++)
cin >> vector2[i];
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i + k] == vector1[j])
{
count++;
k++;
}
else
k = 0;
cout << count << endl;
system("pause");
return 0;
}
Why at all do you need k? The problem is about all inclusions of all elements right? If O(n^2) complexity is fine, then...
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i] == vector1[j])
count++;
One obvious disadvantage of the code above is that you'll get the total sum of all occurences of elements from vector1 in vector2. The key idea remains the same in case you need to know, which elements exactly appeared in another array and how many times, you'll just have to use map or other vector.