0% found this document useful (0 votes)
28 views13 pages

Laboratory 4

The document is a laboratory report that includes coding exercises on graphs, searching, and sorting algorithms. It contains the instructions, output screenshots, code screenshots, and source code for two coding problems - writing a program to find the shortest path distances from a node in a graph, and writing a program to test a sequential search function. It also includes the student's comments on their findings from completing the coding exercises, noting some initial difficulties with the online system but that they were able to complete the coding problems and learned from the experience.

Uploaded by

Jacob Lintag
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)
28 views13 pages

Laboratory 4

The document is a laboratory report that includes coding exercises on graphs, searching, and sorting algorithms. It contains the instructions, output screenshots, code screenshots, and source code for two coding problems - writing a program to find the shortest path distances from a node in a graph, and writing a program to test a sequential search function. It also includes the student's comments on their findings from completing the coding exercises, noting some initial difficulties with the online system but that they were able to complete the coding problems and learned from the experience.

Uploaded by

Jacob Lintag
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/ 13

Laboratory Report 4

GRAPHS, SEARCHING, AND SORTING


Name: Arthur Jacob V. Lintag Date: 31/01/2024
Program: CPE Section: E02

A. Instructions:
(Please include the following: 1. Screenshots of the output from MindTap, 2. Screenshots of the IDE from
MindTap, and 3. The source code from MindTap.)

Screenshot of Output
(Insert images here from the output of the console window of MindTap)

Resize the image if necessary.

Screenshot of Codes
(Insert images here)

Screenshot of your code in MindTap. Use the snipping tool to properly capture the codes.

Resize the image properly so that it fits in the table. Use a whole page for this part if necessary.

Source Code
(insert Source Code here)

Paste the actual code from MindTap here. Use the following format: Courier New 9

#include <iostream>

using namespace std;

int main() {
cout << "Hello World" << endl;
return 0;
}

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 1
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING
B. Coding Exercises:
1. Write a program that outputs the shortest distance from a given node to every other node in the graph.

Screenshot of Output

Screenshot of Codes

Source Code
_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 2
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING
#include <iostream>
#include <fstream>

#include "weightedGraph.h"

using namespace std;

int main()
{

int x;

weightedGraphType shortestPathGraph(50);
shortestPathGraph.createWeightedGraph();
shortestPathGraph.shortestPath(0);
shortestPathGraph.printShortestDistance(0);

cout << endl;


system("pause");
return 0;
}

2. Write a program to test the function you designed in Exercise 3. Note: Have the function,seqOrdSearch,
return -1 if the item is not found in the list. (return the index of the item if found).

Screenshot of Output

Screenshot of Codes
_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 3
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING

Source Code

SearchSortAlgorithm.h

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 4
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING
template<class elemType>
int seqOrdSearch(const elemType list[], int length, const elemType& item)
{
for(int i=0;i<length;i++){
if(list[i]==item){
return i;
}
}
// Write the function definition here
return -1; // return -1 if item not found
} //end seqOrdSearch

Main.cpp

#include <iostream>
#include "searchSortAlgorithms.h"

using namespace std;

int main() {
// Write your main here
const int size=10;
int array[]={11,22,34,56,78,92,102,150,160,178};
int input=0, index;

while(true){
cout<<"Enter an integer to search (-1 to quit): ";
cin>>input;
if(input==-1){
break;
}else {
index=seqOrdSearch(array,size,input);

if(index==-1){
cout<<"Not found!"<<endl;

}else{
cout<<"Found at index "<<index<<endl;
}
}
}

return 0;
}S

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 5
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING
C. Findings, Observations, and Comments:
Guiding Questions :
1. Did you solve the coding exercise/s?
2. Are there any difficulties during coding or set-up that you encountered?
3. What are your findings, observations, and comments in the coding exercise?

(Please type your findings, observations, and comments according to the guide questions in a paragraph
format.)
1. Yes, but it took me time to code it and It kinda confuses me because in github, the Lab 4-1
was bugging and I can’t figure it out if I’m doing it right.
2. Yes, One of the problems that I encountered during the code was the github itself, because
the companion can’t check my codes if that is correct. But doing the code, I don’t really
have any difficulties doing the code so it was okay.
3. I learned a lot on doing in sorting, mostly the sequential order search, it was fine doing the
code but I also took some to finish it so It was good doing this laboratory

