Determinant calculation error - c++

#include<iostream.h>
#include<conio.h>
#include<math.h>
double Det (double a[2][2], int size);
void form(double a[2][2], int c, int size);
double b[2][2];
double Det(double a[2][2], int size) {
if (size==1) {
return a[0][0];
}
else {
double ans = 0.0;
int c;
for (c=0;c<size;c++) {
form(a,c,size);
ans += pow(-1.0,(double)c)*Det(b,size-1);
}
return ans;
}
}
void form(double a[2][2], int c, int size) {
int i,j=0,k,l=0;
for (i=0;i<size;i++) {
for (j=0;j<size;j++) {
if ((i!=0)&&(j!=c)) {
if (l==size-1) {
k++;
l=0;
}
b[k][l]=a[i][j];
l++;
}
}
}
}
int main() {
double mat[2][2] = {{1.0,2.0},{3.0,7.0}};
cout << Det(mat,2);
getch();
return 0 ;
}
I am writing a program in C++ to calculate the determinant of a matrix using recursion. I know that there are many better algorithms to do the same. However, I have been asked to implement this one. As a test case, I have used the matrix specified in the code. When I this run this code, I get an answer of -4 instead of 1. I do not understand what is wrong with this code. Could someone please help? Thanks in advance.

you made several mistakes in your code, use the one below
#include<iostream>
#include<math.h>
using namespace std;
double Det (double a[2][2], int size);
void form(double a[2][2], int c, int size);
double b[2][2];
double Det(double a[2][2], int size)
{
if(size==1)
return a[0][0];
else
{
double ans=0.0;
int c;
for(c=0;c<size;c++)
{
form(a,c,size);
ans+=pow(-1.0,(double)c) * a[0][c] * Det(b,size-1);
}
return ans;
}
}
void form(double a[2][2], int c, int size)
{
int i,j=0,k = 0,l=0;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if((i!=0)&&(j!=c))
{
if(l==size-1)
{
k++;
l=0;
}
b[k][l]=a[i][j];
l++;
}
}
}
}
int main()
{
double mat[2][2]={{1.0,2.0},{3.0,7.0}};
cout<<Det(mat,2);
return 0 ;
}
This gives you 1. The mistakes, you forget to initialize k to 0, and you forget to multiply a[0][c].

Related

adding 2 large integer with array

