Hey guys how is everybody doing? Why when we want to take array from user we first declare array like int x[2] which means reser | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Hey guys how is everybody doing? Why when we want to take array from user we first declare array like int x[2] which means reser

Reserve 2 places in array but doest mean their value equals 2 but why we intialiaze in for loop like for(I =0) doesn't that mean we set value of I so how we compare a place to value?

5th Apr 2019, 12:56 AM
poula morcos
poula morcos - avatar
1 Resposta
+ 1
Poula Morcos, Think of array like a book. While a regular variable is like piece of paper that can only carry a single value (page in terms of book), an array is a variable that can contain many values (pages in terms of book). In C++ arrays must contain all similar type of values, no mixing of various types allowed. When we do int x[2] we ask the computer to reserve an array with 2 elements (pages) ready to use. When we want to use a particular page we refer to them by index (a positive number starting from zero). Since we have 2 pages, we refer page 1 as x[0], and page 2 as x[1]. Remember, the maximum index is the number of elements (pages) reserved minus one. We can later on modify each array element's value or copy the array element's value to another variable. Note that this method of array declaration creates a static array, which can't be resized. For dynamic container you can look at vector. https://www.sololearn.com/learn/261/?ref=app
7th Apr 2019, 5:51 AM
Ipang