Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SID:5052369
Assessment Title: Report on your individual contribution to the coding of the programming learning toolkit.
Explanation of the code: (in relation to use of classes) The class I have made is intended to search and organise a vector array of strings. This means a user will search a keyword and the program will match the substring entered with the strings, and print out the ones that it found, putting it in alphabetical order. This class was put into a header file, so that the class and functions within it in the search.cpp file can be easily included into other files, making it much easier to use with other developers code in a group project such as this.
@Coventry University
Page 1
Version 1
Explanation of the code: (in relation to encapsulation) The vector string book (which is test vector to make sure the code works correctly) is in the class. Currently it is set as private, meaning that it is a vector that can only be used within the class. This encapsulating the variable within the class, making it so that other classes or functions cannot use this variable without making a function allowing other files to use the variable under a different name. It makes sure that the main part of the code, the variable it is based around, is not affected by other files, which is useful in large programs such as this. Evidence: (e.g. screenshots of code and testing etc.)
@Coventry University
Page 2
Version 1
Explanation of the code: (in relation to methods/functions) Here I have a constructor and 2 functions. There are all created in the header file file and then called and created in the search.cpp file. The constructor is there to hold the variables data. There are 2 functions, bubbleSort and linearSearch. The first function organises the data alphabetically, and the second one finds the substring that the user entered within any of the strings if any at all. An object is then made in main.cpp to call on the linearSearch function, so that it prints off and works as it should.
Explanation of the code: (in relation to use of appropriate arrays/lists) I have used a vector to form an array of strings. The main reason the group decided to use vectors over a normal array is due to the fact vectors sizes can change dynamically, making it much easier to add more strings to the array. By using book.push_back, it is easy to add another string, so there isnt a lot of coding to be added in using this method. As there are ways that more strings can be added after the program is run, the code does not need to be changed each time a new book is added to the vector array. This makes it much easier to have large arrays that can be added to at any time. @Coventry University Page 3
Version 1
Explanation of the code: (in relation to loops) Using for loops in both my functions is essential. For the bubble sort, iteration is required to make sure that after each step, there isnt any more organising into alphabetical order required. In the algorithm of the bubbleSort function, it uses both the for loops to organise the files so that when it is searched for, no matter how many are returned, it will always come back alphabetical, making it easier for a user to find what they are looking for. Then, I used a for loop to compare each of the strings in the vector, so that it can return all possible values.
Seach.h file
1 //search.h file 2 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <string> 7 8 using namespace std; 9 10 class searchClass{ 11 private: 12 vector<string> book; only be used within the class 13 public: 14 searchClass(); 15 void bubbleSort(); 16 void linearSearch(); 17 };
@Coventry University
Page 4
Version 1
Search.cpp file
1 //search.cpp file 2 3 #include "search.h" 4 5 searchClass::searchClass() 6 { 7 book.push_back("Game of Thrones"); 8 book.push_back("Harry Potter"); //These variables are here as a test. They are here to test the functions, giving them a vector to work with 9 book.push_back("Lord of the Rings"); 10 book.push_back("Hunger Games"); 11 } 12 13 void searchClass::bubbleSort() //This calls the bubble sort function from the class in search.h 14 { 15 bool swapped; 16 for (int i = 1; i < book.size(); i++) 17 { 18 swapped = false; 19 for(int j = 0; j < book.size() - i; j++) 20 { 21 if (book.at(j) > book.at(j + 1)) //This is a bubble sort, this will organise all of the strings in the vector alphabetically 22 { 23 string temp = book.at(j); 24 book.at(j) = book.at(j + 1); 25 book.at(j + 1) = temp; 26 swapped = true; 27 } 28 } 29 } 30 } 31 32 void searchClass::linearSearch() //Like bubble sort function, this is called from the class in search.h 33 { 34 string bookSearch; 35 cout << "Please keep all searches in lower case" << endl << endl; 36 cout << "Please enter your search:" << endl; 37 getline(cin, bookSearch); //allows the input of a string from a user 38 cout << endl; 39 40 cout << "Your search returned the following:" << endl << endl; 41 42 for (string s : book) //The ranged based for loop is here 43 { 44 string holder = s; // this makes the string holder equal to s, so we can transform s and the original title will be unaffected 45 46 transform(s.begin(), s.end(), s.begin(), ::tolower); //this transforms all the letters of s to lower case, to compare with bookSearch 47 if ((s.find(bookSearch)) != string::npos) //This checks if bookSearch is a substing of s, by saying it is not equal to a substring not being found 48 { 49 cout << holder << endl; //This prints out holder, which has been uneffected by the transformation 50 } 51 } 52 }
Main.cpp file
1 // main.cpp file 2 3 #include "search.h" 4 5 int main(){ 6 searchClass searchClass; printed on the program 7 searchClass.linearSearch(); 8 }
@Coventry University
Page 5
Version 1
@Coventry University
Page 6