Finding values in a 2 dimensional array

  • Context: MHB 
  • Thread starter Thread starter fvnn
  • Start date Start date
  • Tags Tags
    Array
Click For Summary
SUMMARY

The discussion focuses on calculating values in a two-dimensional array B based on the summation of elements from a one-dimensional array A using pseudo code. The specific example provided uses A[1,2,3,4] and illustrates how to compute B[i][j] values through nested loops. The inner loop iterates over the range defined by i and j, summing the corresponding elements of A. The participant confirms that B[1][3] equals 6, derived from the sum of A[1], A[2], and A[3].

PREREQUISITES
  • Understanding of nested loops in programming
  • Familiarity with array indexing, particularly 1-based indexing
  • Basic knowledge of summation algorithms
  • Proficiency in a programming language such as C
NEXT STEPS
  • Implement the pseudo code in C to verify the results
  • Explore array indexing differences between programming languages
  • Study cumulative sum algorithms for optimization
  • Learn about multi-dimensional arrays and their applications
USEFUL FOR

Programmers, computer science students, and anyone interested in understanding array manipulations and summation techniques in programming.

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.
 

Similar threads

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