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 26-Mar-2008, 15:21
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point
Exclamation

HELP: Arrays


So I have an exam on Arrays this Friday in my programming class. I am trying to go through some homework problems but have had no luck. Therefore I decided to go over the example problems used in class which can help me solve the homework problems and study for the test. I don't really know how to go about these problems however. If anyone can explain how to arrive to the solution or show me the codes that would be extremely helpful and appreciated!

THANK YOU

Given the following boolean array declaration, set each element of the array to alternating values (true at index 0, false at index 1, etc.)

Code:
boolean[] flags = new boolean[21];

Given the following int array, write the appropriate Java code that will
calculate and display the sum and the average of the values in the array:

Code:
int[] numbers = {67, 48, 127, -36, 89, 23, -16, 161, -22, 57, 22, 75, 91, -86};

Given the following three int arrays, write the appropriate Java code that will copy the largest of the values at the same relative index position of list1 and list2 into the array largestValues. Then print all three arrays, the elements of the same index position for all three arrays on the same line.

Code:
int[] list1 = {24, 33, 45, 21, 16, 90, 6, 12, 19, 72, 38, 14}; int[] list2 = {12, 36, 54, 21, 19, 81, 19, 8, 41, 54, 35, 14}; int[] largestValues = new int[list1.length];


Given the following array of Strings, write the appropriate Java code that will print the total number of characters in the array, the average length of each element, and the longest element in the array.

Code:
String[] words = {"twas", "brillig", "and", "the", "slithy", "toves", "did", "gyre", "in", "the", "wabe"};
  #2  
Old 26-Mar-2008, 15:51
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 395
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: HELP: Arrays


Quote:
Originally Posted by sneakerhead724
Given the following boolean array declaration, set each element of the array to
alternating values (true at index 0, false at index 1, etc.)

JAVA Code:
boolean[] flags = new boolean[21];
int i;
for( i=0; i<21; i=i+1 )
{ if( i%2 == 1 ) flags[i] = true;
  else flags[i] = false;
}
Basically with arrays, you have a collection of a certain type (in this case, they are booleans) and you can access them using the [] operator. flags[0] accesses the first element and flags[20] accesses the last one. flags[21] will cause an error.

I'll let you try some of the other examples. If you have questions, tell us what you tried and tell us what went wrong and we'll probably be able to help some more. Good luck.
  #3  
Old 28-Mar-2008, 13:49
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: HELP: Arrays


Hey guys thanks for your help I've solved all the problems except for the last one. Ive tried it so many times but If anyone can please help before my test I appreciate it thank you!!!
  #4  
Old 28-Mar-2008, 14:23
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 395
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: HELP: Arrays


Quote:
Originally Posted by sneakerhead724
Given the following array of Strings, write the appropriate Java code that will print the total number of characters in the array, the average length of each element, and the longest element in the array.
JAVA Code:
String[] words = {"twas", "brillig", "and", "the", "slithy", "toves", "did", "gyre", "in", "the", "wabe"};
int NumCharacters = 0;
int LongestStringIndex = 0;
int i;
for(i=0;i<words.length;i++)
{ NumCharacters += words[i].length();
  if(words[i].length() > words[LongestStringIndex].length()) LongestStringIndex = i;
}
double AverageLength = ((double)NumCharacters) / words.length;
  #5  
Old 28-Mar-2008, 15:10
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: HELP: Arrays


hey fakepoo thank you so much for the help but how would I print out the longest element of that array and wouldn't that be the word "brillig"? How can I print that out
  #6  
Old 28-Mar-2008, 15:27
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 395
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: HELP: Arrays


You have the index of the longest element in LongestStringIndex so:
JAVA Code:
System.out.println("The longest string is: " + words[LongestStringIndex]);
  #7  
Old 28-Mar-2008, 16:02
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: HELP: Arrays


Hey fakepoo funny thing LOL I meant the question before that I didnt do but for the array of strings problem I did have that question of how to print it out which u answered. What's the code for the one with array list1 and list2 .....
  #8  
Old 31-Mar-2008, 09:05
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 395
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: HELP: Arrays


Here is how I would do it:

Loop from 0 to the largestValues.length-1. If list1[ loop index ] is greater than list2[ loop index ] then copy the value from list1 into largestValues. Otherwise copy the value from list2.

I'll leave it to you to put it in java.
 

Recent GIDBlogNew Corolla Altis, 10th Generation - Part I 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
Need Help with input files. Efferus CPP / C++ Forum 2 24-Nov-2007 16:19
Arrays as function arguments - return arrays from functions pisuke C Programming Language 2 25-Jul-2007 11:03
Dynamic vs Static Arrays WaltP Miscellaneous Programming Forum 5 16-Feb-2006 15:54
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 22-Nov-2004 23:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:15.


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