Anon4ymous12345






I have defined a structure at the top with s1...s4 , a, d. I n the main message handler case paint i have calculated s Idea .a and have stored them in
the structure at "a".
Now i would like to sort the structure objects with respect to "a" & give out the 5 smallest "a" values. But the porblem i am facing is
that i need to move the entire structures not only "a" as i need to use the s1 , s2 ,s3 & s4 values of those smallest " groups"

For eg :

s1 s2 s3 a
1 2 5 10
3 4 1 9
8 9 2 7
2 3 4 11

I need to say that the 3rd group is the smallest( wrt a ) , the 2nd group comes next & so on ....

Heplp would be great if sumone cld tell me how i could sort the Strctures so !

Code Snippet

[code]




for code see my next post . Thanks for ur help :-)



Code Snippet

[/code]













Re: Visual C++ General sorting struct

Simple Samples

It is my understanding of your question that you need to sort instances of a structure to retrieve the instances in sorted order. Does that sound correct

If so, then why not use a C++ standard class such a std :: map I assume that the reason is because this is a class assignment. I also assume that the reason you posted so much code is because you did not write it and you don't know what parts are relevant to the question.

Please try to trim the code down to just the minimum necessary to ask the question and then please ask a specific question about the code. In other words, please post just a portion of the code and be clear about what you need help with. You say "move the entire structures" so perhaps that is what you are asking about but since the amount of code is so big it is confusing what you are asking about.






Re: Visual C++ General sorting struct

Anon4ymous12345

Hi !
Yes I need to sort them & use them later. The problem i am facing is that with any sort only one element of the structure gets moved, its corresponding info doesnt move. SO i need somenthing that will move the whole object "row".
I would like to implement it in c ... i can not use c++ ... ( no its not homework ;-) )

So here the parts of the code whoch i fell is relevant. I have left the function "bubble sort " at the end ... i thought i can realise the sort with bubble sort.
Code Snippet


#include <stdlib.h>
#include <windows.h> /* Compiler's include files's */
#include <string.h>
#include <stdio.h>


HWND hWndMain; /* handle for main window */

LONG FAR PASCAL MainMessageHandler (HWND, UINT, WPARAM, LPARAM);



int PASCAL
WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR CmdLine, int nCmdShow)
{

}


void bubbleSort(int numbers[], int array_size);



struct Sensor

{
int nr,n,a;
float d;
double s1,s2,s3,s4;

};




