Access over 20 million homework & study documents

search

C++ CST 280 Week 2 Practice 2A

Type

Lab

Rating

Showing Page:
1/6
// CPPWeek2Practice2B.cpp
//
#include "stdafx.h"
//******************************************************************
// CST 280 In-Class List Algorithm Practice
//******************************************************************
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_LIST_SIZE = 100; // Max list size
// Function prototypes
void GetList(int inList[], int& listsize, int maxlistsize, ifstream&
inFile);
void DisplayList(int theList[], int listsize);
void ReverseList(int theList[], int& numItems);
void RemoveDuplicates(int theList[], int& listSize);
void MergeList(int List1[], int &list1_size, int List2[], int &list2_size,
int List3[], int &list3_size);
int Merge(int List1[], int list1_size, int List2[], int list2_size, int
List3[]);
void MergeSort(int inList[], int listsize);
void DeleteValue(int inList[], int& listsize, int newint);
void InsertValue(int inList[], int& listsize, int newint);
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Assignment 7: YOUR_NAME(S)_HERE" << endl << endl;
int List1[MAX_LIST_SIZE], List2[MAX_LIST_SIZE], List3[MAX_LIST_SIZE],
ScratchList[MAX_LIST_SIZE];
int list1_size, list2_size, list3_size;
// Read both input lists from files
ifstream inList1("list1.txt");
GetList(List1, list1_size, MAX_LIST_SIZE, inList1);
ifstream inList2("list2.txt");
GetList(List2, list2_size, MAX_LIST_SIZE, inList2);
// Display list 1
cout << "List 1 (initial): " << endl;
DisplayList(List1, list1_size);
// Display list 2
cout << "List 2 (initial): " << endl;
DisplayList(List2, list2_size);
// ==================================================
//Reverse the order of List 1
cout << "Reversing the order of List 1" << endl;
ReverseList(List1, list1_size);
//Remove duplicates from List 2 (key in algorithm below)
cout << "Removing the duplicates from List 2" << endl;
RemoveDuplicates(List2, list2_size);
//Display both lists
// Display list 1
cout << "List 1 (after reverse): " << endl;

Sign up to view the full document!

lock_open Sign Up
DisplayList(List1, list1_size);
// Display list 2
cout << "List 2 (after removing duplicates): " << endl;
DisplayList(List2, list2_size);
//For List 1: insert values 14 and 24, and delete the value 11
InsertValue(List1, list1_size, 14);
InsertValue(List1, list1_size, 24);
cout << "List 1 (after inserting 14 and 24): " << endl;
DisplayList(List1, list1_size);
DeleteValue(List1, list1_size, 11);
cout << "List 1 (after removing 11): " << endl;
DisplayList(List1, list1_size);
//Merge List 1 and List 2 together into List 3
cout << "Merging List1 and List2 in List3" << endl;
MergeList(List1, list1_size, List2, list2_size, List3, list3_size);
//Display combined list : List 3
cout << "List 3 (merged): " << endl;
DisplayList(List3, list3_size);
// ==================================================
system("pause");
return 0;
}
/************************************************************/
/* This function Merge two list in one list. The resulting */
/* list would be sorted using mere sort algorithm */
/* passed.
*/
/* Parameters: */
/* List1 array of integers to be merged */
/* list1_size size of the array List1 */
/* List2 array of integers to be merged */
/* list2_size size of the array List2 */
/* List3 merged array of integer */
/* list3_size size of the array List3 */
/************************************************************/
void MergeList(int List1[], int &list1_size, int List2[], int &list2_size,
int List3[], int &list3_size)
{
list3_size = Merge(List1, list1_size, List2, list2_size, List3);
MergeSort(List3, list3_size);
}
/************************************************************/
/* This function Merge two list in one list */
/* passed.
*/
/* Parameters: */
/* List1 array of integers to be merged */
/* list1_size size of the array List1 */
/* List2 array of integers to be merged */
/* list2_size size of the array List2 */
/* List3 merged array of integer */
/* Returns the length of the merged array */
/************************************************************/
int Merge(int List1[], int list1_size, int List2[], int list2_size, int
List3[])

Sign up to view the full document!

lock_open Sign Up
{
int i1, i2, i3;
i1 = 0; i2 = 0; i3 = 0;
/* while both lists are non-empty */
while (i1 < list1_size && i2 < list2_size) {
if (List1[i1] < List2[i2]) {
List3[i3++] = List1[i1++];
}
else {
List3[i3++] = List2[i2++];
}
}
while (i1 < list1_size) {
/* copy remainder of List1 */
List3[i3++] = List1[i1++];
}
while (i2 < list2_size) {
/* copy remainder of List2 */
List3[i3++] = List2[i2++];
}
return i3;
}
/************************************************************/
/* This function Sort the passed array using merge sort */
/* algorithm
*/
/* passed.
*/
/* Parameters: */
/* inList array of integers to be sorted */
/* listsize size of the array */
/************************************************************/
void MergeSort(int inList[], int listsize)
{
int temp[MAX_LIST_SIZE];
int mid, i;
if (listsize < 2) {
return;
}
else {
mid = listsize / 2;
MergeSort(inList, mid);
MergeSort(inList + mid, listsize - mid);
Merge(inList, mid, inList + mid, listsize - mid, temp);
for (i = 0; i < listsize; i++) {
inList[i] = temp[i];
}
}
}
/************************************************************/
/* This function delete int value in the list of integer */
/* passed.
*/
/* Parameters: */
/* inlist array of integers to insert into */
/* listsize number of integers in list */
/* newint the new integer value to delete */
/************************************************************/
void DeleteValue(int inList[], int& listsize, int newint)
{
for (int i = 0; i < listsize; i++){
if (inList[i] == 11){

Sign up to view the full document!

lock_open Sign Up

Unformatted Attachment Preview

// CPPWeek2Practice2B.cpp //#include "stdafx.h"//******************************************************************// CST 280 In-Class List Algorithm Practice //******************************************************************#include #include using namespace std;const int MAX_LIST_SIZE = 100; // Max list size// Function prototypesvoid GetList(int inList[], int& listsize, int maxlistsize, ifstream& inFile);void DisplayList(int theList[], int listsize);void ReverseList(int theList[], int& numItems);void RemoveDuplicates(int theList[], int& listSize);void MergeList(int List1[], int &list1_size, int List2[], int &list2_size, int List3[], int &list3_size);int Merge(int List1[], int list1_size, int List2[], int list2_size, int List3[]);void MergeSort(int inList[], int listsize);void DeleteValue(int inList[], int& listsize, int newint);void InsertValue(int inList[], int& listsize, int newint);int _tmain(int argc, _TCHAR* argv[]){cout << "Assignment 7: YOUR_NAME(S)_HERE" << endl << endl;int List1[MAX_LIST_SIZE], List2[MAX_LIST_SIZE], List3[MAX_LIST_SIZE], ScratchList[MAX_LIST_SIZE];int list1_size, list2_size, list3_size;// Read both input lists from files ifstream inList1("list1.txt");GetList(List1, list1_size, MAX_LIST_SIZE, inList1);ifstream inList2("list2.txt");GetList(List2, list2_size, MAX_LIST_SIZE, inList2);// Display list 1 cout << "List 1 (initial): " << e ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Great! 10/10 would recommend using Studypool to help you study.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4