![]() |
|
#1
|
|||
|
|||
I'm New - ARRAY question!hello! I am very much new to programming and I was wondering if anyone could help me out with a homework question.
8.) Create an Array named "Numbers" that can hold 10 numbers. Initialize all the array elemetns to 1 using a loop. Thank you for any help!!! |
|
#2
|
||||
|
||||
Re: I'm New - ARRAY question!Hi newbie,
Welcome to GIDForums. First, what is an array? An array is a group of contiguous memory locations that all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number (or index or subscript) of the particular element in the array. For example, consider this array: Code:
This array contains 10 elements. A program refers to any one of these elements by giving the name of the array followed by the position number of the particular element in square brackets ([]). The first element in every array has position number zero (sometimes called the zeroth element). Thus, the first element of array num is num[ 0 ], the second element of array num is num[ 1 ], the seventh element of array num is num[ 6 ], and, in general, the ith element of array c is c[ i - 1 ]. Array names follow the same conventions as other variable names. You can create an array in java using the new statement. For example, Like this: JAVA Code:
JAVA Code:
By default, every element in an integer array contains zero. If you want to change any element to 1, then you can do like this: JAVA Code:
If you want to change every value of the array, you can use a loop. For example, consider a for loop. JAVA Code:
So, inside the for loop, we have to store 1 in each array element. We can do like this: JAVA Code:
Here is a simple challenge for you: Store 0 in num[0]. Store 1 in num[1]. ... ... Store 9 in num[9]. Hint: Use a loop. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: I'm New - ARRAY question!To create that loop for the problem you gave me would you do it like this:
CPP / C++ / C Code:
?? Last edited by LuciWiz : 07-Dec-2005 at 01:14.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#4
|
||||
|
||||
Re: I'm New - ARRAY question!The answer you gave will store 0 in all the members of the array.
Consider the for loop. JAVA Code:
i varies from 0 to 9. Lets see it more clearly. Initially, i value is 0. Then it checks for the condition, whether i is less than 10. i = 0; i < 10? Yes. So, go inside the loop. num[i] ==> num[0] = 1. Increment i. i = 1; i < 10? Yes. Go inside the loop. num[i] ==> num[1] = 1. .. .. i = 9; i < 10? Yes. Go inside the loop. num[i] ==> num[9] = 1. Increment i. i is now 10. i < 10? False. So quit the loop. Did you understand how this works? For storing 0 in num[0] and 1 in num[1]... then you have to do like this: JAVA Code:
In the second iteration, num[i] = i ==> num[1] = 1 and so on. If you are interested, here is another challenge: Store 9 in num[0]. store 8 in num[1]. ... store 1 in num[8] store 0 in num[9]. How would you do it? Just do a test program, and print the values and see. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| fundamental c++ dynamic array question | pixienick | MS Visual C++ / MFC Forum | 1 | 20-Aug-2005 10:49 |
| Help with an array using pointers | glulu76 | C++ Forum | 1 | 01-May-2005 12:21 |
| Noob question on c arrays and functions | brett | C Programming Language | 1 | 20-Apr-2005 04:59 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 22:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The