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

4.1 Instructions You need to write the code by yourself. Your implementation mus

ID: 3871177 • Letter: 4

Question

4.1 Instructions You need to write the code by yourself. Your implementation must use C/C++ and your code must run on the Linux machine general.asu.edu. Please refer to the programming guide (available under the Assignments folder on Blackboard) for using the general.asu.edu server, as well as compiling and running C/C++ code under Linux. For this question, you need to provide a Makefile that compiles your program to an executable named a2 that runs on the Linux machine general.asu.edu. Our TA will write a script to compile and run all student submissions; therefore, exccuting the command make in the Code folder must produce the executable a2 also located in the Code folder. 4.2 Requirements In this question, you will write a serial program that executes a sequence of commands that operate on individual files. Valid commands includo: Start Name, wherc Name is a character string of maximum length 20 alphabetic characters represcnting the name of the data file (Name.txt). The structure of Name.txt is an integer N indicating the total number of data entries contained in this file, followed by N additional lines of integers (the actual data entries). This command first reads N from the file Name.txt, dynamically allocates an array of integers of size N (if it has not already been allocated), and then reads the remaining N data entries into the array. The commands that follow until thc End command arc to be applicd to the resulting data array. The output of the Start command is Processing data from: Name.txt End Name, which indicates the end of the processing for the data in file Name.txt. The Start and End commands will always come in pairs with matching names. Any memory dynamically allocated for Name must be freed on an End command. The output of the End command is End of processing data from: Name.txt

Explanation / Answer

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int size, arr[50], i, j, temp;

cout<<"Enter Array Size : ";

cin>>size;

cout<<"Enter Array Elements : ";

for(i=0; i<size; i++)

{

cin>>arr[i];

}

cout<<"Sorting array using selection sort ... ";

for(i=1; i<size; i++)

{

temp=arr[i];

j=i-1;

while((temp<arr[j]) && (j>=0))

{

arr[j+1]=arr[j];

j=j-1;

}

arr[j+1]=temp;

}

cout<<"Array after sorting : ";

for(i=0; i<size; i++)

{

cout<<arr[i]<<" ";

}

getch();

}

#include <iostream.h>

int a[50];

void merge(int,int,int);

void merge_sort(int low,int high)

{

int mid;

if(low<high)

{

mid=(low+high)/2;

merge_sort(low,mid);

merge_sort(mid+1,high);

merge(low,mid,high);

}

}

void merge(int low,int mid,int high)

{

int h,i,j,b[50],k;

h=low;

i=low;

j=mid+1;

while((h<=mid)&&(j<=high))

{

if(a[h]<=a[j])

{

b[i]=a[h];

h++;

}

else

{

b[i]=a[j];

j++;

}

i++;

}

if(h>mid)

{

for(k=j;k<=high;k++)

{

b[i]=a[k];

i++;

}

}

else

{

for(k=h;k<=mid;k++)

{

b[i]=a[k];

i++;

}

}

for(k=low;k<=high;k++) a[k]=b[k];

}

void main()

{

int num,i;

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int size, arr[50], i, j, temp;

cout<<"Enter Array Size : ";

cin>>size;

cout<<"Enter Array Elements : ";

for(i=0; i<size; i++)

{

cin>>arr[i];

}

cout<<"Sorting array using selection sort... ";

for(i=0; i<size; i++)

{

for(j=i+1; j<size; j++)

{

if(arr[i]>arr[j])

{

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

}

}

}

cout<<"Now the Array after sorting is : ";

for(i=0; i<size; i++)

{

cout<<arr[i]<<" ";

}

getch();

}

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