+ All Categories
Home > Education > Programas c angelwha3

Programas c angelwha3

Date post: 18-Aug-2015
Category:
Upload: angel-garcia
View: 16 times
Download: 3 times
Share this document with a friend
Popular Tags:
40
1 PROGRAMAS EN C MIGUEL ANGEL GARCIA WHA CURSO DE MAESTRIA DE ALGORITMOS 2015 Pointers and structures Exercise 45: pointer algebra Using the following declarations: int A[]={2,6,5,1,3}; int *p; p=A; what is the result of the instructions: 1) *p 6) p 11) p+(*(p+4)-3) 2) *p+2 7) A 3) *(p+2) 8) &A[0] 4) &p 9) &A[0]+3 5) &p+1 10) A+3
Transcript
Page 1: Programas c angelwha3

1

PROGRAMAS EN C MIGUEL ANGEL GARCIA WHA

CURSO DE MAESTRIA DE ALGORITMOS 2015

Pointers and structures Exercise 45: pointer algebra Using the following declarations: int A[]={2,6,5,1,3}; int *p; p=A; what is the result of the instructions: 1) *p 6) p 11) p+(*(p+4)-3) 2) *p+2 7) A 3) *(p+2) 8) &A[0] 4) &p 9) &A[0]+3 5) &p+1 10) A+3

Page 2: Programas c angelwha3

2

CODIGO: #include <stdio.h> #include <stdlib.h> int main(){ printf("Tarea 45 Angel Wha\n\n"); int A[]={2,6,5,1,3}; int *p; p=A; printf("El 1 resultado es: %d\n", *p); printf("El 2 resultado es: %d\n", *p + 2); printf("El 3 resultado es: %d\n", *(p+2)); printf("El 4 resultado es: %d\n", &p); printf("El 5 resultado es: %d\n", &p+1); printf("El 6 resultado es: %d\n", p); printf("El 7 resultado es: %d\n", A); printf("El 8 resultado es: %d\n", &A[0]); printf("El 9 resultado es: %d\n", &A[0]+3); printf("El 10 resultado es: %d\n", A+3); printf("El 11 resultado es: %d\n", p+(*(p+4)-3)); system("pause"); return 0; }

Page 3: Programas c angelwha3

3

SALIDA DEL CODIGO:

Exercise 46: complex number and structure Create a structure to store a complex number and write three functions (for addition, multiplication and norm) that handle this new structure. CODIGO: SALIDA DEL CODIGO:

Page 4: Programas c angelwha3

4

Exercise 47: structures (cont.) Write a program that will stores the personal information of a student (such as name, date of birth, address) in a structure that you will define and then prints the record on the screen. A table of characters can be defined using pointers such as char *myline. CODIGO: #include <stdio.h> #include <stdlib.h> int main(){ printf("Tarea 47 Angel Wha\n"); ////////// NOMBRE DEL ESTUDIANTE //////////////// char nombre[1000]; char *p1; printf("Ingresa el nombre del estudiante:\n"); //scanf("%s",&nombre); /*El gets lo que hace es guardar un cadena de la entrada estandar hasta encontrar encontrar '\n'. '\n' = enter */ gets(nombre); p1 = nombre; //////////////////////////////////////// //////////////////// DIRECCION DEL ESTUDIANTE ///////////////// char direccion[1000]; char *p2; printf("Ingresa la direccion del estudiante:\n"); gets(direccion); p2 = direccion; ///////////////////////////////////////

Page 5: Programas c angelwha3

5

////////////////////////// FECHA DE NACIMIENTO DEL ESTUDIANTE //////////////// char fecha[1000]; char *p3; printf("Ingresa la fecha de nacimiento del estudiante:\n"); gets(fecha); p3 = fecha; //////////////////////////////////////////////// /////////////// IMPRESION DE LOS DATOS DEL ESTUDIANTE ////////////////// printf("Los datos del estudiante son: \n%s\n %s\n %s\n",p1,p2,p3); printf("\n\n"); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 6: Programas c angelwha3

6

Exercise 48: pointers and functions Write a function of type void that takes as input a table of integers and returns the minimum maximum and sum in three variables. We will assume that the table contains 50 numbers. CODIGO: #include <stdio.h> #include <stdlib.h> int main(){ printf("Tarea 48 Angel Wha\n"); ////////// NOMBRE DEL ESTUDIANTE //////////////// int *p1; int numero,i; printf("Ingresa el tamaño del arreglo:\n"); scanf("%d",&numero); int a[numero]; if(numero<=50){ p1 = &a[numero]; //RELLENANDO ARREGLO DE VALORES for(i=0;i<numero;i++){ printf("ingresa el valor de a[%d]=",i); //scanf("%d",&a[i]); scanf(" %d",&p1); }

Page 7: Programas c angelwha3

7

//IMPRIMIENDO ARREGLO CON LOS VALORES for(i=0;i<numero;i++){ //printf(" %d",a[i]); printf(" %d",p1); } }else{ printf("no puedes ingresar mas datos mayores a 50"); } //////////////////////////////////////// printf("\n\n"); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 8: Programas c angelwha3

8

Exercise 49: conundrum What does this function do?

CODIGO: #include <stdio.h> #include <stdlib.h> int f(int n, int (*f)()){ return n>0?n*f(n-1,f):1; } int main(){ printf("Tarea 49 Angel Wha\n"); int x1=8,*x2; f(8,&x1); //printf("El resultado de la funcion es: %d",f(n); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 9: Programas c angelwha3

9

Exercise 50: conundrum 2 What are the outputs of this code? Explain.

CODIGO: #include <stdio.h> #include <stdlib.h> int t1[] = {0,0,1,1,1}, t2[]= {0,0,1,1,1}; int main(){ printf("Tarea 50 Angel Wha\n"); int *p1 = t1, *p2 = t2; while (!*p1++ || !*p2++); printf("\n p1 - t1 es: %d\n",p1 - t1); //5 printf("\n p2 - t2 es: %d\n",p2 - t2); //3 printf("\n la impresion de ambos es: %d %d\n",p1 - t1, p2 - t2); //5 3 system("pause"); return 0; }

Page 10: Programas c angelwha3

10

SALIDA DEL CODIGO:

Exercise 51: chained lists Using pointers and structures, create a chained list in a C program. The elements of the list will describe the record of an employee. The global structure will follow this scheme:

The C program will provide two functions to add a new record and remove the last

record.

Page 11: Programas c angelwha3

11

CODIGO: #include<stdio.h> #include<stdlib.h> int salario(int *d, int *h, int *s){ //operacion multiplicacion int dinero,horas,salario; salario=dinero*horas; d = dinero; h = horas; s = salario; printf("Tu salario es de: %d\n",salario); return 0; } int main() { printf("***Salario de un empleado con PUNTEROS(APUNTADORES)***\n\n"); int din,sal,hrs; printf("Cuando te pagan por hora:\n"); scanf("%d",&din); printf("Cuantas horas trabajaste?:\n"); scanf("%d",&hrs); int x1,x2,x3; salario(x1,x2,x3); system("pause"); return 0; }

Page 12: Programas c angelwha3

12

SALIDA DEL CODIGO:

Strings Exercise 52: storing and printing Write a small C program that prints on the screen a text supplied by the user. The string will not be more than 80 characters in size.

Page 13: Programas c angelwha3

13

CODIGO: #include <stdio.h> #include <stdlib.h> int main(){ printf("Tarea 52 Angel Wha\n"); char texto[80]; printf("Ingresa el texto a mostrar:\n"); gets(texto); if(texto>=80){ printf("\n"); printf("El texto ingresado es: %s\n",texto); }else{ printf("El texto no debe de ser mayor a 80 caracteres"); } printf("\n\n"); system("pause"); return 0; }