ive been trying to add 2 large integer on c++ using array and the output is always 0000 and i cant identified the problem
what im trying to achieve is like 30000000000 + 50000000 = 30050000000.
and this is the code that i wrote
, can anyone help me trying to figure it out why the output is 0000?
#include <math.h>
using namespace std;
int array1[100];
int array2[100];
int hasil[100];
int func1(int n)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array1[b]=n%10;
n=n/10;
b--;
}
int func2(int p)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array2[b]=p%10;
p=p/10;
b--;
}
int func3(int i)
{ int satuan,puluhan,bil;
for(i=sizeof(array2[100]);i>0;i--)
{
int bil = array1[100]+array2[100];
satuan=bil%10;
hasil[i]=satuan+puluhan;
puluhan=bil/10;
}
}
int main ()
{ int n,p,i;
int func1(n);
int func2(p);
int func3(i);
int satuan=0;
int puluhan=0;
int x,y;
cout<<"masukan bilangan pertama = ";
cin>>n; cout<<endl;
cout<<"masukan bilangan ke dua = ";
cin>>p; cout<<endl;
for(x=sizeof(hasil[100]);x>0;x--)
{
y=0;
cout<<hasil[y];
y++;
}
} ```

Tried to make a simple adder in c++.Compiled successfully but the output is wrong

created NOT,AND,OR and XOR functions(using NAND) then created the 'adder_prim' and 'adder_carry' functions for the primary output and the carry. Used the standard full adder circuit and put it in a loop(10 cycles so should be able to add til 2031).
entered the input in the code itself(X is 139,Y is 74) just to see if it's working properly or not.
instead of 216(correct ans) it's coming 196 and i have no clue why.
int NAND(int i,int j)
{
int A;
A=((i==1)&&(j==1))?0:1;
return A;
}
int NOT(int i)
{
int A=NAND(i,i);
return A;
}
int AND(int i,int j)
{
int A=NOT(NAND(i,j));
return A;
}
int OR(int i,int j)
{
int A=NAND((NAND(i,i)),NAND(j,j));
return A;
}
int XOR(int i,int j)
{
int A=OR(AND(i,NOT(j)),AND(NOT(i),j));
return A;
}
int adder_prim(int,int,int);
int adder_carry(int,int,int);
int _tmain(int argc, _TCHAR* argv[])
{
int Z[10];
int C=0;
int X[]={0,0,1,0,0,0,1,0,1,1};
int Y[]={0,0,0,1,0,0,1,0,1,0};
for(int i=0;i<10;i++)
{
Z[i]=adder_prim(X[i],Y[i],C);
C=adder_carry(X[i],Y[i],C);
}
for(int j=0;j<10;j++)
{
cout <<Z[j];
}
getch();
return 0;
}
int adder_prim(int a,int b,int c)
{
int O=XOR(XOR(a,b),c);
return O;
}
int adder_carry(int a,int b,int c)
{
int C=OR(AND(XOR(a,b),c),AND(a,b));
return C;
}
You need to add the least significant bit first, not the most significant bit:
for(int i=9;i>-1;i--)
{
Z[i]=adder_prim(X[i],Y[i],C);
C=adder_carry(X[i],Y[i],C);
}

Segmentation Fault [2D array]

#include <iostream>
using namespace std;
void input_Array(char (&A)[10][10], int x, int y);
int calc_Collision(char (&A)[10][10]);
void display(char (&A)[10][10]);
void init_Array(char (&A)[10][10]);
int main()
{
int m,n,test;
char A[10][10];
init_Array(A);
cin>>test;
while (test>0)
{
cin>>m>>n;
input_Array(A,m,n);
display(A);
cout<<"FLAG";
cout<<calc_Collision(A);
test--;
}
}
//Calculates no. of ways to select two 1's in each column
int calc_Collision(char (&A)[10][10])
{
int count=0;
int sum=0;
int select(int x, int y);
for (int j = 0; j<10; j++)
{
count=0;
for(int i = 0; i<10; i++)
{
if (A[i][j]=='1')
{
count++;
}
}
sum=sum + select(count,2);
}
return sum;
}
//Returns no. of ways to select y items from x items
int select(int x, int y)
{
int fact(int a);
return (fact(x)/(fact(y)*fact(x-y)));
}
//Returns a!
int fact(int a)
{
if (a==0)
{
return 1;
}
else
{
return (a*fact(a-1));
}
}
void display(char (&A)[10][10])
{
for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
cout<<A[i][j]<<"\t";
}
cout<<"\n";
}
}
Input Format:
no. of trials
no. of rows (whitespace) no. of columns
row1 (No space, 1's or 0's only)
row2...
Output:
The 2D array
Total no. of ways to select two one's in each column
Problem:
The program displays the array fine enough.
But upon coming across calc_Collision(A), the code outputs:
Segmentation fault (core dumped)
"FLAG" is NOT displayed.
Still a beginner here, so ANY help would be appreciated.
int select(int x, int y){
int fact(int a);
return (fact(x)/(fact(y)*fact(x-y)));
}
int fact(int a){
if (a==0) {
return 1;
}
else {
return (a*fact(a-1));
}
}
Notice that if x in select is less than 2, then fact(x-y) will call itself indefinitely. This is because the variable a in fact will be negative. This occurs when the size of the input array has less than 10 columns, resulting in the last column becoming empty. This causes the iteration of count to become 0 in calc_Collision. The segmentation fault does not occur if the input has 10 columns.
Since you are only computing nC2 (N choose 2), the select function can be rewritten as:
int select(int x){
return (x*(x-1))/2;
}

Merging two arrays in ascending order

I know the logic how to merge two arrays but the problem is how to code.
This was my code n it is giving correct ans but my sir told me that do it again,please tell me what I have to add in this code,
#include<iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
mergeArrays(arrayA,size1,arrayB,size);
}
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i];
cout<<" "<<array3[k];
}
int j=0;
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++)
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j];
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++];
}
}
I had searched this in many places but could not corrected my code
I had written this code but it is not giving correct ans.
#include<iostream>
using namespace std;
int merge(int *a,int *b,int aSize,int bSize);
int main()
{
const int aSize={8};
const int bSize={12};
int arrayA[aSize]={10,25,37,49,50,51,55,60};
int arrayB[bSize]={2,5,26,27,29,32,40,45,70,80,90,95};
merge(arrayA,arrayB,aSize,bSize);
return 0;
}
int merge(int *a,int *b,int aSize ,int bSize)
{
int cSize=aSize+bSize;
int *c=new int[cSize];
int j=0,k=0;
int i=0;
while(i<=aSize&&j<=bSize )
{
if(a[aSize ]<=b[bSize])
{
c[k]=a[aSize];
k++;
i++;
}
else
{
c[k]=b[bSize];
k++;
j++;
}
}
for(int i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
delete[]c;
return c[k++];
}
your sir request you do Merging two arrays in ascending order. so i think you should return a new array, fill with array1 and array2's element, and the elements should be ascending order. here is a implement.(suppose your input arraies is already in ascending order.)
#include <iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2, int outArray[]);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
int outArray[size1+size];
int len = mergeArrays(arrayA,size1,arrayB,size, outArray);
cout <<" "<< len;
for (int i = 0; i< size1+size; ++i){
cout <<" " << outArray[i];
}
}
int mergeArrays(int array1[], int size1, int array2[], int size2, int outArray[])
{
int i=0, j=0, k=0;
int retSize = size1+size2;
while (k<retSize){
if (i==size1){// only left array2, copy it
for (; j<size2; ++j){
outArray[k++] = array2[j];
}
}else if (j == size2) { // only left array1, copy it
for (; i<size1; ++i){
outArray[k++] = array1[i];
}
}
else if (array1[i] > array2[j]){ // copy the min value to outArray
outArray[k++] = array2[j++];
}else{
outArray[k++] = array1[i++];
}
}
return k;
}
now, let's look at your first code:
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i]; // k is not changed, so you just assign array1's each value to array3[0]
cout<<" "<<array3[k];
}
int j=0;
// what's the purpose of this loop?
// and in loop, you don't use i, you just repeat set array3[0] = array2[0]!!
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++) // if array2's length bigger than array1's, will enter this loop.
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j]; // this just repeat assign array2's each value to array3[i]!!
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++]; // you delete array3, but at here you access it!! this will crash!
// also, in this for i loop, you have return, so it will only execute once.
}
// reach function end and no return if not enter for loop.
}
I haven't looked at your second code. I think you still need to do more study.

order statistic implmentation

hey everybody i am trying to implement code for finding order statistic but i am getting a error ..in algorithm two variables are passed in random function is there any way to pass two variables in it if yes than how?? and if no then what can be its alternate ... your help will be appreciated
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int RandomizedSelect(int*,int,int,int);
int RandomizedPartition(int*, int, int);
int partion(int*,int,int);
void main()
{
int n,x,i;
cout<<"Enter length of array";
cin>>n;
int *a=new int[n];
cout<<"Enter elements of array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter element less than size of array";
cin>>x;
int z=RandomizedSelect(a,0, n, x);
cout<<z;
getch();
}
int RandomizedSelect(int A[],int p, int r,int i)
{
if (p == r)
{
return A[p];
}
int q = RandomizedPartition(A, p, r);
int k = q - p + 1;
if (i == k)
{
return A[q];
}
else if (i < k)
{
return RandomizedSelect(A, p, q-1, i) ;
}
else return RandomizedSelect(A, q+1, r, i - k);
}
int Partition(int A[],int p,int r)
{
int x=A[r],temp;
int i=p-1,j;
for(j=p;j<r-1;j++)
{
if(A[j]<=x)
{
i++;
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
temp=A[i+1];
A[i+1]=A[r];
A[r]=temp;
return i+1;
}
int RandomizedPartition(int A[],int p,int r)
{
int temp;
int j = random(p,r);
temp=A[r];
A[r]=A[j];
A[j]=temp;
return Partition(A, p, r);
}
Above code is not working in some case.Here is the corrected code. Below code is working properly I have checked for all cases.
#include<iostream>
#include<cstdlib>
using namespace std;
int RandomizedSelect(int*,int,int,int);
int RandomizedPartition(int*, int, int);
int partion(int*,int,int);
int main()
{
int n,x,i;
cout<<"Enter length of array: ";
cin>>n;
int *a=new int[n];
cout<<"Enter elements of array: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter element less than size of array: ";
cin>>x;
int z=RandomizedSelect(a,0, n-1, x);
cout<<z;
}
int RandomizedSelect(int A[],int p, int r,int i)
{
if (p == r)
{
return A[p];
}
int q = RandomizedPartition(A, p, r);
int k = q - p + 1;
if (i == k)
{
return A[q];
}
else if (i < k)
{
return RandomizedSelect(A, p, q-1, i) ;
}
else return RandomizedSelect(A, q+1, r, i - k);
}
int Partition(int A[],int p,int r)
{
int x=A[r],temp;
int i=p-1,j;
for(j=p;j<r;j++)
{
if(A[j]<=x)
{
i++;
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
temp=A[i+1];
A[i+1]=A[r];
A[r]=temp;
return i+1;
}
int RandomizedPartition(int A[],int p,int r)
{
int temp;
int j = p + rand()%(r-p+1);
temp=A[r];
A[r]=A[j];
A[j]=temp;
return Partition(A, p, r);
}
To select a random value between p and r, you may use this formula:
p + rand()%(r-p+1);
rand() gives you a pseudo-random integer value in [0, RAND_MAX]. Then you take the remainder (%), thus converting to a value in [0, r-p] (as long as r-p+1 is less than RAND_MAX). Adding it to p, you get a value in [p, r].