#include<stdio.h>#include<string.h>#include<stdlib.h>#include<assert.h>structnode{structnode*next;intdata;};intproduct(structnode*head1,structnode*head2);structnode*strings_to_list(intlen,char*strings[]);intmain(intargc,char*argv[]){// create two linked lists from command line argumentsintdash_arg=argc-1;while(dash_arg>0&&strcmp(argv[dash_arg],"-")!=0){dash_arg=dash_arg-1;}structnode*head1=strings_to_list(dash_arg-1,&argv[1]);structnode*head2=strings_to_list(argc-dash_arg-1,&argv[dash_arg+1]);intresult=product(head1,head2);printf("%d\n",result);return0;}// product should the sum of the elements in list1 multiplied by// the corresponding element in list2// if one list is longer than the other, the extra list elements are ignoredintproduct(structnode*head1,structnode*head2){if(!head1||!head2){return0;}else{return(head1->data*head2->data)+product(head1->next,head2->next);}}// create linked list from array of stringsstructnode*strings_to_list(intlen,char*strings[]){structnode*head=NULL;for(inti=len-1;i>=0;i=i-1){structnode*n=malloc(sizeof(structnode));assert(n!=NULL);n->next=head;n->data=atoi(strings[i]);head=n;}returnhead;}
Q2
#include<stdio.h>#include<stdlib.h>#include<string.h>enumroom_type{LECTURE,SEMINAR,LABORATORY};structroom{intcapacity;enumroom_typetype;};// Return the average capacity of all seminar roomsintaverage_capacity_room(intsize,structroomrooms[size]){inti=0;intnumber_of_seminar=0;inttotal=0;while(i<size){if(rooms[i].type==SEMINAR){total+=rooms[i].capacity;number_of_seminar++;}i++;}if(number_of_seminar==0){return0;}returntotal/number_of_seminar;}// This is a simple main function which could be used// to test your average_capacity_room function.// It will not be marked.// Only your average_capacity_room function will be marked.#define TEST_ARRAY_SIZE 6intmain(void){structroomtest_array[TEST_ARRAY_SIZE]={{.capacity=10,.type=LECTURE},{.capacity=25,.type=SEMINAR},{.capacity=30,.type=LABORATORY},{.capacity=40,.type=SEMINAR},{.capacity=50,.type=LECTURE},{.capacity=60,.type=LABORATORY}};printf("%d\n",average_capacity_room(TEST_ARRAY_SIZE,test_array));return0;}
Q3
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<assert.h>structnode{structnode*next;intdata;};intmixed(structnode*head);intmember(inti,structnode*head);structnode*strings_to_list(intlen,char*strings[]);intmain(intargc,char*argv[]){// create linked list from command line argumentsstructnode*head=strings_to_list(argc-1,&argv[1]);intresult=mixed(head);printf("%d\n",result);return0;}// mixed should return 1 if list contains both even and odd numbers// mixed should return 0 otherwiseintmixed(structnode*head){if(!head||!head->next){return0;}if((head->data%2)!=(head->next->data%2)){return1;}else{returnmixed(head->next);}}// create linked list from array of stringsstructnode*strings_to_list(intlen,char*strings[]){structnode*head=NULL;for(inti=len-1;i>=0;i=i-1){structnode*n=malloc(sizeof(structnode));assert(n!=NULL);n->next=head;n->data=atoi(strings[i]);head=n;}returnhead;}
Q4
#include<stdio.h>// Do not change this #define, or your program will fail the autotests!#define NO_TRIPLE -1// find_triple should find and return the value in the array that// occurs three times, if one exists.// If no values occur three times, it should return NO_TRIPLE.intfind_triple(intlength,intarray[]){for(inti=0;i<length;i++){intcount=0;intk=-1;for(intj=0;j<length;j++){if(array[i]==array[j]){count++;k=i;}}if(count==3){returnarray[k];}}returnNO_TRIPLE;}#define TEST_ARRAY_SIZE 8// This is a simple main function which could be used// to test your find_triple function.// It will not be marked.// Only your find_triple function will be marked.intmain(void){inttest_array[TEST_ARRAY_SIZE]={3,1,4,1,5,9,2,6};intresult=find_triple(TEST_ARRAY_SIZE,test_array);printf("%d\n",result);return0;}
#include<stdio.h>#define MAX_SIZE 100intmain(void){intscanned_length;intlength_count=0;intscanned_value;intscanned_num[MAX_SIZE];// Prompt user for the specific number of integers to be taken from inputprintf("How many numbers: ");scanf("%d",&scanned_length);// Take in scanned_length number of integersprintf("Please enter numbers: ");while(length_count<scanned_length){scanf("%d",&scanned_value);scanned_num[length_count]=scanned_value;length_count++;}// Iterate over the array and print the minimum and maximum values in the arrayintindex=0;if(scanned_length>0){intminimum=scanned_num[index];intmaximum=scanned_num[index];while(index<scanned_length){if(scanned_num[index]<minimum){minimum=scanned_num[index];}elseif(scanned_num[index]>maximum){maximum=scanned_num[index];}index++;}printf("Minimum: %d\nMaximum: %d\n",minimum,maximum);}return0;}
Q7
#include<stdio.h>structtime{intdays;inthours;intminutes;};voidincrement_time(structtime*current_time);intmain(void){structtimecurrent_time;current_time.days=3;current_time.hours=4;current_time.minutes=59;printf("One minute ago: %d days, %d hours and %d minutes\n",current_time.days,current_time.hours,current_time.minutes);increment_time(¤t_time);printf("Now: %d days, %d hours and %d minutes\n",current_time.days,current_time.hours,current_time.minutes);return0;}// increments the time by 1 minutevoidincrement_time(structtime*current_time){current_time->minutes++;if(current_time->minutes==60){current_time->minutes=0;current_time->hours++;}if(current_time->hours==24){current_time->hours=0;current_time->days++;}}
Q8
#include<stdio.h>#include<stdlib.h>#include<assert.h>#define MAX_LIST_LEN 100structnode{intdata;structnode*next;};structnode*delete_last(structnode*head);voidprint_list(structnode*head);structnode*array_to_list(intlen,intarray[]);// DO NOT CHANGE THE MAIN FUNCTIONintmain(void){// Get list sizeintlist_size;printf("Total numbers: ");scanf(" %d",&list_size);// Read in numbersintlist[MAX_LIST_LEN]={0};intindex_count=0;while(index_count<list_size&&scanf(" %d",&list[index_count])){index_count++;}// Create linked list from input numbersstructnode*head=NULL;if(index_count>0){// List has elementshead=array_to_list(list_size,list);}structnode*new_head=delete_last(head);print_list(new_head);return0;}// Delete the last element from a given linked liststructnode*delete_last(structnode*head){if(head==NULL){returnhead;}structnode*prev=NULL;structnode*curr=head;while(curr->next!=NULL){prev=curr;curr=curr->next;}if(prev==NULL){free(curr);returnNULL;}prev->next=curr->next;free(curr);returnhead;}// DO NOT CHANGE THIS FUNCTION// prints the given linked listvoidprint_list(structnode*head){structnode*curr=head;while(curr!=NULL){printf("%d -> ",curr->data);curr=curr->next;}printf("X\n");}// DO NOT CHANGE THIS FUNCTION// create linked list from array of stringsstructnode*array_to_list(intlen,intarray[]){structnode*head=NULL;inti=len-1;while(i>=0){structnode*n=malloc(sizeof(structnode));assert(n!=NULL);n->next=head;n->data=array[i];head=n;i-=1;}returnhead;}
#include<stdio.h>#include<stdlib.h>#include<assert.h>#include<stdbool.h>#define MAX_LEN 4096structcard{intnum;structcard*next;};structcard*deck_read(intplayer_num);voidprint_deck(structcard*deck);intmain(void){structcard*player1=deck_read(1);structcard*player2=deck_read(2);structcard*pile=NULL;structcard*curr=player1;boolhas_snapped=false;while(player1&&player2&&!has_snapped){structcard*popped=curr;if(curr==player1){player1=player1->next;curr=player2;}else{player2=player2->next;curr=player1;}popped->next=pile;pile=popped;if(pile&&pile->next&&pile->num==pile->next->num){printf("Snap! Matched card %d\n",pile->num);has_snapped=true;}}if(!has_snapped){if(!player1){printf("Player 2 has won!\n");}elseif(!player2){printf("Player 1 has won!\n");}}printf("Player 1's deck: ");print_deck(player1);printf("Player 2's deck: ");print_deck(player2);printf("Pile: ");print_deck(pile);return0;}voidprint_deck(structcard*deck){while(deck!=NULL){printf("%d -> ",deck->num);deck=deck->next;}printf("X\n");}// Function to create a deck for the specified player.// Returns a pointer to the top card in the deck.structcard*deck_read(intplayer_num){intnum;printf("Enter Player %d's deck values:\n",player_num);structcard*head=NULL;structcard*tail=NULL;while(scanf("%d",&num)==1){if(num==-1)break;structcard*new_card=malloc(sizeof(structcard));new_card->num=num;new_card->next=NULL;if(head==NULL){head=tail=new_card;}else{tail=tail->next=new_card;}}returnhead;}