using a header template

hi, i need to input a certian amount of data, and then put it in order so that i can find the median. to do this, ive been given this header template that i need to call and use.

this is in the given header file, i need to call this function to order the input data in order.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T>
int addInOrder (T* array, int& size, T value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved >= 0 && value < array[toBeMoved]) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[toBeMoved+1] = value;
  ++size;
  return toBeMoved+1;
}



heres the part of code that isnt working for me. i ask how much data im using, inputing it into the array, and then running it through the template to put it in order, however i am doing something wrong.
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
#include <iostream>
#include <cmath>
#include "arrayUtils.h"

using namespace std;

int main()
{
int size;
int datapoint;
                   
cout<<"please enter the total amount of data entries";
cin>>size;
                    //***set array to proper size;
int data[size];
                //***input and store data into array;
for (int i=0; i<size; i++){
cin>>datapoint;

data[i]=datapoint;}
//***order array into ascending order;

for (int i=0; i<size; i++){

addInOrder(data,size,datapoint);
cout<<endl<<data[i];}

 

  return 0;
}



any advice? thanks!
sporx
Your problem is:

1
2
3
cin>>size;
                    //***set array to proper size;
int data[size];


You can't do that. You must use dynamic memory allocation:
http://www.cplusplus.com/doc/tutorial/dynamic/

Oh, btw, please indent your code better...it is really hard to read.
Last edited on
hmm how should i ask for the size and set an array equal to it then?
thanks!
1
2
int sz = 10;
int* array = new int[ sz ]; // makes an array "sz" elements wide 

could i do this? id run it to find out, but they way i had it the first time also compiled and ran so im still not sure if this is right?
1
2
3
int size;
cin>>size;
int* array= new int[size];


and doing it both this way and the way you told me to gives the same results, without it being properly sorted, the numbers stay in the original order, and it also prints the last data entry twice? any other advice?
Last edited on
Yes, that works.

You have to sort the data. Were you told to write a bubble sort?

I have to see your input code and print code.
my understanding was to use the functions in the given header file to sort the code. the one i posted up top is the one im pretty sure suppose to do it.
my input would just be a cin>>size of array, and then cin>>each piece of the array.
so right now, for testing purposes, my input output looks like this:
please enter the total amount of data entries5
4
3
2
1

4
3
2
1
1


Process returned 0 (0x0)   execution time : 3.900 s
Press any key to continue.


notice its also only letting me put in 4 values instead of five
where it should look like this
please enter the total amount of data entries5
4
3
2
1

1
2
3
4
5*    <---it doesnt let me input the 5th term either


Process returned 0 (0x0)   execution time : 3.900 s
Press any key to continue.


Last edited on
stupid mistake on my part. i figured it out. now i just have to take the median of0 the data.
Last edited on
Topic archived. No new replies allowed.