programas c angelwha2

37
1 PROGRAMAS EN C MIGUEL ANGEL GARCIA WHA CURSO DE MAESTRIA DE ALGORITMOS 2015 Using loops Exercise 24: prime numbers Write a program that tests if a positive integer is a prime number using a straightforward approach. You will assume that your number is smaller than 1000. CODIGO: #include <stdio.h> #include <stdlib.h> int main () { int MAGWi, MAGWj, MAGWprimo, MAGWn=0; MAGWprimo = 1; printf("ingresa el numero:\n"); scanf("%d",&MAGWn); if(MAGWn<=1000){ for (MAGWi = 2; MAGWi < MAGWn; MAGWi++) { for ( MAGWj = 2; MAGWj <= MAGWi/2; MAGWj++) if ((MAGWi%MAGWj) == 0) MAGWprimo = 0; if(MAGWprimo) printf("Los numeros primos son: %d\n", MAGWi); MAGWprimo = 1; } }else{ printf("el numero que ingresastes es mayor a 1000\n"); } system("pause"); return 0; }

Upload: angel-garcia

Post on 15-Apr-2017

200 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Programas c angelwha2

1

PROGRAMAS EN C MIGUEL ANGEL GARCIA WHA

CURSO DE MAESTRIA DE ALGORITMOS 2015

Using loops Exercise 24: prime numbers Write a program that tests if a positive integer is a prime number using a straightforward approach. You will assume that your number is smaller than 1000. CODIGO: #include <stdio.h> #include <stdlib.h> int main () { int MAGWi, MAGWj, MAGWprimo, MAGWn=0; MAGWprimo = 1; printf("ingresa el numero:\n"); scanf("%d",&MAGWn); if(MAGWn<=1000){ for (MAGWi = 2; MAGWi < MAGWn; MAGWi++) { for ( MAGWj = 2; MAGWj <= MAGWi/2; MAGWj++) if ((MAGWi%MAGWj) == 0) MAGWprimo = 0; if(MAGWprimo) printf("Los numeros primos son: %d\n", MAGWi); MAGWprimo = 1; } }else{ printf("el numero que ingresastes es mayor a 1000\n"); } system("pause"); return 0; }

Page 2: Programas c angelwha2

2

SALIDA DEL CODIGO:

Page 3: Programas c angelwha2

3

Exercise 25: factorial Write a C code that computes the factorial of an integer n. When does the code fail (upper limit on n for the result to be correct) and why? CODIGO: #include<stdlib.h> #include<stdio.h> /* Exercise 25: factorial Write a C code that computes the factorial of an integer n. When does the code fail (upper limit on n for the result to be correct) and why? */ int main() { int MAGWnum,MAGWfact=1,MAGWi; printf("Introduce un numero:\n"); scanf("%d",&MAGWnum); for(MAGWi=2;MAGWi<=MAGWnum;MAGWi++) { MAGWfact = MAGWfact * MAGWi; } printf("El factorial del numero que ingresastes es:------>%d\n",MAGWfact); system("pause"); return 0; }

Page 4: Programas c angelwha2

4

SALIDA DEL CODIGO:

Page 5: Programas c angelwha2

5

Exercise 26: xn

Write the C code that computes xn using the three loop functions (for, while and do-while). We will consider n to be an integer. CODIGO: #include <stdio.h> #include <stdlib.h> int main () { int MAGWBase, MAGWExpo, MAGWi; float MAGWPotencia=1; printf ("Introduce el numero a elevar:\n"); scanf ("%d", &MAGWBase); printf ("Introduce la potencia del numero que ingresastes:\n"); scanf ("%d", &MAGWExpo); for (MAGWi=1; MAGWi<=abs(MAGWExpo);MAGWi++){ MAGWPotencia = MAGWPotencia*MAGWBase; } while(MAGWExpo<0){ MAGWPotencia=1./MAGWPotencia; } printf ("El numero resultante es----->: %.2f\n\n",MAGWPotencia); system ("pause"); return 0; }

Page 6: Programas c angelwha2

6

SALIDA DEL CODIGO:

Exercise 27: pyramid Write a C code that permits the user to specify a number of lines and then prints on the shell a pyramid consisting of stars. The pyramid must be centered. Hints: all characters are printed separately and both the number of stars per line and the number of spaces per line must be calculated. The output will look like:

Page 7: Programas c angelwha2

7

CODIGO: #include<stdio.h> #include<stdlib.h> int main (){ int MAGWnumero,MAGWi,MAGWj; printf("Ingresa el numero:\n"); scanf("%d",&MAGWnumero); for(MAGWi=1;MAGWi<=MAGWnumero;MAGWi++){ for(MAGWj=1;MAGWj<=MAGWi;MAGWj++) printf("*"); printf("\n"); } system("pause"); return 0; } SALIDA DEL CODIGO:

Page 8: Programas c angelwha2

8

Exercise 28: min, max, average Using a loop, ask the user to enter positive integer numbers. The program will output the number of values entered, the minimum value, the maximum value and the average of all numbers. The code will exit once a negative integer is entered. CODIGO: #include <stdio.h> #include <stdlib.h> int main () { /* pedir 3 numeros por teclado y mostrar el mayor de los 3 al igual que el menor y el promedio de los 3 numeros */ int MAGWn1,MAGWn2,MAGWn3,MAGWsuma,MAGWpromedio; printf("Ingresa el 1 numero:\n"); scanf("%d",&MAGWn1); printf("Ingresa el 2 numero:\n"); scanf("%d",&MAGWn2); printf("Ingresa el 3 numero:\n"); scanf("%d",&MAGWn3); //MAYOR if(MAGWn1>MAGWn2 && MAGWn1>MAGWn3){ printf("El numero mayor de los 3 numeros es:------>%d\n",MAGWn1); }else if(MAGWn2>MAGWn1 && MAGWn2>MAGWn3){ printf("El numero mayor de los 3 numeros es:------>%d\n",MAGWn2); }else if(MAGWn3>MAGWn1 && MAGWn3>MAGWn2){ printf("El numero mayor de los 3 numeros es:------>%d\n",MAGWn3); }

Page 9: Programas c angelwha2

9

//MENOR if(MAGWn1<MAGWn2 && MAGWn1<MAGWn3){ printf("El numero menor de los 3 numeros es:------>%d\n",MAGWn1); }else if(MAGWn2<MAGWn1 && MAGWn2<MAGWn3){ printf("El numero menor de los 3 numeros es:------>%d\n",MAGWn2); }else if(MAGWn3<MAGWn1 && MAGWn3<MAGWn2){ printf("El numero menor de los 3 numeros es:------>%d\n",MAGWn3); } MAGWsuma = MAGWn1+MAGWn2+MAGWn3; //PROMEDIO MAGWpromedio = MAGWsuma/3; printf("El promedio general de los 3 numeros es:------>%d\n",MAGWpromedio); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 10: Programas c angelwha2

10

Tables and arrays Exercise 32: reversing an array Consider an array X (type float) of length n. Write the C code that prints the array Y, which is X in reverse order. CODIGO: //problema 32 ingresar el tamaño de un arreglo e impirmirlo inverso en sus posiciones #include <stdio.h> #include <stdlib.h> int main (){ int MAGWx,MAGWi; printf("Ingresa el tamano del arreglo:\n"); scanf("%d",&MAGWx); int MAGWa[MAGWx]; //llenas valores en el arreglo for(MAGWi=0;MAGWi<MAGWx;MAGWi++){ printf("a=[%d]",MAGWi); scanf("%d",&MAGWa[MAGWi]); } ///---------- IMPRIMIR INVERTIDO -------------------------- LOL for(MAGWi=MAGWx-1;MAGWi>=0;MAGWi--){ printf("tu arreglo invertido es: a=[%d]\n",MAGWa[MAGWi]); } system("pause"); return 0; }

Page 11: Programas c angelwha2

11

SALIDA DEL CODIGO:

Exercise 33: minimum and position Consider an array X of length n randomly initialized. Write a program that returns the position and value of the minimum. CODIGO: #include <stdio.h> #include <stdlib.h> int main (){ //33 es declarar el tamaño de un arreglo e imprimirlo pero la posicion y el valor minimo int MAGWx,MAGWi; printf("Ingresa el tamano del arreglo:\n"); scanf("%d",&MAGWx); int MAGWa[MAGWx]; //llenas valores en el arreglo for(MAGWi=0;MAGWi<MAGWx;MAGWi++){ printf("a=[%d]",MAGWi); scanf("%d",&MAGWa[MAGWi]);

