Programming questions assignment
COS10007 Developing Technical Software TP 2 2016
© Swinburne University of Technology Page 1
Assignment (4x6.25 = 25 marks)
Q1. Write a complete C++ program to create a table as shown below. User inputs the width of
the table, after creating the shape your program should calculate the sum of the elements in
a particular row entered by the user.
Example run:
Width of the shape? (Enter odd number only) 9 Enter row number to get sum 5 Answer:
1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 1 2 3 4 5 1 2 3 1
Sum of row 5 is 45
Q2. Write the following functions and then a complete C program, assuming the node type used is
as follows:
struct node { int data; struct node* next; }; and head is the head pointer of a list:
struct node* head;
a) Write a sortedInsert() function in C program that takes two parameters: i. a list that is sorted in increasing order, and ii. a single node,
COS10007 Developing Technical Software TP 2 2016
© Swinburne University of Technology Page 2
which inserts the node into the correct sorted position in the list. You should use the following function header: voidSortedInsert(struct node** headRef, struct node* newNode) {
// Your code...
b) Write another function named removeRedundant() which takes a list sorted in increasing order and deletes any duplicate nodes from the list. Ideally, the list should only be traversed once. /* Remove duplicates from a sorted list. */ voidRemoveDuplicates(struct node* head) { // Your code...
Q3. Create a linked list using C, print the linked list and then call a function to delete a specified
node. In your delete function
i. check if the first node matches with the ‘key’ value, if it matches delete the first node. ii. if the key value doesn’t match with the first node then call a find_node () function to find the
location of the particular node that containing the item to be deleted. Print your linked list after the deletion process. Q4. Write a complete C program, that Uses a one-dimension array to read 20 numbers, each of them is between 0 and 100, inclusive. a. Uses a bubbleSort() function to sort the array in ascending order. Discuss the Big O of your sorting algorithm for the best case and worst case scenarios. b. Write another function that improve the performance of your program by using another sorting algorithm (any algorithm learned in the lecture), discuss the Big O of the new algorithm used.
10 20 30 35 NULL
10 20 30 35 NULL
key node