a problem wilth C program

Reply

Join Date: Mar 2005
Posts: 17
Reputation: the_shark is an unknown quantity at this point 
Solved Threads: 0
the_shark's Avatar
the_shark the_shark is offline Offline
Newbie Poster

a problem wilth C program

 
0
  #1
Mar 27th, 2005
Hi friends...

I have a problem with some c codes and I think that you will be Capable to point it up for me.
However, I do not want anybody to post the correct and complete code to me .

I want to enter a string of numbers and the program will check whether the entered string forms a multiplicative identity n*n matrix which means all elements are zero except for the diagonal elements
For example, 3*3 multiplicative identity
1 0 0
0 1 0
0 0 1

I think but I am not pretty sure that the problem is from the outer for-loop. But still I cannot figure the error out and here is my code fragment written in C:
======================================================
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5. int size,track=0,i,j ,sum=0;
  6. char string[400];
  7. printf("Enter the size of the matrix: ");
  8. scanf("%d",&size);
  9. gets(string);
  10. printf("Enter the %d by %d matrix (in row-order) ",size,size);
  11. gets(string);
  12. printf("the matrix elements are = %s \n", string);
  13. //notice that the entered string should be Separated by single space only
  14.  
  15.  
  16. for(j=0;j<(2*size*size-1);j=(j+2*size-1)) //go row by row
  17. {
  18. for (i=0;i<2*size;i=i+2) //go value by value in a single row
  19. {
  20. if (string[j]!=1 && j==track || string[j]!=0 && j !=track)
  21. sum= sum;
  22.  
  23.  
  24. else if ((string[j]==1 && j==track) || (string[j]==0 && j !=track))
  25. sum++;
  26. }
  27. track++;
  28.  
  29. }
  30. if (sum==(size*size) )
  31. printf("This matrix is a multiplicative identity\n");
  32. else
  33. printf("This matrix is not a multiplicative identity\n");
  34. return 0;
  35. }
=========================================
thanks.
and Remember I do not want to see any complete structure of the solution

the_shark


<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: a problem wilth C program

 
0
  #2
Mar 27th, 2005
The most obvious problem is that you're testing for 0 or 1 instead of '0' or '1'. Remember the the contents of your string are characters, not integers. You're also going about the problem in a complicated way, but because you don't want help with that part, I won't show you a simpler solution.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 17
Reputation: the_shark is an unknown quantity at this point 
Solved Threads: 0
the_shark's Avatar
the_shark the_shark is offline Offline
Newbie Poster

Re: a problem wilth C program

 
0
  #3
Mar 28th, 2005
Hi Narue,

First of all, I would like to thank you for editing my post and making the code more readable.

Second of all, I did follow your suggestions as well as other modifications and now my program is working properly.

Third of all, I though my way to develop the problem idea was the only way to do so. However, since you are more expert than me, I understood that the problem can be solved in simpler way.

Can you write down the id ea of the other way?

Quick note:
I am a beginner in C.
So my knowledge includes pointers, functions, string, arrays, structures, loops and condition statements. :o

I hope that your solution will be within my knowledge-base. :cheesy:

Again, thanks for the cooperation.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 41
Reputation: varunrathi is an unknown quantity at this point 
Solved Threads: 1
varunrathi's Avatar
varunrathi varunrathi is offline Offline
Light Poster

Re: a problem wilth C program

 
0
  #4
Mar 28th, 2005
use double dimensional array.
"Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: a problem wilth C program

 
0
  #5
Mar 28th, 2005
>Can you write down the idea of the other way?
Look carefully at the data. You have a 1 followed by 0's, then another 1 and the same amount of 0's. If you count the 0's then you'll see that it's N + 1 where N is the size of the matrix (ie. NxN, or 3x3).

To verify that the matrix is valid, you need all 0's except for the first, and every (N + 1)st beyond that. Those values must be 1. This gives you an immediate solution for counting the 1's:
  1. size_t len = strlen ( s );
  2. for ( i = 0; i < len; i += N + 1 ) {
  3. if ( s[i] != '1' )
  4. invalid();
  5. }
Now the only problem is making sure that the other values are all 0. The simplest way to do this is a trivial loop:
  1. for ( i = 0; i < len; i++ ) {
  2. if ( s[i] != '0' )
  3. invalid();
  4. }
But because there are 1's, that won't work unless you make sure that the 1's won't be counted, or won't make a difference if they are. You're probably ahead of me by now, but consider this:
  1. size_t len = strlen ( s );
  2. for ( i = 0; i < len; i += N + 1 ) {
  3. if ( s[i] != '1' )
  4. invalid();
  5. s[i] = '0'; // Turn a 1 into a 0
  6. }
  7.  
  8. for ( i = 0; i < len; i++ ) {
  9. if ( s[i] != '0' )
  10. invalid();
  11. }
This solution is easy to understand, easy to verify, and has linear complexity.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 17
Reputation: the_shark is an unknown quantity at this point 
Solved Threads: 0
the_shark's Avatar
the_shark the_shark is offline Offline
Newbie Poster

Re: a problem wilth C program

 
0
  #6
Mar 29th, 2005
ahhhhhhha

The solution is much easier than I thought; I'll try to practice such ideas in the future.

Thanks Narue and varunrathi.
the_shark
born to be wild
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC