A structure question.

I am new to C++ and am having a hard time understanding How to pass information from a structure into a function.


//Inside Sorted.h file

struct Sorted
{
int data[MAX_SIZE];
int used;
};


I am trying to pass the data portion of this into the structure so I can
do an insertion sort. (To small to use anything else)


void Insert(Sorted& s, int num)
{
int i = 0;
for ( i = s[i]; i > 0 && s[i] > num; --i)
s[i] = s[i-1];
s[i] = num;
}

I am having a awful time figuring this out. I am trying to pass an array into the void Insert using a pass by Reference into the struct.

Any help would be appricated.
Last edited on
Your passing the structure fine.

But you're only passing a single struture, not an array of them.

It's the data member of Sorted that's an array; change all s[i] to s.data[i]
Last edited on
Thank you the program compiles now, but after the insertion is run the program just crashes. Am I not passing something correctly?
your algorithm is not correct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void ins-sort()
{
 int i,j, x;
 for (i = 1; i < n; i++) // n is number of array
 {
 x = arr[i];
 j = i - 1;

 //shift and insert 
 while ( j >= 0 && arr[j] > x) 
 {
 arr[j+1] = arr[j];
 j--;
 }
 arr[j+1] = x;
}


this is the algorithm of insertaion sort and send to it any argument that you want.
Last edited on
Topic archived. No new replies allowed.