Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am new at programming and I am trying to create a method that allows me search Id inside a [10,4] matrix, however I don't get how to do it without using nested fors and also if and else statement. The problem is related to output, I know the structure isn't correct, but since I don't what else can be done I am trying make it as it is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace menu
{
    class Program
    {
        enum header { id, name, surname, addres };

        public static int id = 1;

        static void Main(string[] args)
        {
            string[,] matrix = new string[10, 4];

            insertStudent(matrix);
            idSearch(matrix);
            Console.ReadKey();
        }


        static int generateId()
        {
            return id++;
        }

        static void insertStudent(string[,] matrix)
        {
            int n = generateId();
            matrix[n - 1, 0] = Convert.ToString(n);

            for (int i = 1; i < matrix.GetLength(1); i++)
            {
                do
                {
                    Console.WriteLine($"Insert {Enum.GetName(typeof(header), i)}");
                    matrix[n - 1, i] = Console.ReadLine();
                }

                while (String.IsNullOrEmpty(matrix[n - 1, i]));

            }
        }

            static void idSearch(string[,] matrix)

        {
            int idChosen=0;
            Console.WriteLine($"Insert ID you want to visualize:");
            int.TryParse(Console.ReadLine(), out idChosen);
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {


                    if (matrix[i, 0] == Convert.ToString(idChosen))
                    {
                        Console.WriteLine(matrix[i, j]);
                    }
                    else
                    {
                        Console.WriteLine("The chosen ID does not exist");
                    }

                }

            }
        }









    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

Right now you printing "The chosen ID does not exist" every time you check an index in your matrix. You want to move that statement to outside of your loop after you've already checked every index. Right now that check is really saying that your ID is not in that specific cell. I've altered your code slightly to reflect this. I also fixed your check to be on matrix[i,j] instead of matrix[i,0]

Also using a nested for loop is OK to use. I don't believe C# has any built in helper methods for searching multidimensional arrays.

bool found = false;
for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                if (matrix[i, j] == Convert.ToString(idChosen))
                {
                    //note that this will print your id
                    Console.WriteLine(matrix[i, j]);
                    //this would print where it found it
                    Console.WriteLine("Found at [" + i + "," + j + "]");
                    found = true;
                }
            }
        }

if (!found)
{
    Console.WriteLine("The chosen ID does not exist");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...