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

In c++: 1.You will need at least four text files filled with random integer valu

ID: 3668905 • Letter: I

Question

In c++:

1.You will need at least four text files filled with random integer values. You don’t need a large number of values. Around 20. This will just make testing easier. Create a file with a random collection of the values; 1, 2, 3, 4, 5, 6, 7, 8, & 9. Save it. Make 3 copies. In each copy put in a single value of 0; towards the beginning of one file, around the middle of another and towards the end of the third. Rename each as appropriate, i.e. early, middle, etc. Keep one without a 0.

2. Search for the target value (i.e. 0). The book or the lecture has several examples of the code for searching algorithms. Implement one of them in a program that searches for the target value in your data files. Compare the results. You cannot use binary search yet. Why? Cite which one you used (page # or slide #).

3. Sort a set of values. The book or the lecture has several examples of the code for sorting algorithms. Implement one of them in a program. Your program should write the sorted results into a file using a name the user provides. Sort all input files and compare the results. Cite which one you used (page # or slide #).

4. Search for the target value, redux. Find an algorithm for binary search. Implement it in a program that searches for the target value in your data file. Remember that you cannot use any of the original files. Cite the reference for the algorithm you use. NOTE: Code is NOT an algorithm. NOTE: You can implement each as a separate program or create a single program giving the user the choice of which activity to perform

Explanation / Answer

//Program:

#include <iostream>
#include <fstream>
#define len 10
using namespace std;

/* Search for the target value (i.e. 0) using linear search algorithm.*/
void linearSearch(int target,string file)
{
   int flag=0;
   ifstream in(file.c_str());
   int num;
   int comp=0;
   while(!in.eof())
   {
       in>>num;
       comp++;
       if(num==target)
       {
           cout<<endl<<"Target is found in file "<<file;
           cout<<endl<<"Number of comparisions: "<<comp;
           flag=1;
           break;
       }
   }
   if(flag==0)
   cout<<endl<<"Target is not found in file "<<file;
}
/*Sort a set of values. in a program. write sorted results into a file using a name the user provides.
Sort all input files and compare the results.*/
/* Sorting using selection sort*/

void SelectionSort(string inputfile,string outputfile)
   {
       //read numbers from file and store it in array
       ifstream in(inputfile.c_str());
       int num,i=0;
       int data[len+1];
       int comp=0;
       while(!in.eof())
       {
       in>>num;
       data[i++]=num;
   }
   //sorting
int j, first, temp;
i=0;
int numLength = len+1;
for (i= numLength - 1; i > 0; i--)
{
first = 0; // initialize to subscript of first element
for (j=1; j<=i; j++) // locate smallest between positions 1 and i.
{
   comp++;
if (data[j] < data[first])
first = j;
}
temp = data[first]; // Swap smallest found with element in position i.
data[first] = data[i];
data[i] = temp;
}
cout<<endl<<inputfile<<" is sorted after "<<comp<<" number of comparisions.";
//storing sorted data in user entered file name
ofstream out(outputfile.c_str());
for(i=0;i<len+1;i++)
out<<data[i]<<" ";
cout<<endl<<"Sorted values stored in "<<outputfile;

return;
}

int main()
{
   /*Create a file with a random collection of the values;*/
   ofstream f1("OrgFile.txt");
   /* store values without a 0.*/
   for(int i=0;i<len;i++)
   {
       f1<<(i+1)<<" ";
   }
   f1.close();
   // making 3 copies of file text1.txt
  
   ofstream f2("early.txt");
   ofstream f3("middle.txt");
   ofstream f4("last.txt");
   ifstream in("OrgFile.txt");
   int num=0;
   int count=0;
   /*put in a single value of 0; towards the beginning of file*/
   f2<<num<<" ";
   while(!in.eof())
   {
       in>>num;
       /*put in a single value 0 around the middle in text3.txt */
       if(count==len/2)
       {
           f3<<"0"<<" ";
       }
       f2<<num<<" ";
       f3<<num<<" ";
       f4<<num<<" ";
       count++;
   }
   /*put in a single value 0 towards the end of the third text4.txt */
   f4<<"0"<<" ";
   in.close();
   /* Search for the target value (i.e. 0) using linear search algorithm.*/
   cout<<endl<<"Result of searching the target value 0, using linear search algorithm.";
   linearSearch(0,"early.txt");
   linearSearch(0,"middle.txt");
   linearSearch(0,"last.txt");
  
   /* Search for user entered target value using linear search algorithm.*/
   cout<<endl<<"Result of searching user entered target value, using linear search algorithm.";
   int target;
   cout<<endl<<"Enter a target value to search: ";
   cin>>target;
   linearSearch(target,"early.txt");
   linearSearch(target,"middle.txt");
   linearSearch(target,"last.txt");
  
   /*Sorting file contents and write in to user entered file name*/
   cout<<endl<<"Result of Sorting the files using selection sort algorithm";
   string outputfile;
   cout<<endl<<"Enter a file name to store result: ";
   cin>>outputfile;
   SelectionSort("early.txt",outputfile);
   cout<<endl<<"Enter a file name to store result: ";
   cin>>outputfile;
   SelectionSort("middle.txt",outputfile);
   cout<<endl<<"Enter a file name to store result: ";
   cin>>outputfile;
   SelectionSort("last.txt",outputfile);
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote