Thread: Problem with switch.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Problem with switch.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define MAXROWS 20
    #define MAXCOLS 20
    
    
    int columnas, filas;
    
    
    void clrMatrix(double matrix[][MAXCOLS])
    /* Establecer en 0.0 todos los elementos de la matriz */
    {
    int i, j;
    for (i = 0; i < MAXROWS; i = i + 1)
    for (j = 0; j < MAXCOLS; j = j + 1)
    matrix[i][j] = 0.0;
    }
    void setMatrix(double matrix[][MAXCOLS])
    /* Asignar números aleatorios a los elementos de la matriz */
    {
    int i, j;
    static unsigned int s;
    s = s + time(NULL) * 100;
    srand(s);
    for (i = 0; i < MAXROWS; i = i + 1)
    for (j = 0; j < MAXCOLS; j = j + 1)
    matrix[i][j] = ((double) rand() / RAND_MAX);
    
    
    }void prnMatrix(double matrix[][MAXCOLS])
    /* Imprime los valores de la matriz */
    {
    int i, j;
    printf("\n");
    for (i = 0; i < filas; i = i + 1)
    {
    for (j = 0; j < columnas; j = j + 1)
    printf("%10.2f ", matrix[i][j]);
    printf("\n");
    }
    }
    
    
    void movMatrix(double dstMatrix[][MAXCOLS], double srcMatrix[][MAXCOLS])
    /* Mueve, i. e. “asigna” los valores de la matriz srcMatrix a dstMatrix */
    {
    int i, j;
    for (i = 0; i < MAXROWS; i = i + 1)
    for (j = 0; j < MAXCOLS; j = j + 1)
    dstMatrix[i][j] = srcMatrix[i][j];
    }
    
    
    void addMatrix(double dstMatrix[][MAXCOLS], double srcMatrix[][MAXCOLS])
    /* Suma de matrices */
    {
    int i, j;
    for (i = 0; i < MAXROWS; i = i + 1)
    for (j = 0; j < MAXCOLS; j = j + 1)
    dstMatrix[i][j] += srcMatrix[i][j];
    }
    void subMatrix(double dstMatrix[][MAXCOLS], double srcMatrix[][MAXCOLS])
    /* Resta de matrices */
    {
    int i, j;
    for (i = 0; i < MAXROWS; i = i + 1)
    for (j = 0; j < MAXCOLS; j = j + 1)
    dstMatrix[i][j] -= srcMatrix[i][j];
    }
    void mulMatrix(double dstMatrix[][MAXCOLS], double srcMatrix[][MAXCOLS])
    /* Multiplicacion de matrices (requiere validación de las dimensiones)*/
    {
    int i, j, k;
    for (i = 0; i < MAXROWS; i = i + 1)
    for (j = 0; j < MAXCOLS; j = j + 1)
    for (k = 0; k < MAXCOLS; k = k + 1)
    {dstMatrix[i][j] = dstMatrix[i][j] + (dstMatrix[k][j] * srcMatrix[i][k]);
    }
    }
    void clrRow(double dstMatrix[][MAXCOLS], int row)
    /* Puesta en 0.0 de los elementos “fila” dada (row) de una matriz */
    {
    int i;
    for(i = 0; i < MAXCOLS; i = i + 1)
    dstMatrix[row][i] = 0.0;
    }void clrCol(double dstMatrix[][MAXCOLS], int col)
    /* Puesta en 0.0 de los elementos “columna” dada (col) de una matriz */
    {
    int i;
    for(i = 0; i < MAXROWS; i = i + 1)
    dstMatrix[i][col] = 0.0;
    }
    
    
    void getRow(double dstVector[], double srcMatrix[][MAXCOLS], int row)
    /* Devuelve, i. e. “asigna” los elementos de una fila de una matrizx a un vector */
    {
    int i;
    for (i = 0; i < MAXCOLS; i = i + 1)
    dstVector[i] = srcMatrix[row][i];
    }
    
    
    void prnVector(double vector[MAXROWS])
    /* Imprime los valores de un vector */
    {
    int i;
    printf("\n");
    for (i = 0; i < MAXROWS; i = i + 1)
    printf("%8.2f\n", vector[i]);
    }
    
    
    
    
    int main()
    {
    double A[MAXROWS][MAXCOLS];
    double B[MAXROWS][MAXCOLS];
    double C[MAXROWS][MAXCOLS];
    double D[MAXROWS];
    double E[MAXCOLS];
    
    
    int salir=1, raro,z=1, p,s;
    
    
    
    
    clrMatrix(A);
    clrMatrix(B);
    clrMatrix(C);
    setMatrix(A);
    setMatrix(B);
    setMatrix(C);
    
    
    do
    { 
                 printf (" Que opcion desea elegir?\n");
                 printf ("1.Crear matriz \n2.Suma de matrices \n3.Resta de matrices.\n");
                 printf ("4.Multiplicacion de matrices.\n5.Matriz Transpuesta\n6.Encontrar punto de cabalgadura de una matriz\n");
                 printf ("7.Borrar matrices guardadas\n");
                 scanf ("%d", &raro);
                 
                 switch(raro)
                 {
                 
                 case 1:
                  
                      printf ("\n Cuantas filas tendran las matrices? (Tenga en cuanta que la suma no se efectuara si las matrices no son cuadradas)\n");
                      scanf  ("%d", &filas);
                      printf ("\n Cuantas columnas tendran las matrices?\n");
                      scanf  ("%d", &columnas);
                      printf("\nMatriz A:\n");
                      prnMatrix(A);
                      printf("\nMatriz B:\n");
                      prnMatrix(B);
                      z=0;
                      break;
                      
                 case 2:
                       if (z==0)
                       { printf ("Sumatoria de la matriz A + B, tenga en cuenta que si las matrices no poseen el mismo tamaño no se efectuara la suma\n");
                         addMatrix(A, B);
                         printf("\nMatriz A:\n");
                         prnMatrix(A);
                         }
                         else
                         printf (" No se han creado matrices para sumar\n");
                         
                         break;
                         
                 case 3:
                      if (z==0)
                       { printf ("Resta de la matriz A - B, tenga en cuenta que si las matrices no poseen el mismo tamaño no se efectuara la resta\n");
                         subMatrix(A, B);
                         printf("\nMatriz A:\n");
                         prnMatrix(A);
                         }
                         else
                         printf (" No se han creado matrices para restar\n");
                         
                         break;
                        
                      }
                        case 4:
                         if (z==0)
                       { printf ("Multiplicacion de la matriz A x B\n");
                         mulMatrix(A, B);
                         prnMatrix(A);
                       }
                         else
                         printf (" No se han creado matrices para multiplicar\n");
                         
                         break;                 
                 
                       
                      
                      
    
    
                      
    
    
    } while(salir==1);
    
    
    }

    case label not within switch statement. It select case 4 yet when i take it out it compiles and runs just well.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    When I first looked at your post, and saw the first ~30 lines of code, I was going to say "your problem is that your code is poorly formatted". Then I thought "no, be fair, read the whole post first". So I did. I got to the bottom, saw the error message, and guess what? I was right.

    Your problem is that your formatting and indentation are crappy. Bad indentation doesn't cause compiler errors, but it makes it easy to make other mistakes that cause compiler errors. When your code is hard to read, you can't spot the very obvious error (stated quite clearly by the compiler). That makes mistakes easy to make, and hard to find or fix.

    The line with 'case 4:' is not inside the switch statement. Your curly brackets are all messy, so you don't realize that you have one a closing curly brackets, before the case 4 that shouldn't after it.

    Read this page: SourceForge.net: Indentation - cpwiki. Pick a clear indentation style, and use it in all your C code, no exceptions. Be consistent in your indentation and style. Clarity and readability are paramount. Also, when posting your code on forums, using spaces instead of tab characters for indentation helps make your code look good on the forum. Most editors/IDEs have a setting for that somewhere, and offer auto-indentation features. Learn to use them, they'll save you tons of time chasing silly errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch problem
    By GolDRoger in forum C Programming
    Replies: 4
    Last Post: 02-09-2012, 12:21 PM
  2. switch problem
    By joshhud in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 12:35 PM
  3. switch problem
    By sh4k3 in forum C Programming
    Replies: 18
    Last Post: 06-11-2007, 11:27 PM
  4. switch problem
    By kocika73 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 11:56 AM
  5. Switch Problem
    By Tynnhammar in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2004, 11:57 AM