Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [c#] stampare a video elementi in diagonale

    :master: Ciao a tutti..volevo chiedere una cosa che forse a qualcuno potrà sembrare banale, ma non riesco a trovare una soluzione.
    Dopo aver caricato una matrice devo stamparne a video la diagonale principale, ma vorrei visualizzarla esattamente in diagonale. Come potrei fare?
    Grazie a tutti in anticipo per la risposta.

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    codice:
    <%@ Page Language="VB" %>
    
    <%@ Import Namespace="l=libreria.ModuloWeb" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim v As Double(,) = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}
            Dim sb As New StringBuilder()
            sb.Append("<table>")
            For i As Integer = 1 To 4
                sb.Append("<tr>")
                For j As Integer = 1 To 4
                    sb.Append("<td>")
                    If i = j Then
                        sb.Append(v(i - 1, j - 1).ToString())
                    Else
                        sb.Append("")
                    End If
                    sb.Append("</td>")
                Next
                sb.Append("</tr>")
            Next
            sb.Append("</table>")
            
            Me.Literal1.Text = sb.ToString()
            
        End Sub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Literal ID="Literal1" runat="server"></asp:Literal>
        </div>
        </form>
    </body>
    </html>
    Pietro

  3. #3
    oh oh..non credo di aver capito
    aspetta ti mando il programma che ho appena scritto; ho appena cominciato con questo linguaggio.





    using System;

    namespace Matrice_K
    {
    class Program
    {
    static void Crea_Matrice(ref int n)
    {
    Console.Write("Inserisci il numero delle righe (e delle colonne) che verrà assegnato alla matrice quadrata: ");
    n = Convert.ToInt32(Console.ReadLine());
    }
    static void Carica(ref int[,] Matrice, ref int n)
    {
    Matrice = new int[n, n];
    for (int i = 0; i < n; i++)
    {
    for (int j = 0; j < n; j++)
    {
    Console.Write("Inserisci elemento di posto {0} - {1}:\t", i + 1, j + 1);
    Matrice[i, j] = Convert.ToInt32(Console.ReadLine());
    }
    }
    }
    static void Stampa(ref int[,] Matrice, ref int n, ref int d)
    {
    d = 0;
    for (int i = 0; i < n; i++)
    {
    Console.Write("\t{0}", Matrice[i, d]);
    Console.WriteLine("");
    d++;
    // qui vorrei stampare a video la diagonale principale
    }
    }
    static void Main(string[] args)
    {
    int[,] Matrice = new int[0, 0];
    int n = 0, d = 0;
    Crea_Matrice(ref n);
    Carica(ref Matrice, ref n);
    Stampa(ref Matrice, ref n, ref d);
    }
    }
    }

  4. #4
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Forse non hai visto che qui si trattano pagine asp.net e non applicazioni console

    Comunque, vediamo se così va bene :master:

    codice:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication_c
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[,] matrice = crea_matrice();
                carica_matrice(matrice);
                stampa_matrice(matrice);
    
                Console.WriteLine("Premi un tasto per finire...");
                Console.ReadKey();
            }
    
    
            private static int[,] crea_matrice()
            {
                Console.Write("Inserisci il numero delle righe (e delle colonne) che verrà assegnato alla matrice quadrata: ");
                int n = int.Parse( Console.ReadLine());
                return new int[n, n];
            }
    
            private static void carica_matrice(int[,] matrice)
            {
                for (int i = 0; i <= matrice.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= matrice.GetUpperBound(1); j++)
                    {
                        Console.WriteLine("matrice[{0}, {1}]: ", i, j);
                        matrice[i, j] = int.Parse(Console.ReadLine());
                    }
                }
            
            }
    
            private static void stampa_matrice(int[,] matrice)
            {
                Console.WriteLine();
    
                for (int i = 0; i <= matrice.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= matrice.GetUpperBound(1); j++)
                    {
                        if (i == j)
                            Console.Write(matrice[i, j] + "\t");
                        else
                            Console.Write("*\t");
                    }
                    Console.WriteLine();
                }
            
            }
    
    
        }
    }
    Pietro

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.