Saturday, December 5, 2020

Introduction to Programming in C Slot 1: Question 3 Solution

Slot 1: Question 3

Due on 2020-12-05, 11:00 IST
A complex number is represented as a pair of floating point numbers
- (a,b) represents the complex number a+ib, where i is the square root
of -1.


You are given a 3x3 matrix (let us call it M) of such complex
numbers. The matrix is called Hermetian if for every row i and column
j, M[i][j] is the conjugate of M[j][i]. That is to say, if M[i][j]
represents the complex number a+ib, then M[j][i] should represent the
complex number a-ib.

You must output the number of entries M[i][j] such that M[i][j] is NOT
the transpose of M[j][i]. (If the matrix is actually Hermetian, then
this number will be 0.)

Hint: You can use

      scanf("(%f %f) ",&x, &y);

to read a pair of floating point numbers in the given format into
  float variables x and y. Please note that there is a space after the
  closing bracket ')' and the closing double quote.


Your last recorded submission was on 2020-12-05, 10:42 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
#include<stdlib.h>
3
int main(){
4
  float real[3][3];
5
  float comp[3][3];
6
  for(int i=0;i<3;i++){
7
    for(int j=0;j<3;j++){
8
    scanf("(%f %f) ",&real[i][j], &comp[i][j]);
9
    }
10
  }
11
  int c=0;
12
  for(int i=0;i<3;i++){
13
    for(int j=0;j<3;j++){
14
      if(!(real[i][j]==real[j][i]&&(comp[i][j]==-comp[j][i]||comp[j][i]==-comp[i][j]))){
15
        c++;
16
      }
17
    }
18
  }
19
  printf("%d",c);
20
}
21

No comments:

Post a Comment