HELP!! Assignment due tonight! Attached is an overview of problem Problem 3 (20
ID: 3871222 • Letter: H
Question
HELP!! Assignment due tonight! Attached is an overview of problem Problem 3 (20 points). This problem requires the use of the Problem solving Process. Write an application which includes and algorithm that takes an array of any size, selects the high and low integer from the array of integers with each pass and builds a new array of integers by inserting the high and low selection with each pass. Your output should show the original array of integers and the output of each pass on a new line. Note: You can assume all integers are positive, but your code must work for an even and odd number of integers and an array of size from 5 to 30. Example Output nput Array: 42,24, 7, 13, 36, 52 Pass 1: 7, 52] Pass 2: (7, 13, 42, 52 Pass 3: [7, 13, 24, 36, 42, 52]Explanation / Answer
I Understand the Problem:
Problem is to find the lowest and highest number from an array. Next second lowest and second highest number and so on. In addition store print most lowest along with next lowest and highest in each pass.
II Design a Solution
Solution is designed by Arrays and Sorting. First an array is inputed. Ten it is sorted. With the helip of loops the passes are solved with old high/low and new high/low numbers
III Implement the solution
In Java
import java.util.Scanner;
public class High_Low
{
public static void main(String[] args)
{
int n,i,j,s,l;
int a[] = new int[n];
int s[] = new int[n];
int p[] = new int[n];
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for(i=0;i<n;i++)
{
s[i]=a[i];
}
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (s[i] > s[j])
{
s = s[i];
s[i] = s[j];
s[j] = s;
}
}
}
for(i=0;i<n;i++)
{
System.out.print("Input Array"+a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++;)
{
p[j++]=s[j++];
k=j;
}
for(j=k;j<i+j;j++;)
{
p[j++]=s[n--];
}
System.out.print("Pass:"+i+1);
for(l=0;l<i+j;l++)
{
System.out.print(p[l]);
}
printf("/n");
}
}
}
In C
main()
{
int a[100],n,i,j,s,l,s[100],p[50];
printf("Enter size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter n positive elements one by one");
scandf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
s[i]=a[i];
}
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (s[i] > s[j])
{
s = s[i];
s[i] = s[j];
s[j] = s;
}
}
}
for(i=0;i<n;i++)
{
printf("Input Array:%d ",a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++;)
{
p[j++]=s[j++];
k=j;
}
for(j=k;j<=i+j;j++;)
{
p[j++]=s[n--];
}
printf("Pass: %d:",i+1);
for(l=0;l<i+j;l++)
{
printf("%d",p[l]);
}
printf("/n");
}
}
IV Execute Test Plan
Input Array: 4 6 3 9 11 2
Pass 1: 2 11
Pass 2: 2 3 9 11
Pass 3: 2 3 4 6 9 11
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.