SlideShare a Scribd company logo
1 of 13
IN C LANGUAGE: I've been trying to finish this program for the last few days, and I'm stuck
on one part of it, and I can't figure out what to do.
The goal is to read in a file (separated by commas), put the information into a linked list, and
then sort that linked list by either first or last name using function pointers.
Everything I have so far works, aside from the fact that my output is never sorted. I'm doing
something wrong with the listSort function, and I don't know what it is.
Can anyone please just take a look at what I have and offer a suggestion or solution? It might not
even be an issue with the function itself, but something somewhere else.
I would greatly appreciate it.
Please do not make any changes to the variables in the structs. Month has to be a string, it
cannot be an integer.
driver.c:
#include "functions.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
int main(int argc, char* argv[]) {
//creating input and output files
FILE* in = NULL;
FILE* out = NULL;
node_t* head = NULL;
node_t* temp = NULL;
//checking for 4 command line arguments
assert(argc == 4);
//opening input file
in = fopen(argv[1], "r");
assert(in != NULL);
//opening output file
out = fopen(argv[2], "w");
assert(out != NULL);
int sortBy = atoi(argv[4]);
temp = createList(in, &head);
if(sortBy == 1) {
sortList(&head, compare_by_firstname);
}
else if(sortBy == 2) {
sortList(&head, compare_by_lastname);
}
printList(out, temp);
deleteList(&head);
//closing files
fclose(in);
fclose(out);
return 0;
}
functions.c:
#include "functions.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
bool isLeapYear(int year) {
if(year % 4 != 0) {
return false;
}
else if(year % 100 != 0) {
return true;
}
else if(year % 400 != 0) {
return false;
}
else {
return true;
}
}
void printBorder(FILE* out)
{
int i = 0;
for(i = 0; i < 80; i++)
fprintf(out,"*");
fprintf(out, "n");
}
bool checkInvalidDate(node_t *node) {
int year = (int)node->birthday.year;
if(node->birthday.day < 1 || node->birthday.year < 1) {
return true;
}
if(strcmp(node->birthday.month, "February") == 0) {
if(isLeapYear(year)) {
if(node->birthday.day > 29) {
return true;
}
}
else if(node->birthday.day > 28) {
return true;
}
}
else if((strcmp(node->birthday.month, "April") == 0) || (strcmp(node->birthday.month, "June")
== 0) ||
(strcmp(node->birthday.month, "September") == 0) || (strcmp(node->birthday.month,
"November") == 0)) {
if(node->birthday.day > 30) {
return true;
}
}
else {
if(node->birthday.day > 31) {
return true;
}
}
return false;
}
void add(node_t **node, node_t **head) {
node_t* current = *head;
while(current != NULL) {
//comparing first and last names to check for duplicates; if duplicate the info isn't added to the
list
if((strcmp(current->firstName, (*node)->firstName) == 0) && (strcmp(current->lastName,
(*node)->lastName) == 0)) {
return;
}
current = current->next;
}
//adding information as a node to the list
(*node)->next = NULL;
if (*head == NULL) {
*head = *node;
}
else {
node_t* lastNode = *head;
while(lastNode->next != NULL) {
lastNode = lastNode->next;
}
lastNode->next = *node;
}
}
node_t* readNodeInfo(FILE* input) {
node_t* node = (node_t*)malloc(sizeof(node_t));
assert(node != NULL);
//scanning information using scansets and putting info in appropriate variables
fscanf(input, "%[^,], %[^,], %[^,], %d, %d, %[^,], %[^n]", node->lastName, node->firstName,
node->birthday.month,
&node->birthday.day, &node->birthday.year, node->major, node->standing);
return node;
}
node_t* createList(FILE* input, node_t** head) {
node_t* node = NULL;
node_t* temp = NULL;
char ch;
//scans through file and calls appropriate functions to read text and add nodes
while(!feof(input)) {
node = readNodeInfo(input);
if(!checkInvalidDate(node)) {
add(&node, head);
}
temp = node;
ch = fgetc(input);
}
return *head;
}
//prints the list of information to the output file
void printList(FILE* out, node_t* head) {
node_t* temp = head;
if(head == NULL) {
fprintf(stderr, "List is empty.n");
exit(1);
}
else {
printBorder(out);
fprintf(out, "nList Info:n");
while(temp != NULL) {
fprintf(out, "Name:t%s %sn", temp->firstName, temp->lastName);
fprintf(out, "Date of Birth:t%s %d, %dn", temp->birthday.month, temp->birthday.day, temp-
>birthday.year);
fprintf(out, "Major:t%sn", temp->major);
fprintf(out, "Year:t%snn", temp->standing);
temp = temp->next;
}
printBorder(out);
}
}
//uses free() to delete the nodes in the list
void deleteList(node_t** head) {
node_t* temp = *head;
node_t* next = NULL;
while(temp != NULL) {
next = temp->next;
free(temp);
temp = next;
}
*head = NULL;
}
int compare_by_lastname(node_t *a, node_t *b)
{
return strcmp(a->lastName, b->lastName);
}
int compare_by_firstname(node_t *a, node_t *b)
{
return strcmp(a->firstName, b->firstName);
}
void sortList(node_t **head, fun_ptr comp) {
bool swapped = true;
node_t *ptr1 = *head;
node_t *ptr2 = NULL;
while(swapped) {
swapped = false;
ptr1 = *head;
while(ptr1->next != ptr2) {
if(comp(ptr1, ptr1->next) > 0) {
swapNode(&ptr1, &(ptr1->next));
swapped = true;
}
ptr1 = ptr1->next;
}
ptr2 = ptr1;
}
}
void swapNode(node_t** a, node_t** b) {
node_t *temp = *a;
*a = *b;
*b = temp;
}
functions.h:
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <stdio.h>
#include <stdlib.h>
#include "stdbool.h"
//add birthday struct here
typedef struct birthday {
int day;
int year;
char month[50];
} birthday_t;
//add node_t struct here (this struct will represent a node in the
//linked list)
typedef struct node {
char firstName[50];
char lastName[50];
birthday_t birthday;
char major[50];
char standing[50];
struct node *next;
} node_t;
//implement these:
/*Parameters: node - node of struct node
* head - head node of struct node, beginning of linked list
* Return: void
* This function adds a new node to the end of the linked list.
*/
void add(node_t **node, node_t **head);
bool checkInvalidDate(node_t *node);
/*Parameters: input - file pointer pointing to the input file where data is read
* when program is run.
* Return: node_t
* This function reads the information from the provided file and stores it in
* the appropriate variables in a node.
*/
node_t* readNodeInfo(FILE* input);
/*Parameters: input - file pointer pointing to the input file where data is read
* when program is run.
* head - head node of struct node, beginning of linked list
* Return: node_t
* This function calls add() and readNodeInfo() to read in information and then
* add that information to the linked list.
*/
node_t* createList(FILE*, node_t**);
/*Parameters: out - file pointer pointing to the output file where data is written to
* when program is run.
* head - head node of struct node, beginning of linked list
* Return: void
* This function prints the list of information to the output file.
*/
void printList(FILE*, node_t*);
/*Parameters: out - file pointer pointing to the output file where data is written to
* when program is run.
* Return: void
* This function prints the border of asterik's included as part of the output.
*/
void printBorder(FILE*);
/*Parameters: head - head node of struct node, beginning of linked list
* Return: void
* This function uses free() to delete the nodes in the linked list.
*/
void deleteList(node_t**);
typedef int(*fun_ptr)(node_t*, node_t*);
int compare_by_lastname(node_t*, node_t*);
int compare_by_firstname(node_t*, node_t*);
void sortList(node_t**, fun_ptr comp);
bool isLeapYear(int);
void swapNode(node_t** a, node_t** b);
#endif
Here is an example of what the info in the text file the program reads from looks like, for
reference:
Kennedy,Leon,September,29,1998,CS/BS,Senior
Again, thank you. I greatly appreciate your help. I'm at a dead end.

