Finding values in a 2 dimensional array

  • MHB
  • Thread starter fvnn
  • Start date
  • Tags
    Array
In summary, the conversation revolves around understanding the values of B[i][j] using the given pseudo code and array A. The suggestion is to execute the code by hand or write a program in C to verify the result. It is also mentioned that array indices start at 1.
  • #1
fvnn
2
0
Here's the pseudo code:

Code:
for i := 1 to n { //i=1 and n=4
  for j := i to n { //j=1
   B[i][j] := 0
     for k := i to j {
       B[i][j] := B[i][j] + A[k]
     }
  }
}

This is what i understand from the code above - B values are from the sum of a values.
I want to know what the values of B[j] would be if A were A[1,2,3,4]. I don't need an answer i just need guidance on how to solve this.
 
Technology news on Phys.org
  • #2
Do array indices start at 1?

Why don't you execute the code by hand and then write an actual program, say, in C to verify the result? For example, when $i=1$ and $j=3$ the inner loop makes three iterations:

B[1][3] := B[1][3] + A[1];
B[1][3] := B[1][3] + A[2];
B[1][3] := B[1][3] + A[3];

So the B[1][3] ends up with 1 + 2 + 3 = 6.
 

FAQ: Finding values in a 2 dimensional array

How do I find the maximum value in a 2D array?

To find the maximum value in a 2D array, you can use a nested loop to iterate through each element and compare it to a variable that holds the current maximum value. If the element is larger than the current maximum, update the variable with the new value. Once the loop has finished, the variable will hold the maximum value in the array.

Can I find the minimum value in a specific row or column of a 2D array?

Yes, you can find the minimum value in a specific row or column of a 2D array by using a similar approach as finding the maximum value. Instead of comparing the element to a variable that holds the current maximum, you would compare it to a variable that holds the current minimum. Keep in mind that you will need to specify which row or column you want to find the minimum value in.

How can I find the index of a specific value in a 2D array?

To find the index of a specific value in a 2D array, you can use a nested loop to iterate through each element and compare it to the value you are searching for. If the element matches the value, you can use the current row and column indices as the index of the value in the array. If the value is not found, you can return a message indicating that the value does not exist in the array.

Is there a way to sort a 2D array by a specific row or column?

Yes, you can sort a 2D array by a specific row or column by using a sorting algorithm such as bubble sort or quicksort. You would need to specify which row or column you want to use as the basis for sorting, and then compare the elements in that row or column to determine the order of the array.

How can I find all the occurrences of a specific value in a 2D array?

To find all the occurrences of a specific value in a 2D array, you can use a nested loop to iterate through each element and compare it to the value you are searching for. If the element matches the value, you can store the row and column indices in an array or print them out as you go through the loop. This will give you a list of all the locations where the value appears in the array.

Similar threads

Back
Top