template

I need to divide the random number by 10,000 when it runs as doublearray. Can someone point me in the right direction?

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
#include<iostream>
#include<iomanip>
//#include<math.h>
#include<cstdlib>
#include<ctime>
//#include<fstream>

const int N=1000;

using namespace std;

template <class type>

void Bubble(type &ta)
{
    //ofstream outfile("TriBubble.txt");
    //{
    for(int a=0; a<N; a++)
    {
        ta[a]=rand()%N;
    }
    for(int b=0; b<N; b++)
    {
        for(int c=0; c<N; c++)
        {
            if (ta[c]>ta[c+1])
            {
                swap(ta[c],ta[c+1]);
            }
        }
    }
    for(int e=0; e<N; e++)
    {
        cout<<ta[e]<<" ";
    }
    cout<<endl;
    //}
    //outfile.close();
}

void swap (int &a, int &b)
{
    int temp;
    temp=a;
    a=b;
    b= temp;
}

int main()
{
    int a,b;
    int intarray[N];
    char chararray[N];
    double doublearray[N];
    Bubble(intarray);
    Bubble(chararray);
    Bubble(doublearray);
}
Last edited on
I'm afraid you've confused me. I don't understand what you want.

What do you mean by "random number"? What random number? Why do you have to divide it by 10,000?

Also, your bubble sort function is doing waaay too many things. All it should do is sort your array, nothing else. No file I/O, no random initializations, no printing, nothing -- just sorting. Make other functions for those things.

Also, you should declare the function as:

1
2
3
4
5
template <class T, size_t N>
void Bubble(T (&ta)[ N ] )
{
    // sorts ta[] here...
}

That way, you can call it with any array:

1
2
3
4
5
6
7
8
9
10
  int    intarray[] = { 7, 2, 3, 9, 5, 8, 1, 4, 6 };
  double doublearray[ 500 ];

  random_fill(doublearray);  // you would need to write this function

  Bubble(intarray);
  Bubble(doublearray);

  print(cout,intarray);  // you would need to write this function also
  print(cout,doublearray);

Hope this helps.
I wanted to know how I could have "rand()%N" divide by 10,000 because all of the doublearray values have to be divided by 10,000
I still don't know what you are getting at. Why can't you just divide them by 10000?
If I put in divide by 10000 it will do that for all of my other Bubbles and I only want it to divide when type is double. Do you know the if statement that I could use to tell it to divide by 10000 when type is a double?
No. Stop trying to do too many things at once.

Your main function should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  int intarray[N];
  char chararray[N];
  double doublearray[N];

  srand( (unsigned)time( 0 ) );

  random_fill(intarray);
  random_fill(chararray);
  random_fill(doublearray);
  for (size_t n = 0; n < N; n++)
    doublearray[n] /= 10000;

  Bubble(intarray);
  etc

Every function should do one thing.

Good luck!
Thank you!
Topic archived. No new replies allowed.