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

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
AI Thread Summary
The discussion centers on the correct syntax and meaningful use of array declarations in C, particularly regarding the dimension number and initial elements. It emphasizes that the dimension must be at least as large as the number of initial elements, with examples illustrating that "int arr2[]={1,2,3,4};" is equivalent to "int arr1[4]={1,2,3,4};". The conversation also touches on an algorithm expressed in pseudocode, specifically the equation U=U1+U2-L2+1, with participants noting the importance of distinguishing between pseudocode and C syntax. They highlight the need for careful handling of array indices when converting pseudocode to C and suggest that testing the code snippets in a compiler would provide clarity on any errors, rather than relying on others for verification.
shivajikobardan
Messages
637
Reaction score
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
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};"
 
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?
 
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.
 
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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top