c++ PROGRAM to write a spiral matrix? can any one give the code?
#include<stdio.h> int main() { int i,j,m,n,a[100][100]; int p=1,q=1,r=2; scanf("%d",&n); scanf("%d",&m); for (i=1;i<=m;i++) { for (j=1;j<=n;j++) {scanf("%d ",a[i][j]);} } while (m>=1 && n>=1) {for(j=p;j<=n;j++) { i=q; printf("%d ",a[i][j]); } for(i=r;i<=m;i++) { j=n; printf("%d ",a[i][j]); } for(j=n-1;j>=p;j--) { i=m; printf("%d ",a[i][j]); } for(i=m-1;i>p;i--) { j=q; printf("%d ",a[i][j]); } m=m-1; n=n-1; p++; q++; r++; } return 0; } code written so far
why don't we start off writing this code for a smaller matrix and add some code to fill in the values automatically?
so do you want to fill in a matrix in spiral form, or just print it in spiral form? because your code fills in the matrix normally, then tries to print it in spiral order.
``` #include<stdio.h> int main() { int i,j,m,n,a[10][10]; int p=1,q=1,r=2; scanf("%d",&n); scanf("%d",&m); for (i=1;i<=m;i++) { for (j=1;j<=n;j++) {scanf("%d ",a[i][j]);} } while (m>=1 && n>=1) { for(j=p;j<=n;j++) { i=q; printf("%d ",a[i][j]); } for(i=r;i<=m;i++) { j=n; printf("%d ",a[i][j]); } for(j=n-1;j>=p;j--) { i=m; printf("%d ",a[i][j]); } for(i=m-1;i>p;i--) { j=q; printf("%d ",a[i][j]); } m=m-1; n=n-1; p++; q++; r++; } return 0; } ``` I have rewritten your code to be more legible
thanks :)
yeah i want it printed in spiral order the matrix is given by the user
there is always the same error being dispalyed, but i cant figure out... whats wrong
well for now let's run a test matrix that just inputs the numbers 1-16, like ``` int main(void) { int i,j,m,n,a[4][4]; \\ rest of code int x=1; for (i=1;i<=m;i++) { for (j=1;j<=n;j++) { a[i][j] = x; x++; } } ``` or something so we have a control stub, let me compile it and see what I get, one sec..
btw to type code, enclose it in backticks like so: (```) code (```) remove the parentheses
the error you were getting was on the scanf line? invalid argument type 'int' instead of int * ?
canu u give an example i did not quite understand, sorry
i'm saying, your program creates a 100x100 matrix and then asks the user to scan in each of the 10000 numbers, which is a bad test case hence, let's work with a 4x4 matrix that we fill with the numbers 1,2,3,....,16 automatically, so we can debug it more easily
yeah thats the error! I am new to programming I don't understand what is that supposed to mean, sure I will use smaller matrix. thanks How am I supposed to remove the error
since the error says you need to input type int*, you should be using the "address of" operator in that line, &
a[i][j] is a particular integer, &a[i][j] is the address of that location in memory. scanf needs to know where you are storing things in memory to take in values for variable from the user, so the & operator is required before the variable
omg! it shud be scanf("%d",&a[i][j])..... I was so exhausted after thinking and writing this that my brain stopped working. Thankyou very much
welcome, let me know if you have anymore trouble
no prob I am getting the write output for a 3 X 4 matrix. Thank you again
cool, welcome again :)
now i need to do the next problem Write a program that reads an NxN square matrix M that calculates the sum of the elements in individual rows, individual columns and the two main diagonals. Among these sums, print the largest. Consider the following matrix of order 3x3: 1 10 13 2 14 12 3 9 8 The row sum values are 1+10+13=24, 2+14+12=28 and 3+9+8=20. The column sum values are 1+2+3=6, 10+14+9=33 and 13+12+8=33. The diagonal sums are 1+14+8=23 and 13+14+3=30. The expected output is maximum among these sums, which is 33. THANKS ahain :D
Join our real-time social learning platform and learn together with your friends!