#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    14
    Rep Power
    0

    Calling function in main


    I have the following function with variable number of arguments that
    returns the address of a (directed unweighted) graph which has the largest degree among n graphs.
    One argument of the function is the number of graphs for input, n.
    Optional arguments are n pointers to those graphs.

    Note: Maximum degree of a directed graph is the total number of in-degree and out-degree
    edges of a vertex that has the largest number of edges, with loops counted twice.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<stdarg.h>
    #define MAX 10
    
    typedef struct
    {
        int n;//number of vertices in a graph
        void* info[MAX];//information in a vertex
        int am[MAX][MAX];//adjacency matrix
    }GH;
    
    int degree(GH *g)
    {
        int i,j;
        int deg_row[MAX]={0},deg_column[MAX]={0};
        int count_row[MAX]={0},count_column[MAX]={0};
        int max=0,maxDeg,temp,count_total[MAX]={0};
        int transpose[MAX][MAX];
    
        for(i=0;i<g->n;i++)
        {
            for(j=0;j<g->n;j++)
            {
                transpose[i][j]=g->am[j][i];
            }
        }
    
        for(i=0;i<g->n;i++)
        {
            for(j=0;j<g->n;j++)
            {
                if(transpose[i][j]!=0)
                    deg_column[i]++;
                if(i==j)
                {
                    if(transpose[i][j]!=0)
                        deg_column[i]=deg_column[j];
                }
            }
    
            count_column[i]=deg_column[i];
        }
    
            for(i=0;i<g->n;i++)
        {
            for(j=0;j<g->n;j++)
            {
                if(g->am[i][j]!=0)
                    deg_row[i]++;
            }
    
            count_row[i]=deg_row[i];
        }
    
        for(i=0;i<g->n;i++)
        {
            count_total[i]=count_row[i]+count_column[i];
        }
    
        for(i=0;i<g->n;i++)
        {
            if(count_total[i]>count_total[max])
                max=i;
        }
    
        maxDeg=count_total[max];
        return maxDeg;
    }
    
    GH *f(int n,...)
    {
        GH *deg;
        int i,maxDeg[MAX]={0},max=0;
    
        va_list args;
        va_start(args,n);
    
        for(i=0;i<n;i++)
        {
           maxDeg[i]=degree(va_arg(args,GH*));
           if(maxDeg[i]>maxDeg[max])
           {
                max=i;
                break;
           }
        }
        deg=va_arg(args,GH*);
    
        va_end(args);
        return deg;
    }
    
    int main()
    {
        printf("%d %s %d",f(2,{4,{'1','2','3','4'},{{0,1,1,0},{0,0,0,0},{0,0,0,0},{0,1,0,0}}},
                      {4,{'1','2','3','4'},{{1,1,1,0},{0,1,0,0},{0,0,1,0},{0,1,0,1}}}));
        return 0;
    }
    How to print data about a graph that has the largest degree?

    Also, how to pass optional arguments in main function without initialization
    (they are initialized in this code, but they should be pointers)?

    Why is the following call in main function wrong:
    Code:
        printf("%d %s %d",f(2,{4,{'1','2','3','4'},{{0,1,1,0},{0,0,0,0},{0,0,0,0},{0,1,0,0}}},
                      {4,{'1','2','3','4'},{{1,1,1,0},{0,1,0,0},{0,0,1,0},{0,1,0,1}}}));
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    14
    Rep Power
    0
    I have made some changes, but the program doesn't respond:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<stdarg.h>
    #define MAX 10
    
    typedef struct
    {
        int n;//number of vertices in a graph
        void* info[MAX];//information in a vertex
        int am[MAX][MAX];//adjacency matrix
    }GH;
    
    int degree(GH *g)
    {
        int i,j;
        int deg_row[MAX]={0},deg_column[MAX]={0};
        int count_row[MAX]={0},count_column[MAX]={0};
        int max=0,maxDeg,temp,count_total[MAX]={0};
        int transpose[MAX][MAX];
    
        for(i=0;i<g->n;i++)
        {
            for(j=0;j<g->n;j++)
            {
                transpose[i][j]=g->am[j][i];
            }
        }
    
        for(i=0;i<g->n;i++)
        {
            for(j=0;j<g->n;j++)
            {
                if(transpose[i][j]!=0)
                    deg_column[i]++;
                if(i==j)
                {
                    if(transpose[i][j]!=0)
                        deg_column[i]=deg_column[j];
                }
            }
    
            count_column[i]=deg_column[i];
        }
    
            for(i=0;i<g->n;i++)
        {
            for(j=0;j<g->n;j++)
            {
                if(g->am[i][j]!=0)
                    deg_row[i]++;
            }
    
            count_row[i]=deg_row[i];
        }
    
        for(i=0;i<g->n;i++)
        {
            count_total[i]=count_row[i]+count_column[i];
        }
    
        for(i=0;i<g->n;i++)
        {
            if(count_total[i]>count_total[max])
                max=i;
        }
    
        maxDeg=count_total[max];
        return maxDeg;
    }
    
    GH *f(int n,...)
    {
        GH *deg;
        int i,j,maxDeg[MAX]={0},max=0;
    
        va_list args;
        va_start(args,n);
    
        for(i=0;i<n;i++)
        {
           maxDeg[i]=degree(va_arg(args,GH*));
           if(maxDeg[i]>maxDeg[max])
           {
                max=i;
                break;
           }
        }
        deg=va_arg(args,GH*);
    
        va_end(args);
        return deg;
    }
    
    void printGraph(GH *g)
    {
        int i,j;
        printf("%d",g->n);
        printf("%s",g->info);
        for(i=0;i<g->n;i++,printf("\n"))
           for(j=0;j<g->n;j++)
              printf(" %2d",g->am[i][j]);
    }
    
    int main()
    {
        GH gh1 = {4,{'1','2','3','4'},{{0,1,1,0},{0,0,0,0},{0,0,0,0},{0,1,0,0}}};
        GH gh2 = {4,{'1','2','3','4'},{{1,1,1,0},{0,1,0,0},{0,0,1,0},{0,1,0,1}}};
    
        GH* res=f(2,gh1,gh2);
        printGraph(res);
    
        return 0;
    }
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    You must compile with warnings enabled and pay attention to them.

    The info member is an array of pointers to void, which you've initialized with small integers (single characters). I point this out because it's just not right.

    You expected the varargs to be pointers to structures, yet you passed the structures instead. This also a blunder.

    You've returned from f a pointer to GH yet did not allocate space for it. That deg memory on the stack is not available to calling functions. Without having a clue why a single GH result makes sense, and without populating it with reasonable values, we write following which may help a little bit.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<stdarg.h>
    #define MAX 10
    
    typedef struct {
      int n;//number of vertices in a graph
      char info[MAX];//information in a vertex
      int am[MAX][MAX];//adjacency matrix
    } GH;
    
    int degree(GH*g) {
      int i,j;
      int deg_row[MAX]={0},deg_column[MAX]={0};
      int count_row[MAX]={0},count_column[MAX]={0};
      int max=0,maxDeg,count_total[MAX]={0};
      int transpose[MAX][MAX];
      for(i=0;i<g->n;i++)
        for(j=0;j<g->n;j++)
          transpose[i][j]=g->am[j][i];
      for(i=0;i<g->n;i++) {
        for(j=0;j<g->n;j++) {
          deg_column[i] += !! transpose[i][j];
          if((i==j) && transpose[i][j])
    	deg_column[i]=deg_column[j];
        }
        count_column[i]=deg_column[i];
      }
      for(i=0;i<g->n;i++) {
        for(j=0;j<g->n;j++)
          if(g->am[i][j]!=0)
    	deg_row[i]++;
        count_row[i]=deg_row[i];
      }
      for(i=0;i<g->n;i++)
        count_total[i]=count_row[i]+count_column[i];
      for(i=0;i<g->n;i++)
        if(count_total[i]>count_total[max])
          max=i;
      maxDeg=count_total[max];
      return maxDeg;
    }
    
    GH*f(GH*deg, int n,...) {
      int i,maxDeg[MAX]={0},max=0;
      va_list args;
      va_start(args,n);
      for(i=0;i<n;i++) {
        maxDeg[i]=degree(va_arg(args,GH*));
        if(maxDeg[i]>maxDeg[max]) {
          max=i;
          break;
        }
      }
      deg=va_arg(args,GH*);
      va_end(args);
      return deg;
    }
    
    void printGraph(GH*g) {
      int i,j;
      printf("%d",g->n);
      putchar(g->info[0]);
      for(i=0;i<g->n;i++,putchar('\n'))
        for(j=0;j<g->n;j++)
          printf(" %2d",g->am[i][j]);
    }
    
    int main() {
      GH
        res,
        gh1 = {4,{'1','2','3','4'},{{0,1,1,0},{0,0,0,0},{0,0,0,0},{0,1,0,0}}},
        gh2 = {4,{'1','2','3','4'},{{1,1,1,0},{0,1,0,0},{0,0,1,0},{0,1,0,1}}};
      memset(&res,0,sizeof(res));
      f(&res, 2,&gh1,&gh2);
      printGraph(&res);
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    14
    Rep Power
    0
    I don't understand how the output of your code is zero.
    It seems that the program prints only the value of n (from structure), but why that value is zero at the end?
    Why not four (number of vertices)?

    Also, when the function printGraph() is called, why the program is not printing adjacency matrix and vertices of the found graph
    (only the value of n which is incorrect - zero)?
  8. #5
  9. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    my code has in main

    memset(&res,0,sizeof(res));

    which sets all the bytes in res to 0.
    f does nothing to change those bytes.

    I didn't actually read your first post until now. If you want to be certain to find the maximum of noise you must look at all the noise. Your break statement in f stops the search. And when thereafter you invoke va_arg again you'd return a pointer that was unsearched! Or entirely invalid.
    Here are a few new functions
    Code:
    GH*f(int n,...) {
      GH*g = NULL, *maxg = NULL;
      int i,deg,maxDeg = 0;
      va_list args;
      va_start(args,n);
      for(i=0;i<n;i++) {
        g = va_arg(args, GH*);
        deg = degree(g);
        if(maxDeg < deg) {
          maxDeg = deg;
          maxg = g;
        }
      }
      va_end(args);
      return maxg;
    }
    
    void printGraph(GH*g) {
      int i,j;
      printf("%d<%c>\n",g->n,g->info[0]);
      for(i=0;i<g->n;i++,putchar('\n'))
        for(j=0;j<g->n;j++)
          printf(" %2d",g->am[i][j]);
    }
    
    int main() {
      GH
        *res,
        gh1 = {4,{'1','2','3','4'},{{0,1,1,0},{0,0,0,0},{0,0,0,0},{0,1,0,0}}},
        gh2 = {4,{'1','2','3','4'},{{1,1,1,0},{0,1,0,0},{0,0,1,0},{0,1,0,1}}};
      res = f(2,&gh1,&gh2);
      printGraph(res);
      return 0;
    }

    Comments on this post

    • code_user agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    14
    Rep Power
    0
    Thanks for the help.

IMN logo majestic logo threadwatch logo seochat tools logo