More Related Content

Similar to IN C LANGUAGE- I've been trying to finish this program for the last fe.docx

C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked listSourav Gayen
 
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docxIn C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docxBlake0FxCampbelld
 
Please answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docxPlease answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docxcgraciela1
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdfdeepua8
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptxSanthiya S
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhvasavim9
 
C-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdfC-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdfherminaherman
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxgordienaysmythe
 
So basically I worked really hard on this code in my CS150 class and.pdf
So basically I worked really hard on this code in my CS150 class and.pdfSo basically I worked really hard on this code in my CS150 class and.pdf
So basically I worked really hard on this code in my CS150 class and.pdfeyewaregallery
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdfanandatalapatra
 
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docxodiliagilby
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
There are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxThere are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxclarkjanyce
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdfherminaherman
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfezzi552
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfarchgeetsenterprises
 
C programming. Answer question only in C code In the eighth part, yo.pdf
C programming. Answer question only in C code In the eighth part, yo.pdfC programming. Answer question only in C code In the eighth part, yo.pdf
C programming. Answer question only in C code In the eighth part, yo.pdfmohammedfootwear
 

Similar to IN C LANGUAGE- I've been trying to finish this program for the last fe.docx (20)

C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docxIn C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
 
C Programming Project
C Programming ProjectC Programming Project
C Programming Project
 
