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

write a pseudo code for the following code #include <iostream> #include <fstream

ID: 3778740 • Letter: W

Question

write a pseudo code for the following code

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <climits>
#include <stdio.h>

using namespace std;

int swapBubble=0;

void swap(int *a,int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
  
return;
}

void bubbleSort(int array[100],int n)
{
cout<<"Elements:"<<n<<endl;
const char *filename = "fileBubble.txt";
FILE *output = fopen( filename, "w" );
bool swapped=false;
for(int i=0;i<n;i++)   
{
swapped=false;
for(int j=0;j<n;j++){
if(array[j]>array[j+1])
{
swap(&array[j],&array[j+1]);
swapped=true;
swapBubble++;
}
}
if(!swapped)
break;

}
int k=0;
while(k<n )
{   
fprintf(output,"%d ",array[k]);   
if((k+1)%10==0 ) fprintf(output, " " );
k++;
}
fclose( output);
cout<<"Filename:"<<filename<<endl;
cout<<"Number of integers:"<<n<<endl;
cout<<"Number of swaps:"<<swapBubble<<endl;

}
int main() {
   const char *delim = " ";
const char *filename = "file.txt";
FILE *input = fopen( filename, "r" );
char buffer[ 1024 ];
char *tok;
int array[100],i=0;
if( input == NULL ){

fprintf( stderr, "File Not opened!!!! ");

}else{

while( fgets(buffer, 1024, input) != NULL ){

tok = strtok( buffer, delim );
while( tok != NULL ){
printf( "%s ", tok );
int x=atoi(tok);
array[i++]=x;
tok = strtok( NULL, delim );
}

}}

if( ferror(input) ){
perror( "Error:" );
}

fclose( input);

bubbleSort(array,i-1);

   return 0;
}

Explanation / Answer

Pseudo Code:
1. Open a file in read mode.
2. Print error message if the file is empty.
3. Read file line by line
   a. separate every input in tok from space as delimiter
   b. convert tok to integer and store it in array.
4. repeat step 3 till file is completely read.
5. call bubbleSort function with array and no. of elements in array.
6. open a file in write mode.
7. traverse all the elements of array.
   a. set swapped to false.
   b. start inner loop to traves the array.
       i. check if the order of elements in array is ascending order ie jth element is smaller the (j+1)th element.
       ii. if point i. is true the continue traversing.
       iii. if point i. returns false swap the elements and set swapped to true and increase the count by 1.
   c. repeat step 7.b till the entire array is travesed.
   d. if no swapping is performed in inner loop exit outer loop.
8. repeat step 7 till either it breaks out of the loop or the array is completely traversed.
9. print the sorted elements of array in file with a new line character after every 10 elements.
10. print file name, number of elements and number of swaps to stdout.