Mostrando entradas con la etiqueta vectores. Mostrar todas las entradas
Mostrando entradas con la etiqueta vectores. Mostrar todas las entradas

viernes, 24 de agosto de 2012

multiplicacion de matrices en c#

algoritmo implementado en c sharp que multiplica 2 matrices
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
    class Program
    {
        static void Main(string[] args)
        {
            int respuesta = 0;
            Console.WriteLine("cantidad de reglones");
            int r = int.Parse(Console.ReadLine());
            Console.WriteLine("cantidad de columnas");
            int c = int.Parse(Console.ReadLine());
            int[,] matriz1 = new int[r, c];
            Console.WriteLine("cantidad de reglones de la matriz a multiplicar");
            int r2 = int.Parse(Console.ReadLine());
            Console.WriteLine("cantidad de columnas de la matriz a multiplicar");
            int c2 = int.Parse(Console.ReadLine());
            int[,] matriz2 = new int[r2, c2];

            //si el numero de filas de la 1 es igual al numero de columnas de la 2 entonces
            //creamos una matriz de respuestas con su respectivo tamano
            //de lo contrario mostramos un error por pantalla

            if (c == r2)
            {
                int[,] matrizRespuestas = new int[r, c2];//CREAMOS LA MATRIZ RESULTADO

                // COMIENZA CICLO PARA LLENAR LA PRIMERA MATRIZ
                for (int i = 0; i < r; i++)
                {
                    for (int j = 0; j < c; j++)
                    {
                        Console.WriteLine("matriz 1 ingrese un valor en la posicion :{0},{1} ", i, j);
                        matriz1[i, j] = int.Parse(Console.ReadLine());

                    }
                }
                //COMIENZA CICLO PARA LLENAR LA SEGUNDA MATRIZ
                Console.WriteLine("LISTO!! AHORA INGRESE LOS VALORES DE LA SEGUNDA MATRIZ");
                for (int i = 0; i < r2; i++)
                {
                    for (int j = 0; j < c2; j++)
                    {
                        Console.WriteLine("matriz 2 ingrese un valor en la posicion :{0},{1} ", i, j);
                        matriz2[i, j] = int.Parse(Console.ReadLine());

                    }
                }
                // FIN PARA LLENAR LAS DOS MATRICES
                // AHORA VAMOS A MULTIPLICARLAS
                //PARA ELLO PRIMERO TENEMOS QUE MILTIPLICAR Y SUMAR VECTORES
                for (int i = 0; i < r; i++)
                {
                    for (int j = 0; j < c2; j++)
                    {
                        for (int k = 0; k < c; k++)
                        {
                            respuesta += matriz1[i, k] * matriz2[k, j];
                        }
                        matrizRespuestas[i, j] = respuesta;
                        respuesta = 0;
                    }

                }
                Console.WriteLine("!!!!!!LA RESPUESTA ES !!!!!!");
                string igual = "";
                for (int i = 0; i < r; i++)
                {
                    for (int j = 0; j < c2; j++)
                    {
                        igual = igual + (matrizRespuestas[i, j].ToString());
                    }
                    Console.WriteLine("[" + igual + "]\n");
                    igual = "";

                }
                Console.Read();




            }
            else
            {
                Console.WriteLine("!!!!!!!!!!!no se puede multiplicar estas matrices !!!!!");
                Console.Read();
            }

        }
    }
}

miércoles, 12 de octubre de 2011

llenar un vector con n posiciones y despues imprimirlo en python

algoritmo para llenar un vector implementado en python.

n = int(input('digite la cantidad de vectores a sumar'))
contador = 0
vector =[0]*n
if n <0:
    n = n*(-1)
while contador < n :
    vector[contador]= int(input('digite el numero que va en esta posicion '))  
    contador = contador +1
print (vector)