int main(int ari,char **arc){//./a.out 5,3 4,3 if(ari<3){ printf("error execute like %s rowsOneMatriz,columnsOneMatriz rowsTwoMatriz,columnsTwoMatriz\n",arc[0]); return 1; } int upper,*dimensions=(int *)malloc(sizeof(int)*(MATRIZES*2)); //dimensions containts rows and columns of matrices one and two dimensions[0]=arc[1][0]-48; //set rows matriz one dimensions[1]=arc[1][2]-48; //set columns matriz one dimensions[2]=arc[2][0]-48; //set rows matriz two dimensions[3]=arc[2][2]-48; //set columns matriz two upper=getUpper(dimensions,MATRIZES*2);//get upper number of array integer dimension int pow3=getPow3(upper);//get pontencia of 3 same or mayor that upper printf("\t\t\tenter values for matriz one\n"); int **matrizOne=getMatriz(dimensions[0],dimensions[1],pow3); printf("\t\t\tenter values for matriz two\n"); int **matrizTwo=getMatriz(dimensions[2],dimensions[3],pow3); printf("\t\t\tmatriz one\n"); //printMatriz(matrizOne,dimensions[0],dimensions[1]); printMatriz(matrizOne,pow3,pow3); printf("\t\t\tmatriz two\n"); //printMatriz(matrizTwo,dimensions[2],dimensions[3]); printMatriz(matrizTwo,pow3,pow3); int **response=multiplication(matrizOne,matrizTwo,0,0,0,0,pow3); printf("\t\t\tresponse\n"); printMatriz(response,dimensions[0],dimensions[3]); printMatriz(response,pow3,pow3); return 0; }
//Main int main(int argc, char *argv[]) { //VALIDAR IP Y PUERTO RECIBIDO //conexion con el server establecerConexionConElServidor(argv); inicializarMatriz(matriz); //Espero el mensaje de me conexion exitosa con el servidor recv( caller_socket , buffer, TAMBUF ,0); printf("\nMensaje de Server:%s\n", buffer); //vector que va a guardar las coordenadas ingresadas por el usuario char movimientos[7]; int i; //Recibe y convierte buffer (protoclo), Imprime Matriz de juego recibida al iniciar la partida printf("MATRIZ DE JUEGO: ESCRIBA FIN PARA FINALIZAR\n"); printMatriz(matriz); while(!fin_partida){ printf("Ingrese las coordenadas de las fichas a develar con el siguiente formato:\n"); printf("X1 Y1 X2 Y2\n"); gets(movimientos); //Valida coordenadas a develar ingresadas por el usuario if(strcmp(movimientos,"FIN")!=0) { while(!validarMovimiento(movimientos)){ fflush(stdin); printf("Formato ingresado incorrecto, reingrese\n"); printf("X1 Y1 X2 Y2\n"); gets(movimientos); } printMatriz(matriz); enviarCoordenadas(matriz,buffer,movimientos); } /* printf("resultado comparacion: %d \n",strcmp( buffer, "fin" )); printf("mensaje buffeu: %s \n", buffer);*/ if(strcmp( movimientos, "FIN" ) == 0 || aciertos==50) fin_partida = true; //printf("\nMensaje de Server:%s\n", buffer); //sleep(10); //PARA PROBAR } printf("*****************************SALISTE DE LA PARTIDA, CANTIDAD DE ACIERTOS:%d*****************************8\n", aciertos); exit(0); }
int main(){ printf("--INICIÓ PROCESO HIJO--\n"); init(); //inicializa operaciones int i, j, n; int shmid1,shmid2; key_t llave1,llave2; float *shm1; float *shm2; llave1 = 5677; llave2 = 5678; pid_t pid; char *argv[2]; argv[0] = "Nieto"; argv[1] = NULL; /*Creación de bloque de memoria compartida*/ if((shmid1 = shmget(llave1, sizeof(float)*100, IPC_CREAT | 0666)) < 0) { perror("Error al obtener memoria compartida: shmget\n"); exit(0); } if((shmid2 = shmget(llave2, sizeof(float)*100, IPC_CREAT | 0666)) < 0) { perror("Error al obtener memoria compartida: shmget\n"); exit(0); } if((shm1 = shmat(shmid1, NULL, 0)) == (float *) -1){ perror("Error al enlazar la memoria compartida: shmat\n"); exit(0); } if((shm2 = shmat(shmid2, NULL, 0)) == (float *) -1){ perror("Error al enlazar la memoria compartida: shmat\n"); exit(0); } /*Obtención de las matrices del proceso padre*/ Matriz m1 = crear(10, 10); Matriz m2 = crear(10, 10); for(i=0; i<10; i++){ for(j=0; j<10; j++){ n= (i*10) + j; m1->filas[i][j] = *(shm1+n); m2->filas[i][j] = *(shm2+n); } } /*Valores de las matrices para el proceso nieto*/ srand(time(NULL)); for(i=0; i<100; i++){ *(shm1+i) = rand() % 11; } for(i=0; i<100; i++){ *(shm2+i) = rand() % 11; } /*Creación del proceso hijo del hijo*/ if((pid= fork())==-1) printf("Error al crear el proceso hijo del proceso hijo\n"); if(pid == 0){ execv(argv[0], argv); } else{ Matriz m = mult(m1, m2); printf("\nMatriz 1 recibida del padre:\n"); printMatriz(m1); printf("\nMatriz 2 recibida del padre:\n"); printMatriz(m2); printf("\nM1*M2:\n"); printMatriz(m); esperar(0); //esperamos que termine suma printf("Hijo: Enviando mult a padre...\n"); /*Guardando el producto en el bloque de memoria compartida*/ for(i=0; i<10; i++){ for(j=0; j<10; j++){ n= (i*10) + j; *(shm1+n) = m->filas[i][j]; } } printf("--FINALIZÓ PROCESO HIJO--\n"); liberar(1); //termina multiplicación exit(0); } }
int main () { int numLinhas, numColunas; printf ("Escolha o tamanho da matriz esparsa (MxN) > "); scanf ("%d %d", &numLinhas, &numColunas); if (numLinhas <= 0 || numColunas <= 0) { fprintf (stderr, "Matriz não pode ter dimensão menor que 1\n"); return -1; } matrizEsparsa *mat = criaMatrizEsparsa (numLinhas, numColunas); int opcao, y, x, ret; double valor; double det; do { menu (); scanf ("%d", &opcao); switch (opcao) { case 1: printf ("Escolha a posição da matriz a ser consultada (MxN) > "); scanf ("%d %d", &y, &x); ret = consultaValor (mat, y, x); if (ret != ERRO) { printf ("Matriz[%d][%d] = %d\n", y, x, ret); } break; case 2: printf ("Escolha o valor a ser inserido > "); scanf ("%lf", &valor); printf ("Escolha em que posição da matriz o valor será inserido (MxN) > "); scanf ("%d %d", &y, &x); if (insereValor (mat, y, x, valor) != ERRO) { printf ("Valor %6.3lf inserido em %dx%d\n", valor, y, x); } break; case 3: printf ("Escolha a linha a ser somada > "); scanf ("%d", &y); ret = somaLinha (mat, y); if (ret != ERRO) { printf ("Soma da linha %d = %d\n", y, ret); } break; case 4: printf ("Escolha a coluna a ser somada > "); scanf ("%d", &x); ret = somaColuna (mat, x); if (ret != ERRO) { printf ("Soma da coluna %d = %d\n", x, ret); } break; case 5: printMatriz (mat); break; case 6: det = determinante (mat); if (det != ERRO) { printf ("Determinante da matriz = %f", det); } break; case 7: gaussSeidel (mat); break; } puts (""); } while (opcao != 0); apagaMatrizEsparsa (mat); return 0; }
int Principal::profundidade(vector<vector<int> > start, vector<vector<int> > goal){ vector<pair < vector <vector<int> > , int > > open; vector<pair < vector <vector<int> > , int > > closed; pair < vector <vector<int> > , int > X; vector<vector<vector <int> > > children; vector<vector<vector <int> > > temp; int id_pai = -1; int flag; int cont = 0; closed.clear(); open.push_back(make_pair(start, id_pai)); while(open.empty() == false){ X = open.front(); open.erase(open.begin()); if(matriz_cmp(X.first, goal)){ this->setLog(QString("Algoritmo: Busca em profundidade.")); this->setLog(QString("Executando Movimentos...")); temp.push_back(X.first); while(X.second >= 0){ X.first = closed.at(X.second).first; X.second = closed.at(X.second).second; temp.push_back(X.first); cont++; } for(int a = temp.size()-1; a >= 0; a--){ printMatriz(temp.at(a)); } this->setLog(QString("Numero de Estados: "+ QString("%1").arg(cont) ) ); this->setLog(QString("Numero Total de Estados Visitados: "+QString("%1").arg( id_pai+1) ) ); return 0; } else { children = generateChildren(X.first); closed.push_back(X); //printMatriz(X.first); id_pai++; flag = 0; int i = 0; int j = 0; for( i = children.size() - 1; i >= 0; i--){ for(j=0;j<open.size(); j++){ if(matriz_cmp(children.at(i), open.at(j).first)){ flag = 1; } } for(j = 0; j < closed.size(); j++){ if(matriz_cmp(children.at(i), closed.at(j).first)){ flag = 1; } } if(flag == 0){ open.insert(open.begin(), make_pair(children.at(i), id_pai)); } flag = 0; } } } this->setLog(QString("Fail")); return 1; }
int main(){ init(); //inicializa operaciones int i, j, n; int shmid1,shmid2; key_t llave1,llave2; float *shm1; float *shm2; llave1 = 5677; llave2 = 5678; pid_t pid; char *argv[2]; argv[0] = "Hijo"; argv[1] = NULL; /*Creación de bloque de memoria compartida*/ if((shmid1 = shmget(llave1, sizeof(float)*100, IPC_CREAT | 0666)) < 0) { perror("Error al obtener memoria compartida: shmget\n"); exit(0); } if((shmid2 = shmget(llave2, sizeof(float)*100, IPC_CREAT | 0666)) < 0) { perror("Error al obtener memoria compartida: shmget\n"); exit(0); } if((shm1 = shmat(shmid1, NULL, 0)) == (float *) -1){ perror("Error al enlazar la memoria compartida: shmat\n"); exit(0); } if((shm2 = shmat(shmid2, NULL, 0)) == (float *) -1){ perror("Error al enlazar la memoria compartida: shmat\n"); exit(0); } printf("shmid1: %d\n", shmid1); printf("shmid2: %d\n", shmid2); reset(0); //ponemos en 0 el semáforo reset(1); //ponemos en 0 el semáforo bloquear(0); //suma bloquear(1); //mult /*Valores de las matrices para el proceso hijo*/ srand(time(NULL)); for(i=0; i<100; i++){ *(shm1+i) = rand() % 11; } for(i=0; i<100; i++){ *(shm2+i) = rand() % 11; } /*Creación del proceso hijo*/ if((pid= fork())==-1) printf("Error al crear el proceso hijo\n"); if(pid == 0){ execv(argv[0], argv); } else{ esperar(1); //esperamos operaciones /*Obtención de los resultados de la suma y producto*/ Matriz mult = crear(10, 10); Matriz sum = crear(10, 10); for(i=0; i<10; i++){ for(j=0; j<10; j++){ n= (i*10) + j; mult->filas[i][j] = *(shm1+n); sum->filas[i][j] = *(shm2 + n); } } printf("Calculando inversas...\n"); /*Matrices Inversas*/ Matriz invMult = inv(mult); Matriz invSum = inv(sum); printf("\nMatriz inversa de la MULTIPLICACIÓN realizada por el HIJO:\n"); printMatriz(invMult); printf("\nMatriz inversa de la SUMA realizada por el NIETO:\n"); printMatriz(invSum); printf("\n"); guardaMatriz(invMult, "multiplicacion.txt"); guardaMatriz(invSum, "suma.txt"); printf("Terminado.\n"); exit(0); } }