Page 14: Programas c angelwha3

14

SALIDA DEL CODIGO:

Exercise 53: handling strings Write a small C program that prints each word of a user-supplied text on a new line. CODIGO: #include <stdio.h> #include <stdlib.h> int main(){ printf("Tarea 52 Angel Wha\n"); char texto[80]; printf("Ingresa el texto a mostrar:\n"); gets(texto); if(texto>=80){ printf("\n"); printf("la cadena completa del texto es: %s\n",texto);

Page 15: Programas c angelwha3

15

//int i,j; //for(i=0;i<80;i++){ printf("la cadena en partes del texto es:\n\n %c",texto); } /* }else{ printf("El texto no debe de ser mayor a 80 caracteres"); } */ printf("\n\n"); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 16: Programas c angelwha3

16

Exercise 54: lower to upper case Using the integer representation of a character, write a function that replaces all the lower case characters in a string by upper case characters. CODIGO: #include <stdio.h> #include <stdlib.h> int cadena_mayusculas(int m){ char cadena[50]; int i; printf("Escribe un texto o una palabra:\n"); //scanf("%s", &cadena); gets(cadena); for(i = 0; cadena[i]; i++) //para mayusculas cadena[i] = toupper(cadena[i]); //para minusculas //cadena[i] = tolower(cadena[i]); printf("El texto o palabra que ingresastes en mayusculas es:\n %s\n", cadena); return 0; } int main(){ printf("Tarea 54 Angel Wha\n\n"); int m1; cadena_mayusculas(m1); system("pause"); printf ("\n"); return 0; }

Page 17: Programas c angelwha3

17

SALIDA DEL CODIGO:

Exercise 55: strcpy Write three version of a function that takes one string as input and copies it on a second string. You will first use the tabular structure of a string then pointers and finally pointer arithmetic. CODIGO: #include <stdio.h> #include <stdlib.h> int cadena(char *t1, char *t2, char *t3){ char texto1[]="CADENA 1: miguel angel garcia wha"; char texto2[]="CADENA 2: sandra quintero fandubs"; char texto3[]="CADENA 3: natalia de jesus rivera gallardo"; t1 = texto1; t2 = texto2; t3 = texto3;

Page 18: Programas c angelwha3

18