Page 12: Programas c angelwha2

12

} printf("\n"); ///---------- IMPRIMIR CON VALOR Y POSICION MENOR DEL ARREGLO -------------------------- LOL int MAGWmenor; MAGWmenor = MAGWa[0]; for(MAGWi=1;MAGWi<MAGWx;MAGWi++){ if(MAGWa[MAGWi]<MAGWmenor){ MAGWmenor = MAGWa[MAGWi]; } } printf("El menor es: %d\n\n", MAGWmenor); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 13: Programas c angelwha2

13

Exercise 35: sorting an array (simple sort) Consider an array of integers of size n, whose values are set randomly using the rand() function. Write a C code that sorts the elements from the smallest to the largest value. One will proceed as follow: 1) search for the minimum of the elements 0 to n-1. Swap the minimum and first element. 2) Search for the minimum of the elements 1 to n-1. Swap the minimum and second element. 3) … 4) repeat until the array is sorted. CODIGO: #include<stdio.h> #include<stdlib.h> int MAGWv[100000],MAGWn; void MAGWnum_ordenados() { int MAGWvalor_temporal,MAGWq,MAGWw; for (MAGWq=0;MAGWq<MAGWn-1;MAGWq++){ for (MAGWw=MAGWq+1;MAGWw<MAGWn;MAGWw++) { if (MAGWv[MAGWw]<MAGWv[MAGWq]) { MAGWvalor_temporal=MAGWv[MAGWw];/*se asigna el valor encontrado*/ MAGWv[MAGWw]=MAGWv[MAGWq];/*ahora el valor de v[q] toma el de v[w] para poder compararlo de nuevo con el siguiente numero*/ MAGWv[MAGWq]=MAGWvalor_temporal; } } } }

Page 14: Programas c angelwha2

14

int MAGWt; void MAGWorden()/*para poder imprimirlos en orden*/ { for (MAGWt=0;MAGWt<MAGWn;MAGWt++) { printf("\t%d\n",MAGWv[MAGWt]); } } int main() { int MAGWi; printf("cant. de numeros: "); scanf("%d",&MAGWn); for (MAGWi=0; MAGWi<=MAGWn-1;MAGWi++) { printf("\ningrse numero %d: ",MAGWi+1); scanf("%d",&MAGWv[MAGWi]); } printf("\norden en el que fueron ingresado:\n\n"); MAGWorden();/*llamamos nuetra operacion para imprimirlo con el orden en el que fueron ingresados*/ MAGWnum_ordenados();/*luego ya con el metoo de seleccion*/ printf("\n"); printf("\nmetodo de ordenamiento:\n\n"); MAGWorden();

Page 15: Programas c angelwha2

15

/* //forma estatica int temp,b,x,i; int a[10]={8,4,2,9,6,3,5,7,1,10}; for (b=0;b<11;b++) { for (i=0;i<8;i++) { if (a[i+1]<a[i]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } } printf("\nordenamiento por el emtodo de burbuja (bubblesort)"); for (x=0;x<9;x++) { printf("\n%d\n",a[x]); } */ system("pause"); return 0; }

Page 16: Programas c angelwha2

16

SALIDA DEL CODIGO:

Exercise 36: matrix product Write the C code that computes the product of two 3x3 matrices. The code must print the result on the screen. CODIGO: #include <stdio.h> #include <stdlib.h> int main () { //multiplicacion de 2 matrices de 3*3 y sacar el producto de ambas //declaramos variables int MAGWf1,MAGWc1,MAGWi,MAGWj;

Page 17: Programas c angelwha2

17

