0

I am trying to write a small program to calculate the addition of two vectors from the user and storing it in the array. I want to get two X and Y's (like this {x,y})from the user and then add them both together.

I tried to use a 2D array like so

int[,] array = new int[0, 1] {};

but I want the user to enter the values of it. I don't know enough about c# so if anyone knows how can I solve this problem would apparitions your help

1 Answer 1

2

If you want to get a vector from user, you can try asking user to provide its components separated by some delimiter(s), e.g.

  private static int[] ReadVector(string title) {
    while (true) { // keep asking user until valid input is provided
      if (!string.IsNullOrEmpty(title))
        Console.WriteLine(title);

      string[] items = Console
        .ReadLine()
        .Split(new char[] { ' ', '\t', ';', ',' }, 
               StringSplitOptions.RemoveEmptyEntries);

      if (items.Length <= 0) {
        Console.WriteLine("You should provide at least one component");

        continue; 
      } 

      bool failed = false; 
      int[] result = new int[items.Length];

      for (int i = 0; i < items.Length; ++i) {
        if (int.TryParse(items[i], out int value))
          result[i] = value;
        else {
          Console.WriteLine($"Syntax error in {i + 1} term");
          failed = true;

          break;
        }  
      }

      if (!failed)
        return result; 
    }
  }

Then you can use this routine like this:

  // We don't want any 2D matrices here, just 2 vectors to sum
  int[] A = ReadVector("Please, enter vector A");
  int[] B = ReadVector("Please, enter vector B");  

  if (A.Length != B.Length) 
    Console.WriteLine("Vectors A and B have diffrent size");
  else {
    // A pinch of Linq to compute C = A + B 
    int[] C = A.Zip(B, (a, b) => a + b).ToArray();

    Console.WriteLine($"[{string.Join(", ", A)}] + ");
    Console.WriteLine($"[{string.Join(", ", B)}] = ");
    Console.WriteLine($"[{string.Join(", ", C)}]");  
  }  

Edit: Sure you can sum vectors with a help of good old for loop instead of Linq:

   int[] C = new int[A.Length];

   for (int i = 0; i < A.Length; ++i)
     C[i] = A[i] + B[i]; 
Sign up to request clarification or add additional context in comments.

4 Comments

большое спасибо, this is exactly what I wanted and works perfectly, but still don't quite understand the whole code so I will try to go through it agian and again.
One more question, Is there any other way to do the last calculation other than using Linq?
@Rasta Mosi: sure, you can use good old for loop to sum corresponding components
C[0]=A[0]+B[0]; C[1]=A[1]+B[1]; or you can use for(int i=0;i<2;i++)C[i]=A[i]+B[i];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.