0% found this document useful (0 votes)
40 views

Data Structures and Algorithms - Kaunda - James

The document is an assignment brief and feedback form for a data structures and algorithms course at Cavendish University Zambia. It provides instructions for the student to complete Assignment 1, which involves developing a program to sort province and capital city names in Zambia into a two-dimensional array, prompt the user to identify each capital, and report the number correct. The form also lists assessment criteria the lecturer will use to provide feedback.

Uploaded by

James Kaunda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Data Structures and Algorithms - Kaunda - James

The document is an assignment brief and feedback form for a data structures and algorithms course at Cavendish University Zambia. It provides instructions for the student to complete Assignment 1, which involves developing a program to sort province and capital city names in Zambia into a two-dimensional array, prompt the user to identify each capital, and report the number correct. The form also lists assessment criteria the lecturer will use to provide feedback.

Uploaded by

James Kaunda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CAVENDISH UNIVERSITY ZAMBIA

ASSIGNMENT BRIEF AND FEEDBACK FORM

STUDENT NUMBER: _ _____ 102569__ ____

INTAKE TWO 3
ONE/TWO YEAR

LECTURER: MR SINKALA

SUBJECT: COM 321 DATA STRUCTURERS AND ALGORITHM.

ASSIGNMENT NO. 1

DATE HANDED OUT: 03/04/2023

DATE DUE IN: 15/05/2023

DAY/EVE/DL DL

ASSIGNMENT BRIEF

GIVEN SHOULD BE IN THIS BOX

STUDENT INSTRUCTIONS:
1. This form must be attached to the front of your assignment
2. The assignment must be handed in without fail by the submission date (see assessment schedule for your course)
3. Ensure that the submission form is date stamped by the reception staff when you hand it in
4. Late submission will not be entertained unless with prior agreement with the subject tutor
5. All assessable assignments must be word processed

ASSIGNMENT GUIDANCE

This assignment is intended to assess the student’s knowledge in all of the


following areas. However, greater emphasis should be given to those items marked
with a
(Tutor: - please tick as applicable)
ASSESSABLE SKILLS Please Tick.
Good and adequate interpretation of the questions

Knowledge and application of the relevant theories

Use of relevant and practical examples to back up theories

Ability to transfer and relate subject topics to each other

Application or use of appropriate models

Evidence of library research

Knowledge of theories

Written Business English communication skills

Use of visual (graphs) communications

Self Assessed ‘time management’

Evidence of field research

Tutor’s Mark Contribution

MARKS
(Administration only*)

LECTURERS FEEDBACK:

PART 1

*
SORTING ARRAYS

Using a program of your choice, Develop a program that asks the user to enter a capital for a
Zambian Province. Upon receiving the user input, the program reports whether the user input is
correct. For this application, the 10 Provinces and their capital cities are stored in a two-
dimensional array in order by Province name. Display the current contents of the array then use a
bubble sort to sort the content by capital. Next, prompt the user to enter answers for all the
province capitals and then display the total correct count. The user's answer is not case-sensitive.

import java.util.Scanner;

