In C coding: Create 2 functions and 1 test case for each function in main() with
ID: 3826417 • Letter: I
Question
In C coding:
Create 2 functions and 1 test case for each function in main() with expected output listed in comments.
function1() - Linked List - Given a reference to a linked list:
(1) print out the number of nodes in the list,
(2) print out the positive values from the list,
(3) calculate, print out, and return the sum of the negative values from the list.
function2() - pointers - Given a pointer to the first character that is the start of a c-string (remember c-strings end with a ''):
(1) determine and print out the length of the c-string without using strlen (do not use a strlen() for this portion of the code),
(2) print out the lower case characters from the c-string,
(3) calculate, print out, and return the average number of words that contain the first letters of your name ZXCV.
Explanation / Answer
Here are the functions for the first question:
Assume the node is a structure defined as:
struct node
{
int info;
struct node *next;
};
(1) print out the number of nodes in the list,
int countNodes(struct node *head)
{
int count = 0;
while(head != NULL)
{
count++;
head = head->next;
}
return count;
}
(2) print out the positive values from the list,
void printPositives(struct node *head)
{
while(head != NULL)
{
if(head->info > 0)
printf("%d ", head->info);
head = head->next;
}
printf(" ");
}
(3) calculate, print out, and return the sum of the negative values from the list.
int sumOfNegatives(struct node *head)
{
int sum = 0;
while(head != NULL)
{
if(head->info < 0)
sum += head->info;
head = head->next;
}
printf("The sum of negative values is: %d ", sum);
return sum;
}