/* t1 = &texto1; t2 = &texto2; t3 = &texto3; */ strcpy(t2,t1); printf("%s\n",t2); strcpy(t2,t3); printf("%s\n",t2); /* strcpy(texto2,texto1); printf("%s\n",texto2); strcpy(texto2,texto3); printf("%s\n",texto2); */ return 0; } int main(){ printf("Tarea 55 Angel Wha\n"); int x1,x2,x3; cadena(x1,x2,x3); //cadena(&x1,&x2,&x3); printf("\n\n"); system("pause"); return 0; }

Page 19: Programas c angelwha3

19

SALIDA DEL CODIGO:

Exercise 56: palindrome Write a program that tests if a user-supplied sentence is a palindrome. The program should not be case sensitive. CODIGO: //Comporbador de palindromos #include <stdio.h> #include <stdlib.h> #include <string.h> int palindromo(char * principio, char * final){ if ((final - principio) < 2) return 1; else if( (*principio) != (*final) ) return 0; else return palindromo(++principio, --final); }

Page 20: Programas c angelwha3

20

int main() { printf("Tarea 56 Angel Wha\n\n"); char cadena[20]; char *final; int cuenta; printf("Ingresa una frase:\n"); gets(cadena); final=cadena + strlen(cadena) - 1; printf("\n"); if (palindromo(cadena, final)) printf("Tu frase es palindromo \n"); else printf("Tu frase no es palindromo \n"); system("pause"); printf (""); return 0; } SALIDA DEL CODIGO:

Page 21: Programas c angelwha3

21

Exercise 57: characters and strings Given a string as input, write a function that counts the numbers, the lower case, upper case and special characters. CODIGO: #include <stdio.h> //#include <conio.h> //#include <string.h> int contar_vocales(char *, char *); int main(){ char cad[300],cad2[300]; int longi; float porc; printf("Ingresa el texto o palabra:\n"); gets(cad); gets(cad2); //longi = strlen(cad); //printf("La cantidad de Vocales: %d\n",contar_vocales(cad)); printf("La cantidad de Vocales: %d %d\n",contar_vocales(cad,cad2)); //printf("\nCantidad de caracteres: %d",longi); system("pause"); return 0; } int contar_vocales(char *cad,char *cad2){ int cont=0; char *aux=cad; char *aux2=cad2; while(*aux){ if(*aux=='a'||*aux=='e'||*aux=='i'||*aux=='o'||*aux=='u') cont++; aux++; }

Page 22: Programas c angelwha3

22

while(*aux2){ if(*aux2=='A'||*aux2=='B'||*aux2=='C'||*aux2=='D'||*aux2=='E'||*aux2=='F' ||*aux2=='G'||*aux2=='H'||*aux2=='I'||*aux2=='J'||*aux2=='K'||*aux2=='L' ||*aux2=='M'||*aux2=='Ñ'||*aux2=='N'||*aux2=='O'||*aux2=='P'||*aux2=='Q' ||*aux2=='R'||*aux2=='S'||*aux2=='T'||*aux2=='U'||*aux2=='V'||*aux2=='W' ||*aux2=='X'||*aux2=='Y'||*aux2=='Z') cont++; aux2++; } return cont; } SALIDA DEL CODIGO: Exercise 58: extracting characters From a user-supplied string, write a program that extracts the sentences one by one. CODIGO: SALIDA DEL CODIGO:

Input/Output and files

For these exercises we assume that the files contain ASCII characters (not binary files).

Page 23: Programas c angelwha3

23

Exercise 59: reading from a file Write a program that reads the content of a file and prints it on the screen. CODIGO: #include <stdio.h> #include <stdlib.h> int main (){ printf("Tarea 59 Angel Wha\n\n"); FILE* archivo; archivo = fopen("ejemplo1_wha.txt","r"); if(archivo!=NULL){ char c; do{ c = fgetc(archivo); printf("%c",c); }while (c!=EOF); fclose(archivo); } if(archivo!=NULL){ printf("\narchivo abierto con exito!\n"); }else{ printf("\nerror al abrir el archivo!\n"); } printf("\n"); system("pause"); return 0; }

Page 24: Programas c angelwha3

24

SALIDA DEL CODIGO:

Exercise 60: writing in a file Write a program that prints x and x2 in a file for x=1 to 10 by steps of 0.1. CODIGO: SALIDA DEL CODIGO:

Page 25: Programas c angelwha3

25

Exercise 61: reading and writing Using the structure student (ex 47) that contains the personal information of a student, write a program that asks the user to enter his/her information and stores it in an existing file named student.txt. If the file does not exist, it will be created. After the user has supplied his information, the program will print on the screen all the student information available in the file. CODIGO: #include <stdio.h> #include <stdlib.h> int main (){ /* char nom_arch[]="ejemplo2_wha.txt"; FILE*fp = fopen(nom_arch,"w"); if(fp!=NULL){ printf("\narchivo abierto con exito!\n"); }else{ printf("\nerror al abrir el archivo!\n"); } int veces=0; int i; printf("dame numero de alumnos:\n"); scanf("%d",&veces); char a[100]; char b[100]; char c[100]; for(i=0;i<veces;i++){ printf("Nombre del alumno: Fecha de nacimiento Direccion:\n"); scanf("%s %s %s",&a,&b,&c); //fscanf(fp,"%s, %s, %s,\n",&a,&b,&c); fprintf(fp,"%s, %s, %s,\n",a,b,c); printf("\n",fp); } fclose(fp); FILE *wha = fopen ("ejemplo2_wha.txt","r"); if(wha!=NULL){ while(fgets(a,sizeof (a),wha)!=NULL){ printf("%s",a,b,c); } } fclose(wha); */

