User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,592 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,867 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 580 | Replies: 2
Reply
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 4
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Troubleshooting A Challenging Debugging Problem

  #1  
Jun 4th, 2007
Hello,

The code below calculates the determinant of the matrix. I know that the method of global 2-d pointers is a poor programming practice (so u don't need to reply saying that!!!), but it is a simple method for an amateur programmer. As the code didn't work, I set two watches,
(After giving the input as a 3x3 matrix with elements numbered from 1 to 9) one on e[k][l] & another on b[k1][l1]. To my surprise, b[k1][l1] got modified on its own inspite the fact that e[k][l] at k=1 and l=1 had the value of 5.

Somebody plz help me out.

-Jishnu.
  1. #include<iostream.h>
  2. #include<iomanip.h>
  3. #include<conio.h>
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<math.h>
  7. void show(float **a,int n);
  8. float det(float **b,int n);
  9. void minor(float **a,int o,int i,int j);
  10. void main()
  11. {
  12. clrscr();
  13. int o,i,j;
  14. float **a,ans;
  15. cout<<"Enter The Order Of Matrix:";
  16. cin>>o;
  17. for(i=0;i<o;i++)
  18. a[i]=new float[o];
  19. for(i=0;i<o;i++)
  20. for(j=0;j<o;j++)
  21. cin>>a[i][j];
  22. ans=det(a,o);
  23. cout<<ans;
  24. getch();
  25. }
  26. void show(float **a,int n)
  27. {
  28. cout<<"\n";
  29. for(int i=0;i<n;i++)
  30. {
  31. for(int j=0;j<n;j++)
  32. {cout<<setw(8)<<a[i][j]<<" ";}
  33. cout<<"\n\n";
  34. }
  35. }
  36. float **b;
  37. float det(float **c,int n)
  38. {
  39. float ans=0;
  40. float *d;
  41. d=new float[n];
  42. for(int i=0;i<n;i++)
  43. d[i]=c[0][i];
  44. if(n>2)
  45. {
  46. for(int i=0;i<n;i++)
  47. {
  48. minor(c,n,0,i);
  49. if(i%2==0)
  50. ans+=d[i]*det(b,n-1);
  51. else
  52. ans-=d[i]*det(b,n-1);
  53. }
  54. }
  55. if(n==2)
  56. {
  57. return(c[0][0]*c[1][1]-c[0][1]*c[1][0]);
  58. }
  59. return ans;
  60. }
  61. void minor(float **e,int o,int i,int j)
  62. {
  63. int k1=0,l1;
  64. for(int k=0;k<o;k++)
  65. {
  66. l1=0;
  67. for(int l=0;l<o;l++)
  68. {
  69. if(k==i)
  70. {k1--;break;}
  71. if(l==j)
  72. continue;
  73. [b][color=red]b[k1][l1]=e[k][l];:-O [/color][/b]
  74. l1++;
  75. }
  76. k1++;
  77. }
  78. }
Last edited by Ancient Dragon : Jun 4th, 2007 at 8:59 am. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,641
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: A Challenging Debugging Problem

  #2  
Jun 4th, 2007
>>float **a;
that is the declaration of a 2-dimensional array of floats. Somewhat similar to this when both dimensions are known: float a[2][5]

Before allocating memory for the second dimension (5 in the above dimension) you have to allocate memory for the first (2 in the above example)

Line 18 is using an uninitialized variable a. You have to allocate the matrix like this:
a = new float*[o]; // allocate first dimension
for(int i = 0; i < o; i++)
{
    a[i] = new float(o);
    // initialize each element
    for(int j = 0; j < o; j++)
         a[i][j] = 0.0F;
}
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Dec 2005
Posts: 3,338
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 378
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: A Challenging Debugging Problem

  #3  
Jun 4th, 2007
I suppose we'll have to wait a while longer for you to discover indentation as well as code tags

Also, whether you intend this to be a C program or a C++ program (and not some random mix like it is at the moment).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC