| | |
a problem wilth C program
![]() |
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:
======================================================
=========================================
thanks.
and Remember I do not want to see any complete structure of the solution
the_shark
<< moderator edit: added code tags: [code][/code] >>
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:
======================================================
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main() { int size,track=0,i,j ,sum=0; char string[400]; printf("Enter the size of the matrix: "); scanf("%d",&size); gets(string); printf("Enter the %d by %d matrix (in row-order) ",size,size); gets(string); printf("the matrix elements are = %s \n", string); //notice that the entered string should be Separated by single space only for(j=0;j<(2*size*size-1);j=(j+2*size-1)) //go row by row { for (i=0;i<2*size;i=i+2) //go value by value in a single row { if (string[j]!=1 && j==track || string[j]!=0 && j !=track) sum= sum; else if ((string[j]==1 && j==track) || (string[j]==0 && j !=track)) sum++; } track++; } if (sum==(size*size) ) printf("This matrix is a multiplicative identity\n"); else printf("This matrix is not a multiplicative identity\n"); return 0; }
thanks.
and Remember I do not want to see any complete structure of the solution
the_shark
<< moderator edit: added code tags: [code][/code] >>
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.
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.
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.
>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:
Now the only problem is making sure that the other values are all 0. The simplest way to do this is a trivial loop:
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:
This solution is easy to understand, easy to verify, and has linear complexity.
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:
C Syntax (Toggle Plain Text)
size_t len = strlen ( s ); for ( i = 0; i < len; i += N + 1 ) { if ( s[i] != '1' ) invalid(); }
C Syntax (Toggle Plain Text)
for ( i = 0; i < len; i++ ) { if ( s[i] != '0' ) invalid(); }
C Syntax (Toggle Plain Text)
size_t len = strlen ( s ); for ( i = 0; i < len; i += N + 1 ) { if ( s[i] != '1' ) invalid(); s[i] = '0'; // Turn a 1 into a 0 } for ( i = 0; i < len; i++ ) { if ( s[i] != '0' ) invalid(); }
I'm here to prove you wrong.
![]() |
Similar Threads
- Random Number Problem For Lottery Program (Visual Basic 4 / 5 / 6)
- problem relating to c program.... (IT Professionals' Lounge)
- client server communication problem in CSocket program (C)
- problem splitting c++ program (C++)
Other Threads in the C Forum
- Previous Thread: change button caption at runtime with code
- Next Thread: help with "disabling" a button
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file floatingpointvalidation fork forloop frequency givemetehcodez grade gtkgcurlcompiling gtkwinlinux hacking highest histogram inches input intmain() iso kernel keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf opendocumentformat openwebfoundation owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault sequential single socket socketprograming socketprogramming stack standard string systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