CW4-1 (submitted through LAB-4 documentation due to malfunctioning website in github)

Screenshot of Output

Screenshot of Codes

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 6
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 7
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 8
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING

Source Code

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page | 9
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING
// create a class
class GraphTheory
{
// required variable
int V;

// create a vector of int type for adjacebt node


vector<int> *adjacent;

// create a function to display graph


void showGraph(int v, bool visited[])
{
visited[v] = true;
cout << v << " ";

// use iterator
vector<int>::iterator i;

// iterate loop for all adjacent


for (i = adjacent[v].begin(); i != adjacent[v].end(); ++i)
{
// check if node is not visited
if (!visited[*i])

showGraph(*i, visited);
}
}

// constructor
public:

GraphTheory(int V)
{
this->V = V;
adjacent = new vector<int>[V];
}

// function to connect the nodes


void connect(int v, int w)
{
// push the node to adjacent
adjacent[v].push_back(w);
}

// Function for DFS


void DFS()
{
// requied pointer for visited nodes
bool *visited = new bool[V];

// iterate loop
for (int i = 0; i < V; ++i)
{

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page |
10
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING
// if node is not visited
visited[i] = false;
}

// iterate loop
for (int i = 0; i < V; ++i)
{
if (visited[i] == false)

// call the showGraph function


showGraph(i, visited);
}

cout << "\n";


}
};

// driver method
int main()
{
// variable for file operation
string filename;

fstream file;

// required variable
int totalNodes;
int startNode, endNode;

// display message
cout << "\nEnter the file name for node data: ";

// take user input


cin >> filename;

// open the file


file.open(filename, ios::in);

// if file not available


if (!file)
{
cout << "\nFile Error!!!\nExiting...\n";
return 0;
}

file >> totalNodes;

GraphTheory g(totalNodes);

// iterate loop
for (int i = 0; i < totalNodes; ++i)
{
file >> startNode;

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page |
11
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING
// while true
while (true)
{
file >> endNode;

// if end node is -999


if (endNode == -999)

// break the loop


break;

g.connect(startNode, endNode);

}
}

// call the DFS function


g.DFS();

// return
return 0;
}

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page |
12
Laboratory Report 4
GRAPHS, SEARCHING, AND SORTING
D. Score Sheet

Poor Fair Good Excellent


Criteria Score
(25%) (50%) (75%) (100%)
The report was The report was The report was The report was
incomplete, complete, complete, neat, complete, neat,
I. Completeness,
messy, and had messy, and had and had 1 or 2 and all the
Documentation, and
many many unanswered questions had
Organization of Report
unanswered unanswered questions. been answered.
questions. questions.
The program The program The program The program
had had corrected had corrected had appropriate
inappropriate indention, but indention, elements and
elements and inappropriate adequate structures,
II. Coding Design and
structures, elements and program correct
Patterns
incorrect structures and documentation, indention, and
indention, and poor program but inappropriate excellent
poor program documentation. elements and program
documentation. structures. documentation.
The findings. The findings. The findings. The findings.
observations, observations, observations, observations,
and comments and comments and comments and comments
were not based based on the were based on were based on
on the gathered gathered data the gathered the gathered
III. Findings, Observations, data and results. and results but data and results data and results
and Comments All thoughts were not and were mostly and were
were elaborated. Not elaborated. Most completely
inconsistent and all thoughts were of the thoughts elaborated. All
unclear. consistent and were consistent the thoughts
clear. and clear. were consistent
and clear.
The words used The words used The words used The words used
were were were were
inappropriate, appropriate; appropriate, had appropriate, had
IV. Grammar and wrong grammar however, bad proper grammar proper grammar
Composition usage, and poor grammar and usage, but poor usage, and
sentence poor sentence sentence excellent
construction was construction construction was sentence
observed. were observed. observed. construction.

SCORE:

CHECKED
Signature
Rating
Date

_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page |
13

You might also like