Can anyone please explain me arrays in loops Please( explain in long paragraph) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can anyone please explain me arrays in loops Please( explain in long paragraph)

help me

2nd Nov 2018, 12:47 PM
Fan Of Mr Beast
Fan Of Mr Beast - avatar
5 Answers
+ 2
Pardeep Parmar tag your question properly -- "help" "please" are not considered tags at all since we all here to help! Make it clear in which language and with a little description. And as a bonus please take your time and read this instruction on how to post a minimal, complete, and verifiable question in SL's term. ;) " Here are some tips to make sure your question qualifies: - Post only programming-related QUESTIONS and ANSWERS; - SEARCH for similar QUESTIONS or ANSWERS before posting; - Include relevant TAGS; - Follow community RULES: https://www.sololearn.com/Content-Creation-Guidelines/ DO NOT - Post spam/advertisement; - Use inappropriate language. * Post general discussions and open-ended questions in your feed. " By doing so, you increase your chance to get a good answer. :)
2nd Nov 2018, 1:29 PM
Babak
Babak - avatar
+ 1
fist you specify in which language you are comfortable . ? this is in c++ In this tutorial, we are going to talk about arrays. An array lets you declare and work with a collection of values of the same type. Let’s say you want to declare four integers. With the knowledge from the last few tutorials you would do something like this: int a , b , c , d; What if you wanted to declare a thousand variables? That will take you a long time to type. This is where arrays come in handy. An easier way is to declare an array of four integers, like this: int a[4]; The four separate integers inside this array are accessed by an index. Each element can be accessed, by using square brackets, with the element number inside. All arrays start at element zero and will go to n-1. (In this case from 0 to 3.) Note: The index number, which represents the number of elements the array is going to hold, must be a constant value. Because arrays are build out of non-dynamic memory blocks. In a later tutorial we will explain arrays with a variable length, which uses dynamic memory. So if we want to fill each element you get something like this: int a[4]; a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; If you want to use an element, for example for printing, you can do this: cout << a[1]; Arrays and loops One of the nice things about arrays is that you can use a loop to manipulate each element. When an array is declared, the values of each element are not set to zero automatically. In some cases you want to “re-initialize” the array (which means, setting every element to zero). This can be done like in the example above, but it is easier to use a loop. Here is an example: #include<iostream> using namespace std; int main() { int a[4]; int i; for ( i = 0; i < 4; i++ ) a[i] = 0; for ( i = 0; i < 4; i++ ) cout << a[i] << '\n'; return 0; } Note: In the first “for loop” all elements are set to zero. The second “for loop” will print each element.
2nd Nov 2018, 6:28 PM
Subham Subhasis Patra
Subham Subhasis Patra - avatar
2nd Nov 2018, 6:29 PM
Subham Subhasis Patra
Subham Subhasis Patra - avatar
0
If you have ever been a cashier at say a grocery store, an array is the buggy full of groceries and a loop is you scanning each item so FOR EACH grocery item you are Scanning, then bagging. A loop is how you perform an action for each item in an array or list. So in Javascript for example, if I were to scan and bag each item it would look like this... let groceries = ["apple", "grapes", "bread", "pencils"]; for(item in groceries){ scan(item); bag(item); } Arrays and loops are very simple but also very powerful. Hope this helps.
2nd Nov 2018, 5:53 PM
Brandon
Brandon - avatar