Lab 8
Lab 8
Chapters 2 to 10
Problem 1: [5 marks]
file: problem1.cpp
Write a program that reads a list of int from a file named "numbers.txt" into a vector, sorts the list using
bubble sort algorithm and outputs the sorted list to the console.
Instructions:
1. Read int from "numbers.txt" file and store them in a vector.
2. Implement a selection sort algorithm to sort the int in the vector in ascending order.
3. Output the sorted int to the console.
Requirements:
1. Your program should use a vector to store the int.
2. Your program should read int from a file named "numbers.txt".
a. Create a function that does the ff:
i. Accepts the filename as parameter.
ii. Reads the contents of the file and store in a vector
1. Note: use int as data type (vector<int> &)
iii. When the function terminates, the vector would now contain the data
3. Your program should implement the bubble sort algorithm to sort the int.
a. Create a function that does the following:
i. Accepts vector<int>
ii. Performs bubble sort
4. Your program should output the sorted int to the console.
5. Your program should handle any errors that may occur when reading the file.
Lab 8
Chapters 2 to 10
Problem 2: [5 marks]
File: problem2.cpp
Generate a random data using mockaroo.com and rename the file as "users.csv" containing user
information in the following format: "id, firstname, lastname, ip_address". You are tasked with writing a
program to read this file, store the data in a vector, sort it using Bubble Sort based on the lastName, and
write the sorted information to a new file "sorted_users.csv".
2. Implement a method to read the user information from the file "users.csv" and populate the vector
3. Implement Bubble Sort to sort the data stored in vector based on their last_name.
4. Implement a method to write the sorted user information to the file "sorted_users.csv".
Test your program by creating a sample "users.csv" file and ensuring that the sorted user
information is correctly written to the "sorted_users.csv" file.