1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
yulyashka [42]
2 years ago
8

1. Create an array of ints with 20 elements. With a random number generator, seeded with time, assign a random number between 1

and 100 to each element of the array. Using the selection sort algorithm discussed in class, sort the array from smallest to largest. The sorting of the array should be done inside a user defined function. This function should not return any values to main using a return statement. It should have the following parameters: the array, and the number of elements in the array. Only the sorting should be done in the function. Everything else (including the random number generation, the printing of pre-sorted array, and the printing of the post-sorted array) should be executed in main. Print to the screen the array in a single line before it is sorted. Print to the screen the array in a single line after it is sorted. Sample output on a similar problem with a 5 element array is: Original array 36 47 54 82 15 Sorted array 15 36 47 54 82

Computers and Technology
1 answer:
nikdorinn [45]2 years ago
5 0

Answer:

#include <iostream>

#include<time.h>

using namespace std;

void sort(int a[20],int n)   //selection sort

{

   int i,j,temp;

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

   {

       for(j=i+1;j<n;j++)  //finds minimum element

       {

           if(a[i]>a[j])     //swaps minimum element to its right place

           {

               temp=a[i];

               a[i]=a[j];

               a[j]=temp;

           }

       }

   }

}

int main()

{

   srand(time(NULL));

   int arr[20],n=20,i;

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

       arr[i]=rand() % 100 + 1;    //assigns random number to each array element

   cout<<"Before sorting\n";

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

       cout<<arr[i]<<" ";   //prints array before calling sort func

   sort(arr,20);     //sort the passed array

   cout<<"\nAfter sorting\n";

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

       cout<<arr[i]<<" ";  //prints array after sorting

   return 0;

}

OUTPUT :

Please find the attachment below.

Explanation:

The program above first assigns random number from 1 to 100 to each element of array using rand(). srand(time(NULL)) is used to generate new number every time otherwise same random number will be produced at a time. Then sorting method is called in which selection sort algorithm is used.

Selection sort algorithm finds the minimum element in each pass and place it into its right position. For example - 34,54,12,5 is passed

After Pass 1 - 5  54  12  34

After Pass 2 - 5  12  54  34

After Pass 3 -  5  12  34  54

You might be interested in
Which action could be used to determine if a host is compromised and flooding traffic onto the network?
timofeeve [1]

<span>Disconnect the host from the network. 
</span><u>if that doesnt work then try these options</u> 
Check the host hard drive for errors and file system issues.
<span>Examine the Device Manager on the host for device conflicts
</span>Unseat and then reconnect the hard drive connectors on the host.

4 0
3 years ago
The Polish mathematician Wacław Sierpiński described the pattern in 1915, but it has appeared in Italian art since the 13th cent
soldi70 [24.7K]

Answer:

/ Sierpinski.java

public class Sierpinski {

     // method to find the height of an equilateral triangle with side length =

     // length

     public static double height(double length) {

           // formula= length*sqrt(3)/2

           double h = (length * Math.sqrt(3)) / 2.0;

           return h;

     }

     // method to draw a filled triangle (pointed downwards) with bottom vertex

     // x,y

     public static void filledTriangle(double x, double y, double length) {

           // finding height

           double h = height(length);

           // filling triangle as a polygon

           // passing x, x-length/2, x+length/2 as x coordinates

           // and y, y+h, y+h as y coordinates

           StdDraw.filledPolygon(new double[] { x, x - (length / 2.0),

                       x + (length / 2.0) }, new double[] { y, y + h, y + h });

     }

     // method to draw an n level sierpinski triangle

     public static void sierpinski(int n, double x, double y, double length) {

           // checking if n is above 0 (base condition)

           if (n > 0) {

                 // drawing a filled triangle with x, y as bottom coordinate, length

                 // as side length

                 filledTriangle(x, y, length);

                 // drawing the triangle(s) on the top

                 sierpinski(n - 1, x, y + height(length), length / 2);

                 // drawing the triangle(s) on the left side

                 sierpinski(n - 1, x - (length / 2.0), y, length / 2);

                 // drawing the triangle(s) on the right side

                 sierpinski(n - 1, x + (length / 2.0), y, length / 2);

           }

     }

     public static void main(String[] args) {

           // parsing first command line argument as integer, if you dont provide

           // the value while running the program, this program will cause

           // exception.

           int n = Integer.parseInt(args[0]);

           // length of the outline triangle

           double length = 1;

           // finding height

           double h = height(length);

           // drawing a triangle (pointed upwards) to represent the outline.

           StdDraw.polygon(new double[] { 0, length / 2, length }, new double[] {

                       0, h, 0 });

           // drawing n level sierpinski triangle(s) with length / 2, 0 as x,y

           // coordinates and length / 2 as initial side length

           sierpinski(n, length / 2, 0, length / 2);

     }

}

7 0
2 years ago
HELP! Answer to question 2?
Vesnalui [34]
One advantage of Binary Search Algorithm: 
Compared to Linear search it is much faster because linear search starts its searching right from the first value but binary searches for something by splitting the array in two again and again and again. 

One disadvantage of Binary Search Algorithm:
Have to be sorted, unlike linear search that doesn't have to be. 
4 0
3 years ago
Match the order in which you should develop a plan:
Advocard [28]

Develop

Present

Approve

Brainliiest please?

4 0
2 years ago
We need goku and naruto fight story someone
lana [24]

Answer:

goku comes in and turns super sayain  naruto come in with kurama and naruto get goku with a rasagen and geku is still fist fightin while naruto is using his kurama and turns into kurama and knocks out goku now naruto thinks he won and goku IS HITIN HIM WITH A KAMEKAMEHA GOKU IS SIROUS NOW

Explanation:

8 0
2 years ago
Read 2 more answers
Other questions:
  • What is the meaning for science?
    14·1 answer
  • A company is utilizing servers, data storage, data backup, and software development platforms over an Internet connection.
    14·1 answer
  • Which of the following is true regarding packaged software and custom software? Group of answer choices Packaged software are ap
    13·1 answer
  • Over the past few years a very definite need has arisen in the electrical trades for:
    15·1 answer
  • How can you solve real world mathematical problems using two linear equations in two variables​
    11·1 answer
  • Cnt115-02* which of the following is a private ip address and can't be routed across the internet?
    12·1 answer
  • What is the process of adding random characters at the beginning or end of a password to generate a completely different hash ca
    10·1 answer
  • You need to replace a broken monitor on a desktop system. You decide to replace it with a spare monitor that wasn't being used.
    15·2 answers
  • What is the correct format to use when<br> inserting a date in Excel?
    15·1 answer
  • The ________ approach to motivation suggests a connection between internal physical states and outward behavior. achievement dri
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!