Rotation of matrix errors

This is a discussion on Rotation of matrix errors within the C Programming forums, part of the General Programming Boards category; Hi everyone. im new to programming (only few weeks). And i was given this coursework to find out the rotation ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    11

    Unhappy 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);
          }
    Last edited by kyber; 11-17-2010 at 07:18 PM. Reason: more data

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    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.
    Last edited by brack; 11-17-2010 at 07:36 PM.

  3. #3
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    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

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    11

    Question

    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??

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    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...!

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Quote Originally Posted by brack View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 02:30 PM
  2. Matrix Rotation
    By disruptivetech in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2008, 02:11 PM
  3. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 07:36 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21