Wikipedia:Reference desk/Archives/Computing/2014 March 24
Appearance
Computing desk | ||
---|---|---|
< March 23 | << Feb | March | Apr >> | March 25 > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
March 24
[edit]matrix
[edit]101.222.245.26 (talk) 17:21, 24 March 2014 (UTC)i need to write get a code in c without using any functns ,i have just arrays and pointers and stdio.h.the test case is for 2*2,3*3,4*4 matrix.what i wrote is just giving me for 2*2 and 3*3 .any suggestions.
- What exactly is your code supposed to do ? Read in a matrix from a file and print it out ? If you are off by 1, that may be an error due to C starting arrays at 0 instead of 1. So, if you are reading in a 3x3 array and then printing out elements [1,1]-[3,3] instead of [0,0]-[2,2], that would cause a similar problem. You can show your code here for more answers, if this advise doesn't help. StuRat (talk) 17:40, 24 March 2014 (UTC)
- "i need to write get a code in c without using any functions". Tell us why you "need to write" and why you cannot "using any functions". If you are writing in C language, then writing C functions is a natural way to program. Not writing any functions is highly unnatural. Anyway, what you need are algorithmn for matrices which you can find in math textbooks at college or university levels. The actual computation language is just a tool to use for computations. 202.177.218.59 (talk) 01:27, 25 March 2014 (UTC)
- It's hard to say exactly what our OP is asking here - but I presume "without using any functns" means "without using any external library functions"...or something like that - otherwise you could simply find a linear algebra package someplace and solve the problem with a single function call. Also, I'm a little concerned that this is probably a homework question, it's hard to imagine a non-homework situation where forbidding the use of an external library would make sense - and we're not allowed to help you do your homework. But in any case, there is simply no way we can help to debug this code without seeing it. SteveBaker (talk) 16:41, 25 March 2014 (UTC)
- StuRat, +1.
so sorry,actually i needed to print it in spiral form which i couldnot save in question.my code is here.
#include <stdio.h>
int main()
{
int m,n;
int p,q,a[10][10];
scanf("%d%d",&m,&n);
for(p=0;p<m;p++)
{
for(q=0;q<n;q++)
{
scanf("%d%d",&a[p][q]);
}
}
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while (k < m && l < n)
{
/* Print the first row from the remaining rows */
for (i = l; i < n; ++i)
{
printf("%d ", a[k][i]);
}
k++;
/* Print the last column from the remaining columns */
for (i = k; i < m; ++i)
{
printf("%d ", a[i][n-1]);
}
n--;
/* Print the last row from the remaining rows */
if ( k < m)
{
for (i = n-1; i >= l; --i)
{
printf("%d ", a[m-1][i]);
}
m--;
}
/* Print the first column from the remaining columns */
if (l < n)
{
for (i = m-1; i >= k; --i)
{
printf("%d ", a[i][l]);
}
l++;
}
}
return 0;
}
101.222.245.45 (talk) 18:32, 26 March 2014 (UTC)
- I do see lots of 1's that should probably be 0's, since C arrays start at 0. I suggest you manually write out which array elements should contain which value, and go through the program manually, keeping track of each value for each variable as you go. This will help you understand what your program is doing better.
- Also, just hard-code a printout of the array, as a[0][0],a[0][1],a[0][2] on one line, then a[1][0],a[1][1],a[1][2] on the next line, and a[2][0],a[2][1],a[2][2] on the last line, and avoid any use of loops, etc., which could cause confusion. Once you know what's in your array it will be a lot easier to debug the rest of your program. StuRat (talk) 16:24, 27 March 2014 (UTC)
- Also keep in mind that if you declare an array, say,
float matrix[4][4]
, a valid index is between 0 and 3 because the bracketed numbers are the number of rows/columns, not the last valid index. (This hit me when I was new to C++, too.) - Trying to access, say, matrix[4][4] is illegal. On top of that, C doesn't check if your index is invalid, but read/write from/to the address anyway. This will usually corrupt other variables.
- (Unfortunately, the data corruption article doesn't talk much about bugs causing data corruption; its primary concern is malfunctioning or damaged storage hardware. Memory safety is more relevant here.)
- BTW (and by BTW I mean something completely different), Inaccessible boot device is at least a questionable redirect IMO, and the target doesn't touch the issue in the slightest. I think it must go or be retargeted, but someone with more experience in the WP deletion processes should look into it. - ¡Ouch! (hurt me / more pain) 07:54, 26 March 2014 (UTC)
- Also keep in mind that if you declare an array, say,