Please answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docxPlease answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docx
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
 
C-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdfC-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
 
So basically I worked really hard on this code in my CS150 class and.pdf
So basically I worked really hard on this code in my CS150 class and.pdfSo basically I worked really hard on this code in my CS150 class and.pdf
So basically I worked really hard on this code in my CS150 class and.pdf
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
 
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
There are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxThere are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docx
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
C programming. Answer question only in C code In the eighth part, yo.pdf
C programming. Answer question only in C code In the eighth part, yo.pdfC programming. Answer question only in C code In the eighth part, yo.pdf
C programming. Answer question only in C code In the eighth part, yo.pdf
 

More from GordonpACKellyb

In the case of both the dominant and recessive phenotypes are equally.docx
In the case of both the dominant and recessive phenotypes are equally.docxIn the case of both the dominant and recessive phenotypes are equally.docx
In the case of both the dominant and recessive phenotypes are equally.docxGordonpACKellyb
 
In specialized transduction Multiple Choice only phage genes near the.docx
In specialized transduction Multiple Choice only phage genes near the.docxIn specialized transduction Multiple Choice only phage genes near the.docx
In specialized transduction Multiple Choice only phage genes near the.docxGordonpACKellyb
 
In raspberries- height is determined by the S (- short) and s (- tall) (1).docx
In raspberries- height is determined by the S (- short) and s (- tall) (1).docxIn raspberries- height is determined by the S (- short) and s (- tall) (1).docx
In raspberries- height is determined by the S (- short) and s (- tall) (1).docxGordonpACKellyb
 
In March 2010- ONC completed the announcement of the State Health Info.docx
In March 2010- ONC completed the announcement of the State Health Info.docxIn March 2010- ONC completed the announcement of the State Health Info.docx
In March 2010- ONC completed the announcement of the State Health Info.docxGordonpACKellyb
 
In Python solve the following and please show code via Jupyter Noteboo.docx
In Python solve the following and please show code via Jupyter Noteboo.docxIn Python solve the following and please show code via Jupyter Noteboo.docx
In Python solve the following and please show code via Jupyter Noteboo.docxGordonpACKellyb
 
In order for an audit-evaluation team to be conducted with skill and p.docx
In order for an audit-evaluation team to be conducted with skill and p.docxIn order for an audit-evaluation team to be conducted with skill and p.docx
In order for an audit-evaluation team to be conducted with skill and p.docxGordonpACKellyb
 
In medium- such as triple sugar iron (TSI)- a needle with a bacterial.docx
In medium- such as triple sugar iron (TSI)- a needle with a bacterial.docxIn medium- such as triple sugar iron (TSI)- a needle with a bacterial.docx
In medium- such as triple sugar iron (TSI)- a needle with a bacterial.docxGordonpACKellyb
 
In January- Oriole Tool -& Die requisitions raw materials for producti (1).docx
In January- Oriole Tool -& Die requisitions raw materials for producti (1).docxIn January- Oriole Tool -& Die requisitions raw materials for producti (1).docx
In January- Oriole Tool -& Die requisitions raw materials for producti (1).docxGordonpACKellyb
 
