Help with calculations with tables ( matrixes )

This is a discussion on Help with calculations with tables ( matrixes ) within the C Programming forums, part of the General Programming Boards category; hi! I am having trouble with this private project of mine. I am not much of a programmer, so i ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27

    Help with calculations with tables ( matrixes )

    hi!

    I am having trouble with this private project of mine. I am not much of a programmer, so i am stuck now with my equations but am not
    able to complete my code... would be greatful for help!I have started the code by lookin into books, sites, etc, but it is far from over

    I have 4 sensors giving me values & they are saved in a table along with the "serial number" & a parameter- distance ( d )

    Nr d S1 S2 S3 S4

    1 0.5 2000 2102 2000 2333
    2 1 2100 2234 2200 2400
    3 1.5 2230 2299 2300 2500
    4 2 2290 2402 2400 2600
    5 2.5 2400 2499 2400 2700
    .
    .
    .
    100



    Now in the first step i want to find from the 100 readings the 5 most suitable, for that i find the "smallest distance"
    with


    a[i] = sqrt( ( s(i)1 -s11) * (s(i)1 - s11 ) + (s(i)2 - s12 )*( s(i)2 - s12 ) + (s(i)3 - s13) * (s(i)3-s13) + ( s(i)4 - s14 ) * ( s(i)4 - s14 ) );

    Where i is the number of the reading, the number which increases ... 1,2,3,4 are the sensors 1-4

    after this i need to sort the a[i] & pick out the five with the smallest distance.
    with the five sets ( with the d, s1, s2 , s3 & s4 )i have to do further calcultaions. Nut will be greatful for help till here :-)
    attached is the code i have tried till here.

    Regards!

    Code:
    int a[100]
    
    for ( i=0;i<n;i++)
    {
    
      a[i] = sqrt( ( s(i)1 -s11) * (s(i)1 - s11 ) + (s(i)2 - s12 )*( s(i)2 - s12 ) + (s(i)3 - s13) * (s(i)3-s13) + ( s(i)4 - s14 ) * ( s(i)4 - s14 ) );
    
    }
    
    
         int x,n,i,j;
    
    
            
    
           
            for(i=0;i<n;i++)
            {
                    scanf("%d",&a[i]);
            }
    
            for(i=0;i<100;i++)
            {
                    for(j=0;j<n-1;j++)
                    if(a[j]>a[j+1])
    
                    {
    
                            x=a[j];
                            a[j]=a[j+1];
                            a[j+1]=x;
                    }
            }
    
            printf("array in ascending order\n");
            for(i=0;i<n;i++)
    
            {
                    printf("%d\n",a[i]);
            } 
    
    
      
         printf (" smallest distances  : %d %d %d %d %d",a[1],a[2],a[3],a[4],a[5]);

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Well, If you wanted to get the first 5 smalled distance. What i wuld do is sort the array with reference to the distance value. Once it is sorted you get the five smalled distance at the top. Get the top file sensor values.

    ssharish2005

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    hm ... i havent explained it well enough. The distance calculated is not the distance "d" in the table. for every set of reading there is
    a calulated distance a(i) ... now i needed the smallest 5 a(i) and take the corresponding readng set ...
    Since i am not familiar with the structures & arrays in c i was hoping for a syntax suggestion !

    Thx

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    1 0.5 2000 2102 2000 2333

    you could store this in an struct


    Code:
    struct Sensor
    {
        int no;
        int Distance;
        float value[5];
        float CalDist;
    };
    And declare 100 elements of type struct sensor. And calculate the distance and store them in calDist and when that is finish. Sort the struct elements with reference to CalDist( Which means u are sorting thr whole struct).

    Hope you get an idea.

    ssharish2005

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Code:
    struct READING {
         DWORD     nr;
         float         d;
         DWORD     s1;
         DWORD     s2;
         DWORD     s3;
         DWORD     s4;
         }
    
    READING* g_Reading[100];
    
    void main(){
         
         // allocate and fill g_Reading here
    
         sort(g_Reading);
    
         // do stuff with the sorted list of readings
    
         return;
         }
    
    void sort(LPVOID lParam){
         READING* l_Readings[100],temp;
         DWORD x;
    
         l_Readings = (READING*)lParam;
    
         x = 1;
         while(x<100){
              if(l_Readings[x-1]->d < l_Readings[x]->d){
                   temp = l_Readings[x];
                   l_Readings[x] = l_Readings[x-1];
                   l_Readings[x-1] = temp;
                   x=1;
                   } else x++;
              }
         return;
         }
    Last edited by abachler; 06-18-2007 at 11:11 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    Quote Originally Posted by ssharish2005 View Post
    1 0.5 2000 2102 2000 2333

    you could store this in an struct


    Code:
    struct Sensor
    {
        int no;
        int Distance;
        float value[5];
        float CalDist;
    };
    And declare 100 elements of type struct sensor. And calculate the distance and store them in calDist and when that is finish. Sort the struct elements with reference to CalDist( Which means u are sorting thr whole struct).

    Hope you get an idea.

    ssharish2005
    Ya i understand now ! i will try & get back !

    Have a nice evening !

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    do not use globals
    do not use void main
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    hi!

    i wanted to do some more numerical steps. I have got the induvidual functions but am struggling
    to piece them together, basically because my c structure & syntax knowledge isnt so good ...

    The outline of my problem once again :

    I have 4 sensors giving me values & they are saved in a table along with the "serial number" & a parameter- distance ( d )

    ( see 1st post for table )

    i have tried to save the things in a struct. I then caluclated a second distance "a" with a formula & then wanted to sort the values a
    in order to get the 5 "groups" of readings with the smallest "a" ...

    Then i wanted to calculate ( using the gauss jordan algo ) the coefficients lam2, ... lam5. The FUnction for the algo is at the bottom & should be correct
    The gauß jordan algo, basically calculates Ax = B ... I tried to fill matrix A using Sys ...In the matrix b have a term s[0] ... this is my actuall
    reading which i get in.

    Finally using lam2...lam5 i calculate a distance d0 ...in the equation again the distances of the 5 earlier selected "groups comein.

    Would be terrific if someone could suggesst ammendments to my code m so that it does what i want ...

    Here m attempt so far :

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include "..\cbw.h"
    
    void main ()
        {
    
    		struct Sensor 
    
    	{
           int nr,d,n ;
    	   double s1,s2,s3,s4;
    
    	};
    
    	struct s[100];
    
    
    
    for ( i=0;i<n;i++)
    {
    
      a[i] = sqrt( ( s[i].s1 -s11) * (s[i].s1 - s11 ) + (s[i].s2 - s12 )*( s[i].s2 - s12 ) + (s[i].s3 - s13) * (s[i].s3-s13) + ( s[i].s4 - s14 ) * ( s[i].s4 - s14 ) );
    
    }
    	
    	
      for (s[i] = 0; i < n; i++)
    
    	  numbers[i] = s[i];
    
      //perform bubble sort on array
      bubbleSort(s[i].a, n);
    
     
      for (i = 0; i < 4; i++)
        printf("%i\n", s[i].a[i]);
    
    gj4(double Sys_[4][5], double *x_, double *b_)
    
    {
    
    Sys_[0][0]= s[1].s1;
    Sys_[0][1]= s[2].s1 - s[1].s1;
    Sys_[0][2]= s[3].s1 - s[1].s1;
    Sys_[0][3]= s[4].s1 - s[1].s1;
    Sys_[0][4]= s[5].s1 - s[1].s1;
    
    Sys_[1][0]= s[1].s2;
    Sys_[1][1]= s[2].s2 - s[1].s2;
    Sys_[1][2]= s[3].s2 - s[1].s2;
    Sys_[1][3]= s[4].s2 - s[1].s2;
    Sys_[1][4]= s[5].s2 - s[1].s2;
    
    Sys_[2][0]= s[1].s3;
    Sys_[2][1]= s[2].s3 - s[1].s3;
    Sys_[2][2]= s[3].s3 - s[1].s3;
    Sys_[2][3]= s[4].s3 - s[1].s3;
    Sys_[2][4]= s[5].s3 - s[1].s3;
    
    Sys_[3][0]= s[1].s4;
    Sys_[3][1]= s[2].s4 - s[1].s4;
    Sys_[3][2]= s[3].s4 - s[1].s4;
    Sys_[3][3]= s[4].s4 - s[1].s4;
    Sys_[3][4]= s[5].s4 - s[1].s4;
    
    double b_[4] = {s[0].s1, s[0].s2, s[0].s3, s[0].s4};
    
    
    double x_[5] = {1, (int)lam2, (int)lam3, (int)lam4};
    
    
    if (gj4(Sys_, x_, b_)==0)
    	   	   		  {
    	   	   			  printf("Matrix ist singulär.\n");
    	   	   			  isNearest = true;
    	   	   			  break; 
    	   	   			  break;
    	   	   		  }
    
    
    }
    
    s[0].d0 = s[1].d1 + (int)lam2 * ( s[2].d2 - s[1].d1 ) + (int)lam3 * ( s[3].d3 - s[1].d1 ) + (int)lam4 * ( s[4].d4 - s[1].d1 ) + (int)lam5 * ( s[5].d5 - s[1].d1 );
          
      
      
    }
      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;
          }
        }
      }
    }
    
    	
    	
    	
    	
    	// Gauss-Jordan-Elimination
    int gj4(double Sys_[4][5], double *x_, double *b_)
    {
    
        int col_a, row_a, col_i, row_i, switch1;
    
        int pivot_a[] = {0, 0, 0, 0};
        int pivot_i[] = {0, 0, 0, 0, 0};
    
        double help1, help2;
    
        for(col_a=0; col_a<5; col_a++)
        {
    		for(row_a=0; row_a<4; row_a++)
    		{
    
    			if (Sys_[row_a][col_a]!=0 && pivot_a[row_a]!=1)
    			{
    
    				pivot_a[row_a] = 1;
    				pivot_i[row_a] = 1;
    
    				help1 = Sys_[row_a][col_a];
    
    				for(col_i=0; col_i<4; col_i++)
    				{
    					Sys_[row_a][col_i] = Sys_[row_a][col_i]/help1;
    				}
    
       				
    				b_[row_a] = b_[row_a]/help1;
    
    
    				for(row_i=0; row_i<4; row_i++)
    				{
    					help2 = Sys_[row_i][col_a];
    
    					if (pivot_i[row_i]!=1 && help2!=0)
    					{
    						b_[row_i] = b_[row_i]-b_[row_a]*help2;
    
    						for(col_i=0; col_i<4; col_i++)
    						{
    							Sys_[row_i][col_i] = Sys_[row_i][col_i]-Sys_[row_a][col_i]*help2;
    						}
    
    					}
    				}
    
    				pivot_i[row_a] = 0;
    
    				}
    			}
    		}
    
           
    		for(row_a=0; row_a<4; row_a++)
    		{
    			switch1 = 0;
    			for(col_a=0; col_a<4; col_a++)
    			{
    				if (Sys_[row_a][col_a]==1)
    				{
    					x_[col_a] = b_[row_a];
    					switch1 = 1;
    				}
    			}
    			if (switch1==0)
    			{
    				
    			}
    		}
    
        return 1;
    }

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Posting a huge lot of code won't help any one to help. And more over you have a very poor code indendaton. Sort out that first. And then ask a question again.

    ssharish2005

  10. #10
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    i am not a professional programmer like you guys are. I just need it for this private project of mine.
    I was hoping for help :-) so pls help if u could ;-)

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A lot of people on here aren't professional programmers, merely hobbyists or under-grads.

    Your going to have to be more specific with your problem, either use a debugger to see how and how-not your program runs, or add a lot of printf()'s before, during and after you do anything 'important'.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by sdherzo View Post
    i am not a professional programmer like you guys are. I just need it for this private project of mine.
    I was hoping for help :-) so pls help if u could ;-)
    Yeah and I'm still trying to build that death ray of mine, but I'm not a professional death-ray builder like all of you people.

    After all, it's not like I should have to struggle to learn how to build it just like you people did. It's because you make money off of your death rays, that's why you owe me, and have to help me for free because I'm just making a private project.

    Help meeeeeeeeeeeeee!!!



    When you realize how silly your response is, then realize that if you don't give a care to ask a proper question properly, others won't care to give you a proper answer properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ style tables
    By MarkZWEERS in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2009, 08:41 AM
  2. C program compile but not doing the calculations
    By abs.emailverify in forum C Programming
    Replies: 8
    Last Post: 11-08-2006, 07:43 AM
  3. String tables in console apps
    By Sentral in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2006, 04:08 AM
  4. multiply 2 matrixes
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 04-14-2005, 01:52 AM
  5. matrixes for collision detection
    By DavidP in forum Game Programming
    Replies: 10
    Last Post: 11-09-2002, 09:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21