In function Determininant i keep getting an error....
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const int maxsize = 10;
ifstream fin;
ofstream fout;
void transpose (double omatrix[][maxsize],double tmatrix [][maxsize], int array_size)
{
for(int i = 0; i < array_size; i++)
{
for(int j = 0; j < array_size; j++)
{
tmatrix[j][i] = omatrix[i][j];
}
}
}
void sub (double omatrix[][maxsize], double smatrix[][maxsize], int array_size, int i, int j)
{
int counter1 = 0, counter2 = 0;
for (int a = 0; a < array_size; a++)
{
if (a != i)
{
for (int b = 0; b < array_size; b++)
{
if (b != j)
{
smatrix[counter1][counter2] = omatrix[a][b];
counter2++;
}
}
counter1++;
}
}
}
double Determininant(double original_matrix[][maxsize], int array_size)
{
if(array_size == 1)
return original_matrix[0][0];
else if(array_size == 2)
return original_matrix[0][0] * original_matrix[1][1] - original_matrix[0][1] * original_matrix[1][0];
double d = 0.0;
double temp[maxsize][maxsize];
for(int i = 0; i < array_size; i++)
{
sub (original_matrix,temp,array_size, 0, i);
d += pow(-1.0,i) * original_matrix[0][i] * d(temp, array_size - 1);
}
return d;
}
void print (const double m[][maxsize], int array_size)
{
for(int i = 0; i < array_size; i++)
{
for(int j = 0; j < array_size; j++)
{
fout << m[i][j] << " ";
}
fout << "\n";
}
fout << "\n";
}
The error is error: 'd' cannot be used as a function.
Any ideas on whats wrong?
Exactly what the error message says: d is a double and you can't call it as a function. Perhaps you meant Determinant(temp, array_size - 1)?
It's the end of this line:
d += pow(-1.0,i) * original_matrix[0][i] * d(temp, array_size - 1);
As casablanca said, do you mean this?:
d += pow(-1.0,i) * original_matrix[0][i] * Determinant(temp, array_size - 1);
d += pow(-1.0,i) * original_matrix[0][i] * d(temp, array_size - 1);
The clause d(temp, array_size - 1) is telling C++ to call function d.
Related
I have following code which is simply tokenizing an array and then deviding on the basis of '<' and '>' characters. It is running fine in VS community 2019 but when I run it in Linux using g++ command it gives
Free(): invalid pointer
and
Aborted (core dumped)
errors. What am I doing wrong.
PS: error seems to be in the instructions where I am deallocating b in rangbrk and langbrk because when I put it in comments it runs fine in Linux too. But it runs fine in VS 2019 anyways.
//#include <sys/types.h>
//#include <sys/wait.h>
//#include <sys/stat.h>
#include <fcntl.h>
//#include <unistd.h>
#include<string.h>
#include<stdio.h>
#include<fstream>
#include<iostream>
#include<stdlib.h>
using namespace std;
//_________________________________________________________
int reads(char str[])
{//a simple function to get input
int s = 1;
char buffer[100];
cout << "enter command" << endl;
cin.getline(buffer, 100);
if (strlen(buffer) == 0)
{
cout << "Couldnt get input\n";
return 0;
}
for (int i = 0; i <= strlen(buffer); i++)
{
str[i] = buffer[i];
if (buffer[i] == 32)
s++;
}
return s;
}
//____________________________________________________________
int tokenizing(char** a, char str[], char buff[50], int x)
{//a function to devide a complete string into smaller parts
//also stores '/bin/command' into buff to pass as first parameter of execvp
int k = 0, m = 0;
bool check = false;
for (int i = 0; i < x; i++)
{
a[i] = new char[50];
for (int j = 0; str[k] != 32; j++)
{
if (str[k] == 0)
{
check = true;
break;
}
a[i][j] = str[k];
if (i == 0)
buff[j + 5] = str[k];
k++;
a[i][j + 1] = 0;
if (i == 0)
buff[j + 6] = 0;
}
m++;
k++;
if (check)
break;
}
return m;
}
//__________________________________________________________
void rangbrk(char**& a, char**& b, int& x, char filename[])
{//saperates the array before and after '>'
for (int i = 0; i < (x - 2); i++)
{
b[i] = new char[50];
for (int j = 0; j < 50; j++)
{
b[i][j] = a[i][j];
}
}
b[x - 2] = NULL;
for (int i = 0; i < 50; i++)
{
filename[i] = a[x - 1][i];
}
char** c;//temprory pointer to swap a and b
c = a;
a = b;
b = c;
x = x - 2;
for(int i=0;i<x-1;i++)
{
delete [] b[i];
}
b=NULL;
}
//__________________________________________________________
void langbrk(char** &a, char** &b, int& x, int index, char filename[])
{//saperates the array before and after '<'
int k = 0;
for (int i = 0; i < x; i++)
{
if ((i != index) && (i != index - 1))
{
b[k] = new char[50];
for (int j = 0; j < 50; j++)
{
b[k][j] = a[i][j];
}
k++;
}
}
b[x - 2] = NULL;
for (int i = 0; i < 50; i++)
{
filename[i] = a[index - 1][i];
}
char** c;//temprory pointer to swap a and b
c = a;
a = b;
b = c;
x = x - 2;
for(int i=0;i<x-1;i++)
{
if(b[i]!=NULL)
delete [] b[i];
}
b=NULL;
}
//__________________________________________________________
int main()
{
char buff[50] = { "/bin/" };
char str[100];
char exit[5] = { 'e','x','i','t',0 };
char outfile[50];
char infile[50];
int x = reads(str);
char** a;
a = new char* [x];
int s = tokenizing(a, str, buff, x);
a[s] = NULL;
bool flag = false;
for (int i = 0; a[i] != NULL; i++)
cout << a[i] << endl;
cout << s << endl;;
for (int i = 0; i < s; i++)
{
if (a[i][0] == '>')//checks if it need to print in a file
{
cout << i;
char** b;
b = new char* [s - 2];
rangbrk(a, b, s, outfile);
flag = true;
cout << "New" << endl;
for (int j = 0; j < s; j++)
cout << a[j] << endl;
i = 0;
}
else if (a[i][0] == '<')
{
cout << i;
char** c;
c = new char * [s - 2];
langbrk(a, c, s, i, infile);
cout << "New" << endl;
for (int j = 0; j < s; j++)
cout << a[j] << endl;
i = 0;
}
}
}
The reads function gets input, tokenizing tokenized the string in form of words. langbrk copies the string having index 1 less than index of '<' in infile and removes them both from main char** a. rangbrk removes '>' and string succeeding it from a and stores it in outfile
I'm stuck trying to get the value of what A[i][j] is pointing to. double a = A[i][j];. How do I correctly do it? Could someone please explain?
// g++ jacobi.cpp -O0 -o jacobi && ./jacobi
#include <iostream>
#include <iomanip>
#include <vector>
#include <climits>
using namespace std;
void print_matrix(vector<vector<double>>& m) {
for (int i = 0; i < m.size(); i++) {
for (int j = 0; j < m[0].size(); j++) {
cout << setw(5) << fixed << setprecision(2) << m[i][j] << " ";
}
cout << endl;
}
cout << "==================================" << endl;
}
// calculate average temperature based on average of adjacent cells
double avg_temp_at(vector<vector<double>>& matrix, int i, int j) {
return (
matrix[i][j] +
(j-1 >= 0 ? matrix[i][j-1] : 0) +
(i-1 >= 0 ? matrix[i-1][j] : 0) +
(j+1 < matrix[0].size() ? matrix[i][j+1] : 0) +
(i+1 < matrix.size() ? matrix[i+1][j] : 0)
) / 5;
}
// sequential Jacobi algorithm
vector<vector<double>> jacobi_relaxation(vector<vector<double>>& matrix, int& threshold) {
vector<vector<double>> B (matrix.size(), vector<double>(matrix[0].size(), 0));
vector<vector<double>>* A = &matrix;
double max_delta = INT_MAX;
while (max_delta > threshold) {
max_delta = 0;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[0].size(); j++) {
B[i][j] = avg_temp_at(*A, i, j);
double a = A[i][j];
double delta = abs(B[i][j] - a);
max_delta = max(max_delta, delta);
}
}
print_matrix(B);
A = &B;
}
return *A;
}
int main() {
int threshold = 1;
int n = 6;
vector<vector<double>> matrix (n, vector<double>(n, 0));
matrix[1][2] = 100;
matrix[2][2] = 100;
matrix[3][2] = 100;
print_matrix(matrix);
vector<vector<double>> x = jacobi_relaxation(matrix, threshold);
}
I tried your code and it gave me error on this line:
double a = A[i][j];
Change that line into this:
double a = (*A)[i][j];
and it will work.
Explanation:
It's basically the same trick as in line B[i][j] = avg_temp_at(*A, i, j);. A is a pointer, which is pointing to a vector. To accessing to pointers "real data" you must use the *.
Here you can find more info about pointers.
Hope it helps.
#include <iostream>
using namespace std;
Declaring the variables and the arrays
int cost[1000][1000];
int d[1000][1000];
int cutPrice;
bool cut_is_vertical[1000][1000];
int max_index[1000][1000];
int n, m;
This function print is for printing the pieces
void print(int n, int m) {
if (max_index[n][m] == 0) {
cout << n << m <<" ";
return;
}
print(max_index[n][m]);
print(n - max_index[n][m]);
}
this is the main program to cut vertically and horizontally:
int main()
{
cin >> n >> m >> cutPrice;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j){
cin >> cost[i][j];
}
}
for (int i = 1; i <= n; ++i) {
d[i][0] = 0;
}
for (int j = 1; j <= m; ++j) {
d[0][j] = 0;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
// Do not cut.
d[i][j] = cost[i][j];
max_index[i][j] = 0;
This is for cutting horizontally
for (int k = 1; k <= i / 2; ++k) {
if (d[i][j] < d[k][j] + d[i - k][j] - cutPrice) {
max_index[i][j] = k;
cut_is_vertical[i][j] = false;
d[i][j] = d[k][j] + d[i - k][j] - cutPrice;
}
}
This is for cutting vertically.
for (int k = 1; k <= j / 2; ++k) {
if (d[i][j] < d[i][k] + d[i][j - k] - cutPrice) {
max_index[i][j] = k;
cut_is_vertical[i][j] = true;
d[i][j] = d[i][k] + d[i][j - k] - cutPrice;
}
}
}
}
cout << d[n][m] << endl;
print(n, m);
return 0;
}
the errors that gives me is at the print function which are:
error: too few arguments to function 'void print(int, int)'
print(max_index[n][m]);
^
note: declared here
void print(int n, int m) {
^
error: too few arguments to function 'void print(int, int)'
print(n - max_index[n][m]);
^
note: declared here
void print(int n, int m) {
^
Can you please pin point me where is the mistake in my print function thanks
You should call function that way:
print(n, m);
instead of:
print(max_index[i][j]);
Your function takes two arguments as error message is telling you.
I have written code for matrix chain multiplication in dynamic programming in c++.
there is an error in the recursive call for printing the correct parenthesization of the matrices. I am taking input from text file and giving output on a text file. please help..
#include <iostream>
#include <fstream>
#include <limits.h>
using namespace std;
int * MatrixChainOrder(int p[], int n)
{
static int m[100][100];
static int s[100][100];
int j, q;
int min = INT_MAX;
for (int i = 1; i <= n; i++)
m[i][i] = 0;
for (int L = 2; L <= n; L++) {
for (int i = 1; i <= n - L + 1; i++) {
j = i + L - 1;
m[i][j] = min;
for (int k = i; k <= j - 1; k++) {
q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
return (*s);
}
void Print(int *s, int i, int j)
{
ofstream outfile("output.text");
if (i == j)
{
outfile << "a1";
}
else
outfile << "(";
{
Print(*s, i, s[i][j]);
Print(*s, s[i][j] + 1, j);
outfile << ")";
}
outfile.close();
}
int main()
{
int arr[100];
int num, i = 0;
ifstream infile("input.text");
while (infile)
{
infile >> num;
arr[i] = num;
i++;
}
i = i - 1;
infile.close();
Print(MatrixChainOrder(arr, i - 1), 0, i - 1);
return 0;
}
In C++ it is better to use std::vector for arrays. Aside from that, you can't mix pointers and arrays like that because the compiler loses track of array size.
For example this doesn't work:
int x[10][20];
void foo(int *ptr)
{
//the numbers 10 and 20 have not been passed through
}
But you can change it to
int x[10][20];
void foo(int arr[10][20])
{
//the numbers 10 and 20 are available
}
MatrixChainOrder is supposed to return a number, according to this link
int MatrixChainOrder(int s[100][100], int p[], int n)
{
int m[100][100];
for (int i = 0; i < 100; i++) m[i][i] = 0;
for (int i = 0; i < 100; i++) s[i][i] = 0;
int q = 0;
for (int L = 2; L <= n; L++) {
for (int i = 1; i <= n - L + 1; i++) {
int j = i + L - 1;
m[i][j] = INT_MAX;
for (int k = i; k <= j - 1; k++) {
q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
return q;
}
int main()
{
int arr[] = { 40, 20, 30, 10, 30 };
int array_size = sizeof(arr) / sizeof(int);
int n = array_size - 1;
int s[100][100];
int minimum = MatrixChainOrder(s, arr, n);
printf("{ 40, 20, 30, 10, 30 } should result in 26000 : %d\n", minimum);
return 0;
}
Likewise you can change your Print function
void Print(int s[100][100], int i, int j)
{
if (i < 0 || i >= 100 || j < 0 || j >= 100)
{
cout << "array bound error\n";
}
//safely access s[i][j] ...
}
I have a computation with a matrix(88147*2000) and it runs very slow.
So I want to use openMP to speed it up. This is my first time to use
openMP so I just use it in "loop-for".
This is my code:
#include<iostream>
#include<fstream>
#include<math.h>
#include<omp.h>
using namespace std;
#define LONGTH 88147
int label[LONGTH] ;
float data[LONGTH][2000] ;
float w[2000];
float e[2000];
void Input()
{
ifstream fin;
float a;
fin.open("/home/data.train");
if (!fin)
{
cout << "file error";
return;
}
for (int i = 0; i < LONGTH; i++)
{
fin >> a;
label[i] = int(a);
for (int j = 0; j < 2000; j++)
{
fin>>data[i][j];
}
}
fin.close();
cout<<"input over"<<endl;
return;
}
void Initial()
{
for (int i = 0; i < 2000; i++)
{
w[i] = 1;
e[i] = 1;
}
return;
}
bool End()
{
for (int i = 0; i < 2000; i++)
{
if (fabs(e[i])>pow(0.1, 6))
return 0;
}
return 1;
}
float Tkj(int i, int j, int k,float w[2000])
{
return w[i] * data[k][i] - w[j] * data[k][j];
}
float En(int n)//*computation*
{
float result = 0;
#pragma omp parallel for num_threads(64) reduction(+:result)
for (int k = 0; k < LONGTH; k++)
{
int tnum = omp_get_thread_num();
float tmp = 0;
int i = label[k] - 1;
for (int j = 0; j < 2000; j++)
{
if (j != i)
{
float l = 0;
if (n == i)
{
l = data[k][i];
float e = exp(Tkj(i, j, k,w));
tmp = tmp + (-e*l) / pow(1 + e, 2);
}
else if (n == j)
{
l = -data[k][j];
float e = exp(Tkj(i, j, k,w));
tmp = tmp + (-e*l) / pow(1 + e, 2);
}
else
{
continue;
}
}
}
result = result + tmp;
}
return result;
}
float Ex(float w[2000])
{
float result = 0;
#pragma omp parallel for num_threads(64) reduction(+:result)
for (int k = 0; k < LONGTH; k++)
{
int i = label[k] - 1;
float tmp = 0;
int tnum = omp_get_thread_num();
for (int j = 0; j < 2000; j++)
{
if (j != i)
{
tmp = tmp + 1 / (1 + exp(Tkj(i,j,k,w)));
}
}
result = result+tmp;
}
return result;
}
int main()
{
Input();
Initial();
float w2[2000] = { 0 };
float b = pow(0.1,5);
int times = 0;
while (!End()&×<=30000)
{
times++;
cout<<times<<endl;
for (int i = 0; i < 2000; i++)
{
e[i] = En(i);
w2[i] = w[i] - b*e[i];
}
if (Ex(w2)<=Ex(w))//better
{
b = b * 2;
for (int i = 0; i < 2000; i++)
w[i] = w2[i];
}
else//worser
{
b = b / 2;
}
}
ofstream fout("/home/w.txt");
for(int i=0;i<2000;i++)
{
fout<<w[i]<<' ';
}
fout.close();
return 0;
}
The function 'En' costs most of time,so I use "#pragma omp parallel for num_threads(64) reduction(+:result)"to speed it up.
I run it in a CentOS server which has 32 cores and use "-fopenmp" to compile it.But it doesn't be a little quicker.
How can I speed this program up more?