I am currently having some probs with my code to decompose an array into an upper(u) and lower(l) array.
I am using the doolittle method
My code:
#include <iostream>
using namespace std;
int main(){
double a[10][10];
double l[10][10];
double u[10][10];
double som=0;
int RANG;
cin >> RANG;
for(int i=0; i<RANG; i++){
for(int j=0; j<RANG;j++){
cin >> a[i][j];
}
}
for(int i=0; i<RANG; i++){
for(int j=0; j<RANG;j++){
u[i][j]=0;
l[i][j]=0;
}
}
for(int i=0;i<RANG;i++) {
l[i][i]=1;
for(int j=i;j<RANG;j++) {
for(int s=0;s<i-1;s++) {
som+= l[i][s]*u[s][j];
}
u[i][j]=a[i][j]-som;
}
for(int k=i+1;k<RANG;k++) {
double som=0;
for(int s=0;s<i-1;s++) {
som+=l[k][s]*u[s][i];
}
l[k][i]=(a[k][i]-som)/u[i][i];
}
}
cout << "l:" << endl;
for(int i=0; i<RANG; i++){
for(int j=0; j<RANG;j++){
cout << l[i][j] << "\t";
}
cout << endl << endl;
}
cout << "u: " << endl;
for(int i=0; i<RANG; i++){
for(int j=0; j<RANG;j++){
cout << u[i][j] << "\t";
}
cout << endl << endl;
}
return 0;
}
plz help if you can...
PS: not sure if it belongs here, might be better on the mathematics site
Have a look at http://download.intel.com/design/PentiumIII/sml/24504601.pdf it got complete solution along with source code!
You might want to check out QuantLib. http://quantlib.org/index.shtml
Here's an example of code that uses the QuantLib library to decompose a 3x3 array. It uses a Cholesky decomposition, but maybe it'll help. http://quantcorner.wordpress.com/2011/02/20/matrix-decomposition-with-quantlib/
Check your 's' iteration it should be
for(int s=0;s
Related
The number of rows and columns of this array are given by the user however the number of rows are not the same (the array is uneven) and also the user will fill the array by entering the elements.
This is the code that I wrote but when I try to take input from user, the code crashes after taking some inputs. Please could you help me out and correct my code and point my flaws. Thank you.
#include <iostream>
//2d array
using namespace std;
int main()
{
int row;
int col_x;
cout << "Enter the row number:" << endl;
cin >> row;
//cout<<"Enter the column number:"<<endl;
//cin>>col;
int **a = new int *[row];
for (int r = 0; r < row; r++)
{
cout << "Enter the column no.of array " << r << endl;
cin >> col_x;
a[r] = new int[col_x];
cout << "Enter the elements in the array:" << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col_x; j++)
{
cin >> a[i][j];
}
}
cout << "The elements in the array:" << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col_x; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
delete[] a;
a = NULL;
return 0;
}
There was an extra for loop. Also, you have to store the size of each row.
And make the proper deallocation of the 2D array.
#include <iostream>
//2d array
using namespace std;
int main()
{
int row;
cout<<"Enter the row number:"<<endl;
cin>>row;
int **a=new int *[row];
int *col_x = new int [row];
for(int r=0;r<row;r++){
cout<<"Enter the column no.of array "<<r<<endl;
cin>>col_x[r];
a[r]=new int[col_x[r]];
cout<<"Enter the elements in the array:"<<endl;
for(int j=0;j<col_x[r];j++){
cin>>a[r][j];
}
}
cout<<"The elements in the array:"<<endl;
for(int i=0;i<row;i++){
for(int j=0;j<col_x[i];j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
for (int i=0; i<row; ++i)
delete[] a[i];
delete []a;
delete []col_x;
return 0;
}
The way you are getting the input from the user is very vague, and is prone to accessing invalid memory. You are getting the same row many times in the inner loop.
Try something like this:
#include <iostream>
//2d array
using namespace std;
int main() {
int row;
int col_x;
cout << "Enter the row number:" << endl;
cin >> row;
//cout<<"Enter the column number:"<<endl;
//cin>>col;
int ** a = new int * [row];
for (int r = 0; r < row; r++) {
cout << "Enter the column no.of array " << r << endl;
cin >> col_x;
a[r] = new int[col_x];
cout << "Enter the elements in the array:" << endl;
for (int j = 0; j < col_x; j++) {
cin >> a[r][j];
}
}
cout << "The elements in the array:" << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col_x; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
delete[] a;
a = NULL;
return 0;
}
Also, note that col_x will hold only the size of the last row. So, it's not working for the printing at the end of the code.
Not sure what you are trying to achieve, but the main issue with above code is, that you are accessing non-existing elements of your array. Maybe your loop over r should end in line 18? Even then, you would have to store the number of columns per row in some external variable. I would suggest to use a std::vector as the container instead of fixed arrays, in your case a std::vector< std::vector<int> >. The vector class has a method size() which stores its actual size.
Since you are using C++, you should take advantage of the containers it provides for you to store your data, in this case a vector of vectors would be appropriate:
Live Sample
#include <iostream>
#include <vector>
using namespace std; //<-- for test, souldn't be used
int main() {
int rows, cols, temp;
vector<vector<int>> matrix;
cout << "Enter the row number:" << endl;
cin >> rows;
for (int i = 0; i < rows; i++){
vector<int> v;
cout << "Enter the column no.of array " << i << endl;
cin >> cols;
cout << "The elements in the array:" << endl;
for (int j = 0; j < cols; j++){
cin >> temp;
v.push_back(temp);
}
matrix.push_back(v);
}
cout << endl;
for( auto i: matrix){ //print results
for(int j: i)
cout << j << " ";
cout << endl;
}
}
#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
}
return 0;
}
You can use std::cout like :
#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
if(i ==0)
std::cout<<"{" <<array[i];
else if(i == n-1)
std::cout<<","<<array[i]<<"}";
else
std::cout<<","<<array[i];
}
return 0;
}
#mystic's answer uses arrays, which works fine. You can also use vector. There are more advanced methods of iterating over a vector, but I have not included that here to keep it simple.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> intVector{};
int n;
int input;
cout << "No. of values : ";
cin >> n;
for (int i = 0; i < n; i++) {
cin >> input;
intVector.push_back(input);
}
// Print out the array
cout << "{";
for(int i = 0; i < intVector.size(); i++) {
cout << intVector[i];
// print out the comma, except for the last number
if(i < intVector.size() - 1) {
cout << ", ";
}
}
cout << "}" << endl;
return 0;
}
If you want to use an iterator for printing the array, you can replace the print loop with this code:
// Print out the array
cout << "{";
for(auto i=intVector.begin(); i!=intVector.end(); ++i) {
if (i != intVector.begin()) {
cout << ", ";
}
cout << *i;
}
cout << "}" << endl;
I'll start by saying that I'm a begginer. I've started learning 2 weeks ago. Today I found some video on YT about this Conway's Game of Life. With things I've learned so far I tought I'll give it a go. So I've made this(code below). It doesn't work like it should be and I'm out of ideas why. Could someone check this code below and tell me what's wrong? I don't want a complete solution, just a tip what I should check.
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main()
{
bool positions[50][50], new_positions[50][50];
int neighbours=0;
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) positions[i][x]=false;
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) new_positions[i][x]=false;
}
//This is some patterns i've put manually into array just for testing,
//they should "be alive" at least for 3-4 cycles
positions[5][5]=true;
positions[5][6]=true;
positions[5][7]=true;
positions[6][5]=true;
positions[5][8]=true;
positions[5][9]=true;
positions[5][10]=true;
positions[5][11]=true;
positions[5][5]=true;
positions[6][5]=true;
positions[7][5]=true;
positions[5][6]=true;
positions[8][5]=true;
positions[5][7]=true;
positions[6][8]=true;
positions[7][9]=true;
positions[8][10]=true;
positions[9][5]=true;
positions[11][11]=true;
positions[11][12]=true;
positions[12][11]=true;
positions[12][12]=true;
while(!(kbhit())) {
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) {
for(int pozX=x-1, pozI=i-1; pozI<=i+1; pozX++) {
//if(pozX!=x && pozI!=i) { //this "if" doesn't work, don't know why
if(pozX>=0 && pozX<50 && pozI>=0 && pozI<50) {
if(positions[pozI][pozX]==true) neighbours++;
}
//}
if(pozX==x+1) {
pozX=x-1;
pozI++;
}
}
if(neighbours==1) neighbours=0;//had to use this instead of previously mentioned if
if(positions[i][x]==true) {
if((neighbours>3) || (neighbours<2)) new_positions[i][x]=false;
}
if(positions[i][x]==false && neighbours==3) new_positions[i][x]=true;
neighbours=0;
}
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) {
if(new_positions[i][x]==true) cout << "X";
else cout << " ";
}
cout << endl;
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) positions[i][x]=false; //clears all cells
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) positions[i][x]=new_positions[i][x]; //sets cell status from this cycle, I know I could do this in one loop, but it is more clear to me this way
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) new_positions[i][x]=false; //clears this cycle "buffor"
}
Sleep(3000);
system("cls");
}
}
How I debugged the code was by using https://www.onlinegdb.com/
Learning to use a debugger is really valuable...
What I have done includes:
I removed the kbhit() test and the Sleep() call and cls because I am not using a windows compiler. You will have to put those back if you use my code.
I changed the bit that displays the board to give it line numbers and to display something in the blank cells and I changed it to avoid calling endl to flush the buffer for every single line.
I changed the way neighbours were calculated. I think you were subtracting 1 to take care of the extra add that was done - I think it is better to just use an if statement to avoid adding it in the first place. Here is my version:
int neighbours=0;
for(int pozI=i-1; pozI<=i+1; pozI++)
for(int pozX=x-1; pozX<=x+1; pozX++)
if(pozX!=x || pozI!=i) // don't check position[i][x]
if(pozX>=0 && pozX<50 && pozI>=0 && pozI<50)
if(positions[pozI][pozX]==true)
neighbours++;
I changed the way the new board was calculated. You weren't ever setting the new position to true if it should stay alive. Here is my version:
// set the new board
new_positions[i][x]=false;
if(positions[i][x]==true)
if(neighbours==2 || neighbours==3)
new_positions[i][x]=true; // staying alive
if(positions[i][x]==false)
if(neighbours==3)
new_positions[i][x]=true; // being born
What I have not done includes:
There are a lot of stylistic issues and whether they are changed is debateable depending on what you consider best practice. Things like the variable names and having everything in main() and having a global using namespace std; and using a C style array instead of a std::array are all things that we could argue about but that belongs on codereview.stackexchange.com so I haven't changed any of that.
Here is the full program:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
bool positions[50][50], new_positions[50][50];
// clear the board
for(int i=0; i<50; i++)
for(int x=0; x<50; x++)
positions[i][x]=false;
//This is some patterns i've put manually into array just for testing,
//they should "be alive" at least for 3-4 cycles
positions[5][5]=true;
positions[5][6]=true;
positions[5][7]=true;
positions[6][5]=true;
positions[5][8]=true;
positions[5][9]=true;
positions[5][10]=true;
positions[5][11]=true;
positions[5][5]=true;
positions[6][5]=true;
positions[7][5]=true;
positions[5][6]=true;
positions[8][5]=true;
positions[5][7]=true;
positions[6][8]=true;
positions[7][9]=true;
positions[8][10]=true;
positions[9][5]=true;
positions[11][11]=true;
positions[11][12]=true;
positions[12][11]=true;
positions[12][12]=true;
// print the initial board
for(int i=0; i<50; i++) {
cout << setw(4) << i << setw(1) << " ";
for(int x=0; x<50; x++)
cout << (positions[i][x]==true?"X":".");
cout << "\n";
}
cout << endl;
for(int steps=0; steps<15; steps++) {
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) {
// count the neighbours
int neighbours=0;
for(int pozI=i-1; pozI<=i+1; pozI++)
for(int pozX=x-1; pozX<=x+1; pozX++)
if(pozX!=x || pozI!=i) // don't check position[i][x]
if(pozX>=0 && pozX<50 && pozI>=0 && pozI<50)
if(positions[pozI][pozX]==true)
neighbours++;
// set the new board
new_positions[i][x]=false;
if(positions[i][x]==true)
if(neighbours==2 || neighbours==3)
new_positions[i][x]=true; // staying alive
if(positions[i][x]==false)
if(neighbours==3)
new_positions[i][x]=true; // being born
}
}
// print the old board and the new one
for(int i=0; i<50; i++) {
cout << setw(4) << i << setw(1) << " ";
for(int x=0; x<50; x++)
cout << (positions[i][x]==true?"X":".");
cout << " ";
for(int x=0; x<50; x++)
cout << (new_positions[i][x]==true?"X":".");
cout << "\n";
}
cout << endl;
// save the generation
for(int i=0; i<50; i++)
for(int x=0; x<50; x++)
positions[i][x]=new_positions[i][x];
}
// print the final board
for(int i=0; i<50; i++) {
cout << setw(4) << i << setw(1) << " ";
for(int x=0; x<50; x++)
cout << (positions[i][x]==true?"X":".");
cout << "\n";
}
cout << endl;
return 0;
}
i wrote a simple sorting code on pass by reference. here i am passing an array to function and then performing the sorting operation. after passing the array i am printing the entire array as entered by user then performing the sorting operation(in descending order) but after sorting when i print the sorted array i get an output that the array index '0' contains the value '41'. if i enter the numbers less than '41' then the sorted array is displayed as '41',and then the other numbers in sorted manner. Please explain why i am getting such an output.
#include<iostream>
using namespace std;
int sort_array(int *p);
int main() {
int arr[10];
for (int i=0; i<10; i++) {
cout << "enter " << (i+1) << " value:";
cin >> arr[i];
cout << "\n";
}
sort_array(arr);
return 0;
}
int sort_array(int *p) {
int c=0;
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << "arr:"<<p[0];
cout<<"\n";
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
if (p[j] < p[j+1]) {
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
cout << "\n";
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << p[0];
}
It appears that you are trying to do a bubble sort on your array in sort_array(), but the logic is wrong. Try using this code instead:
int sort_array(int *p) {
int c=0;
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << "arr:" << p[0];
cout << "\n";
for (int i=0; i < 10; i++) {
for (int j=1; j < (10-i); j++) {
if (p[j-1] > p[j]) {
c = p[j-1];
p[j-1] = p[j];
p[j] = c;
}
}
}
cout << "\n";
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout<<p[0];
}
The problem is in your sorting. j is from 0 to 9, and than you access p[j+1] when j = 9, p[10] is outside your array boundaries.
So fix your following part to proper sorting.
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(p[j]<p[j+1])
{
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
}
Clarification: the code above is the problematic part of the original code posted. This is NOT the fixed sorting. That is the part to be fixed.
for(int i=0;i<10;i++)
{
for(int j=0;j<9;j++)
{
if(p[j]<p[j+1])
{
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
}
the sorting works fine once i changed the limit of inner loop. the problem was accessing the array index [10] but i had it declared till index [9].
I have the following code, I made sure I did everything as explained in Pointer-to-pointer dynamic two-dimensional array
I need my 2d matrix to be dyanmic i.e the user needs to enter the dimensions. But I am getting error in my cin when taking the input form the user. whats wrong with the following code?
int numberOfRows, numberOfColumn;
cout << "Please enter the number of rows: ";
cin >> numberOfRows;
cout << endl << "Please enter the number of columns ";
cin >> numberOfColumn;
int** matrix= new int*[numberOfRows];
for (int i=0; i<numberOfRows; i++)
{
matrix[numberOfRows]= new int[numberOfColumn];
}
for (int i=0; i<numberOfRows; i++)
{
for(int j=0; j<numberOfColumn; j++)
{
cout << "Please enter Row " << (i+1) << " Column "<< (j+1) <<" element: ";
cin>> matrix[i][j];
cout << endl;
}
}
for (int i=0; i<numberOfRows; i++)
{
for(int j=0; j<numberOfColumn; j++)
{
cout << matrix[i][j];
}
}
You have this:
for (int i=0; i<numberOfRows; i++)
{
matrix[numberOfRows]= new int[numberOfColumn];
}
Should be:
for (int i=0; i<numberOfRows; i++)
{
matrix[i]= new int[numberOfColumn];
}
Notice that the matrix[numberOfRows] changed to matrix[i]
While allocating memory in for loop
matrix[i]= new int[numberOfColumn];
instead of matrix[numberOfRows]= new int[numberOfColumn];
Note:you also need to free the allocated memory
corrected code
http://ideone.com/ILw1qa