symmetryc problem

This is a discussion on symmetryc problem within the C Programming forums, part of the General Programming Boards category; I'm doing a program in c, where I need to read a file, place it in an array and compare ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    8

    symmetryc problem

    I'm doing a program in c, where I need to read a file, place it in an array and compare the vertical and horizontal symmetry about ASCII symbols.

    The input file contains a figure created
    on the basis of any ASCII characters.
    For example, the first figure shown in this document:
    * *
    * * * *
    * * * *
    * * * * * * Vertical

    + + + + + + + +
    + + + + + + +
    + + + +
    + + + + + + +
    + + + + + + + + Horizontal
    would be represented in this file follows:
    The program will analyze the figure, detecting all possible
    symmetries that sample, and generating an output file with
    Detailed information on all the symmetries detected. The
    information generated in each case is as follows.

    Yo I have done the following:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "funcions.h"
    
    
    int main(int argc, char** argv)
    {
    	
    	FILE *f1;
    	int **matriu;
    	int test=0;
    	int N;
    	int i=0;
    	int M;
    	char c;
    
    	
    	if ((f1=fopen("ejemplo.c","r")) == NULL)  // obre l'arxiu
    		{
    				printf("No s'ha trobat cap fitxer anomenat aix");
    		}
    			
    		else
    		{
    				int N;
    				int i;
    				int j;
    			
    				fscanf(f1,"%d",&N);
    				fscanf(f1,"%d",&M);	
    			
    				matriu=(int**) malloc((N*M*sizeof(int*)));	//reserva l'espai necessari per la matriu
    	
    				for(i=0; i<N; i++)
    					{ 
    						for (j=0;j<M;j++)
    						matriu[i]=(int*) malloc((N*sizeof(int)));
    					}
    				
    				fLectura(matriu,f1,N);	
    				fEscriure(matriu,f1,N);	//llegeix el fitxer i coloca els valors al lloc corresponent de la matriu
    					//Aqui haura de comparar les files i columnes, i la suma de asteriscos per veure si es reursiva
    		}
    		
    
    // En aquesta part obrin l'arxiu i creem la matriu.
    
    void fLectura(int **matriu,FILE *f1,int N)
    {
    	int a,b,z;
    	for (a=0; a<N; a++)
    	{
    		for (b=0; b<N; b++)
    		{
    			fscanf(f1,"%d",&z);
    			matriu[a][b]=z;
    			//printf("matriu[%d][%d]: %d \n", a,b,matriu[a][b]);
    		}
    	}
    }
    
    void fEscriure(int **matriu,FILE *f1, int N)  
    {
    	int a,b;
    	fprintf(f1 ,"%d ", N);
    	for (a=0; a<N; a++)
    	{
    		for (b=0; b<N; b++)
    		{
    			printf("matriu[%d][%d]: %d \n", a,b,matriu[a][b]);
    			fprintf(f1 ,"%d ", matriu[a][b]);
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So, what's got you stumped, specifically?

    I don't want to play detective with your program, sorry.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    I've been trying to do the function, to view
    symmetry, but can not get me out ... took about
    few days of trying and no way

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Show the output you want, from an input file (exactly), and attach the input file.

    To attach a file, go down the editor page, and click on the "manage attachments" button, and upload the file. The "Attachments" pull down menu at the top of the editor window, doesn't work, AFAIK.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    I was on the post what I have done, the input into and
    the output would like.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In the output file:
    Code:
    *     *
    **   **
    **   **
    *** ***
    simettry: form
    orientation: vertical/horizontal
    Size of array: 4 4
    Character: *
    character times: 8
    I'm unclear about symmetry: form.

    Can you explain that?

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    symetry: form because there are another symmetry of content, but i don't upload this part of work

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    Program to make
    There must be a program by the command line take name of an input text file, and also name 1
    Note that in this type of symmetry, the type of character does not matter. That is, all different characters are white space treated equally. output file. The input file will contain a figure created on the basis of any ASCII characters.
    For example, the first figure shown in this document:
    Code:
    *          * 
    * *      * * 
    * *      * * 
    * * *  * * *
    would be represented in the file of the following form:
    The program will analyze the figure, detecting all possible symmetries that sample, and generating an output file with detailed information on all the symmetries detected.
    The information generated in each case is as follows.
    Output for Shape Symmetry
    The output file should contain:
    1. Repetition of the original figure in its entirety
    2. Type of symmetry: a
    3. Orientation: vertical or horizontal.
    4. What is the size of each half has been divided figure
    5. What is the ASCII character has been predominant used for create the figure. It should be noted that a figure can have symmetry of form, despite using more than one character ASCII
    implementation. If more than one ASCII character appear the same number of times, can be indicated either them (Only one would suffice)
    6. What is the number of occurrences of this character in each half of the figure In the example shown at the beginning of this section, output should include exactly (same order, words, punctuation, uppercase and lowercase letters, without accents, etc..), the following
    content:

    Code:
    *          * 
    * *      * * 
    * *      * * 
    * * *  * * *
    It should be noted that it is possible that the same figure shows both vertical and horizontal symmetry. In this case, you leave one blank line, and then show (repeat) all information
    detailed here for each of the cases, except made of reproduce the original figure, it is only necessary show once All output file.
    In the event that the figure is not symmetrical, the file output should contain only a reproduction of it, and more line stating: "Symmetry: no." Nothing more. Output for Symmetry Content Suppose we want to represent in the file following figure:

    Code:
    * *   * * *
    * *     * +
    * *      *
    * *       *
    *   + * * *
    * * * *
    Which has horizontal symmetry of contents:

    Program to make
    There must be a program by the command line take name of an input text file, and also name 1
    Note that in this type of symmetry, the type of character does not matter. That is, all different characters are white space treated equally. output file. The input file will contain a figure created on the basis of any ASCII characters.
    For example, the first figure shown in this document:
    Code:
    *          * 
    * *      * * 
    * *      * * 
    * * *  * * *
    would be represented in the file of the following form:
    The program will analyze the figure, detecting all possible symmetries that sample, and generating an output file with detailed information on all the symmetries detected.
    The information generated in each case is as follows.
    Output for Shape Symmetry
    The output file should contain:
    1. Repetition of the original figure in its entirety
    2. Type of symmetry: a
    3. Orientation: vertical or horizontal.
    4. What is the size of each half has been divided figure
    5. What is the ASCII character has been predominant used for create the figure. It should be noted that a figure can have symmetry of form, despite using more than one character ASCII
    implementation. If more than one ASCII character appear the same number of times, can be indicated either them (Only one would suffice)
    6. What is the number of occurrences of this character in each half of the figure In the example shown at the beginning of this section, output should include exactly (same order, words, punctuation, uppercase and lowercase letters, without accents, etc..), the following
    content:

    Code:
    *          * 
    * *      * * 
    * *      * * 
    * * *  * * *
    It should be noted that it is possible that the same figure shows both vertical and horizontal symmetry. In this case, you leave one blank line, and then show (repeat) all information
    detailed here for each of the cases, except made of reproduce the original figure, it is only necessary show once All output file.
    In the event that the figure is not symmetrical, the file output should contain only a reproduction of it, and more line stating: "Symmetry: no." Nothing more. Output for Symmetry Content Suppose we want to represent in the file following figure:

    Code:
    * *   * * * 5
    * *     * +  4
    * *      *   3
    * *       *    3
    *   + * * * 5
    * * * *    4
    as their 'vectors of values' are, respectively, <5,4,3> and <3,5,4>. These vectors are the same, but with management different.
    Code:
    *          * 
    * *      * * 
    * *      * * 
    * * *  * * *
    Symmetry: shape 
    Orientation: Vertical 
    Tamani: April 4 
    Character: * 
    Appearances: 8
    The input file is as follows:
    Code:
    * *   * * *
    * *     * +
    * *      *
    * *       *
    *   + * * *
    * * * *
    The output file should contain:
    1. Repetition of the original figure in its entirety
    2. Type of symmetry: content
    3. Orientation: vertical or horizontal.
    4. What is the size of each half has been divided figure
    5. What is the ASCII character has been predominant
    used for create the figure. If more than one ASCII character appear the same number of times it can be mentioned any of them (only one would suffice)
    6. What is the number of occurrences of this character in each half of the figure
    7. The 'vector of values' sorted descending
    8. Figure Play 'equivalent to the original' that sample symmetry of form
    In the example in this section, the output would following:
    Code:
    * *   * * *
    * *     * +
    * *      *
    * *       *
    *   + * * *
    * * * *
    
    Symmetry: content 
    Orientation: horizontal 
    S size: 3 6 
    Character: * 
    Appearances: 12 
    Values: 5 4 3 
    ***** 
    **** 
    *** 
    *** 
    **** 
    *****
    It should be noted that it is possible that the same figure shows both vertical and horizontal symmetry. In this case, you leave one blank line, and then show (repeat) all information detailed here for each of the cases, except made of reproduce the original figure, it is only necessary show once all output file.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't read whatever language that is in the program. What did the instructor want to see you use for an algorithm?

    I'll be off the forum for several hours now, but will get back to you.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    The instructor wants me to do a program where from a text file with ASCII elements, say if either side is symmetric, setting ASCII house in a position of the matrix array divided by the center, horizontally or vertically, to see if either side is symmetrical, so I'm not out is the comparison, I need a function to do this.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by raizard View Post
    The instructor wants me to do a program where from a text file with ASCII elements, say if either side is symmetric, setting ASCII house in a position of the matrix array divided by the center, horizontally or vertically, to see if either side is symmetrical, so I'm not out is the comparison, I need a function to do this.
    OK, i understand. I know how I would do it, but I thought the instructor might have told you how he wanted to have you do it.

    My idea is to make another matrix, equal in dimension to half the original. Then transpose the upper half of the original (for example), into the new matrix.

    Then in a for loop, scan through the new array and the lower half of the original array, and see if there are any differences. If one element has a different value in that comparison, then the symmetry is broken for that transposition (say horizontal for this example).

    Now repeat it with a zero'd out new matrix, for the vertical symmetry test, transposing the left hand side of the matrix, into the new matrix, and then comparing it with the right half of the original matrix. Again, using a for loop.

    Sound right to you?

    There may be a slick way to do this with just offsets - let me ponder that one.

    Ah! Catalan! - I was thinking "Close to Spanish but no, maybe Portuguese - but nope. What language IS this?
    Last edited by Adak; 08-29-2010 at 04:52 PM.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    yes, my instructor want to i do it, but i'm trying but i can't do...

    This is my code but it fail...
    I need it for tomorrow...


    Code:
    #include <stdio.h>
    #include <string.h>
    #define N 3
    #define M 4
    main()
    
    {
    char c='*',d='+';
    	
    int f[N][M] = {{d,d,d,c},{c,c,c,c},{c,d,d,c}};
    int x;
    int y=0;
    int cont=1;;
    
    for (y=0;y<M;y++)
    {
    	while (x<=((N/2)-1) && cont<=(N/2))
    	{
    		if ((f[x][y])!=(f[N-cont][y]))
    		{
    			printf("No te simetria vertical");
    		}
    		cont++;
    		x++;
    	}
    }
    printf("Te simetria vertical");
    
    cont=1;
    y=0;
    
    for (x=0;x<N;x++)
    {
    	while (y<=((M/2)-1) && cont<=(M/2))
    	{
    		if ((f[x][y])!=(f[x][M-cont]))
    		{
    			printf("No te simetria horitzontal");
    		}
    		cont++;
    		y++;
    	}
    }
    
    printf("Te simetria horitzontal");
    
    cont=1;
    x=0;
    
    }

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    The horizontal fails us when the matrix is 4x4, 6x6 and works with 3x3, 5x5 ...
    The vertical Always Fails ....

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is your program, with a few tweaks. For both horizontal and vertical symmetry, it works ONLY from the center of the array. That's what I interpret symmetry to mean.

    Post back if you find a bug here. I didn't test it much due to time constraints.


    Code:
    /* tests for symmetry in horizontal or vertical mode, of an array
    
    Status: needs testing
    
    */
    
    
    #include <stdio.h>
    #include <string.h>
    #define N 4
    #define M 4
    #define d '+'   
    #define c '*'
    
    #pragma warn -rch
    
    int main() {
      int bad, i, j, row, col, mid;	/* i counts forward, j counts backward */
      int f[N][M] = {
      {c,c,c,c},
      {c,c,c,d},
      {c,c,c,d},
      {c,c,c,c}};
    //  {c,c,c,c}};
      printf("\n\n");
      /* show the f array */
      for(row=0;row<N;row++) {
        for(col=0;col<M;col++) {
          printf("%c ", f[row][col]);
        }
        putchar('\n');
      }
      printf("\n\n");
      /* begin horizontal symmetry test */
      if(N%2==1) {
        mid = N/2;
        j=N-1;
        bad=0;
        for(row=0;row<mid;row++) {
          for(col=0;col<M;col++) {
            if(f[row][col] != f[j][col]) {
              bad=1;
              break;
            }
          }
          if(bad) 
            break;
          else
            --j;
        }
      }
      else {  //N is even
        mid=N/2;
        j=N-1;
        bad=0;
        for(row=0;row<mid;row++) {
          for(col=0;col<M;col++) {
            if(f[row][col] != f[j][col]) {
              bad=1;
              break;
            }
          }
          if(bad) 
            break;
          else
            --j;
        }
      }
      if(bad) 
        printf("\n no horizonatal symmetry was found");
      else
        printf("\n horizontal symmetry was found");
    
      /* begin vertical symmetry testing */
      if(M%2==1) {  /* has odd number of columns */
        mid=M/2;
        bad=0;
        j=mid+1;
    
        for(row=0;row<N;row++) {
          for(col=0;col<mid;col++) {
            if(f[row][col] != f[row][col+j]) {
              bad=1;
              break;
            }
          }
          if(bad) 
            break;
        }
      }
      else {  //M is even
        mid=M/2;
        bad=0;
        j=mid;
        for(row=0;row<N;row++) {
          for(col=0;col<mid;col++) {  
            if(f[row][col] != f[row][col+j]) {
              bad=1;
              break;
            }
          }
          if(bad) 
            break;
        }
      }
      if(bad) 
        printf("\n no vertical symmetry was found");
      else
        printf("\n vertical symmetry was found");
    
      printf("\n\n\t\t\t    press enter when ready");
      row=getchar();
      return 0;
    
    }
    Last edited by Adak; 08-30-2010 at 05:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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