Page 26: Programas c angelwha3

26

char nom_arch[]="ejemplo2_wha.txt";//nombre del archivo char cadena [100],cadena2 [100],cadena3 [100]; FILE *fp;//apuntador a archivo fp = fopen(nom_arch,"a");//w para escribir y r para leer :& if(fp!=NULL){ printf("\narchivo abierto con exito!\n"); }else{ printf("\nerror al abrir el archivo!\n"); system("pause"); return -1; } printf("escribe el nombre del alumno:\n"); //fputs = file put string scanf("%[ '̂\n']s",&cadena); printf("escribe la fecha de nacimiento del alumno:\n"); //fputs = file put string scanf("%[ '̂\n']s",&cadena2); printf("escribe la direccion del alumno:\n"); //fputs = file put string scanf("%[ '̂\n']s",&cadena3); //cadena = cadena,cadena2,cadena3; fputs(cadena,fp); fputs("\n",fp); printf("datos escritos en el archivo!!!!!!!\n"); fclose(fp);//esto siempre hay que ponerlo que nunca se me olvide para cerrar el archivo :) printf("\n"); system("pause"); return 0; }

Page 27: Programas c angelwha3

27

SALIDA DEL CODIGO:

Putting things together Exercise 62: direct Monte-Carlo and the value of pi. Using random numbers, design a simple method to estimate the value of pi. Write the corresponding code and print in a file the convergence of the solution. Hint: compare the surface of a square and a circle. CODIGO: #include <stdlib.h> #include <stdio.h> #include <math.h> #include <string.h> #define SEED 35791246 int main() {

Page 28: Programas c angelwha3

28

char nom_arch[]="ejemplo62.txt";//nombre del archivo char cadena [100]; FILE *fp;//apuntador a archivo fp = fopen(nom_arch,"w");//w para escribir y r para leer :& if(fp!=NULL){ printf("\narchivo abierto con exito!\n"); }else{ printf("\nerror al abrir el archivo!\n"); system("pause"); return -1; } int niter=0; double x,y; int i,count=0; /* # of points in the 1st quadrant of unit circle */ double z; double pi; printf("Enter the number of iterations used to estimate pi: "); scanf("%d",&niter); /* initialize random numbers */ srand(SEED); count=0; for ( i=0; i<niter; i++) { x = (double)rand()/RAND_MAX; y = (double)rand()/RAND_MAX; z = x*x+y*y; if (z<=1) count++; } pi=(double)count/niter*4; printf("# of trials= %d , estimate of pi is %g \n",niter,pi); fputs(&pi,fp); fputs("\n",fp); printf("datos escritos en el archivo!!!!!!!\n"); fclose(fp);//esto siempre hay que ponerlo que nunca se me olvide para cerrar el archivo :) printf("\n"); system("pause"); return 0; }

Page 29: Programas c angelwha3

29

SALIDA DEL CODIGO:

Exercise 63: tic-tac-toe Write the C code that permits to play the tic-tac-toe game. The winner will be saved in a file. CODIGO: #include <stdio.h> #include <stdlib.h> int main() { int i = 0; int player = 0; int go = 0; int row = 0; int column = 0; int line = 0; int winner = 0; char board[3][3] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'} };

Page 30: Programas c angelwha3

30

for( i = 0; i<9 && winner==0; i++) { printf("\n\n"); printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]); printf("-----------\n"); printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]); printf("-----------\n"); printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]); player = i%2 + 1; do { printf("\nJugador %d, eligue un numero cualquiera para reemplazarla con %c:\n",player,(player==1)?'X':'O'); scanf("%d", &go); row = --go/3; column = go%3; } while(go<0 || go>9 || board[row][column]>'9'); board[row][column] = (player == 1) ? 'X' : 'O'; if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2] == board[2][0])) winner = player; else for(line = 0; line <= 2; line ++) if((board[line][0] == board[line][1] && board[line][0] == board[line][2])|| (board[0][line] == board[1][line] && board[0][line] == board[2][line])) winner = player; } printf("\n\n"); printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]); printf("-----------\n"); printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]); printf("-----------\n"); printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);

