Question

Writing a program in C please help!! My file worked fine before but when I put...

Writing a program in C please help!!

My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below.

example input form commnd line file:

      A  B
      B  C
      E  X
      C  D
      A  C    

The directed edges in this example are: A can go to both B and C. B can go to C. E can go to X. And C to D

The output i am looking for is:

Vertex:              Adjacent to:

A                      B,C

B                      C

C                      D

D

E                      X

X

c code:

List.h

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

//structure list
struct list
{

char vertex, adjacent;

struct list * next;

};

typedef struct list List;


#endif // LIST_H_INCLUDED

link.c

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

//create link function
void createlink(char ch1, char ch2, List *head)

{

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;

}

display.c

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

//display function
void displayadj(char ch, List *head)

{

List * th = head;

while (th != NULL)

{

if (th -> vertex == ch)

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

th = th -> next;

}

}

main.c

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

//function declarations
void createlink(char ch1, char ch2, List *head);
void displayadj(char ch, List *head);

//main functions
int main(int argc, char** argv)
{
List * head = NULL;
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\n");

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\n");

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 , head); //create link as vetex ch1,adjacent ch2

num = 0;

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

} else

{

ch1 = ch;

}

}

}

int i = 0;

printf("vetex: adjacent vertices:\n");

for (; i < 26; i++)

{

if (array[i] == 1)

{
ch = 'A' + i;

printf("%c ", ch);

displayadj(ch,head);

printf("\n");

}

}
}

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#ifndef LIST_H_INCLUDED

#define LIST_H_INCLUDED

//structure list

struct list

{

char vertex, adjacent;

struct list * next;

};

typedef struct list List;

#endif // LIST_H_INCLUDED

link.c

#include <stdio.h>

#include <stdlib.h>

#include "List.h"

//create link function

void createlink(char ch1, char ch2, List *head)

{

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;

}

display.c

#include <stdio.h>

#include <stdlib.h>

#include "List.h"

//display function

void displayadj(char ch, List *head)

{

List * th = head;

while (th != NULL)

{

if (th -> vertex == ch)

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

th = th -> next;

}

}

main.c

#include <stdio.h>

#include <stdlib.h>

#include "List.h"

//function declarations

void createlink(char ch1, char ch2, List *head);

void displayadj(char ch, List *head);

//main functions

int main(int argc, char** argv)

{

List * head = NULL;

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\n");

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\n");

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 , head); //create link as vetex ch1,adjacent ch2

num = 0;

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

} else

{

ch1 = ch;

}

}

}

int i = 0;

printf("vetex: adjacent vertices:\n");

for (; i < 26; i++)

{

if (array[i] == 1)

{

ch = 'A' + i;

printf("%c ", ch);

displayadj(ch,head);

printf("\n");

}

}

}

else{

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

}

}

Add a comment
Know the answer?
Add Answer to:
Writing a program in C please help!! My file worked fine before but when I put...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Implement a program that: reads a number of personal records (for example, using PERSON struct from...

    Implement a program that: reads a number of personal records (for example, using PERSON struct from the earlier lab) from the standard input, creates a database of personal records, allows for adding new entries to the database, allows for deleting entries from the database, includes functions to acquire a record of personal data, and includes functions to display (print) a single selected record from the database, and also allows for printing all records in the database. NOTES: if name is...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h>...

    Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...

  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT