Week 10 Code Examples
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct node {
struct node *next;
char data;
};
// List 1: 'a' 'b' 'c' (3 nodes)
// List 2: 'd' 'e' 'f' 'g' 'h' (5 nodes)
// difference function should return 2
int list_size(struct node *head) {
int len = 0;
struct node *curr = head;
while (curr != NULL) {
len++;
curr = curr->next;
}
return len;
}
int difference(struct node *head1, struct node *head2) {
int len1 = list_size(head1);
int len2 = list_size(head2);
int diff = len1 - len2;
return abs(diff);
}
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create two linked lists from command line arguments
int dash_arg = argc - 1;
while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
dash_arg = dash_arg - 1;
}
struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);
int diff = difference(head1, head2);
printf("%d\n", diff);
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = strings[i][0];
head = n;
}
return head;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct node {
struct node *next;
int data;
};
int num_equal(struct node *head1, struct node *head2);
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create two linked lists from command line arguments
int dash_arg = argc - 1;
while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
dash_arg = dash_arg - 1;
}
struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);
int result = num_equal(head1, head2);
printf("%d equal values found\n", result);
return 0;
}
// return the number of values in the first linked list
// that are equal to the corresponding values in the
// second linked list.
// 1->5->10->3->X
// 2->5->10->X
// Returns 2
// the 5s and 10s are equal
// the 1s are not in the same position
int num_equal(struct node *head1, struct node *head2) {
int counter = 0;
struct node *curr1 = head1;
struct node *curr2 = head2;
while (curr1 != NULL && curr2 != NULL) {
if (curr1->data == curr2->data) {
counter++;
}
curr1 = curr1->next;
curr2 = curr2->next;
}
return counter;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct node {
struct node *next;
char data;
};
// List 1: 'a' 'b' 'c' (3 nodes)
// List 2: 'd' 'e' 'f' 'g' 'h' (5 nodes)
// difference function should return 2
int difference(struct node *head1, struct node *head2) {
return -1;
}
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create two linked lists from command line arguments
int dash_arg = argc - 1;
while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
dash_arg = dash_arg - 1;
}
struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);
int diff = difference(head1, head2);
printf("%d\n", diff);
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = strings[i][0];
head = n;
}
return head;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct node {
struct node *next;
int data;
};
int num_equal(struct node *head1, struct node *head2);
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create two linked lists from command line arguments
int dash_arg = argc - 1;
while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
dash_arg = dash_arg - 1;
}
struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);
int result = num_equal(head1, head2);
printf("%d equal values found\n", result);
return 0;
}
// return the number of values in the first linked list
// that are equal to the corresponding values in the
// second linked list.
// 1->5->10->3->X
// 2->5->10->1->X
// Returns 2
// the 5s and 10s are equal
// the 1s are not in the same position
int num_equal(struct node *head1, struct node *head2) {
return -1;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
// Return the number of elements divisible by 3
int count_multiples(int size, int arr[]) {
int count = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 3 == 0) {
count++;
}
}
return count;
}
int main(void) {
// Only your function is called during testing
// Any changes in this main function will not
// be used in testing
int array1[SIZE] = {9, -3, 1, 5, 12};
printf("%d are multiples of 3\n", count_multiples(SIZE, array1));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Return the number of elements that are odd at
// corresponding indexes of both arr1 and arr2
int count_odd(int size1, int arr1[], int size2, int arr2[]) {
return -42;
}
int main(void) {
// Only your function is called during testing
// Any changes in this main function will not
// be used in testing
int array1[] = {9, -3, 1, 5, 2};
int array2[] = {3, 2, -7, 6};
printf("%d odd at the same index\n", count_odd(5, array1, 4, array2));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct steps {
int number;
char direction;
};
// Return the total of steps in the given direction
int step_count(char dir, int size, struct steps array[]) {
int count = 0;
for (int i = 0; i < size; i++) {
if (array[i].direction == dir) {
count += array[i].number;
}
}
return count;
}
// This is a simple main function which could be used
// to test your step_count function.
// It will not be marked.
// Only your step_count function will be marked.
#define TEST_ARRAY_SIZE 5
int main(void) {
struct steps test_array[TEST_ARRAY_SIZE] = {
{ .number = 3, .direction = 'l'},
{ .number = 5, .direction = 'l'},
{ .number = 2, .direction = 'l'},
{ .number = 13, .direction = 'd'},
{ .number = 30, .direction = 'l'}
};
printf("%d\n", step_count('l', TEST_ARRAY_SIZE, test_array));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct initials {
char first_initial;
char last_initial;
};
// Return the total number of structs that don't
// have both initials set to capital letters
int struct_capitals(int size, struct initials array[]) {
int count = 0;
for (int i = 0; i < size; i++) {
if (array[i].first_initial < 'A' || array[i].first_initial > 'Z' ||
!isupper(array[i].last_initial)) {
count++;
}
}
return count;
}
// This is a simple main function which could be used
// to test your struct_capitals function.
// It will not be marked.
// Only your struct_capitals function will be marked.
#define TEST_ARRAY_SIZE 5
int main(void) {
struct initials test_array[TEST_ARRAY_SIZE] = {
{ .first_initial = 'A', .last_initial = 'F'},
{ .first_initial = '!', .last_initial = 'Z'},
{ .first_initial = 'Z', .last_initial = 'X'},
{ .first_initial = 'N', .last_initial = 's'},
{ .first_initial = 't', .last_initial = 'a'}
};
printf("%d\n", struct_capitals(TEST_ARRAY_SIZE, test_array));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
// Return the number of elements divisible by 3
int count_multiples(int size, int arr[]) {
return -1;
}
int main(void) {
// Only your function is called during testing
// Any changes in this main function will not
// be used in testing
int array1[SIZE] = {9, -3, 1, 5, 2};
printf("%d are multiples of 3\n", count_multiples(SIZE, array1));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Return the number of elements that are odd at
// corresponding indexes of both arr1 and arr2
int count_odd(int size1, int arr1[], int size2, int arr2[]) {
return -42;
}
int main(void) {
// Only your function is called during testing
// Any changes in this main function will not
// be used in testing
int array1[] = {9, -3, 1, 5, 2};
int array2[] = {3, 2, -7, 6};
printf("%d odd at the same index\n", count_odd(5, array1, 4, array2));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct steps {
int number;
char direction;
};
// Return the total of steps in the given direction
int step_count(char dir, int size, struct steps array[]) {
return -1;
}
// This is a simple main function which could be used
// to test your step_count function.
// It will not be marked.
// Only your step_count function will be marked.
#define TEST_ARRAY_SIZE 5
int main(void) {
struct steps test_array[TEST_ARRAY_SIZE] = {
{ .number = 3, .direction = 'l'},
{ .number = 5, .direction = 'l'},
{ .number = 2, .direction = 'u'},
{ .number = 13, .direction = 'd'},
{ .number = 30, .direction = 'l'}
};
printf("%d\n", step_count('l', TEST_ARRAY_SIZE, test_array));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct initials {
char first_initial;
char last_initial;
};
// Return the total number of structs that don't
// have both initials set to capital letters
int struct_capitals(int size, struct initials array[]) {
}
// This is a simple main function which could be used
// to test your struct_capitals function.
// It will not be marked.
// Only your struct_capitals function will be marked.
#define TEST_ARRAY_SIZE 5
int main(void) {
struct initials test_array[TEST_ARRAY_SIZE] = {
{ .first_initial = 'A', .last_initial = 'F'},
{ .first_initial = '!', .last_initial = 'Z'},
{ .first_initial = 'Z', .last_initial = 'X'},
{ .first_initial = 'N', .last_initial = 's'},
{ .first_initial = 't', .last_initial = 'a'}
};
printf("%d\n", struct_capitals(TEST_ARRAY_SIZE, test_array));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct node {
struct node *next;
int data;
};
// Delete the first instance of a duplicate in the given list
// 1->3->5->3->1->X
// should return
// 1->3->5->3->X
struct node *delete_duplicate(struct node *head) {
struct node *curr = head;
while (curr != NULL) {
//check if there is a duplicate of curr->data
//and delete it
struct node *scan = curr->next;
struct node *prev_scan = curr;
while (scan != NULL) {
if (curr->data == scan->data) {
// I want to delete it
prev_scan->next = scan->next;
free(scan);
scan = prev_scan->next;
} else {
prev_scan = scan;
scan = scan->next;
}
}
curr = curr->next;
}
return head;
}
// DO NOT CHANGE CODE BELOW
struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);
int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = NULL;
if (argc > 1) {
// list has elements
head = strings_to_list(argc - 1, &argv[1]);
}
struct node *new_head = delete_duplicate(head);
print_list(new_head);
return 0;
}
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
int i = len - 1;
while (i >= 0) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
i -= 1;
}
return head;
}
// print linked list
void print_list(struct node *head) {
printf("[");
struct node *n = head;
while (n != NULL) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
n = n->next;
}
printf("]\n");
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct node {
struct node *next;
char data;
};
// duplicate should duplicate every node in the list, inserting the new
// node after the original node
// a z c d d..... a a z z c c d d d d
struct node *duplicate(struct node *head) {
struct node *curr = head;
while (curr != NULL) {
struct node *new_node = malloc(sizeof(struct node));
new_node->data = curr->data;
new_node->next = curr->next;
curr->next = new_node;
curr = curr->next->next;
}
return head;
}
////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE THE CODE BELOW //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, &argv[1]);
// If you're getting an error here,
// you have returned an uninitialized value
struct node *new_head = duplicate(head);
print_list(new_head);
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof(struct node));
assert(n != NULL);
n->next = head;
n->data = strings[i][0];
head = n;
}
return head;
}
// print linked list
void print_list(struct node *head) {
printf("[");
for (struct node *n = head; n != NULL; n = n->next) {
// If you're getting an error here,
// you have returned an invalid list
printf("%c", n->data);
if (n->next != NULL) {
printf(", ");
}
}
printf("]\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct node {
struct node *next;
int data;
};
// Delete the first instance of a duplicate in the given list
// 1->3->5->3->1->X
// should return
// 1->3->5->3->X
struct node *delete_duplicate(struct node *head) {
return NULL;
}
// DO NOT CHANGE CODE BELOW
struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);
int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = NULL;
if (argc > 1) {
// list has elements
head = strings_to_list(argc - 1, &argv[1]);
}
struct node *new_head = delete_duplicate(head);
print_list(new_head);
return 0;
}
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
int i = len - 1;
while (i >= 0) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
i -= 1;
}
return head;
}
// print linked list
void print_list(struct node *head) {
printf("[");
struct node *n = head;
while (n != NULL) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
n = n->next;
}
printf("]\n");
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct node {
struct node *next;
char data;
};
// duplicate should duplicate every node in the list, inserting the new
// node after the original node
// a z c d d..... a a z z c c d d d d
struct node *duplicate(struct node *head) {
return head;
}
////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE THE CODE BELOW //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, &argv[1]);
// If you're getting an error here,
// you have returned an uninitialized value
struct node *new_head = duplicate(head);
print_list(new_head);
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof(struct node));
assert(n != NULL);
n->next = head;
n->data = strings[i][0];
head = n;
}
return head;
}
// print linked list
void print_list(struct node *head) {
printf("[");
for (struct node *n = head; n != NULL; n = n->next) {
// If you're getting an error here,
// you have returned an invalid list
printf("%c", n->data);
if (n->next != NULL) {
printf(", ");
}
}
printf("]\n");
}
#include <stdio.h>
#include <stdlib.h>
#define NUM_ROW 4
#define NUM_COL 4
// sum_min should return the sum of
// the minimum values in each row of the array
int min_row_sum(int size, int array[NUM_ROW][NUM_COL]) {
int total_sum = 0;
for (int row = 0; row < size; row++) {
//find the minimum of this row
int min = array[row][0];
for (int col = 0; col < size; col++) {
if (array[row][col] < min){
min = array[row][col];
}
}
//add the minimum of that row to the total
total_sum += min;
}
return total_sum;
}
// This is a simple main function which could be used
// to test your sum_min function.
// It will not be marked.
// Only your sum_min function will be marked.
int main(void) {
int test_array[NUM_ROW][NUM_COL] = {
{1, 2, 3, 4}, {4, 2, 2, 0}, {9, 9, 9, 9}, {5, 2, 5, 5}};
int result = min_row_sum(NUM_COL, test_array);
printf("%d\n", result);
return 0;
}
#include <stdio.h>
#define MAX_SIZE 10000
// reads integers into an array from terminal until a number is entered
// which when multiplied by another number previously entered
// results in 56.
// ./array_multiplied
// 2
// 3
// 28
// 28 * 2 = 56
// ./array_multiplied
// 99
// 4
// 1
// 7
// 56
// 56 * 1 = 56
int main(void) {
int numbers[MAX_SIZE];
int i = 0;
while (scanf("%d", &numbers[i]) == 1) {
// looks through all previous values
for (int data_index = 0; data_index < i; data_index++) {
if(numbers[data_index] * numbers[i] == 56) {
printf("%d * %d = 56\n", numbers[i], numbers[data_index]);
return 0;
}
}
i++;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define NUM_ROW 4
#define NUM_COL 4
// sum_min should return the sum of
// the minimum values in each row of the array
int min_row_sum(int size, int array[NUM_ROW][NUM_COL]) {
return 42;
}
// This is a simple main function which could be used
// to test your sum_min function.
// It will not be marked.
// Only your sum_min function will be marked.
int main(void) {
int test_array[NUM_ROW][NUM_COL] = {
{1, 2, 3, 4}, {4, 2, 2, 0}, {9, 9, 9, 9}, {5, 2, 5, 5}};
int result = min_row_sum(NUM_COL, test_array);
printf("%d\n", result);
return 0;
}
#include <stdio.h>
#define MAX_SIZE 10000
// reads integers into an array from terminal until a number is entered
// which when multiplied by another number previously entered
// results in 56.
// ./array_multiplied
// 2
// 3
// 28
// 28 * 2 = 56
// ./array_multiplied
// 99
// 4
// 1
// 7
// 56
// 56 * 1 = 56
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct node {
struct node *next;
int data;
};
// You want to traverse a linked list and
// find the largest number in it
// For example, 20, 4, 3, 6
// Would return 3
// return -1 if list is empty
int smallest(struct node *head) {
int min;
if (head == NULL) {
min = -1;
}
int min = head->data;
struct node *curr = head;
while (curr->next != NULL) {
if (head->data < min) {
min = head->data;
curr = curr->next;
}
}
return min;
}
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, &argv[1]);
printf("%d\n", smallest(head));
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
// INSERT CODE IN HERE
return 0;
}
// The following code is meant to take two string inputs from the user and
// concatenate them together. However, there are a number of mistakes in the
// code that are causing it to not work as intended. Can you find and fix the
// errors? Good luck!
// YOU MAY NOT USE strcat
#include <stdio.h>
#include <string.h>
void strip_newline(char *string);
int main(void) {
char str1[50];
char str2[50];
char concat[100];
int length1;
int length2;
printf("Enter the first string: ");
fgets(str1, 50);
strip_newline(str1);
printf("Enter the second string: ");
fgets(str1, 50);
strip_newline(str2);
length1 = strlen(str1);
length2 = strlen(str2);
if (lenth1 + lengh2 >= 100) {
printf("Error: Strings are too long to concatenate\n");
return 1;
}
for (int i = 0; i <= len1 + len2; i++) {
concat[i] = str1[i];
}
for (int i = len1; i <= len2; i++) {
concat[i] = str2[i];
}
printf("%c\n", concat[0]);
return 0;
}
// Removes newline from end of the given string if it exists
// NOTE: This function is correct as given.
// You do not need to modify it for this question.
void strip_newline(char *string) {
int length = strlen(string);
if (length != 0 && string[length - 1] == '\n') {
string[length - 1] = '\0';
}
}
// The following code is meant to take two string inputs from the user and
// concatenate them together. However, there are a number of mistakes in the
// code that are causing it to not work as intended. Can you find and fix the
// errors? Good luck!
// YOU MAY NOT USE strcat
#include <stdio.h>
#include <string.h>
void strip_newline(char *string);
int main(void) {
char str1[50];
char str2[50];
char concat[100];
int length1;
int length2;
printf("Enter the first string: ");
fgets(str1, 50);
strip_newline(str1);
printf("Enter the second string: ");
fgets(str1, 50);
strip_newline(str2);
length1 = strlen(str1);
length2 = strlen(str2);
if (lenth1 + lengh2 >= 100) {
printf("Error: Strings are too long to concatenate\n");
return 1;
}
for (int i = 0; i <= len1 + len2; i++) {
concat[i] = str1[i];
}
for (int i = len1; i <= len2; i++) {
concat[i] = str2[i];
}
printf("%c\n", concat[0]);
return 0;
}
// Removes newline from end of the given string if it exists
// NOTE: This function is correct as given.
// You do not need to modify it for this question.
void strip_newline(char *string) {
int length = strlen(string);
if (length != 0 && string[length - 1] == '\n') {
string[length - 1] = '\0';
}
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct node {
struct node *next;
int data;
};
// You want to traverse a linked list and
// find the largest number in it
// For example, 3, 4, 5, 6
// Would return 6
int largest(struct node *head) {
return 42;
}
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, &argv[1]);
printf("%d\n", largest(head));
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}
/*Problem 1:
find_range takes one parameter: a pointer to the head of
the list to search through. find_range should return a single integer:
the difference between the smallest value and the largest value in the list.
For example, if the list contained:
3->4->5->6->X
find_range should return 3, as 6 - 3 = 3
Testing
list_find_range.c</b> also contains a <b>main</b> function which
allows you to test your <b>find_range</b> function.
This main function:
converts the command-line arguments to a linked list
assigns a pointer to the first node in the linked list to head
calls find_range(head)
prints the result.
Do not change this main function. If you want to change it, you have
misread the question.
Your find_range function will be called directly in marking.
The main function is only to let you test your find_range function
Here is how the main function allows you to test find_range:
dcc list_find_range.c -o list_find_range
./list_find_range 3 4 5
The range is: 2
./list_find_range 10 9 8 7 6 5 4 3 2 1
The range is: 9
./list_find_range
The list is empty!
Assumptions/Restrictions/Clarifications.
find_range should return only a single integer: the range of the list.
find_range should not change the linked list it is given.
find_range should not change the next or data fields of list nodes.
find_range should not use arrays.
find_range should not call malloc.
find_range should not call scanf (or getchar or fgets).
find_range should not print anything. It should not call printf.
Do not change the definition of struct node.
Do not change the supplied main function. It will not be tested or marked.
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
// Do not change these #defines, or your program will fail the autotests!
#define EMPTY_LIST -42
struct node {
struct node *next;
int data;
};
// find_range should return the range of the list, or
// EMPTY_LIST if the list is empty.
int find_range(struct node *head) {
return 42;
}
////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE THE CODE BELOW //
////////////////////////////////////////////////////////////////////////
int find_range(struct node *head);
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create linked list from command line arguments
struct node *head = strings_to_list(argc - 1, &argv[1]);
// If you're getting an error here,
// you have returned an uninitialized value
int range = find_range(head);
if (range == EMPTY_LIST) {
printf("The list is empty!\n");
} else {
printf("The range is: %d\n", range);
}
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct node {
struct node *next;
int data;
};
// Given two linked lists,
// count the number of even numbers in both linked lists
// and return the difference (should be the absolute value).
// For example:
// 2->1->4->X
// 1->2->3->4->100->X
// Would print out
// 100
int even_diff(struct node *head1, struct node *head2) {
// PUT YOUR CODE HERE
return -42;
}
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create two linked lists from command line arguments
int dash_arg = argc - 1;
while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
dash_arg = dash_arg - 1;
}
struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);
printf("%d\n", even_diff(head1, head2));
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}
// To find elements in list 6->1->7->12->NULL that are divisible by 3 run like:
// ./list_find_divisible 3 6 1 7 12
// 2
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
struct node {
struct node *next;
int data;
};
// find_divisible should return the number of items in the list
// divisible by the given value
int find_divisible(int value, struct node *head) {
return 42;
}
////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE THE CODE BELOW //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
if (argc == 1) {
printf("Usage %s value list_data\n", argv[0]);
return 0;
}
// create linked list from command line arguments
int value = atoi(argv[1]);
struct node *head = strings_to_list(argc - 2, &argv[2]);
// If you're getting an error here,
// you have returned an uninitialized value
printf("%d\n", find_divisible(value, head));
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}
// print linked list
void print_list(struct node *head) {
printf("[");
for (struct node *n = head; n != NULL; n = n->next) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
}
printf("]\n");
}
// An isogram is a word, in which no letter of the alphabet
// occurs more than once. Write a C program that reads in
// strings until Ctrl+D, and checks whether the string is an isogram.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
// Sample input/output
// hello
// This is not an isogram
// table
// This is an isogram
// Yay
// This is not an isogram
// CtrlD
// Write a C program that reads in words from standard input until Ctrl+D,
// and checks whether a word read in as a command line argument is a substring
// in the word. If so, then the word that was read in should be printed again
// Assume max length of strings read is is 100 characters
// $./array_substring courage age bloom encourage
// blooming
// blooming
// potato
// encourage
// encourage
// rage
// rage
// egg
#include <stdio.h>
#include <string.h>
#define MAX 100
#include <stdio.h>
#include <stdlib.h>
// max_subarray_sum should return the largest contiguous sum
// of a subarray in a given array
// eg
// For an array with elements 8, 3, 5, 4, -1, 3, -5, -4
// you would get 22 from elements 8 + 3 + 5 + 4 + -1 + 3
int max_subarray_sum(int size, int array[]) {
return 42;
}
// This is a simple main function which could be used
// to test your sum_cont_subarray function.
// It will not be marked.
// Only your sum_cont_subarray function will be marked.
int main(void) {
int test_array[] = {8, 3, 5, 4, -8, -3, -5, -4};
int max_sum = max_subarray_sum(8, test_array);
printf("%d\n", max_sum);
return 0;
}
// Read integers until a negative integer is read
// and then print odd and even integers on separate lines
// Assume a max of 1000 integers will be read before the first negative input
// e.g
// $ ./array_odd_even
// 1
// 2
// 3
// 2
// -42
// Odd numbers were: 1 3
// Even numbers were: 2 2
#include <stdio.h>
#define MAX_NUMBERS 1000
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
struct node {
struct node *next;
int data;
};
// delete_div should delete the first node that is divisible by the given value
// If there are no nodes that are divisible by the given value, it should return
// the list unchanged.
struct node *delete_div(int value, struct node *head) {
return NULL;
}
////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE THE CODE BELOW //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);
// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
if (argc == 1) {
printf("Usage %s value list_data\n", argv[0]);
return 0;
}
// create linked list from command line arguments
int value = atoi(argv[1]);
struct node *head = strings_to_list(argc - 2, &argv[2]);
// If you're getting an error here,
// you have returned an uninitialized value
struct node *new_head = delete_div(value, head);
print_list(new_head);
return 0;
}
// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}
// print linked list
void print_list(struct node *head) {
printf("[");
for (struct node *n = head; n != NULL; n = n->next) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
}
printf("]\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct node {
struct node *next;
int data;
};
// Insert a new node into a sorted linked list, maintaining
// the sorted order
// 3 7 10 16 (insert 8)..... 3 7 8 10 16
struct node *insert_sorted(struct node *head, int number) {
// PUT YOUR CODE HERE, REMEMBER TO CHANGE
// THE RETURN
return NULL;
}
// DO NOT CHANGE THE CODE BELOW
struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);
int main(int argc, char *argv[]) {
if (argc == 1) {
printf("Usage %s value list_data\n", argv[0]);
return 0;
}
// create linked list from command line arguments
int value = atoi(argv[1]);
struct node *head = strings_to_list(argc - 2, &argv[2]);
struct node *new_head = insert_sorted(head, value);
print_list(new_head);
return 0;
}
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
int i = len - 1;
while (i >= 0) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
i -= 1;
}
return head;
}
// print linked list
void print_list(struct node *head) {
printf("[");
struct node *n = head;
while (n != NULL) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
n = n->next;
}
printf("]\n");
}