LONG FAR PASCAL
MainMessageHandler(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HDC hDC; /* handle for the display device */
PAINTSTRUCT ps; /* holds PAINT information */
TEXTMETRIC tm; /* holds TEXT information */

static HRGN hRgn; /* Rectangle region Handles */
static int CharWidth, CharHeight;

static unsigned short DataVal, DataVal1, DataVal2, DataVal3;
static unsigned long DataVal0, DataVal10, DataVal20, DataVal30;




struct Sensor s[100];
int i, j,n,nr;


double Sys_[4][5];

double b_[4];

double z_[5];

int s11,s12,s13,s14;

int d,s1,s2,s3,s4;

int lam2,lam3,lam4,lam5;



FILE *fp;
fp = fopen("Data2.txt", "r");

for ( i = 1; i<=100; i ++)

{
fscanf(fp, "%d %f %d %d %d %d ", &(s[i].nr), &(s[i].d), &(s[i].s1), &(s[i].s2), &(s[i].s3), &(s[i].s4));

}




switch (Message) /* Windows Message Loop */
{



case WM_PAINT: /* Repaint client area of window */
hDC = BeginPaint(hWndMain, &ps);
x = CharWidth * 2; /* Position cursor within window */
y = CharHeight; /* One line down and 2 chars in */



TextOut(hDC, x, y, str, strlen (str));

y += CharHeight; /* Print current index */

cbAIn (BOARD_NUM, 0, ADRANGE, &DataVal);
cbAIn (BOARD_NUM, 1, ADRANGE, &DataVal1);
cbAIn (BOARD_NUM, 2, ADRANGE, &DataVal2);
cbAIn (BOARD_NUM, 3, ADRANGE, &DataVal3);

mu = t / ( t + 3 );

DataVal0 = (double)mu * ( (double)DataVal - (double)DataVal0 ) + DataVal0;
DataVal10 = (double)mu * ( (double)DataVal1 - (double)DataVal10 ) + DataVal10;
DataVal20 = (double)mu * ( (double)DataVal2 - (double)DataVal20 ) + DataVal20;
DataVal30 = (double)mu * ( (double)DataVal3 - (double)DataVal30 ) + DataVal30;

for ( i = 0 ; i <= 100 ;i++ )
{

s[i].a = ( ( s[i].s1 -DataVal0) * (s[i].s1 - DataVal0 ) + (s[i].s2 - DataVal10 )*( s[i].s2 - DataVal10 ) + (s[i].s3 - DataVal20) * (s[i].s3-DataVal20) + ( s[i].s4 - DataVal20 ) * ( s[i].s4 - DataVal20 ) );




}





y += CharHeight*2; /* Print raw data value */
sprintf (OutString,"Sensor0 = %f ", s[1].a);
TextOut(hDC, x, y, OutString, strlen (OutString));



y += CharHeight*2; /* Print raw data value */
sprintf (OutString,"Sensor1 = %u ", s[2].a);
TextOut(hDC, x, y, OutString, strlen (OutString));



y += CharHeight*2; /* Print raw data value */
sprintf (OutString,"Sensor2 = %u ", s[3].a);
TextOut(hDC, x, y, OutString, strlen (OutString));


y += CharHeight*2; /* Print raw data value */
sprintf (OutString,"Sensor3 = %u ", s[4].a);
TextOut(hDC, x, y, OutString, strlen (OutString));





SetTextAlign(hDC, TA_LEFT | TA_TOP);
EndPaint(hWndMain, &ps);
break;




return (0l);
}


void bubbleSort(int numbers[], int array_size)
{
int i, j, temp;

for (i = (array_size - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}






Re: Visual C++ General sorting struct

Anon4ymous12345

reply will be really welcome as this is very urgent :-((




Re: Visual C++ General sorting struct

Sdi

I don't see any calls to 'bubbleSort', but I also don't see what 'bubbleSort' can possibly do for you; it will sort an array of 'int', but there's no such array in the code.



Re: Visual C++ General sorting struct

Anon4ymous12345

i had called bubble sort just before printing the values ( after calculating s [ i ] ) I removed it before posting it here ...
Ya i am not sure that bubble sort is the right thing ( i got the code for bubble sort from the net )
since i wanted to sort it according to s[ i ].a & "a" is defined as int i thought it cld be ok ...

what code would you suggest for my problem




Re: Visual C++ General sorting struct

Simple Samples

If you are using VC 2005 then there is no reason you can't use C++. If you must have a C solution, then you should become familiar with the C runtime lbray; for this, look at the qsort function specifically. As far as "with any sort only one element of the structure gets moved" is concerned, that is definitely not true. A sort would be useless for most situations if that were true. So for any general-purpose sorting function, look at the description with the assumption that there is always data associated with each key.

The following are some worthwhile articles about sorting.

An important point is that the quicksort algorithm is faster than a bubble sort and you have a reliable and efficient implementation provided to you with the C standard.






Re: Visual C++ General sorting struct

Simple Samples

Anon4ymous12345 wrote:
reply will be really welcome as this is very urgent :-((

There was a time many years ago when I had to wait a whole day to get results after I submitted my program for a compile and run. When I complained, I was told to take extra time before submitting my programs to catch as many problems as I could before submission of the program.

My experience though is that when present developers are in a hurry, developers are less likely to do the things they normally should do. So if you are in a hurry, take time to get things done well the first time; spending time can often save time. If you do want help from people in this forum, then you will get the best results by taking the time to be clear and concise.






Re: Visual C++ General sorting struct

Anon4ymous12345

Thanks for the reply. Yes i also thoight of quicksort. But there again i did not know how to sort the structure. I mean if i "just" use the quick sort code, it will agin only interchange one number & not the whole set.
Would be greatful if someone suggests a code on how to sort the struct in my example using qsort ( or bubble )




Re: Visual C++ General sorting struct

Shakje

Create an array of pointers which points to the structures you're using, modify the bubble sort so it takes in this array and performs its checks on the 'a' variable, then create a temporary pointer that points to the element you're swapping and swap it with the one to swap. Eg.

Code Snippet

struct object *pObj = ppObjs[a];

ppObjs[a] = ppObjs[b];

ppObjs[b] = pObj;

Where a and b are the indices of the objects to move around.






Re: Visual C++ General sorting struct

Anon4ymous12345

Thanks :-)




Re: Visual C++ General sorting struct

Simple Samples

Anon4ymous12345 wrote:
Would be greatful if someone suggests a code on how to sort the struct in my example using qsort ( or bubble )

There was a time not long ago that the convenience of the internet did not exist. I hope you appreciate the convenience of being able to ask a question that someone anywhere in the world can answer. Most answers are provided by volunteers and most volunteers prefer that you do searches to get answers when answers are available. So here is an example:

http://search.msn.com/results.aspx q=%2Bqsort+stdlib+sample+example+struct&FORM=MSNH

That is an example of searching. Among those results is 13. Library Functions comp.lang.c FAQ list, which I think would help you.