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

4 Programming Question (40 points) 4.1 Instructions You need to write the code b

ID: 3883392 • Letter: 4

Question

4 Programming Question (40 points) 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, executing the command make in the Code folder must produce the executable a2 also located in the Code folder. 4.2 Reauirements In this question, you will write a serial program that executes a sequence of commands that operate on individual files. Valid commands include: » Start Name, where Name is a character string of maximum length 20 alphabetic characters representing 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 the End command are to be applied 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<stdio.h>

#include<stdlib.h>

#include<time.h>

int main()

{

int array[10];

int i,j,n,temp;

n=10;

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

array[i]=rand();

for(i=1;i<n;i+=){

j=1;

while((j>0)&&(array[j-1]>array[j])){

temp=array[j-1];

array[j-1]=array[j];

array[j]=temp;

j--;

}

printf("sorted array ");

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

printf("%d ",array[i]);

return 0;

}

merge sort:

void mergesort(int low,int high){

if(low<high){

int mid=(low+high)/2;

mergesort(low,mid);

mergesort(mid+1,high);

mergesort(low,mid,high);

}

}

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

int h=low,i=low,j=mid+1,k;

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

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

b[j]=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];

}

selection sort:

void swap(int *x,int *y){

int temp=*x;

*x=*y;

*y=temp;

}

void selectionsort(int arr[],int n)

{

int i,j,minIndex;

for(i=0;i<n-1;i++){

minIndex=i;

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

if(arr[j]<arr[minIndex])

minIndex=j;

swap(&arr[minIndex,&arr[i]);

}

}

void printArray(int arr[],int size)

{

int i;

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

printf("%d",arr[i]);

printf(" ");

}

int main(){

int arr[]={9,4,2,3,6,5,7,1,8,0};

int n=sizeof(arr)/sizeof(arr[0]);

selectionsort(arr,n);

printf("sorted array: ");

printf(arr,n);

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