Interviewing supervisors to identify the specific tasks performed by a.docx
Interviewing supervisors to identify the specific tasks performed by a.docxInterviewing supervisors to identify the specific tasks performed by a.docx
Interviewing supervisors to identify the specific tasks performed by a.docxGordonpACKellyb
 
Internal validity is important because it determines--- If the Hawthor.docx
Internal validity is important because it determines--- If the Hawthor.docxInternal validity is important because it determines--- If the Hawthor.docx
Internal validity is important because it determines--- If the Hawthor.docxGordonpACKellyb
 
int FUNCTION(unsigned int pid) -{ Complete the function (in C) to deal.docx
int FUNCTION(unsigned int pid) -{ Complete the function (in C) to deal.docxint FUNCTION(unsigned int pid) -{ Complete the function (in C) to deal.docx
int FUNCTION(unsigned int pid) -{ Complete the function (in C) to deal.docxGordonpACKellyb
 
Instructions Write a program that displays a table of Celsius temperat.docx
Instructions Write a program that displays a table of Celsius temperat.docxInstructions Write a program that displays a table of Celsius temperat.docx
Instructions Write a program that displays a table of Celsius temperat.docxGordonpACKellyb
 
Instructions- Handwrite the code on paper and include the command prom.docx
Instructions- Handwrite the code on paper and include the command prom.docxInstructions- Handwrite the code on paper and include the command prom.docx
Instructions- Handwrite the code on paper and include the command prom.docxGordonpACKellyb
 
In which of the following situations should an employee provide a new.docx
In which of the following situations should an employee provide a new.docxIn which of the following situations should an employee provide a new.docx
In which of the following situations should an employee provide a new.docxGordonpACKellyb
 
In what order did the following evolutionary adaptations appear- (from.docx
In what order did the following evolutionary adaptations appear- (from.docxIn what order did the following evolutionary adaptations appear- (from.docx
In what order did the following evolutionary adaptations appear- (from.docxGordonpACKellyb
 
In what way does the Italian culture affect your consumer behavior- D.docx
In what way does the Italian culture affect  your consumer behavior- D.docxIn what way does the Italian culture affect  your consumer behavior- D.docx
In what way does the Italian culture affect your consumer behavior- D.docxGordonpACKellyb
 
In this diagram showing the replication of DNA- label the following it.docx
In this diagram showing the replication of DNA- label the following it.docxIn this diagram showing the replication of DNA- label the following it.docx
In this diagram showing the replication of DNA- label the following it.docxGordonpACKellyb
 
In the valuation multiple approach- 1- Is it important to match on fir.docx
In the valuation multiple approach- 1- Is it important to match on fir.docxIn the valuation multiple approach- 1- Is it important to match on fir.docx
In the valuation multiple approach- 1- Is it important to match on fir.docxGordonpACKellyb
 
In the diagram shown above of the transcription initiation complex- wh.docx
In the diagram shown above of the transcription initiation complex- wh.docxIn the diagram shown above of the transcription initiation complex- wh.docx
In the diagram shown above of the transcription initiation complex- wh.docxGordonpACKellyb
 
In the ERD below- the existence of an ENROLLMENT instance depends on t.docx
In the ERD below- the existence of an ENROLLMENT instance depends on t.docxIn the ERD below- the existence of an ENROLLMENT instance depends on t.docx
In the ERD below- the existence of an ENROLLMENT instance depends on t.docxGordonpACKellyb
 

More from GordonpACKellyb (20)

In the case of both the dominant and recessive phenotypes are equally.docx
In the case of both the dominant and recessive phenotypes are equally.docxIn the case of both the dominant and recessive phenotypes are equally.docx
In the case of both the dominant and recessive phenotypes are equally.docx
 
In specialized transduction Multiple Choice only phage genes near the.docx
In specialized transduction Multiple Choice only phage genes near the.docxIn specialized transduction Multiple Choice only phage genes near the.docx
In specialized transduction Multiple Choice only phage genes near the.docx
 
In raspberries- height is determined by the S (- short) and s (- tall) (1).docx
In raspberries- height is determined by the S (- short) and s (- tall) (1).docxIn raspberries- height is determined by the S (- short) and s (- tall) (1).docx
In raspberries- height is determined by the S (- short) and s (- tall) (1).docx
 
In March 2010- ONC completed the announcement of the State Health Info.docx
In March 2010- ONC completed the announcement of the State Health Info.docxIn March 2010- ONC completed the announcement of the State Health Info.docx
In March 2010- ONC completed the announcement of the State Health Info.docx
 
In Python solve the following and please show code via Jupyter Noteboo.docx
In Python solve the following and please show code via Jupyter Noteboo.docxIn Python solve the following and please show code via Jupyter Noteboo.docx
In Python solve the following and please show code via Jupyter Noteboo.docx
 
In order for an audit-evaluation team to be conducted with skill and p.docx
In order for an audit-evaluation team to be conducted with skill and p.docxIn order for an audit-evaluation team to be conducted with skill and p.docx
In order for an audit-evaluation team to be conducted with skill and p.docx
 
In medium- such as triple sugar iron (TSI)- a needle with a bacterial.docx
In medium- such as triple sugar iron (TSI)- a needle with a bacterial.docxIn medium- such as triple sugar iron (TSI)- a needle with a bacterial.docx
In medium- such as triple sugar iron (TSI)- a needle with a bacterial.docx
 
In January- Oriole Tool -& Die requisitions raw materials for producti (1).docx
In January- Oriole Tool -& Die requisitions raw materials for producti (1).docxIn January- Oriole Tool -& Die requisitions raw materials for producti (1).docx
In January- Oriole Tool -& Die requisitions raw materials for producti (1).docx
 
Interviewing supervisors to identify the specific tasks performed by a.docx
Interviewing supervisors to identify the specific tasks performed by a.docxInterviewing supervisors to identify the specific tasks performed by a.docx
Interviewing supervisors to identify the specific tasks performed by a.docx
 
Internal validity is important because it determines--- If the Hawthor.docx
Internal validity is important because it determines--- If the Hawthor.docxInternal validity is important because it determines--- If the Hawthor.docx
Internal validity is important because it determines--- If the Hawthor.docx
 
int FUNCTION(unsigned int pid) -{ Complete the function (in C) to deal.docx
int FUNCTION(unsigned int pid) -{ Complete the function (in C) to deal.docxint FUNCTION(unsigned int pid) -{ Complete the function (in C) to deal.docx
int FUNCTION(unsigned int pid) -{ Complete the function (in C) to deal.docx
 
Instructions Write a program that displays a table of Celsius temperat.docx
Instructions Write a program that displays a table of Celsius temperat.docxInstructions Write a program that displays a table of Celsius temperat.docx
Instructions Write a program that displays a table of Celsius temperat.docx
 
Instructions- Handwrite the code on paper and include the command prom.docx
Instructions- Handwrite the code on paper and include the command prom.docxInstructions- Handwrite the code on paper and include the command prom.docx
Instructions- Handwrite the code on paper and include the command prom.docx
 
In which of the following situations should an employee provide a new.docx
In which of the following situations should an employee provide a new.docxIn which of the following situations should an employee provide a new.docx
In which of the following situations should an employee provide a new.docx
 
In what order did the following evolutionary adaptations appear- (from.docx
In what order did the following evolutionary adaptations appear- (from.docxIn what order did the following evolutionary adaptations appear- (from.docx
In what order did the following evolutionary adaptations appear- (from.docx
 
In what way does the Italian culture affect your consumer behavior- D.docx
In what way does the Italian culture affect  your consumer behavior- D.docxIn what way does the Italian culture affect  your consumer behavior- D.docx
In what way does the Italian culture affect your consumer behavior- D.docx
 
In this diagram showing the replication of DNA- label the following it.docx
In this diagram showing the replication of DNA- label the following it.docxIn this diagram showing the replication of DNA- label the following it.docx
In this diagram showing the replication of DNA- label the following it.docx
 
In the valuation multiple approach- 1- Is it important to match on fir.docx
In the valuation multiple approach- 1- Is it important to match on fir.docxIn the valuation multiple approach- 1- Is it important to match on fir.docx
In the valuation multiple approach- 1- Is it important to match on fir.docx
 
In the diagram shown above of the transcription initiation complex- wh.docx
In the diagram shown above of the transcription initiation complex- wh.docxIn the diagram shown above of the transcription initiation complex- wh.docx
In the diagram shown above of the transcription initiation complex- wh.docx
 
In the ERD below- the existence of an ENROLLMENT instance depends on t.docx
In the ERD below- the existence of an ENROLLMENT instance depends on t.docxIn the ERD below- the existence of an ENROLLMENT instance depends on t.docx
In the ERD below- the existence of an ENROLLMENT instance depends on t.docx
 

Recently uploaded

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Recently uploaded (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 

IN C LANGUAGE- I've been trying to finish this program for the last fe.docx

  • 1. IN C LANGUAGE: I've been trying to finish this program for the last few days, and I'm stuck on one part of it, and I can't figure out what to do. The goal is to read in a file (separated by commas), put the information into a linked list, and then sort that linked list by either first or last name using function pointers. Everything I have so far works, aside from the fact that my output is never sorted. I'm doing something wrong with the listSort function, and I don't know what it is. Can anyone please just take a look at what I have and offer a suggestion or solution? It might not even be an issue with the function itself, but something somewhere else. I would greatly appreciate it. Please do not make any changes to the variables in the structs. Month has to be a string, it cannot be an integer. driver.c: #include "functions.h" #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> int main(int argc, char* argv[]) { //creating input and output files FILE* in = NULL; FILE* out = NULL; node_t* head = NULL; node_t* temp = NULL; //checking for 4 command line arguments assert(argc == 4); //opening input file in = fopen(argv[1], "r"); assert(in != NULL);
  • 2. //opening output file out = fopen(argv[2], "w"); assert(out != NULL); int sortBy = atoi(argv[4]); temp = createList(in, &head); if(sortBy == 1) { sortList(&head, compare_by_firstname); } else if(sortBy == 2) { sortList(&head, compare_by_lastname); } printList(out, temp); deleteList(&head); //closing files fclose(in); fclose(out); return 0; } functions.c: #include "functions.h" #include <stdio.h> #include <stdlib.h> #include <assert.h>
  • 3. #include <string.h> #include <stdbool.h> bool isLeapYear(int year) { if(year % 4 != 0) { return false; } else if(year % 100 != 0) { return true; } else if(year % 400 != 0) { return false; } else { return true; } } void printBorder(FILE* out) { int i = 0; for(i = 0; i < 80; i++) fprintf(out,"*"); fprintf(out, "n"); }
  • 4. bool checkInvalidDate(node_t *node) { int year = (int)node->birthday.year; if(node->birthday.day < 1 || node->birthday.year < 1) { return true; } if(strcmp(node->birthday.month, "February") == 0) { if(isLeapYear(year)) { if(node->birthday.day > 29) { return true; } } else if(node->birthday.day > 28) { return true; } } else if((strcmp(node->birthday.month, "April") == 0) || (strcmp(node->birthday.month, "June") == 0) || (strcmp(node->birthday.month, "September") == 0) || (strcmp(node->birthday.month, "November") == 0)) { if(node->birthday.day > 30) { return true; } } else {
  • 5. if(node->birthday.day > 31) { return true; } } return false; } void add(node_t **node, node_t **head) { node_t* current = *head; while(current != NULL) { //comparing first and last names to check for duplicates; if duplicate the info isn't added to the list if((strcmp(current->firstName, (*node)->firstName) == 0) && (strcmp(current->lastName, (*node)->lastName) == 0)) { return; } current = current->next; } //adding information as a node to the list (*node)->next = NULL; if (*head == NULL) { *head = *node; } else { node_t* lastNode = *head;
  • 6. while(lastNode->next != NULL) { lastNode = lastNode->next; } lastNode->next = *node; } } node_t* readNodeInfo(FILE* input) { node_t* node = (node_t*)malloc(sizeof(node_t)); assert(node != NULL); //scanning information using scansets and putting info in appropriate variables fscanf(input, "%[^,], %[^,], %[^,], %d, %d, %[^,], %[^n]", node->lastName, node->firstName, node->birthday.month, &node->birthday.day, &node->birthday.year, node->major, node->standing); return node; } node_t* createList(FILE* input, node_t** head) { node_t* node = NULL; node_t* temp = NULL; char ch; //scans through file and calls appropriate functions to read text and add nodes while(!feof(input)) { node = readNodeInfo(input); if(!checkInvalidDate(node)) { add(&node, head);
  • 7. } temp = node; ch = fgetc(input); } return *head; } //prints the list of information to the output file void printList(FILE* out, node_t* head) { node_t* temp = head; if(head == NULL) { fprintf(stderr, "List is empty.n"); exit(1); } else { printBorder(out); fprintf(out, "nList Info:n"); while(temp != NULL) { fprintf(out, "Name:t%s %sn", temp->firstName, temp->lastName); fprintf(out, "Date of Birth:t%s %d, %dn", temp->birthday.month, temp->birthday.day, temp- >birthday.year); fprintf(out, "Major:t%sn", temp->major); fprintf(out, "Year:t%snn", temp->standing); temp = temp->next; }
  • 8. printBorder(out); } } //uses free() to delete the nodes in the list void deleteList(node_t** head) { node_t* temp = *head; node_t* next = NULL; while(temp != NULL) { next = temp->next; free(temp); temp = next; } *head = NULL; } int compare_by_lastname(node_t *a, node_t *b) { return strcmp(a->lastName, b->lastName); } int compare_by_firstname(node_t *a, node_t *b) { return strcmp(a->firstName, b->firstName); } void sortList(node_t **head, fun_ptr comp) {
  • 9. bool swapped = true; node_t *ptr1 = *head; node_t *ptr2 = NULL; while(swapped) { swapped = false; ptr1 = *head; while(ptr1->next != ptr2) { if(comp(ptr1, ptr1->next) > 0) { swapNode(&ptr1, &(ptr1->next)); swapped = true; } ptr1 = ptr1->next; } ptr2 = ptr1; } } void swapNode(node_t** a, node_t** b) { node_t *temp = *a; *a = *b; *b = temp; } functions.h: #ifndef FUNCTIONS_H
  • 10. #define FUNCTIONS_H #include <stdio.h> #include <stdlib.h> #include "stdbool.h" //add birthday struct here typedef struct birthday { int day; int year; char month[50]; } birthday_t; //add node_t struct here (this struct will represent a node in the //linked list) typedef struct node { char firstName[50]; char lastName[50]; birthday_t birthday; char major[50]; char standing[50]; struct node *next; } node_t; //implement these: /*Parameters: node - node of struct node * head - head node of struct node, beginning of linked list
  • 11. * Return: void * This function adds a new node to the end of the linked list. */ void add(node_t **node, node_t **head); bool checkInvalidDate(node_t *node); /*Parameters: input - file pointer pointing to the input file where data is read * when program is run. * Return: node_t * This function reads the information from the provided file and stores it in * the appropriate variables in a node. */ node_t* readNodeInfo(FILE* input); /*Parameters: input - file pointer pointing to the input file where data is read * when program is run. * head - head node of struct node, beginning of linked list * Return: node_t * This function calls add() and readNodeInfo() to read in information and then * add that information to the linked list. */ node_t* createList(FILE*, node_t**); /*Parameters: out - file pointer pointing to the output file where data is written to * when program is run. * head - head node of struct node, beginning of linked list
  • 12. * Return: void * This function prints the list of information to the output file. */ void printList(FILE*, node_t*); /*Parameters: out - file pointer pointing to the output file where data is written to * when program is run. * Return: void * This function prints the border of asterik's included as part of the output. */ void printBorder(FILE*); /*Parameters: head - head node of struct node, beginning of linked list * Return: void * This function uses free() to delete the nodes in the linked list. */ void deleteList(node_t**); typedef int(*fun_ptr)(node_t*, node_t*); int compare_by_lastname(node_t*, node_t*); int compare_by_firstname(node_t*, node_t*); void sortList(node_t**, fun_ptr comp); bool isLeapYear(int); void swapNode(node_t** a, node_t** b); #endif Here is an example of what the info in the text file the program reads from looks like, for reference:
  • 13. Kennedy,Leon,September,29,1998,CS/BS,Senior Again, thank you. I greatly appreciate your help. I'm at a dead end.