# Rotation of matrix errors

• 11-17-2010
kyber
Rotation of matrix errors
Hi everyone. im new to programming (only few weeks).
And i was given this coursework to find out the rotation of a matrix(2d). so i know, mathematically i have to multiply the sine & cosine matrix with the matrix to be rotated.
so than i would generate a new matrix(which i have to display).

i tried it. the output wasnt what i wanted. so i edited it (loads of times)
please help. this is the code so far. i know its kinda rubbish, but i have edited it a lot, and now im not sure where ive wound up.

and the error props in line 11..which is where ive declare r[100][100]
Code:

```#include <stdio.h> #include <math.h> #define DEG 0.0174532925199432958; main() {        float degrees,theta,b[100][100],Pi=3.14;       float a[100][100];       int i,j,k;       float r[100][100]={{cos(theta),(-sin(theta))},{sin(theta),cos(theta)}};                         printf(" 2d matrix rotation program\n");             printf("Please enter the angle in degrees you want the matrix to be rotated\n");       scanf("%d",&degrees);             theta=degrees*DEG;             for(i=0;i<2;i++){                       printf("Enter the data for row no. %2d\n",i+1);                       for(j=0;j<2;j++){                                           scanf("%f",&a[i][j]);                                           }                       }             for(i=0;i<2;i++){                     for(j=0;j<2;j++){                                         b[i][j]=0;                                         for(k=0;k<2;k++){                                                             b[i][j]+=a[i][k]*(r[k][j]);                                                             }                                         }                     }             printf("The rotated matrix is:\n");       for(i=0;i<2;i++){                         for(j=0;j<2;j++){                                           printf("%4.2f",b[i][j]);                                           }                         printf("\n");                               }       return (0);       }```
• 11-17-2010
brack
First of all your variable, named "degrees" is float(as i can see you declare this), but in your scanf you use %d which is for integers! You should use %f instead. Also when you compile you should write something like:
gcc file.c -lm. With the -lm you actually connect the program with math.h.
• 11-17-2010
hellork
Try compiling with warnings enabled. Should give some tips.
cat code|gcc -g -Wall -pedantic -xc -o x -
<stdin>:8:5: warning: initializer element is not computable at load time
<stdin>:5:40: warning: unused variable ‘Pi’
<stdin>:8:28: warning: ‘theta’ is used uninitialized in this function
• 11-17-2010
kyber
thank you for the replies. like i said, i had done a lot of editing, so initially the scanf for degrees was read at %f. i changed it back again, and also removed a few unused variables..but the output is the same as the input.

eg.
Input
12 56
32 66

Output
12 56
32 66

is there another way to do this?? i just want some sort of reference tbh. i couldnt find anything online..and ones i found were complicated.
so do i go for functions??
• 11-17-2010
brack
you just want to rotate an array right? why you make this so complicated? you can just you two arrays and save in the second the rotaded elements...!
• 11-17-2010
kyber
Quote:

Originally Posted by brack
you just want to rotate an array right? why you make this so complicated? you can just you two arrays and save in the second the rotaded elements...!

thanks. finally got it to work