Question

In: Computer Science

I am trying to sort a group of 10 randomly generated numbers from least to greatest...

I am trying to sort a group of 10 randomly generated numbers from least to greatest in c++.

Right now, my code isn't working and is giving odd results. There is extra unecessary code since I was trying to find out what wasn't working.

In this example I am trying to use the bubble sort method but bucket sort would also be okay. Please keep the format the same and don't use any other functions not currently in the code, thanks.

void arraysort()

{

const int N = 10;

int x[N];

for(int i = 0; i < N; i++)

{

x[i] = 1 + gRandom-> Rndm() * 10;

cout<<x[i]<<" ";

        }

cout<<endl;

int temp;

for(int i = 0; i < N-1; i++)

{

if(x[i] > x[i+1]){

cout<<x[i]<<", "<<x[i+1]<<endl;

temp = x[i];

x[i] = x[i+1];

x[i+1] = temp;

continue;

}

if(x[i] < x[i+1]){

cout<<"here"<<", "<<x[i]<<", "<<x[i+1]<<endl;

continue;

}

}

cout<<endl<<endl<<endl;

for(int i = 0; i < N; i++)

{

cout << x[i] << " ";

}

Solutions

Expert Solution

#include<iostream>
#include <stdlib.h>
#include<time.h>
#include <cstdlib>
using namespace std;

void arraysort()

{

const int N = 10;
srand(time(NULL));
int x[N];
cout<<"Before sorting: ";
for(int i = 0; i < N; i++)
{
x[i] = 1 + rand() * 10;
cout<<x[i]<<" ";
}
cout<<endl;
cout<<"After sorting: ";
int temp;
for(int i=0; i<N; i++)
{
for(int j=i+1; j<N; j++)
{
//If there is a smaller element found on right of the xay then swap it.
if(x[j] < x[i])
{
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
cout<<endl;
for(int i = 0; i < N; i++)
{
cout << x[i] << " ";
}
cout<<endl;
}

int main()
{


arraysort();
}


Related Solutions

I am to write a code that uses two queues, and print the generated random numbers...
I am to write a code that uses two queues, and print the generated random numbers and content of both queues. I need to use the enqueue and dequeue functions in the code.  The instructions are below. Program should be in C Instructions: Generate n random numbers with values between 10 - 100. Note: n>9 if u write a function (for example, called generateRand) to do this - what is the data or input we have to give it? Create a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
how would you use randomly generated numbers to find 30 random numbers from 1 to 500?
how would you use randomly generated numbers to find 30 random numbers from 1 to 500?
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
1. I am trying to determine the level of measurement of my data type? I am...
1. I am trying to determine the level of measurement of my data type? I am looking for advice on Nominal, Ordinal, Interval, and Ratio 2. Does the data set have any categorical variables? I am trying to Describe the data set below in very general terms? This data consist of 8 variables: Which are GRE Scores, TOEFL Scores, University Rating, Statement of Purpose, Letter of Recommendation Strength, Undergraduate GPA, . Research Experience, and Chance of Admit. Name Type Description...
I am trying to start saving for retirement. I am investing all my cash into the...
I am trying to start saving for retirement. I am investing all my cash into the S&P 500, which will assume consistently 9.8% interest, compounded annually. I initially put a lump sum of $100 into my account, and I will deposit $10 every second week. a) After 10 years, how much money will I have invested? b) After 10 years, if I sold all of my stocks, how much money will I have in my account? c) After 25 years,...
I am trying to create a classified balance sheet and I am unsure what is involved...
I am trying to create a classified balance sheet and I am unsure what is involved when reporting the current assets, liabilities and owners equity?
JavaScript (HTML) Task: Please read 10 numbers that are list below, sort the numbers and then...
JavaScript (HTML) Task: Please read 10 numbers that are list below, sort the numbers and then print those numbers. 10 numbers = { 9, 3, 2, 1, 10, 30, 4, 6, 7, 8} [Reference JavaScript code] <html> <body>     <H1>prompt()</h1>     <p id="pro"></p>     <script>        // Array creation        var num= new Array();               var ix = 0;        // Read 10 numbers        for (ix = 0; ix < 10; ix++)        {num[ix] = prompt("Enter a number");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT