Int arr1[4]= {1,2,3,4} or int arr[3]={1,2,3,4}

  • C/C++
  • Thread starter shivajikobardan
  • Start date
In summary, the algorithm is confusing and the compiler might give an error if you use the element numbers as array indices.
  • #1
shivajikobardan
674
54
TL;DR Summary
declaring an array in C
I'm wondering which one is the correct syntax? Of course both are correct syntax, but which one is meaningful? my confusion comes from this algorithms:
1675608095901.png

Source: classic data structures by debasis samanta
 
Last edited:
Technology news on Phys.org
  • #2
The dimension number should be at least as large as the number of initial elements specified. It can be larger. Your second example where it is too small should give a compiler error message. You can leave it undefined and the compiler will set it at the number of initial elements in the list: "int arr2[]={1,2,3,4};" is the same as "int arr1[4]={1,2,3,4};"
 
  • #3
FactChecker said:
The dimension number should be at least as large as the number of initial elements specified. It can be larger. Your second example where it is too small should give a compiler error message. You can leave it undefined and the compiler will set it at the number of initial elements in the list: "int arr2[]={1,2,3,4}" is the same as "int arr1[4]={1,2,3,4}"
can you take a look at updated question? is the algorithm wrong? where it says U=U1+U2-L2+1?
 
  • #4
shivajikobardan said:
can you take a look at updated question? is the algorithm wrong? where it says U=U1+U2-L2+1?
First, the algorithm is expressed in pseudocode, which should not be confused with C.
I think the numbers work out.
I get that the total number of elements in the appended array are ##(U_1-L_1+1)+(U_2-L_2+1) = (U_1+U_2-L_2+1)-L_1+1 = U-L+1##.
I think that it is confusing to use the element numbers as array indices, but that is language specific and this is pseudocode. Be careful with the array indices if you convert this to C.
 
  • #5
You could have run both snippets of code through the compiler and seen which one caused an error. Why do you need us to do this for you?

It has got to be slower to wait for us than to do it yourself.
 
Last edited:

Similar threads

  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
17
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
Back
Top