Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CIS22B C++ Lab 1 Q1: Write a program that will - 1. Read in the contents of a te

ID: 3710724 • Letter: C

Question

CIS22B C++ Lab 1

Q1: Write a program that will - 1. Read in the contents of a text file up to a maximum of 1024 words you create your own input. When reading the 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort 3. Search any item input by user in your sorted list using the Binary Search algorithm implemented in its own function. file contents, you can discard words that are single characters to avoid symbols, special characters etc. algorithm implemented in its own function. 4. Use string comparisons as taught in CIS 22A for comparing/ ordering words, i.e. words starting with numbers sort lower than words starting with uppercase letters which are lower than words starting with lowercase letters. So a word appearing once with one set of case is different than its second appearance with a different set of case, e.g. 'Do and 'do' are not the same 5. If a word appears twice using exactly the same case, it can be stored twice side-by-side in the array and either index can be returned in the search. 6. Your program will o first ask the user for a location+name from where to read the file and location+name where to save the output o read the contents into an array, ignoring single character words, o sort the contents of the array in alphabetically ascending order and o then start a loop to allow the user to search for one or more words in the array your loop should have an appropriate exit condition. If the word is found, the program should output which array location the word was found in, if not found then it should output an appropriate message o Provide clear prompts as necessary for good user interactivity 7. Your output should be sent to both screen and an output file concurrently. Screen output should contain the entire user interaction. The file output should contain all the user interaction that went to the screen as well as the listing of the sorted array cin/cout should only be in those 3 functions. Compress all of these into a single ZIP file format to upload 8. User interactivity should be limited to your main, input and/or output functions only what that means is your 9. Your output will consist of at least 4 files your CPP file, your text input, the screen output and file outputs 10. I will test your program with my own input file This assignment is worth 20 points 1.4 points for sort and search functions combined 2. 6 points for the main program code and input/output, including clarity of user interaction. 3. 4 points for code documentation not including pseudocode 4. 4 points for the pseudo-code and its resemblance to the actual code 5. 2 points for following all submission related instructions carefully

Explanation / Answer

here is your program : ------------------->>>>>>>>>

#include<iostream>
#include<fstream>
#include<string>
#define MAX 1024

using namespace std;

void readFile(string arr[MAX],int &n,ifstream &ifs){
n = 0;
string s;
if(ifs.is_open()){
  ifs>>s;
  while(!ifs.eof()){
   if(s.length() > 1){
    arr[n] = s;
    n++;
   }
   if(n >= MAX){
    break;
   }
   ifs>>s;
  }
  ifs.close();
}
}

void selectionSort(string arr[MAX],int n){
string small = "";
small.reserve(30);
int pos = 0;
string temp = "";
temp.reserve(30);
for(int i = 0;i<n;i++){
  small = arr[i];
  pos = i;
  for(int j = i+1;j<n;j++){
   if(small > arr[j]){
    small = arr[j];
    pos = j;
   }
  }
  
  if(pos != i){
   temp = arr[pos];
   arr[pos] = arr[i];
   arr[i] = temp;
  }
}
}

void printWords(string arr[MAX],int n,ofstream &ofs){
cout<<" Words Are : ";
ofs<<" Words Are : ";
cout<<endl;
ofs<<endl;
for(int i = 0;i<n;i++){
  cout<<arr[i]<<" ";
  ofs<<arr[i]<<" ";
}
cout<<endl;
ofs<<endl;
}

int BinarySearch(string arr[MAX],int n,string word){
int i = 0;
int j = n-1;
int mid;
while(i < j){
  mid = (i+j)/2;
  if(word < arr[mid]){
   j = mid-1;
  }else if(word > arr[mid]){
   i = mid + 1;
  }else{
   return mid;
  }
}
return -1;
}

int main(){
string words[MAX];
int n = 0;
string ifile;
string ofile;
cout<<" Enter The File Name to extract words : ";
cin>>ifile;
cout<<" Enter the File name where to output the results : ";
cin>>ofile;
//opening files
ofstream ofs;
ifstream ifs;
ifs.open(ifile.c_str());
if(!ifs.is_open()){
  cout<<" Input File Opening error !!! ";
  exit(0);
}
//reading words from the file
readFile(words,n,ifs);
ofs.open(ofile.c_str());
if(!ofs.is_open()){
  cout<<" Output File Creating error ";
  exit(0);
}
//sorting
selectionSort(words,n);
//printing words
printWords(words,n,ofs);
string check;
check.reserve(6);
string word;
word.reserve(30);
int ind = -1;
while(true){
  cout<<" Enter The Word To Search : ";
  ofs<<" Enter The Word To Search : ";
  cin>>word;
  ofs<<word<<endl;
  ind = BinarySearch(words,n,word);
  if(ind == -1){
   cout<<" The Word Not Found in words array : ";
   ofs<<" The Word Not Found in words array : ";
  }else{
   cout<<" The Word Found at the "<<ind<<"th location in words Array ";
   ofs<<" The Word Found at the "<<ind<<"th location in words Array ";
  }
  cout<<" Do you want to search more ... (yes,no)";
  cin>>check;
  if(check == "no"){
   break;
  }
}
ofs.close();
ifs.close();

return 0;
}