SlideShare a Scribd company logo
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 list
Sourav 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.docx
Blake0FxCampbelld
 
C Programming Project
C Programming ProjectC Programming Project
C Programming Project
Vijayananda Mohire
 
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
cgraciela1
 
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
deepua8
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
Santhiya S
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
vasavim9
 
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
herminaherman
 
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
JUSTSTYLISH3B2MOHALI
 
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
gordienaysmythe
 
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
eyewaregallery
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
KamalSaini561034
 
#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
anandatalapatra
 
__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
odiliagilby
 
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.docx
clarkjanyce
 
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
herminaherman
 
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
ezzi552
 
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
archgeetsenterprises
 
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
mohammedfootwear
 

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.docx
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 
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
GordonpACKellyb
 

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

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 

Recently uploaded (20)

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 

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.