//creamos la matriz 1 int MAGWm1[3][3]; //rellenamos posiciones de la matriz 1 for(MAGWi=0;MAGWi<3;MAGWi++){ for(MAGWj=0;MAGWj<3;MAGWj++){ printf("m1[%d][%d]=",MAGWi,MAGWj); scanf("%d",&MAGWm1[MAGWi][MAGWj]); } } //imprimimos matriz 1 for(MAGWi=0;MAGWi<3;MAGWi++){ printf("\n"); for(MAGWj=0;MAGWj<3;MAGWj++){ printf("%d",MAGWm1[MAGWi][MAGWj]); //scanf("%d",m1[i][j]); } } printf("\n\n"); //------------ 2 MATRIZ----------------------------- //creamos la matriz 2 int MAGWm2[3][3]; //rellenamos posiciones de la matriz 2 for(MAGWi=0;MAGWi<3;MAGWi++){ for(MAGWj=0;MAGWj<3;MAGWj++){ printf("m2[%d][%d]=",MAGWi,MAGWj); scanf("%d",&MAGWm2[MAGWi][MAGWj]); } } //imprimimos matriz 2 for(MAGWi=0;MAGWi<3;MAGWi++){ printf("\n"); for(MAGWj=0;MAGWj<3;MAGWj++){ printf("%d",MAGWm2[MAGWi][MAGWj]); //scanf("%d",m2[i][j]); } }

Page 18: Programas c angelwha2

18

printf("\n\n"); //----------- PRODUCTO DE MATRIZ DE //declaramos la matriz 3 int MAGWm3[3][3],MAGWk; printf("EL PRODUCTO DE LAS 2 MATRIZES DE 3*3 ES:\n\n"); //multiplicamos la matriz 3 for(MAGWi=0;MAGWi<3;MAGWi++){ //printf("\n"); for(MAGWj=0;MAGWj<3;MAGWj++){ MAGWm3[MAGWi][MAGWj] = 0; for(MAGWk=0;MAGWk<3;MAGWk++){ MAGWm3[MAGWi][MAGWj] = MAGWm3[MAGWi][MAGWj]+MAGWm1[MAGWi][MAGWj]*MAGWm2[MAGWi][MAGWj]; } } } //imprimimos la matriz 3 for(MAGWi=0;MAGWi<3;MAGWi++){ printf("\n"); for(MAGWj=0;MAGWj<3;MAGWj++){ printf("%d",MAGWm3[MAGWi][MAGWj]); //scanf("%d",m2[i][j]); } } printf("\n\n"); system("pause"); return 0; }

Page 19: Programas c angelwha2

19

SALIDA DEL CODIGO:

Exercise 37: concatenation of two tables Consider X and Y, two sorted arrays of integers. Write a C code that concatenates the two tables into one table Z (sorted) which contains the elements of X and Y. CODIGO: #include<stdio.h>

Page 20: Programas c angelwha2

20

int main() { int MAGWi=0,MAGWj=0,MAGWcon=0,MAGWns, MAGWmax1=0, MAGWmax2=0, MAGWmayor; int MAGWv1[MAGWmax1]; int MAGWv2[MAGWmax2]; int MAGWv3[MAGWns]; printf("concatenar dos vectores \n"); printf("Introduzca el tamaño del primer vector: "); scanf("%d",&MAGWmax1); printf("Introduzca el tamaño del segundo vector: "); scanf("%d",&MAGWmax2); printf("\nValores primer vector: \n"); for(MAGWi=0;MAGWi<MAGWmax1;MAGWi++){ printf("ingrese el numero: "); scanf("%d",&MAGWv1[MAGWi]) ; } printf("\nValores segundo vector:\n"); for(MAGWi=0;MAGWi<MAGWmax2;MAGWi++){ printf("ingrese el numero: "); scanf("%d",&MAGWv2[MAGWi]) ; } MAGWns=MAGWmax1+MAGWmax2; for(MAGWi=0;MAGWi<MAGWmax1;MAGWi++){ MAGWv3[MAGWi]=MAGWv1[MAGWi]; } for(MAGWi=0;MAGWi<MAGWmax2;MAGWi++){ MAGWv3[MAGWi+MAGWmax1]=MAGWv2[MAGWi]; } for(MAGWi=0;MAGWi<MAGWns;MAGWi++){ printf("%d ",MAGWv3[MAGWi]); }

Page 21: Programas c angelwha2

21

for(MAGWi=1; MAGWi<MAGWns; MAGWi++) { for(MAGWj=MAGWns-1;MAGWj>=MAGWi;MAGWj--){ if(MAGWv3[MAGWj-MAGWi]>MAGWv3[MAGWj]){ MAGWmayor=MAGWv3[MAGWj-MAGWi]; MAGWv3[MAGWj-MAGWi]=MAGWv3[MAGWj]; MAGWv3[MAGWj]=MAGWmayor; } } } printf("\nLos Datos De La Matriz Fueron Ordenados de Menor a Mayor "); printf("\n"); for(MAGWi=0; MAGWi<MAGWns; MAGWi++) { printf("%d\t",MAGWv3[MAGWi]); } printf("\n"); system("pause"); return(0); } SALIDA DEL CODIGO:

Page 22: Programas c angelwha2

22

Functions and recursive programming Exercise 39: factorial function Write a C function facto that computes the factorial of a number n (integer) as in exercise 25. The factorial function is a typical recursive problem. Rewrite the equation defining the factorial function in a recursive form. Write the recursive version of the code and call the function facto_rec. CODIGO: #include<stdio.h> #include <stdlib.h> int MAGWfactorial(int MAGWnum) { if(MAGWnum < 0) return 0; else if(MAGWnum > 1) return MAGWnum*MAGWfactorial(MAGWnum-1); /* Recursividad */ return 1; /* Condición de terminación, n == 1 */ } int main(){ int MAGWnum=0; printf("Ingresa el numero:\n"); scanf("%d",&MAGWnum); printf("el factorial del numero que ingresastes es:%d\n\n",MAGWfactorial(MAGWnum)); system("pause"); return(0); }

Page 23: Programas c angelwha2

23

SALIDA DEL CODIGO:

Exercise 40: xn

Rewrite xn in its recursive form and write the associate C function that you will call pow_rec. CODIGO: #include<stdio.h> #include <stdlib.h> int MAGWpotencia(int MAGWnum, int MAGWp) { if(MAGWp==0) return 1; else return (MAGWnum*MAGWpotencia(MAGWnum,MAGWp-1)); /* Recursividad */ }

Page 24: Programas c angelwha2

24

int main() { int MAGWnum=0,MAGWp=0, MAGWr; printf("Ingresa el numero:\n"); scanf("%d",&MAGWnum); printf("Ingresa la potencia:\n"); scanf("%d",&MAGWp); MAGWr=MAGWpotencia(MAGWnum,MAGWp); printf("la potencia del numero que ingresastes es:%d\n\n",MAGWr); system("pause"); return(0); } SALIDA DEL CODIGO:

Page 25: Programas c angelwha2

25

Exercise 44: is_prime Write a function that returns 1 if a integer number is prime, 0 otherwise. CODIGO: #include <stdio.h> #include <stdlib.h> //declaramos la funcion entera int MAGWprimo(int); int main() { //declaramos variables int MAGWn, MAGWr; //pedimos el numero al usuario para que lo ingrese por teclado printf("Ingresa el numero para saber si es primo o no:\n"); scanf("%d",&MAGWn); //llamamos la funcion asignada a una variable MAGWr = MAGWprimo(MAGWn); //checamos si el numero es primo o no if (MAGWr== 1){ printf("el numero es primo\n", MAGWn); }else{ printf("el numero no es primo\n", MAGWn); } system("pause"); return 0; }

Page 26: Programas c angelwha2

26

//funcion llamada primo con un parametro int MAGWprimo(int MAGWa) { //declaramos variables de la funcion necesarias int MAGWc; //ciclo que recorre si la variable o parametro de la funcion es igual al cociente o division y da como residuo 0 for (MAGWc = 2 ; MAGWc <= MAGWa - 1 ; MAGWc++){ if ( MAGWa%MAGWc == 0 ){ return 0; } } //si no checamos que ambas variables sean iguales if ( MAGWc == MAGWa ){ return 1; } } SALIDA DEL CODIGO:

Page 27: Programas c angelwha2

27

PROGRAMAS EXTRAS DE TAREA HECHOS EN CLASE Y

EN CASA EN C MIGUEL ANGEL GARCIA WHA CURSO DE

MAESTRIA DE ALGORITMOS 2015

1- Calcular la potencia de un numero M^n

CODIGO: #include <stdio.h> #include <stdlib.h> int main(){ int MAGWnumero=0, MAGWpotencia=0, MAGWtemporal=0, MAGWi=1; printf("Calcula la potencia de m elevado a n:\n"); printf("Ingrese un numero :\t"); scanf("%d", &MAGWnumero); //Condicional para verificar el numero if(MAGWnumero==0){ printf("Ingrese otro numero :\t"); scanf("%d", &MAGWnumero); printf("Ingrese una potencia :\t"); scanf("%d", &MAGWpotencia); printf("------------------------------------\n"); MAGWtemporal = MAGWnumero;

Page 28: Programas c angelwha2

28

//Ciclo de potenciación for(MAGWi=1; MAGWi<MAGWpotencia; MAGWi++){ //Acumulamos en número //La múltiplicación de numero por temporal. MAGWnumero=MAGWnumero*MAGWtemporal; } printf("el numero es:\t %d \n", MAGWnumero); } else{ printf("Ingrese una potencia :\t"); scanf("%d", &MAGWpotencia); printf("------------------------------------\n"); MAGWtemporal = MAGWnumero; //Ciclo de potenciación for(MAGWi=1; MAGWi<MAGWpotencia; MAGWi++){ //Acumulamos en número //La múltiplicación de numero por temporal. MAGWnumero=MAGWnumero*MAGWtemporal; } printf("el numero es:\t %d \n", MAGWnumero); } system("PAUSE"); return(0); }

Page 29: Programas c angelwha2

29

SALIDA DEL CODIGO:

2- Factorial de un numero M! CODIGO: #include <stdio.h> #include <stdlib.h> int main () { int MAGWnumero=0,MAGWi=0,MAGWfactorial; printf("Este programa calculara el factorial del numero entero\n "); printf("ingresa un numero entero:\t " ); scanf("%d",&MAGWnumero); MAGWfactorial=1; for (MAGWi=1 ; MAGWi<=MAGWnumero ; MAGWi++) { MAGWfactorial=MAGWi*MAGWfactorial; } printf("El factorial del numero ingresado es %d\n",MAGWfactorial); system("PAUSE"); return(0); }

Page 30: Programas c angelwha2

30

SALIDA DEL CODIGO:

3- Imprimir todos los números primos entre 1 y N CODIGO: #include <stdio.h> #include <stdlib.h> int main () { int MAGWi, MAGWj, MAGWprimo, MAGWn=0; MAGWprimo = 1; printf("ingresa el numero:\n"); scanf("%d",&MAGWn);

Page 31: Programas c angelwha2

31

for (MAGWi = 2; MAGWi < MAGWn; MAGWi++) { for ( MAGWj = 2; MAGWj <= MAGWi/2; MAGWj++) if ((MAGWi%MAGWj) == 0) MAGWprimo = 0; if(MAGWprimo) printf("Los numeros primos son: %d\n", MAGWi); MAGWprimo = 1; } system("pause"); return 0; } SALIDA DEL CODIGO:

Page 32: Programas c angelwha2

32

4- Conversor decimal a binario,hexadecimal y octal NO LO PUDE REALIZAR :C

5- MULTIPLICACION DE MATRICES CODIGO: #include <stdio.h> #include <stdlib.h> //multiplicacion de matrices int main (){ int MAGWi=0,MAGWj=0,MAGWk=0,MAGWf,MAGWc,MAGWf2=0,MAGWc2=0; printf("multiplicacion de matrices:\n\n"); //if(a[f][c]=b[f][c]){ printf("ingresa la dimencion de la primer matriz:\n"); printf("dame el numero de filas de la matriz:\n"); scanf("%d",&MAGWf); printf("dame el numero de columnas de la matriz:\n"); scanf("%d",&MAGWc); printf("\n ingresa la dimencion de la segunda matriz:\n"); printf("dame el numero de filas de la matriz:\n"); scanf("%d",&MAGWf2); printf("dame el numero de columnas de la matriz:\n"); scanf("%d",&MAGWc2); int MAGWm1[MAGWf][MAGWc]; int MAGWm2[MAGWf2][MAGWc2]; int MAGWm3[MAGWf][MAGWc2];

Page 33: Programas c angelwha2

33

