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.
#include <iostream>
#include <cmath>
#include "arrayUtils.h"
usingnamespace 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;
}
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= newint[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?
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.