Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am writing a program in C please Help!!, The program is working the way I want

ID: 3708265 • Letter: I

Question

I am writing a program in C please Help!!,

The program is working the way I want it to, but I want to have each function in its own separate file (one function per file) and each function should use an include statement for my header file. I also want to use one appropriate header file for all my structures and for the prototypes of the functions. Also I am not supposed to have any global variables in my header file.

I have the struct in a seperate header file and incude it and it works fine. But when I put the fuctions in a header file it does seem to compile.

Please help I am a bit confused about the meaning of these instructions and how to implement them.

Thank you

#include <stdio.h>
#include <stdlib.h>

struct list

{

char vertex, adjacent;

struct list * next;

};

typedef struct list List;

List * head = NULL;

void createlink(char ch1, char ch2)

{

List * th;

if (head == NULL)

{

    head = (List * ) malloc(sizeof(List));

    head-> vertex = ch1;

    head -> adjacent = ch2;

    head -> next = NULL;

    return;

}

th = head;

while (th -> next != NULL)

    th = th -> next;

th -> next = (List * ) malloc(sizeof(List));

th -> next -> vertex = ch1;

th -> next -> adjacent = ch2;

th -> next -> next = NULL;

return;

}

void displayadj(char ch)

{

List * th = head;

while (th != NULL)

{

    if (th -> vertex == ch)

      printf("%c ", th -> adjacent); //if a vertex match print its adjacent

    th = th -> next;

}

}

int main(int argc, char** argv)

{

FILE * f1;

if(argc == 2){
f1 = fopen(argv[1], "r");

char ch, ch1;

int array[26] = {
    0
};

if (f1 == NULL)

{

    printf("error no such file ");

    return 0;

}

while ((ch = fgetc(f1)) != EOF)

{

    if (ch >= 'A' && ch <= 'Z')

      if (array[ch - 'A'] == 0)

      {

        array[ch - 'A'] = 1;

      }

}

f1 = fopen(argv[1], "r");

int num = 0;

char ch2;

//printf("i'm displaying the file now ");

while ((ch = fgetc(f1)) != EOF)

{

    if (ch >= 'A' && ch <= 'Z')

    {

      num++;

      if (num % 2 == 0) //if a char between A and Z comes again in a line it takes as ch1 adjacent

      {

        ch2 = ch;

        createlink(ch1, ch2); //create link as vetex ch1,adjacent ch2

        num = 0;

        //printf("%c %c ",ch1,ch2);

      } else

      {

        ch1 = ch;

      }

    }

}

int i = 0;

printf("vetex: adjacent vertices: ");

for (; i < 26; i++)

{

    if (array[i] == 1)

    {
      ch = 'A' + i;

      printf("%c ", ch);

      displayadj(ch);

      printf(" ");

    }

}
}

else{
   //printf("Please provide one command line argument");
}
}

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

struct list

{

char vertex, adjacent;

struct list * next;

};

typedef struct list List;

List * head = NULL;

void createlink(char ch1, char ch2)

{

List * th;

if (head == NULL)

{

    head = (List * ) malloc(sizeof(List));

    head-> vertex = ch1;

    head -> adjacent = ch2;

    head -> next = NULL;

    return;

}

th = head;

while (th -> next != NULL)

    th = th -> next;

th -> next = (List * ) malloc(sizeof(List));

th -> next -> vertex = ch1;

th -> next -> adjacent = ch2;

th -> next -> next = NULL;

return;

}

void displayadj(char ch)

{

List * th = head;

while (th != NULL)

{

    if (th -> vertex == ch)

      printf("%c ", th -> adjacent); //if a vertex match print its adjacent

    th = th -> next;

}

}

int main(int argc, char** argv)

{

FILE * f1;

if(argc == 2){

f1 = fopen(argv[1], "r");

char ch, ch1;

int array[26] = {

    0

};

if (f1 == NULL)

{

    printf("error no such file ");

    return 0;

}

while ((ch = fgetc(f1)) != EOF)

{

    if (ch >= 'A' && ch <= 'Z')

      if (array[ch - 'A'] == 0)

      {

        array[ch - 'A'] = 1;

      }

}

f1 = fopen(argv[1], "r");

int num = 0;

char ch2;

//printf("i'm displaying the file now ");

while ((ch = fgetc(f1)) != EOF)

{

    if (ch >= 'A' && ch <= 'Z')

    {

      num++;

      if (num % 2 == 0) //if a char between A and Z comes again in a line it takes as ch1 adjacent

      {

        ch2 = ch;

        createlink(ch1, ch2); //create link as vetex ch1,adjacent ch2

        num = 0;

        //printf("%c %c ",ch1,ch2);

      } else

      {

        ch1 = ch;

      }

    }

}

int i = 0;

printf("vetex: adjacent vertices: ");

for (; i < 26; i++)

{

    if (array[i] == 1)

    {

      ch = 'A' + i;

      printf("%c ", ch);

      displayadj(ch);

      printf(" ");

    }

}

}

else{

   //printf("Please provide one command line argument");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote