GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Dec-2005, 20:00
NewBieFemAle NewBieFemAle is offline
New Member
 
Join Date: Dec 2005
Posts: 2
NewBieFemAle is on a distinguished road

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  
Old 06-Dec-2005, 20:15
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
num[ 0 ] num[ 1 ] num[ 2 ] num[ 3 ] . . . num[ 9 ]

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:
int num[] = new int[ 10 ];
or like this:
JAVA Code:
int num[]; // declares the array
num = new int[ 10 ]; // allocates the array

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:
num[0] = 1; // num[0] contains 1.
num[5] = 10; //num[5] contains 10.

If you want to change every value of the array, you can use a loop.
For example, consider a for loop.
JAVA Code:
for( int i = 0; i < 10; i++)
Will vary the value of i from 0 to 9.

So, inside the for loop, we have to store 1 in each array element.
We can do like this:
JAVA Code:
for( int i = 0; i < 10; i++)
   num[i] = 1;

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  
Old 06-Dec-2005, 22:10
NewBieFemAle NewBieFemAle is offline
New Member
 
Join Date: Dec 2005
Posts: 2
NewBieFemAle is on a distinguished road

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:
for( int i = 0; i < 10; i++)
   num[i] = 0;

??
Last edited by LuciWiz : 07-Dec-2005 at 01:14. Reason: Please insert your C code between [c] & [/c] tags
  #4  
Old 07-Dec-2005, 03:56
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
for( int i = 0; i < 10; i++)
       num[i] = 1;

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:
for( int i = 0; i < 10; i++)
   num[i] = i;
So, in the first iteration, num[i] = i ==> num[0] = 0.
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 GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 11:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.