Sample ints-out.txt 1 2 3 3 4 4 5 6 6 7 8 9 -1 Your solution shall implement thi
ID: 3547798 • Letter: S
Question
Sample ints-out.txt
1 2 3 3 4 4 5 6 6 7 8 9 -1
Your solution shall implement this pseudocode.
Function Merge (In: a as array of ints, b as array of ints; Out: c as array of ints; In: n as int) Returns Nothing
Define int variables named i, j, and k all initialized to 0
While i is less than n or j is less than n Do
If i is less than n and j is less than n and ai is less than or equal to bj Then
ck ? ai
Increment i
Else If i is less than n and j is less than n and ai is greater than bj Then
ck ? bj
Increment j
Else If i is less than n Then
ck ? ai
Increment i
Else
ck ? bj
Increment j
End If
Increment k
End While
End Function Merge
Function ReadFile (Out: a as array of ints, b as array of ints, n as int) Returns Nothing
Define an ifstream object named fin and open "ints-in.txt" for reading
Call ReadList(a, n)
Call ReadList(b, n)
Close the input file
End Function ReadFile
Function ReadList (InOut: fin as ifstream; Out: a as array of ints, n as int) Returns Nothing
n ? 0
Sentinel loop that reads a number from fin until the sentinel is reached
an ? number
Increment n
End Loop
End Function ReadList
Function WriteFile (In: a as array of ints, n as int) Returns Nothing
Define an ofstream object named fout and open "ints-out.txt" for writing
Vary loop (for loop) that varies an index variable i from 0 to n-1
Send ai to fout followed by a space
End Loop
Send -1 to fout
Close the output file
End Function WriteFile
Function Main () Returns Nothing
Define three int arrays: a of size 100, b of size 100, and c of size 200
Define an int variable named n
Call ReadFile(a, b, n)
-- The Sorter class is in the Sorter.zip archive on the course website.
Define and create a Sorter object named sorter
Call SelectionSort() on the sorter object to sort a into ascending order
Call BubbleSort() on the sorter object to sort b into ascending order
Call Merge() to merge the sorted arrays a and b
Call WriteFile() to write c to "ints-out.txt"
End Function Main
3 Code Template
//**************************************************************************************************************
// FILE: a04.cpp
//**************************************************************************************************************
#include <fstream>
#include <iostream>
#include "Sorter.hpp"
using namespace std;
// Write the function header for Merge()
???
{
int i = 0, j = 0, k = 0;
while (i < n || j < n) {
// Write the if-elseif-elseif-... statement in the body of the while loop
???
}
}
void ReadFile(int a[], int b[], int& n)
{
ifstream fin("ints-in.txt");
ReadList(fin, a, n);
ReadList(fin, b, n);
fin.close();
}
// Write the function header for ReadList()
???
{
n = 0;
int number;
// Implement the sentinel loop
???
}
void WriteFile(int a[], int n)
{
ofstream fout("ints-out.txt");
// Write a vary loop where variable i varies from 0 to n - 1. In the loop send a[i] and ' ' to fout.
???
fout << -1;
fout.close();
}
int main()
{
// Define int arrays a, b, and c and int variable n.
???
// Call ReadFile() passing a, b, and n
???
// Define and create a Sorter object named sorter
???
// Call SelectionSort() on sorter to sort a into ascending order.
// Hint: to access the ASCENDING constant data member of the Sorter class you write Sorter::ASCENDING.
???
// Call BubbleSort() on sorter to sort b into ascending order
???
// Call Merge() passing a, b, c, and n
???
// Call WriteFile() passing c and 2n
???
return 0;
}
Explanation / Answer
#include<iostream>
#inclue<fstream>
#include "Sorter.hpp"
using namespace std;
void Merge(int a[],int b[],int c[],n)
{
int i=0,j=0,k=0;
while(i<n||j<n)
{
if(i<n&&j<n&&a[i]<=b[j])
{
c[k] = a[i];
i++;
}
else if(i<n&&j<n&&a[i]>b[j])
{
c[k]=b[j];
j++;
}
else if(i<n)
{
c[k] = a[i];
i++;
}
else
{
c[k] = b[j];
j++;
}
k++;
}
}
void ReadFile(int a[],int b[],int *n)
{
ifstream fin("ints-in.txt");
ReadList(fin,a,n);
ReadList(fin,b,n);
fin.close();
}
void ReadList(ifstream fin,int a[],int *addr)
{
int n=0,num;
while(!fin.eof())
{
a[n] = fin>>num;
n++;
}
*addr = n;
}
void WriteFile(int a[],int n)
{
ofstream fout("ints-out.txt");
for(int i=0;i<=n-1;i++)
{
fout<<a[i]<<' ';
}
fout<<-1;
fout.close();
}
void main()
{
int a[100],b[100],c[100];
int *n;
ReadFile(a,b,n);
Sorter sorter;
sorter.SelectionSort(a);
sorter.BubbleSort(b);
Merge(a,b,c,*n);
WriteFile(c,2*n);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.