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 and need Help!!: Currently I am asking for the user

ID: 3707992 • Letter: I

Question

I am writing a program in C and need Help!!:

Currently I am asking for the user to input a file once the program is running. But I want the user to enter the file into command line instead when compiling and keep the rest of the program the same.

ie. FILE *fp = fopen(argv[1], "r");//open file in read format

Thank you,

//CODE:

#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()

{

FILE *f1;

char * str=(char*)malloc(sizeof(char)*50);

printf("enter file path here ");

scanf("%s",str);

f1=fopen(str,"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(str,"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(" ");

}

}

}

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