Page 31: Programas c angelwha3

31

if(winner == 0) printf("\nNadie Gano!\n"); else printf("\nJugador %d, Gano!\n", winner); char nom_arch[]="tic_tac_toe.txt";//nombre del archivo char cadena [100]; FILE *fp;//apuntador a archivo fp = fopen(nom_arch,"a");//w para escribir y r para leer :& if(fp!=NULL){ printf("\narchivo abierto con exito!\n"); }else{ printf("\nerror al abrir el archivo!\n"); system("pause"); return -1; } //printf("escribe el nombre del alumno:\n"); //fputs = file put string //scanf("%[ '̂\n']d",&winner); fputs(&winner,fp); fputs("\n",fp); printf("datos escritos en el archivo!!!!!!!\n"); fclose(fp);//esto siempre hay que ponerlo que nunca se me olvide para cerrar el archivo :) printf("\n"); system("pause"); return 0; }

Page 32: Programas c angelwha3

32

SALIDA DEL CODIGO:

Exercise 64: towers of Hanoi The towers of Hanoi is a mathematical game where a number of disks of decreasing size from bottom to top are set on the left of a set of three rods. The goal is to move the stack of disks to another rod, obeying the following rules: -only one disk may be moved at a time -each move consists of taking the upper disk from one rod and sliding it onto another rod -no disk may be placed on top of a smaller disk. Write a C program that solves this problem and prints to the screen the different moves. Hint: the solution is purely recursive. CODIGO: SALIDA DEL CODIGO:

Page 33: Programas c angelwha3

33

PROGRAMAS EXTRAS DE TAREA HECHOS EN CLASE Y

EN CASA EN C MIGUEL ANGEL GARCIA WHA CURSO DE

MAESTRIA DE ALGORITMOS 2015

1- ORDENAR NUMEROS DE MAYOR A MENOR CON PUNTEROS CODIGO: #include <stdio.h> #include <stdlib.h> int ordena(int *n1, int *n2, int *n3){ int numero1,numero2,numero3,aux; printf("Ingresa el 1 numero:\n"); scanf("\n%d",&numero1); printf("Ingresa el 2 numero:\n"); scanf("\n%d",&numero2); printf("Ingresa el 3 numero:\n"); scanf("\n%d",&numero3); /* n1 = &numero1; n2 = &numero2; n3 = &numero3; */ n1 = numero1; n2 = numero2; n3 = numero3;

Page 34: Programas c angelwha3

34

if(n1<n2) { aux=n1; n1=n2; n2=aux; } if(n1<n3) { aux=n1; n1=n3; n3=aux; } if(n2<n3) { aux=n2; n2=n3; n3=aux; } /* if(numero1<numero2) { aux=numero1; numero1=numero2; numero2=aux; } if(numero1<numero3) { aux=numero1; numero1=numero3; numero3=aux; } if(numero2<numero3) { aux=numero2; numero2=numero3; numero3=aux; } */

Page 35: Programas c angelwha3

35

//printf("los numeros ordenados son:\n%d %d %d\n\n",numero1,numero2,numero3); printf("los numeros ordenados son:\n%d %d %d\n\n",n1,n2,n3); return 0; } int main () { printf("Programa que ordena 3 numeros de mayor a menor USANDO PUNTEROS(APUNTADORES):\n\n"); int x1,x2,x3; ordena(&x1,&x2,&x3); /* int numero1,numero2,numero3; printf("Ingresa el 1 numero:\n"); scanf("\n%d",&numero1); printf("Ingresa el 2 numero:\n"); scanf("\n%d",&numero2); printf("Ingresa el 3 numero:\n"); scanf("\n%d",&numero3); */ system("pause"); return 0; }

