Nov 5, 2012 at 8:55am UTC
here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include <iostream>
#include <cstdlib>
#include <ctime>
#define ASIZE 25
#define SCALE 100
using namespace std;
void fillarray(int a[], int n);
void sortarray(int a[], int n);
void printarray(int a[], int n);
void initialize(int a[]);
int getrandom(int use[]);
int main()
{
int size=ASIZE; /* SIZE FOR ARRAY */
int array[size]; /* ARRAY OF DATA */
fillarray(array, size);
printarray(array, size);
sortarray(array, size);
printarray(array, size);
return 0;
}
/* SORT ARRAY OF SIZE N */
void sortarray(int a[], int n)
{
int r, s, t; /* LOOP ITERATERS */
int tmp; /* FOR SWAPPING */
for (r=n/2; r>0; r/=2) {
for (s=r; s<n; s++) {
tmp = a[s];
for (t=s; t>=r && tmp<a[t-r]; t-=r)
{
a[t] = a[t-r];
}
a[t] = tmp;
}
}
}
/* PRINT ARRAY OF SIZE N */
void printarray(int a[], int n)
{
for (int x=0; x<n; x++)
{
cout << a[x];
if (x<n-1)
cout << "," ;
}
cout << "\n" ;
}
/* FILL ARRAY OF SIZE N WITH RANDOM NUMBER */
void fillarray(int a[], int n)
{
int unique[SCALE]; /* array to make all unique */
initialize(unique);
srand((unsigned long int ) time(NULL));
for (int x=0; x<n; x++)
a[x]=getrandom(unique);
}
int getrandom(int use[])
{
int n;
do
n=rand()%SCALE;
while (use[n]==1);
use[n]=1;
return n;
}
/* FILL ARRAY WITH 0 TO GARANTEE UNIQUENESS */
void initialize(int a[])
{
for (int x=0; x<SCALE; x++)
a[x]=0;
}
Last edited on Nov 6, 2012 at 12:57am UTC
Nov 5, 2012 at 10:36am UTC
The year is 2012. The C++11 standard is in effect for over a year now. Your C++ program should look similar to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <algorithm>
#include <array>
#include <chrono>
#include <iostream>
#include <limits>
#include <ostream>
#include <random>
namespace {
const unsigned long int defaultArraySize = 25;
std::ostream & operator << (std::ostream &os, const std::array<int , defaultArraySize> &ai)
{
os << "std::array @ " << &ai << " contains:\n" ;
for (int i: ai)
os << i << ' ' ;
os << std::endl;
return os;
}
void fill(std::array<int , defaultArraySize> &ai)
{
std::default_random_engine dre(std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int > uid(std::numeric_limits<int >::min(), std::numeric_limits<int >::max());
std::generate(ai.begin(), ai.end(), [&dre, &uid]() -> int {return uid(dre);});
}
} // namespace
int main()
{
std::array<int , defaultArraySize> ai;
fill(ai);
std::cout << "Before:\n" << ai << std::endl;
std::sort(ai.begin(), ai.end());
std::cout << "After:\n" << ai << std::endl;
}
Last edited on Nov 5, 2012 at 10:46am UTC
Nov 5, 2012 at 12:20pm UTC
Catfish just because c++11 is out doesnt mean that professors will just teach their students only the sort feature. Which is probably the case here and he wants them to do it the old fashion way. Which is a good thing since its always good to know how things work.
Nov 5, 2012 at 3:59pm UTC
I just want to know my code and assignemt match?
Nov 5, 2012 at 4:18pm UTC
Write a well-documented C++ program
I would comment every line so that a NON programmer could read it and tell you what each statement does.
Nov 5, 2012 at 11:56pm UTC
you guys are confusing me
I just want to know my code and assignemt match?
Nov 6, 2012 at 12:10am UTC
No.
Specifically, I don't think that you are implementing bubble sort in your sortarray(). Follow the algorithm description carefully.
You are also supposed to be calling a swap() function for swapping values during sorting. You should not be swapping values directly within the sortarray().
There may be other things, but those two are clear.
Last edited on Nov 6, 2012 at 12:12am UTC
Nov 6, 2012 at 12:26am UTC
I didnt get it man sorry my english limited I didnt understand the assignment. Can someone pls help me ?