c++ week 2
Week 2 - Advanced C++/list1.txt
31 22 20 13 11 9 7 4 2
Week 2 - Advanced C++/list2.txt
3 3 5 5 5 6 8 10 10 10 11 11 15 18 21 21 21 23 25 25 28 29 33 34 36 45 45 45
Week 2 - Advanced C++/listWork.cpp
Week 2 - Advanced C++/listWork.cpp
//******************************************************************
// 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
);
int
main
()
{
cout
<<
"Assignment 7: YOUR_NAME(S)_HERE"
<<
endl
<<
endl
;
int
List1
[
MAX_LIST_SIZE
],
List2
[
MAX_LIST_SIZE
],
List3
[
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
);
// ==================================================
// ENTER REQUIRED LIST ACTIONS HERE
// ( AND DECLARE NECESSARY FUNCTIONS BELOW)
// ==================================================
return
0
;
}
/************************************************************/
/* This function opens a file to retrieve a list of integer */
/* values. Values are stored in an array and passed to the */
/* calling routine. */
/* Parameters: */
/* list array of integers to build */
/* listsize number of integers in list */
/* maxlistsize max size allowable for list array */
/* inFile file containing list */
/************************************************************/
void
GetList
(
int
inList
[],
int
&
listsize
,
int
maxlistsize
,
ifstream
&
inFile
)
{
int
ListElement
;
int
i
=
0
;
inFile
>>
ListElement
;
while
(
inFile
&&
i
<
maxlistsize
)
{
inList
[
i
]
=
ListElement
;
i
++
;
inFile
>>
ListElement
;
}
listsize
=
i
;
// Size of list is lastindex + 1
}
/************************************************************/
/* This function writes a list to console output: one list */
/* item at a time. */
/************************************************************/
void
DisplayList
(
int
theList
[],
int
listsize
)
{
for
(
int
i
=
0
;
i
<
listsize
;
i
++
)
cout
<<
theList
[
i
]
<<
" "
;
cout
<<
endl
<<
endl
;
}
/************************************************************/
/* This function reverses the order of an unordered list */
/* Strategy: As list is traversed one way, path the other */
/* direction is calculated arithmetically. */
/************************************************************/
void
ReverseList
(
int
theList
[],
int
&
numItems
)
{
int
temp
;
for
(
int
i
=
0
;
i
<
numItems
/
2
;
i
++
)
// Traverse half-way across list
{
temp
=
theList
[
i
];
// Swap element with its counterpart
theList
[
i
]
=
theList
[
numItems
-
i
-
1
];
theList
[
numItems
-
i
-
1
]
=
temp
;
}
}
Week 2 - Advanced C++/READ FOR WHAT TO DO.txt
Week 2 - Files will be included in the folder. Practice 2A: http://www3.delta.edu/teklingl/cst280/practice/CST280W02a.pdf Practice 2B: http://www3.delta.edu/teklingl/cst280/practice/CST280W02b.pdf
Week 2 - Advanced C++/unorderPrac.cpp
#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); void DisplayList(int theList[],int listsize); void UnOrdInsert(int list[], int& numElems, int newint); void UnOrdDelete(int list[], int& numElems, int oldint); void UnOrdInsertFrontRand(int list[], int& numElems, int newint); void UnOrdInsertFrontFixed(int list[], int& numElems, int newint); int main() { int InList[MAX_LIST_SIZE]; // Array of list elements int ListSize; // Index of last list element GetList(InList,ListSize,MAX_LIST_SIZE); cout << endl << "Initial list:" << endl; DisplayList(InList,ListSize); UnOrdInsert(InList,ListSize,10); // Insert value of 10 cout << endl << "After inserting 10:" << endl; DisplayList(InList,ListSize); UnOrdDelete(InList,ListSize,19); // Delete value of 19 cout << endl << "After deleting 19:" << endl; DisplayList(InList,ListSize); UnOrdInsertFrontRand(InList,ListSize,22); // Insert value of 22 cout << endl << "After inserting 22 at front:" << endl; DisplayList(InList,ListSize); UnOrdInsertFrontFixed(InList,ListSize,99); // Insert value of 99 cout << endl << "After inserting 99 at front:" << endl; DisplayList(InList,ListSize); return 0; } /************************************************************/ /* This function opens a file to retrieve a list of integer */ /* values. Values are stored in an array and passed to the */ /* calling routine. */ /* Parameters: */ /* list array of integers to build */ /* listsize number of integers in list */ /* maxlistsize max size allowable for list array */ /************************************************************/ void GetList(int inList[],int& listsize,int maxlistsize) { ifstream InFile ("unorderList.txt"); int ListElement; int i = 0; InFile >> ListElement; while( ! InFile.eof() && i < maxlistsize) { inList[i] = ListElement; i++; InFile >> ListElement; } listsize = i; // Size of list is lastindex + 1 } /************************************************************/ /* This function writes a list to console output: one list */ /* item at a time. */ /************************************************************/ void DisplayList(int theList[],int listsize) { for (int i = 0;i < listsize; i++) cout << theList[i] << " "; cout << endl; } /************************************************************/ /* This function receives an integer, an array containing */ /* an unordered list, and the size of the list. It inserts */ /* a new integer item at the end of the list. */ /************************************************************/ void UnOrdInsert(int list[], int& numElems, int newint) { list[numElems] = newint; // Insert new element at end of list numElems++; // Increment size of list } /************************************************************/ /* This function receives an integer, an array containing */ /* an unordered list, and the size of the list. It locates */ /* and deletes the integer integer from the list. */ /************************************************************/ void UnOrdDelete(int list[], int& numElems, int oldint) { int ptr = 0; // Scan list for deletion target while (oldint != list[ptr] && ptr < numElems) ptr++; if (ptr < numElems) // If target found, then { list[ptr] = list[numElems-1]; // Move last list item to overwrite target numElems--; // Decrement size of list } } /************************************************************/ /* This function receives an integer, an array containing */ /* an unordered list, and the size of the list. It inserts */ /* a new integer item at the beginning of the list. */ /* */ /************************************************************/ void UnOrdInsertFrontRand(int list[], int& numElems, int newint) { } /************************************************************/ /* This function receives an integer, an array containing */ /* an unordered list, and the size of the list. It inserts */ /* a new integer item at the beginning of the list. */ /* */ /* POSTCONDITION: Order of original list is undisturbed. */ /************************************************************/ void UnOrdInsertFrontFixed(int list[], int& numElems, int newint) { }