Laboratory 4
Laboratory 4
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)
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>
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"
int main()
{
int x;
weightedGraphType shortestPathGraph(50);
shortestPathGraph.createWeightedGraph();
shortestPathGraph.shortestPath(0);
shortestPathGraph.printShortestDistance(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"
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
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>
_________________________________________________________________________________________________
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;
// use iterator
vector<int>::iterator i;
showGraph(*i, visited);
}
}
// constructor
public:
GraphTheory(int V)
{
this->V = V;
adjacent = new vector<int>[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)
// 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: ";
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;
g.connect(startNode, endNode);
}
}
// 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
SCORE:
CHECKED
Signature
Rating
Date
_________________________________________________________________________________________________
CPE104L Data Structures and Algorithms
School of Electrical, Electronics, and Computer Engineering Page |
13