[go: up one dir, main page]

0% found this document useful (0 votes)
95 views2 pages

Lab 8

This document contains instructions for two problems related to sorting data from files using bubble sort. Problem 1 involves reading integers from a file into a vector, sorting the vector using bubble sort, and outputting the sorted values. Functions must be used to read from the file into a vector and perform the sorting. Problem 2 involves generating random user data with fields like ID and name, saving to a CSV file. A program must then read this into a vector, sort by last name using bubble sort, and write the sorted data to a new CSV file. Functions are needed to read from and write to the CSV files.

Uploaded by

David Cross
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)
95 views2 pages

Lab 8

This document contains instructions for two problems related to sorting data from files using bubble sort. Problem 1 involves reading integers from a file into a vector, sorting the vector using bubble sort, and outputting the sorted values. Functions must be used to read from the file into a vector and perform the sorting. Problem 2 involves generating random user data with fields like ID and name, saving to a CSV file. A program must then read this into a vector, sort by last name using bubble sort, and write the sorted data to a new CSV file. Functions are needed to read from and write to the CSV files.

Uploaded by

David Cross
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/ 2

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 random data using https://www.mockaroo.com/


Fields: id , first_name, last_name, ip_address
Format: csv
Line ending: linux/unix
Do not include header
# of rows: 10

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".

Write a program that does the following:

1. Create a vector to store the string(s).


a. Each string represents 1 entry from the file

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.

You might also like