MHB Finding values in a 2 dimensional array

  • Thread starter Thread starter fvnn
  • Start date Start date
  • Tags Tags
    Array
Click For Summary
The discussion focuses on calculating values in a two-dimensional array B based on the sums of elements from a one-dimensional array A. The pseudo code provided iterates through indices to populate B[i][j] with the sum of A[k] for k ranging from i to j. An example is given where, for i=1 and j=3, B[1][3] equals 6 after summing A[1], A[2], and A[3]. Participants suggest executing the code manually and implementing it in a programming language like C for verification. The conversation emphasizes understanding array indexing and the summation logic.
fvnn
Messages
2
Reaction score
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
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K