I want to determine the intersection of 2 bi-dimensional arrays.
Here is my code:
#include < iostream >
using namespace std;
void type(int mat[][10],int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>mat[i][j];
}
void inter(int a[][10], int n,int b[][10],int m)
{
int k1=0,p, x,ok,j, c[50],i;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
p=0; x=0;
while(p<m)
{
ok=0;
while(x<m)
{
if(a[i][j]==b[p][x]) ok=1;
if(ok) c[k1++]=a[i][j];
x++;
}
p++;
}
for(i=0;i<n;i++)
{
cout<<c[i]<<" ";
cout<<endl;}
}
}
}
} // !!! added in edit!!!
int main()
{
int n,m,mat[10][10],c[30],mat2[10][10];
cout<<"n= ";cin>>n;
type(mat,n);
cout<<"m= "; cin>>m;
type(mat2,m);
inter(mat,n,mat2,m);
return 0;
}
It doesn't show me the expected answer. It shoes me numbers like these : 1
4758024
1
4758024
Can somebody help me?
Related
The code given below is a code for n*n matrix however on executing this code gives garbage values as output for n>=3.Kindly suggest me why?Also if possibly can somebody tell me how i can return 2D array from a function in c++.Thanks in advance.
#include<iostream>
using namespace std;
void rowcol(int[][10],int[][10],int,int);
int det(int arr[][10],int n)
{
int cofact[10][10];
if(n==2)
return (arr[0][0]*arr[1][1])-(arr[0][1]*arr[1][0]);
int val=0,flg=1;
for(int i=0;i<n;i++)
{
rowcol(arr,cofact,i,n);
val=val+(flg*det(cofact,n-1));
flg*=-1;
}
return val;
}
void rowcol(int arr[][10] ,int cofact[][10],int l,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if((i!=l)&&(j!=l))
cofact[i][j]=arr[i][j];
}
}
}
int main()
{
int n,val;
int arr[10][10];
cout<<"Enter the size of matrix:";
cin>>n;
cout<<"\nEnter the array elements row by row";
for(int i=0;i<n;i++)
{
cout<<"\nEnter Row "<<i<<":";
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
val=det(arr,n);
cout<<"\nThe determinant of given matrix is:"<<val;
return 0;
}
I wrote this code for printing the productions after eliminating left recursion.And also i stored these output productions in "all" array. when i print this array, only the last values are being printed with blank lines in the initial rows.How to resolve this problem?
#include<iostream>
#include<cstring>
using namespace std;
class production
{
private:
char lhs;
char rhs[10][10],nr[10][10],rs[10][10],all[30][30];
int noa,m;
public:
production()
{
noa=0;m=0;
for(int i=0;i<30;i++)
for(int j=0;j<30;j++)
all[i][j]='\0';
}
void makeprod(char *str)
{
noa=0;
for(int i=0;i<30;i++)
for(int j=0;j<30;j++)
rhs[i][j]='\0';
lhs=str[0];
char r[20];
strcpy(r,str+3);
int j=0;
for(int i=0;r[i]!='\0';i++)
{
if(r[i]!='/')
rhs[noa][j++]=r[i];
else
{
rhs[noa++][j]='\0';
j=0;
}
}
noa++;
}
void checks(string sr)
{
for(int i=0;i<30;i++)
for(int j=0;j<30;j++)
rs[i][j]=nr[i][j]='\0';
int ct=0,k=0,l=0;
for(int i=0;i<noa;i++)
{
if(lhs==rhs[i][0])
{
strcat(rhs[i],sr.c_str());
strcpy(rs[k],rhs[i]+1);
k++;
}
else
{
strcpy(nr[l],rhs[i]);
strcat(nr[l],sr.c_str());
l++;
}
}
if(l==noa)
{
cout<<lhs<<"->";
for(int i=0;i<noa;i++)
cout<<rhs[i]<<" ";
cout<<"\n";
all[m][0]=lhs;
strcat(all[m],"->");
for(int i=0;i<noa;i++)
{
strcat(all[m],rhs[i]);
strcat(all[m],"/");
}
m++;
}
else
{
cout<<lhs<<"->";
for(int i=0;i<l;i++)
cout<<nr[i]<<" ";
p.all[m][0]=lhs;
strcat(all[m],"->");
for(int i=0;i<l;i++)
{
strcat(all[m],nr[i]);
strcat(all[m],"/");
}
m++;
cout<<"\n"<<sr<<"->";
for(int i=0;i<k;i++)
cout<<rs[i]<<" ";
cout<<" e\n";
all[m][0]=lhs;
strcat(all[m],"->");
for(int i=0;i<k;i++)
{
strcat(all[m],rs[i]);
strcat(all[m],"/");
}
m++;
}
cout<<"m= "<<m<<"\n";
for(int i=0;i<m;i++)
{
for(int j=0;all[i][j]!='\0';j++)
cout<<all[i][j];
cout<<"\n";
}
}
void display()
{
cout<<"m= "<<m<<"\n";
for(int i=0;i<m;i++)
{
for(int j=0;all[i][j]!='\0';j++)
cout<<all[i][j];
cout<<"\n";
}
}
};
int main()
{
production p;
char str[20][20];
string op[5]={"X","Y","Z","U","V"};
cout<<"enter no of productions\n";
int n,l=0;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"enter a production\n";
cin>>str[i];
p.makeprod(str[i]);
p.checks(op[l++]);
}
p.display();
return 0;
}
# include <iostream>
using namespace std;
class mm
{
private:
int k[1000];
int n;
int i;
int a;
int b;
int f;
public:
mm ()
{
a=0;
b=1;
f=0;
i=0;
for(int i=0; i<n;i++)
k[i]=0;
};
~mm()
{
}
void fib(int n)
{
for (int i=0;i<n;i++)
{
if (i<=1)
f=i;
else
{
f=a+b;
a=b;
b=f;
}
k[i]=f;
}
for (int j=0;j<n;j++)
cout<<k[j]<<" ";
}
int se (int n, int i)
{
if (n==1)
return 1;
else
return 1/k[i] + se (n-1, i+1);
}
};
int main()
{
int n;
cout<<"Enter n:";
cin>>n;
mm p;
cout<<"fib: "<<endl;
p.fib(n);
cout<<endl;
cout<<"se: ";
cout<<p.se(n,0);
return 0;
}
Recursion function from main is not responding. Maybe the array k[i] is not working, but I cant find the reason. Can anyone help me?
k[0] is set to 0. When you then call se(n,0) in main, it computes 1/k[0] + se(n-1,1) which is a division by zero.
I am using a simple approach to convert an array into heap in bottom up manner. I could not find the mistake. Can some one please have a look and let me know.
Basically I am making out of a random array in bottom up manner.
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
There is some issue with heapify procedure ,which sometimes give correct result and most of times wrong.
`#include<iostream>
#include<conio.h>
using namespace std;
int min(i
nt x,int y,int z)
{
return ((x>y?x:y)>z?(x>y?x:y):z);
}
int left(int i)
{
return (2*i);enter code here
}
int right(int i)
{
return (2*i+1);
}
int parent(int i)
{
return (i/2);
}
int heapify(int i,int n,int arr[])
{
int temp;
int l=left(i);
int r=right(i);
temp=i;
if(l<=n&&arr[l]<arr[i])
temp=l;
else if(r<=n&&arr[temp]>arr[r])
temp=r;
if(temp!=i)
{
int x=arr[temp];
arr[temp]=arr[i];
arr[i]=x;
heapify(temp,n,arr);
}
//else return 0;
}
int deletemin(int n,int arr[])
{
cout<<arr[1]<<"\ndeLeted\n";
arr[1]=arr[n];
}
int insert(int x,int size,int arr[])
{
arr[size]=x;
int i=(size);
do
{
i=parent(i);
if(arr[i]!=min(arr[i],arr[left(i)],arr[right(i)]))
heapify(i,(size),arr);
else
return 0;
}while(i!=1);
}
int main()
{
int n,t,arr[100];
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>arr[i];
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
deletemin(n,arr);
n--;
heapify(1,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
int y;
cout<<"what value do you want to insert\n";
cin>>y;
n++;
insert(y,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
}
getch();
return 0;
}
`
One simple observation is that your min function is wrong! It doesn't return the min of 3 numbers. Do it with ifs for the moment!
I am surprised why the following code that calculates all pairs shortest pairs does not show me any output.
Here is the code:
#include <iostream>
#include <conio.h>
using namespace std;
int Min(int a,int b){
return a<=b? a:b;
}
int cost[10][10],a[10][10],i,j,k,c;
int main(){
int n,m;
cout<<"enter number of vertices "<<endl;
cin>>n;
cout<<"enter number of edges "<<endl;
cin>>m;
for (k=1;k<=m;k++)
{
cin>>i>>j>>c;
a[i][j]=cost[i][j]=c;
}
for ( i=1;i<=n;i++){
for ( j=1;j<m;j++){
if (a[i][j]==0 && i!=j)
a[i][j]=40000;
}
}
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for( j=1;j<=n;j++)
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
cout<<" resultant adj matrix \n";
for (i=1;j<=n;j++){
for (j=1;i<=n;i++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
You have some typos:
The last loops should look like this:
for (i=1;i<=n;i++){
for (j=1;j<=n;j++){
Just fix the typos in your loops, especially here:
cout<<" resultant adj matrix \n";
for (i=1;i<=n;i++){
for (j=1;j<=m;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
import java.util.*;
class Main{
static int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
public static void main(String args[])
{
int n,i,j,k;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of nodes :- ");
n=sc.nextInt();
int t=n;
int mat[][]=new int[n+1][n+1];
System.out.println("Consider 5000 as infinity :- ");
System.out.println("Enter the values of adjacency matrix :- ");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
mat[i][j]=sc.nextInt();
}
}
System.out.println("MAT0"+" = ");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(mat[i][j]+" ");
}
System.out.print("\n");
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
mat[i][j]=min(mat[i][j],mat[i][k]+mat[k][j]);
}
}
System.out.println("MAT"+k+" = ");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(mat[i][j]+" ");
}
System.out.print("\n");
}
}
}
}