Thread: modulus

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    modulus

    How ye doin. New to this, friend recommended it.

    Have to do a matrix 12x12 and the user inputs the first row of what is to go in. Then i have to invert this row and put it in the first column. I have been given the formula to do this but not quite sure how to put it into action. this has to be done with all the columns. i presume i have to put it into a for loop in order for it to work. sorry bout the stupidness if this is easy!!

    formula is:
    Pm,n = [(P1,n +(Pm,1 - P1,1)]mod12



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
       
      char userinput[12][12];      /* declaration of array to hold the row*/
      //char temp[2];
      int i;     /* i is te value to go through the array*/
      int j; 
     
    
    printf("Please enter the tone row \n Please note only the first inputted character shall be accepted \n");     /*gets the user to enter tone row*/
    
    
    
     for(i = 0 ; i < 12; i++){ 	//for loop to input the row into the loop	
       scanf("%s", &userinput[i]);
    
      
     }
    
     for (j = 0; j < 12; j++){
    
       for (i = 0; i < 12; i++){
    
       scanf("%s",userinput[j]);
    
       userinput[i][j] = ([i][j] + (([i][j]) - ([i][j])))mod12;
    	 }
     }
    
     
    for(i = 0; i < 12; i++){        //for loop to print out the array after input
    
       printf("%s",userinput[i]);
       }
    
      printf("\n");
    
    }
    This is what i have already.
    Cheers in advance
    Last edited by nicknack; 12-15-2007 at 07:58 AM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    In C the modulus operator is % not mod.

    IE: 10 % 3 = 1

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    have you any idea why "error: parse error before '[' token" would come up when i try to compile it?

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    The line wants to look something like this:
    Code:
    userinput[i][j] = (userinput[i][j] + ((userinput[i][j]) - (userinput[i][j]))&#37;12;
    You cant just say [i][j] and expect the compiler to know that you are referring to the userinput array. Hope that solves your problem.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    scanf("&#37;s", ...) is not safe. Suggest you read my signature.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    mike your working wonders for me here.

    what your saying is working but somehow i cannot finaliza things.

    (a). All the manipulations with the serial forms can be treated by arithmetical formula's. in this case, the formulas for the creation of the first column inverse could be:

    Pm,1 = [(Pm-1,1 + (P1,m-1 - P1,m)]mod12


    the subsequent transposition of the original series:

    Pm,n = [(P1,n + (Pm,1 - P1,1)]mod12.

    so i think that i start on 1 and put in the first formula for the first column, and then create another for loop to loop through the rest of the matrix then??

    Here's what i have now:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
       
      int userinput[12][12];      /* declaration of array to hold the row*/
      //char temp[2];
      int i;     /* i is te value to go through the array*/
      int j;
      
     
    
    printf("Please enter the tone row \n Please note only the first inputted character shall be accepted \n");     /*gets the user to enter tone row*/
    
    
    
     for(j = 0 ; j < 12; j++){ 	//for loop to input the row into the loop	
       scanf("%d", &userinput[j]);
    
    
      
     }
    
    for(j = 0; j < 12; j++){        //for loop to print out the array after input
    
       printf("%d",userinput[j]);
       
     }
      printf("\n");
    
      
    
    
    
    
     for (i = 1; i < 11; i++){
    
       for (j = 1; j < 11; j++){
    
         //scanf("%d",userinput[i][j]);
    
       userinput[i][j] = (userinput[i][j] + ((userinput[i][j]) - (userinput[i][j+1])))%12;
       printf("%d", userinput[i]);
    	 }
     }
    }

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    cheers Elysia, will try keep away! seems to be runnin better now!

    Maybe you might be able to help me with this problem i have here

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, first you could work a little on indentation. Every block ({ and }) should be indented with (at least) 2 spaces, preferably a tab. You're not specifying that row/column you read into either. Since the array is 2D, you would want to specify [row][column] when reading into your array, while you're just reading into array[row].
    For the first row, it might be array[0][column].
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modulus operator with double operands?
    By chickenandfries in forum C Programming
    Replies: 7
    Last Post: 03-28-2008, 07:21 AM
  2. Using the Modulus Operator
    By tlove71 in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2007, 11:59 AM
  3. Modulus algorithm
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-10-2004, 10:34 AM
  4. Floating-point modulus
    By Sang-drax in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2002, 08:20 PM
  5. using modulus & weighting factors
    By task in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 05:52 PM