Search Tutorials

Thursday, 16 May 2013

C code to calculate cyclomatic complexity using verteces,edges and connected components

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10

void main()
{
  FILE *fp;
  int row,col,i,j,edges,nodes;
  char ch;
  int mat[MAX][MAX];

  clrscr();

  fp=fopen("matrix.txt","r");

  row=col=0;

  while(fscanf(fp,"%c",&ch)!=EOF)
  {
     if(ch==' ')
       col++;
     else if(ch=='\n')
     {
       row++;
       col=0;
     }
     else
       mat[row][col]=ch-48;
   }

fclose(fp);

printf("\nThe given adjacency matrix is : \n");

for(i=0;i<=row;i++)
{
  for(j=0;j<=col;j++)
    printf("%d\t",mat[i][j]);
  printf("\n");
}

nodes=row+1;
printf("\nNumber of nodes = %d",nodes);

edges=0;

for(i=0;i<=row;i++)
{
  for(j=0;j<=col;j++)
  {
    if(mat[i][j]==1)
      edges++;
  }
}
printf("\nNumber of edges = %d",edges);
printf("\n\nCyclometic Complexity of graph  = edges-nodes+2p\n");
printf("\t\t\t\t= %d - %d + 2",edges,nodes);
printf("\n\t\t\t\t= %d",edges-nodes+2);


getch();
}


10 comments:

  1. useful code....!!what does the matrix.txt file contains???

    ReplyDelete
    Replies
    1. Contains the adjacency matrix of the Control Flow Graph of a program.

      Delete
    2. we know that it contains adjacency matrix but we want code.

      Delete
  2. the program is not workin

    ReplyDelete
    Replies
    1. Remove clearscreen and instead of void main ...just give int main and run it

      Delete
    2. broo do u have matrix.txt file??

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. where is the adjacent matrix file ???, can anyone share the file

    ReplyDelete
    Replies
    1. yes please where is the file

      Delete

Back to Top