public class Test {

/** Main method */

public static void main(String[] args) {

// Create a Scanner

Scanner input = new Scanner (System.in);

// Store 10 provinces and their capitals in a two-dimensional array

String [][] provincesAndCapitals = getData();

int count = 0; // Correct answer

// repeatedly prompt the user to enter the capital of a province

For (int i = 0; i < provincesAndCapitals. Length; i++) {

System.out.print ("What is the capital of”

+ provincesAndCapitals[i][0] + "? ");

String capital = input.nextLine();

if (isEqual(provincesAndCapitals[i][1], capital)) {

System.out.println("Your answer is correct");


count++;

else {

System.out.println("The correct answer should be " +

provincesAndCapitals[i][1]);

// Display the total correct count

System.out.println("\nThe correct count is " + count);

/** isEqual returns true if a is equal to c */

public static boolean isEqual(String c, String a) {

if (c.length() != a.length())

return false;

for (int i = 0; i < c.length(); i++) {

if (c.charAt(i) != a.charAt(i))

return false;

return true;

/** getData initializes the array with the 10 provinces and their capitals */

Public static String [][] getData () {


String [][] d = {

{"Lusaka", "Lusaka"},

{"Southern", "Livingstone"},

{"Western", "Mongu"},

{"North Western", "Solwezi"},

{"Eastern", "Chipata"},

{"Luapula", "Mansa"},

{"Northern", "Kasama"},

{"Muchinga", "Mpika"},

{"Copperbelt", "Ndola"},

{"Central", "Kabwe"}},

return d;

Part 2
(i) Discuss the concepts of Tree Traversing and give examples.

Tree Traversing is a type of graph traversal in the data structure that refers to the process of
visiting, verifying, and updating each node in a tree data structure just once. The order in which
to examine the nodes of the tree is used to classify these traversals.

There are three types of binary tree traversals.

1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal

JAVA TRAINING: https://www.javatpoint.com/

(II) Discuss the different practical application of stacks

A Stack is a linear data structure that holds a linear, ordered sequence of elements. It is an
abstract data type.

A Stack can be a fixed specific size, or it can be dynamic, i.e., the Stack size can be changed
dynamically. It can be represented by means of Pointer, Array, Structure, and Linked List.

Application of the Stack


1. A Stack can be used for evaluating expressions consisting of operands and operators.
2. Stacks can be used for Backtracking, i.e., to check parenthesis matching in an expression.
3. Stacks can also be used to convert one form of expression to another form.
4. Stacks can be used for systematic Memory Management.

STACKS AND APPLICATIONS:


https://byjus.com/gate/stack-and-its-applications/#:~:text=and%20Linked%20List.-,Application
%20of%20the%20Stack,used%20for%20systematic%20Memory%20Management.

(ii) Differentiate the different data structures one from the other specifically in the way
data is inserted and deleted. Use relevant properties for each data structure to
differentiate.

A Data structure is a storage that is used to store and organize data. It is a way of arranging data
on a computer so that it can be accessed and updated efficiently

Types of Data Structures

Data structures are divided into two categories:

 Linear data structure

 Non-linear data structure

PROGRAMIZ : https://www.programiz.com/dsa/data-structure-types

Difference between Linear and Non-linear Data Structures:


Linear Data Structure Non-linear Data Structure

In a linear data structure, data elements


1 are arranged in a linear order where In a non-linear data structure, data elements
. each element is attached to its previous are attached in hierarchically manner.
and next adjacent.

2 In linear data structure, single level is Whereas in non-linear data structure,


. involved. multiple levels are involved.

3 Its implementation is easy in While its implementation is complex in


. comparison to non-linear data structure. comparison to linear data structure.

While in non-linear data structure, data


4 In linear data structure, data elements
elements can’t be traversed in a single run
. can be traversed in a single run only.
only.

While in a non-linear data structure, memory


5 In a linear data structure, memory is not
is utilized in an efficient way.
. utilized in an efficient way.

6 Its examples are: array, stack, queue,


While its examples are :trees and graphs.
. linked list, etc.

Applications of linear data structures Applications of non-linear data structures are


7
are mainly in application software in Artificial Intelligence and image
.
development. processing.

8 Linear data structures are useful for Non-linear data structures are useful for
. simple data storage and manipulation. representing complex relationships and data
Linear Data Structure Non-linear Data Structure

hierarchies, such as in social networks, file


systems, or computer networks.

GEEKS PROGRAMMING : https://www.geeksforgeeks.org/difference-between-linear-and-


non-linear-data-structures/

#include <iostream>

#include <queue>

using namespace std;

// Node structure

struct Node {

int data;

Node* left;

Node* right;

Node(int val) {

data = val;

left = NULL;

right = NULL;

};
// Function to traverse binary tree using post-order traversal

void postorderTraversal(Node* node) {

if (node == NULL)

return;

postorderTraversal(node->left);

postorderTraversal(node->right);

cout << node->data << " ";

// Function to add a node to the binary tree

Node* insertNode(Node* node, int val) {

if (node == NULL)

return new Node(val);

if (val < node->data)

node->left = insertNode(node->left, val);

else if (val > node->data)

node->right = insertNode(node->right, val);

return node;

// Function to delete a node from the binary tree

Node* deleteNode(Node* node, int val) {

if (node == NULL)

return node;
if (val < node->data)

node->left = deleteNode(node->left, val);

else if (val > node->data)

node->right = deleteNode(node->right, val);

else {

if (node->left == NULL) {

Node* temp = node->right;

free(node);

return temp;

else if (node->right == NULL) {

Node* temp = node->left;

free(node);

return temp;

Node* temp = node->right;

while (temp->left != NULL)

temp = temp->left;

node->data = temp->data;

node->right = deleteNode(node->right, temp->data);

return node;

}
int main() {

Node* root = new Node(50);

insertNode(root, 30);

insertNode(root, 20);

insertNode(root, 40);

insertNode(root, 70);

insertNode(root, 60);

insertNode(root, 80);

cout << "Binary Tree Traversal (Post-Order): ";

postorderTraversal(root);

cout << endl;

deleteNode(root, 20);

cout << "Binary Tree Traversal (Post-Order) after deleting 20: ";

postorderTraversal(root);

cout << endl;

return 0;

Reference
JAVA TRAINING: https://www.javatpoint.com/

STACKS AND APPLICATIONS:


https://byjus.com/gate/stack-and-its-applications/#:~:text=and%20Linked%20List.-,Application
%20of%20the%20Stack,used%20for%20systematic%20Memory%20Management.

PROGRAMIZ : https://www.programiz.com/dsa/data-structure-types

GEEKS PROGRAMMING : https://www.geeksforgeeks.org/difference-between-linear-and-


non-linear-data-structures/

You might also like