Honestly, I don't even know where to begin. I've searched the whole internet for clues and my lecturer's notes are a joke and i'm seriously so desperate right now! If anyone could help at all i'd really appreciate it, even to get it started pleaseeeeeee. This is what i've been asked to do:
Write a program that:
•1) Creates an array containing 100 random double numbers in the [0,250) range;
•2) Sorts them in descending order;
•3) Prints the contents of the sorted vector out. The sorting should be coded as a separate function double* sort_array(double* ).
Even if I could do the first two points, that'd be something.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
int number;
I don't even have a clue what to write after that; i'm presuming there's some double random() function or something
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
Verified answer
Answers from the Internet aren't usually helpful: the requirements are purposefully removed from real life, so a solution written by an engineer won't fit your class requirements, and a solution written by a student that is following a different textbook will be obviously different as well.
Here's a engineering solution:
#include <iostream>
#include <vector>
#include <random>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
random_device seed;
mt19937 rnd(seed());
uniform_real_distribution<> d(0, 250);
std::vector<double> data(100);
generate(data.begin(), data.end(), bind(d, rnd));
sort(data.begin(), data.end(), greater<double>());
for(double d: data)
cout << d << '\n';
}
online demo (edited to compile on IDEone's outdated gcc): http://ideone.com/767K90
Used the source link for 99.999% of the problem, so you will need to put your own feel to it. What needed changing was that the compare sorted ascending, so changed > (greater than) to < (less than). With the random numbers srand() and rand() as a combination, on their own, produce huge numbers but the % operator with 251 tells you that the number will be in the range 0 to 250 inclusive, because % is like "remainder of". The SIZE constant needed adjusting to fit in with your requirement for 100 numbers. And finally reread your question and realized you wanted double numbers, so rewrote identical swap() and compare() functions for double (this is called overloading ... take a look) ... and made data[] array a double ... and with the rand() call cast it to double ... that is the (double) bit ... casting takes a different type and plonks the value onto a datatype that you cast ... if this is possible?!
#include <iostream>
#include <cstdlib>
using namespace std;
const int SIZE = 50;
int globalCount = 0;
bool compare(int, int);
void swap(int&, int&);
bool compare(double, double);
void swap(double&, double&);
bool compare(int n1, int n2)
{
globalCount++;
if ( n1 < n2)
return true;
else
return false;
}
void swap (int& n1, int& n2)
{
int temp;
temp = n1;
n1 = n2;
n2 = temp;
}
bool compare(double n1, double n2)
{
globalCount++;
if ( n1 < n2)
return true;
else
return false;
}
void swap (double& n1, double& n2)
{
double temp;
temp = n1;
n1 = n2;
n2 = temp;
}
int main( )
{
// declare and initialize a large array
double data[SIZE];
srand(0); // seed the random number generator
for (int i = 0; i < SIZE; i++)
{
data[ i ] = (double)(rand( ) % 251);
}
cout << "\nPrint out the unsorted array:";
for (int i = 0; i < SIZE; i++)
{
cout << "\ndata[ " << i << " ] = " << data[ i ];
}
cout << endl;
//Bubble Sort Starts!
int j = 0;
bool again = true;
while ((j<SIZE-1 && again == true))
{
int count = 0;
again = false;
for (int i = 0; i < SIZE-1-j; i++) // index for inner loop is i
{
if (compare(data[i],data[i+1]) == true)
{
swap( data[ i ], data[ i + 1] );
count++;
}
}
if (count > 0)
again = true;
j++;
}
// print out the sorted array and the value of globalCount
for (int i = 0; i < SIZE; i++)
{
cout << "\ndata[ " << i << " ] = " << data[ i ];
}
cout << "\n\n";
cout << globalCount << " Comparison's were done to sort the array.\n";
system("PAUSE");
return 0;
}