c++编程调试、program编程讲解、讲解c++程序设计

- 首页 >> Matlab编程
Practical Questions
#include
#define STOP_NUMBER 100
int main(int argc, char *argv[]) {
int monkey = 1;
int i = 0;
while (i < STOP_NUMBER) {
monkey += i;
i++;
}
printf("Total is: %d\n", monkey);
}
Which of the following Code Style Issues are present in this code?
A: Inconsistent Indentation
B: Bad Variable name(s)
C: Overly Deep Nesting
D: Lack of use of constants
Enter the answer(s) in the file prac_mc.txt, after the [q2], inside the curly brackets.
This question can have more than one answer and a correct answer will identify all the issues in the code.
For example, if you think that A and C are the correct answers, your answer will be: AC
If you think that D alone is the correct answer, your answer will be: D
Short Answer Question 3 (1 mark)
The file part1_q3.c contains this C Code:
#include
#include
struct node {
int data;
struct node *next;
};
int main(void) {
struct node *head = malloc();
}
What code should replace ?
Enter as your answer as the exact text you would write in this program.
Enter this answer in the file prac_mc.txt, after the [q3], inside the curly brackets.
Do not enter any extra characters. Do not write \n for a newline character. Do not enter any explanation.
You can check your answers are in the correct format with 1091 autotest-pracexam prac_mc
You can submit the question with give dp1091 prac_mc prac_mc.txt
There will be 20 Short Answer questions in the exam. See the lab exercise for more information.
Question 1
Passing this question, or question 3, is sufficient to pass the Arrays Hurdle.
Your task is to add code to this function:
// Return the maximum sum of a row in the 2D array.
int max_row_sum(int array[TEST_ARRAY_SIZE][TEST_ARRAY_SIZE], int side_length) {
// PUT YOUR CODE HERE (you must change the next line!)
return 42;
}
Add code so that max_row_sum finds the row in the square two dimensional array with the highest sum, and returns that
value. You are guaranteed the array will only contain positive numbers.
For example if the array is a 3 height by 3 width array
6, 7, 8,
1, 1, 9,
3, 2, 8
Your function should return 21 because:
6 + 7 + 8 == 21
1 + 1 + 9 == 11
3 + 2 + 8 == 13
As you can see, the largest row sum is 21.
Testing
prac_q1.c also contains a simple main function which allows you to test your max_row_sum function.
Your max_row_sum function will be called directly in marking. The main function is only to let you test your max_row_sum
function
Assumptions/Restrictions/Clarifications.
max_row_sum should return a single integer.
max_row_sum should not change the array it is given.
max_row_sum should not call scanf (or getchar or fgets).
max_row_sum can assume the array contains at least one integer.
max_row_sum function should not print anything. It should not call printf.
Your submitted file may contain a main function. It will not be tested or marked.
You can autotest this code with 1091 autotest-pracexam prac_q1
You can submit this code with give dp1091 prac_q1 prac_q1.c
You can check your submission has been accepted with 1091 classrun -check prac_q1
You can see your previous autotests here
Question 2
Passing this question, or question 4, is sufficient to pass the Linked Lists Hurdle.
Note prac_q2.c uses the following familiar data type:
struct node {
struct node *next;
int data;
};
count_last is given one argument, head, which is the pointer to the first node in a linked list. You are guaranteed the list will
not be empty.
Add code to count_last so that its returns the number of values which are the same as the last value in the list.
For example if the linked list contains these 8 values:
16, 12, 8, 12, 13, 19, 21, 12
count_last should return 3, because 12 is the last value, and 12 occurs 3 times in the list (including the last number).
Testing
prac_q2.c also contains a main function which allows you to test your count_last function.
This main function:
Question 3
Passing this question, or question 1, is sufficient to pass the Arrays Hurdle.
converts the command-line arguments to a linked list
assigns a pointer to the first node in the linked list to head
calls count_last(head)
prints the result.
Do not change this main function. If you want to change it, you have misread the question.
Your count_last function will be called directly in marking. The main function is only to let you test your count_last function
Here is how you use main function allows you to test count_last:
$ cp -n prac_q2.c .
$ dcc prac_q2.c -o prac_q2
$ ./prac_q2 16 12 8 12 13 19 21 12
3
$ ./prac_q2 2 4 6 2 4 6
2
$ ./prac_q2 3 5 7 11 13 15 17 19 23 29
1
$ ./prac_q2 2 2 2 3 2
4
Assumptions/Restrictions/Clarifications.
count_last will never receive a linked list with no nodes. That is, the head will never be NULL
count_last should return a single integer.
count_last should not change the linked list it is given. Your function should not change the next or data fields of list nodes.
count_last should not use arrays.
count_last should not call malloc.
count_last should not call scanf (or getchar or fgets).
count_last should not print anything. It should not call printf.
Do not change the supplied main function. It will not be tested or marked.
You can autotest this code with 1091 autotest-pracexam prac_q2
You can submit this code with give dp1091 prac_q2 prac_q2.c
You can check your submission has been accepted with 1091 classrun -check prac_q2
You can see your previous autotests here
In the final exam, question 3 will be an arrays question, more difficult than question 1.
Question 4
Passing this question, or question 2, is sufficient to pass the Linked Lists Hurdle.
Note prac_q4.c uses the following familiar data type:
struct node {
struct node *next;
int data;
};
delete_last is given one argument, head, which is the pointer to the first node in a linked list.
Add code to delete_last so that it deletes the last node from list.
delete_last should return a pointer to the new list.
If the list is now empty delete_last should return NULL.
delete_last should call free to free the memory of the node it deletes.
For example if the linked list contains these 8 elements:
16, 7, 8, 12, 13, 19, 21, 12
Question 5
This question will not satisfy either hurdle.
Question 6
This question will not satisfy either hurdle.
Question 7
This question will not satisfy either hurdle.
Question 8
This question will not satisfy either hurdle.
delete_last should return a pointer to a list with these elements:
16, 7, 8, 12, 13, 19, 21
Testing
prac_q4.c also contains a main function which allows you to test your delete_last 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 delete_last(head)
prints the result.
Do not change this main function. If you want to change it, you have misread the question.
Your delete_last function will be called directly in marking. The main function is only to let you test your delete_last function
cp -n prac_q4.c .
dcc prac_q4.c -o prac_q4
./prac_q4 16 7 8 12 13 19 21 12
[16, 7, 8, 12, 13, 19, 21]
./prac_q4 2 4 6 2 4 6
[2, 4, 6, 2, 4]
./prac_q4 42
[]
./prac_q4
[]
Assumptions/Restrictions/Clarifications.
delete_last should call free to free the memory for the node it deletes
delete_first should not change the data fields of list nodes.
delete_last should not use arrays.
delete_last should not call malloc.
delete_last should not call scanf (or getchar or fgets).
delete_last should not print anything. It should not call printf.
Do not change the supplied main function. It will not be tested or marked.
You can autotest this code with 1091 autotest-pracexam prac_q4
You can submit this code with give dp1091 prac_q4 prac_q4.c
You can check your submission has been accepted with 1091 classrun -check prac_q4
You can see your previous autotests here
In the final exam, question 5 will be an arrays question, more difficult than question 3.
In the final exam, question 6 will be a linked lists question, more difficult than question 4.
In the final exam, question 7 will be a challenging question.
In the final exam, question 8 will be a very challenging question.

站长地图