C++ how to compare two objects of the same class



The equality operator will work on pointers regardless of whether you’ve implemented the equality operator for your class. If it’s giving an error when used with pointers, then you’re doing something wrong.

To define the equality operator as part of your class:



Code:

class test
{
public:
bool operator==(const test &t); // in this method, see if t is equal to *this.
}

sum of the diagonals in a matrix


#include<stdio.h>

int main(){

int a[10][10],i,j,sum=0,m,n;

printf("\nEnter the row and column of matrix: ");
scanf("%d %d",&m,&n);

printf("\nEnter the elements of matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");

for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);

return 0;
}




Sample output:

Enter the row and column of matrix: 3 3
Enter the elements of matrix: 2
3
5
6
7
9
2
6
7
The matrix is
2 3 5
6 7 9
2 6 7
Sum of the diagonal elements of a matrix is: 16

C code to print or display upper triangular matrix | upper triangular matrix in C | Matrix programs in C | Assignments on matrix in C




#include<stdio.h>
int main(){
int a[3][3],i,j;
float determinant=0;

printf("Please Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}

printf("\nSetting zero in upper triangular matrix\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i>=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}


return 0;
}