Please note that no resources will be available during the exam. You will be using the CSE exam environment. During the exam, you will not have access to your CSE account, the internet, etc.
Only the following cheat sheet will be available during the exam:
Note: This part will have approximately 4 to 6 short answer questions. The mark for each question may vary.
Type your answer in the text file provided, save and submit the file. All short written responses will be submitted in a text file, you don't need to draw any diagrams. Follow the instructions provided in the exam to submit your answers for this part.
Briefly discuss the concept of short-circuit evaluation for the C logical operators || and &&. Why is this feature useful? Give one example. Properly explain and justify your answer in the provided file P1Q1.txt, and save the file.
You need to submit the required file (P1Q1.txt) using the command provided in the exam.
Consider the following:
float ff[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6}; float *fp = ff;
What are the similarities between ff and fp? What are the differences?
You need to submit the required file (P1Q2.txt) using the command provided in the exam.
Consider the following code:
#define SIZE 5 int *f1(void) { int nums[SIZE] = {1, 2, 3, 4, 5}; return nums; } int *f2(void) { int * nums = malloc(sizeof(int) * SIZE); int i = 0; while(i < SIZE){ nums[i] = i + 1; i++; } return nums; }
What are the differences between f1 and f2?
Brielfy explain and justify your answer in the provided file P1Q3.txt, and save the file.
You need to submit the required file (P1Q3.txt) using the command provided in the exam.
Refer to: Tutorial 04, Q19 and Q20
Please properly explain and justify your answer in the provided file P2Q4.txt, and save the file.
You need to submit the required file (P1Q4.txt) using the command provided in the exam.
Refer to: Tutorial 09, Q04
Please properly explain and justify your answer in the provided file P2Q5.txt, and save the file.
You need to submit the required file (P1Q5.txt) using the command provided in the exam.
Note: This part will have approximately 4 to 6 programming questions. The mark for each question may vary.
Write a C program q1.c that is given the name of a file as a command-line argument.
Your program should print the file with
all non-digit or non-whitespace character
replaced with an _
(underscore).
The ctype.h library may be useful.
dcc q1.c -o q1 ./q1 example.c ________ _________ ________ __________ ________ __________ ___ ________ _____ ____ _______ _ ___________ __ 3__ ___ _ _ __________1___ ___ _ _ __________2___ ________ _ ___ ___ __ ___ __ _ __ _ __ __ ____ _ ______________ _ _ 2__ _ ______ 0_ _ ./q1 preface.txt ___ _______ _________ _____ __ _________ _____ ______ __ _____ _____ ___ _______ _____ ____ _____ __ ___ ___ ___ __ ______ ________ __ ___ ______ ______ ___ ____ _____ _____ __ ___ _____ __ __ ____ ___ ____ ______ __ ____________ ___________ ___ ___ ____ ___ ____ __ ____ __ ______ __ _____ ___ _____ __ ___ _______ _________ _______ ________ ____ ____ _____ __ ______ __ __________________ __ ___ ___ ___ _______ __ ___ ______ _______ ___ ____ ____ __ _____ ___ ____ __ ___ _______ _____ ___ ___ _______ ______ _____ ____ ______ ______ _________ _____ _____ _______ _____ _____ ___ _______ _____ ____________ _____ ______ ___ ______ ________ _______ _____ ______ 2001 ______ _2591_ _____ ________ ________ ____ 28_ 2021_ _________ _______ _________ ___ _________ ____8 ________ ___ ____ ________ ____ ________ _____ ___ _____ ______ ___ _____ __ ___ _______ _________ _____ _________ _____ _____ ___
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <ctype.h> int main(int argc, char **argv) { assert(argc == 2); FILE *infile = fopen(argv[1], "r"); assert(infile); int c; while ((c = fgetc(infile)) != EOF) { if (isdigit(c) || isspace(c)) putchar(c); else putchar('_'); } fclose(infile); return EXIT_SUCCESS; }
You have been given the file q2.h
that contains
the definition of a Pet
struct.
q2.h
also contains a function prototype for the function
findOldest()
In q2.c
add code to the function findOldest()
so that,
given an array of Pet
structs,
the length of the array,
and a string containing a pet type,
the function returns a pointer to the oldest pet of that type in the array.
You have been given a q2.main.c
that contains a main function
to help you test your code.
You do not need to understand the code in q2.main.c
.
You should not modify the code in q2.main.c
or q2.h
.
You can assume that no two pets in the array of the same type have the same age.
If there are no pets of the given type in the array,
the function should return NULL
.
dcc q2.c q2.main.c -o q2 ./q2 dog 'dog|Murphy|3' 'cat|Izzy|2' 'dog|Blacky|9' The oldest dog is Blacky ./q2 dog 'dog|Murphy|3' 'dog|Bella|5' 'dog|Luna|2' 'dog|Ollie|7' 'dog|Abby|4' The oldest dog is Ollie ./q2 cat 'dog|Murphy|3' 'cat|Izzy|2' 'dog|Remi|7' 'cat|Pepper|5' 'cat|Bandit|3' The oldest cat is Pepper ./q2 bird 'dog|Murphy|3' 'cat|Izzy|2' 'dog|Remi|7' 'cat|Pepper|5' 'cat|Bandit|3' No birds in the list
#include <string.h> #include "q3.h" Pet *findOldest(Pet *pets, int numPets, char *type) { Pet *oldestSoFar = NULL; for (int i = 0; i < numPets; i++) { if (strcmp(pets[i].type, type) == 0) { if (oldestSoFar == NULL || pets[i].age > oldestSoFar->age) { oldestSoFar = &pets[i]; } } } return oldestSoFar; }
One way to enforce that a mathematical expression is evaluated correctly is to use parenthesis.
Adding a pair of parenthesis around an operator forces the evaluation of that operator first.
For example, the expression (2 + 3) * 4 is not equivalent to 2 + 3 * 4.
But using more than one pair of parenthesis around an operator is redundant, as it doesn't change the evaluation order.
For example, the expression ((2 + 3)) * 4 is equivalent to (2 + 3) * 4.
Write a C program q3.c that, for each line of input, determines whether the expression has redundant parenthesis.
If the expression has redundant parenthesis, your program should print the message Redundant Parenthesis.
You can assume that no line of input is more than 4096 characters long.
Note that the input doesn't necessarily have to be a mathematical expression, it could be any string.
You have been provided a stack implementation in Stack.h and Stack.c.
One algorithm for solving this problem is to remove pairs of parenthesis from the inside out.
If there are are any pairs of parenthesis that have no characters inside them, then the expression has redundant parenthesis.
For example, the expression ((2 + 3)) * 4.
After removing the first pair of parenthesis, we get () * 4.
As this has a pair of parenthesis with no characters inside them, the expression has redundant parenthesis.
Note that any string that initally contains a pair of empty parenthesis will have redundant parenthesis.
dcc q3.c Stack.c -o q3 ./q3 ((2 + 3)) * 4 Redundant Parenthesis () Redundant Parenthesis ((x+y)+((z))) Redundant Parenthesis (2 + 3) * 4 ((x+y)+(z)) Hello World (((Hello World))) Redundant Parenthesis CTRL + D
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include "Stack.h" bool hasRedundantParenthesis(char *string); int main(void) { char buffer[BUFSIZ]; while (fgets(buffer, BUFSIZ, stdin) != NULL) { if (hasRedundantParenthesis(buffer)) { printf("Redundant Parenthesis\n"); } } return EXIT_SUCCESS; } bool hasRedundantParenthesis(char *string) { Stack *stack = stackCreate(); for (int i = 0; string[i] != '\0'; i++) { if (string[i] != ')') { stackPush(stack, string[i]); } else { if (stackTop(stack) == '(') { stackDestroy(stack); return true; } else { while (stackPop(stack) != '('); } } } stackDestroy(stack); return false; }
Refer to: Tutorial 08, Q05
You need to submit the required C programming file(s) using the command provided in the exam.
You will be given a few basic tests to help you in completing the task. However you need to properly test your answer before submitting your work.
Refer to: Tutorial 04, Q08
You need to submit the required C programming file(s) using the command provided in the exam.
You will be given a few basic tests to help you in completing the task. However you need to properly test your answer before submitting your work.
-- End --