if(MAGWc == MAGWf2){ //------------------- MATRIZ A ---------------------- //poner valores a matriz a en filas y columnas for(MAGWi=0;MAGWi<MAGWf;MAGWi++){ for(MAGWj=0;MAGWj<MAGWc;MAGWj++){ printf("m1=[%d][%d]",MAGWi,MAGWj); scanf("%d",&MAGWm1[MAGWi][MAGWj]); } } //imprimir matriz a con los valores for(MAGWi=0;MAGWi<MAGWf;MAGWi++){ for(MAGWj=0;MAGWj<MAGWc;MAGWj++){ printf("La matriz m1 es: %d\n",MAGWm1[MAGWi][MAGWj]); } } //-------------------------------- MATRIZ B ------------------------------ //poner valores a matriz b en filas y columnas for(MAGWi=0;MAGWi<MAGWf2;MAGWi++){ for(MAGWj=0;MAGWj<MAGWc2;MAGWj++){ printf("m2=[%d][%d]",MAGWi,MAGWj); scanf("%d",&MAGWm2[MAGWi][MAGWj]); } } //imprimir matriz b con los valores for(MAGWi=0;MAGWi<MAGWf2;MAGWi++){ for(MAGWj=0;MAGWj<MAGWc2;MAGWj++){ printf("La matriz m2 es: %d\n",MAGWm2[MAGWi][MAGWj]); } }

Page 34: Programas c angelwha2

34

//--------------------- MULTIPLICACION DE 2 MATRICEZ EN UNA 3 MATRIZ ----------------- //3 matriz c for (MAGWi=0;MAGWi<MAGWf;MAGWi++){ for (MAGWj=0;MAGWj<MAGWc2;MAGWj++){ MAGWm3[MAGWi][MAGWj]=0; for (MAGWk=0;MAGWk<MAGWc;MAGWk++){ MAGWm3[MAGWi][MAGWj]= MAGWm3[MAGWi][MAGWj]+MAGWm1[MAGWi][MAGWk]*MAGWm2[MAGWk][MAGWj]; } } } printf("\nLA MULTIPLICACION DE LAS MATRICES ES:\n\n"); //IMPRESION for (MAGWi=0;MAGWi<MAGWf;MAGWi++){ for (MAGWj=0;MAGWj<MAGWc2;MAGWj++) { printf("m3[%d][%d]=\t%d\n",MAGWi,MAGWj,MAGWm3[MAGWi][MAGWj]); } } } else{ printf("\n no se puede las dimensiones son diferentes\n\n"); } system("pause"); return 0; }

Page 35: Programas c angelwha2

35

SALIDA DEL CODIGO:

Page 36: Programas c angelwha2

36

6- Contador de palabras el tamaño de palabras y de vocales

CODIGO: #include<stdio.h> #include<stdlib.h> #include<ctype.h> int main (){ //arreglo de caracteres y cuenta tamaño de la palabra y el numero de vocales char MAGWletra,MAGWcadena[1000]; int MAGWi=0,MAGWk,MAGWx; printf("Escribe una cadena de 1000 caracteres\n"); do{ MAGWletra=getchar(); MAGWcadena[MAGWi]=MAGWletra; MAGWcadena[MAGWi] = tolower(MAGWcadena[MAGWi]); MAGWi++; }while(MAGWletra != '\n'&&MAGWi<1000); MAGWcadena[MAGWi]='\0'; //printf("%s\n",cadena); int MAGWvocal=0; printf("la cadena que ingresastes es:%s\n",MAGWcadena); for (MAGWk=0;MAGWcadena[MAGWk]!='\0';MAGWk++) { if (MAGWcadena[MAGWk]=='a'||MAGWcadena[MAGWk]=='e'||MAGWcadena[MAGWk]=='i'||MAGWcadena[MAGWk]=='o'||MAGWcadena[MAGWk]=='u') { MAGWvocal++; } } MAGWx = strlen(MAGWcadena); printf("la cadena tiene:\n%d vocales\n",MAGWvocal); //calcula el total de las letras y el espacio tambien entre cada uno de ellas printf("la cadena tiene tamaño de:\n%d caracteres\n",MAGWx); system("pause"); return 0; }

Page 37: Programas c angelwha2

37

SALIDA DEL CODIGO:

7- Contador de párrafos de 500 palabras, 1000 caracteres y mostrar como un histograma el tamaño de las palabras y su número de palabras.

NO LO PUDE HACER :C