Page 36: Programas c angelwha3

36

SALIDA DEL CODIGO:

2- MULTIPLICAR 2 MATRICES DE 5*5 CON PUNTEROS CODIGO: #include <stdio.h> #include <stdlib.h> int multiplicacion_matrices(int *m1, int *m2, int *m3){ ///////////////////////////// MATRIZ 1 ///////////////////////////////////// int mat1f,mat1c; printf("Ingresa el numero de filas de mat1:\n"); scanf("%d",&mat1f); printf("Ingresa el numero de columnas de mat1:\n"); scanf("%d",&mat1c);

Page 37: Programas c angelwha3

37

int mat1[mat1f][mat1c],i,j; if(mat1[mat1f][mat1c]==mat1[2][2]){ //rellenamos matriz for(i=0;i<mat1f;i++){ for(j=0;j<mat1c;j++){ printf("mat1[%d][%d]=",i,j); scanf("%d",&mat1[i][j]); } } printf("\n\n"); //imprimimos matriz printf("La mat1 es:\n"); for(i=0;i<mat1f;i++){ printf("\n"); for(j=0;j<mat1c;j++){ printf("%d\t",mat1[i][j]); } } /* }else{ printf("las dimensiones de la mat1 no son de 2*2 intenta de nuevo =) saludos!!!"); } */ ////////////////////////////////////////////////////////////////////// printf("\n\n\n"); /////////////////////////////////// MATRIZ 2 /////////////////////////////// int mat2f,mat2c; printf("Ingresa el numero de filas de mat2:\n"); scanf("%d",&mat2f); printf("Ingresa el numero de columnas de mat2:\n"); scanf("%d",&mat2c); int mat2[mat2f][mat2c]; if(mat2[mat2f][mat2c]==mat2[2][2]){

Page 38: Programas c angelwha3

38

//rellenamos matriz for(i=0;i<mat2f;i++){ for(j=0;j<mat2c;j++){ printf("mat2[%d][%d]=",i,j); scanf("%d",&mat2[i][j]); } } printf("\n\n"); //imprimimos matriz printf("La mat2 es:\n"); for(i=0;i<mat2f;i++){ printf("\n"); for(j=0;j<mat2c;j++){ printf("%d\t",mat2[i][j]); } } /*}else{ printf("las dimensiones de la mat2 no son de 2*2 intenta de nuevo =) saludos!!!"); } */ ///////////////////////////////////////////////////////////////////////////// printf("\n\n"); ////////////////////////////////////// MULTIPLICACION DE AMBAS MATRICES EN UNA TERCERA MATRIZ ///////////////////// //int mat3f,mat3c; int mat3[mat1f][mat2c]; int k; for(i=0;i<mat1f;i++){ for(j=0;j<mat2c;j++){ mat3[i][j]= 0; for(k=0;k<mat1c;k++){ mat3[i][j] = mat3[i][j]+mat1[i][k]*mat2[k][j]; } } }

Page 39: Programas c angelwha3

39

printf("La multiplicacion de ambas matrices es:\n"); for(i=0;i<mat1f;i++){ printf("\n"); for(j=0;j<mat2c;j++){ //printf("mat3[%d][%d]=\t%d\n",i,j,mat3[i][j]); } printf("%d\t",mat3[i][j]); } } } }else{ printf("las dimensiones de la mat1 no son de 2*2 intenta de nuevo =) saludos!!!"); } ////////////////////////////////////////////////////////////////////////////// return 0; } int main () { printf("Programa que multiplica 2 matrices de 5*5 USANDO PUNTEROS(APUNTADORES):\n\n"); int x1,x2,x3; multiplicacion_matrices(&x1,&x2,&x3); system("pause"); return 0; }

Page 40: Programas c angelwha3

40

SALIDA DEL CODIGO:


Recommended