473,468 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to replace a number in an array

Alright I have the program I am intended to write basically completely running correctly minus one little snag up I can't seem to wrap my mind around to fix.

The programs intention is to search through an array based upon the parameters you input and find the maximum number.
Then replace the last parameter of the array with the maximum and the place in the array where the maximum was with the last parameters number.

What I can't seem to figure out is how to replace the maximums location with the last number in the selected area of the array i can only manage to replace the last number with the actual maximum

heres the actual question and the code i've worked up
Expand|Select|Wrap|Line Numbers
  1. Consider a integer array called myArray of size 20 that
  2. initially contains random numbers between 1 and 1000. You want to write a method,
  3. blockMax that accepts myArray and two integers, start and end as parameters.
  4. blockMax considers the sub-array of myArray denoted by start and end, and finds the
  5. maximum element in that sub-array. Subsequently, blockMax swaps the max element
  6. and the last element of the sub-array. For example, if initially
  7. myArray is {95, 90, 104, 85, 90, 110, 105, 88, 90, 97, 102, 77, 100, 90, 109,
  8. 80, 34, 9, 567, 876}
  9. and we make the call
  10. blockMax(myArray, 4, 10)
  11. then the result reflected in myArray will be
  12. {95, 90, 104, 85, 90, 102, 105, 88, 90, 97, 110, 77, 100, 90, 109, 80, 34, 9,
  13. 567, 876}
  14.  
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2. public class MaxVal{
  3.         public static void main(String[] args) {
  4.                 int[] myArray = new int[20];
  5.         String startStr, endStr;
  6.         int start, end;
  7.         startStr = JOptionPane.showInputDialog("Input start..(0-19)");
  8.         start = Integer.parseInt(startStr);
  9.         endStr = JOptionPane.showInputDialog("Input end..(start-19)");
  10.         end = Integer.parseInt(endStr);
  11.  
  12.                 for (int i = 0; i < myArray.length; i++){
  13.                         myArray[i] = (int)(Math.random() * 1000);
  14.                 }
  15.  
  16.                 System.out.println("myArray is: ");
  17.  
  18.  
  19.                 printArray(myArray);
  20.  
  21.                 System.out.println("The Array after blockMax: ");
  22.                 blockMax(myArray, start, end);
  23.  
  24.                 printArray(myArray);
  25.         }
  26.  
  27.         static void printArray (int[] array) {
  28.                 for (int i = 0; i < array.length; i++)
  29.                         System.out.print(array[i] + "  ");
  30.                     System.out.println();
  31.         }
  32.  
  33.         static void blockMax(int[] array, int inputStart, int inputEnd) {
  34.                 int max = array[inputStart];
  35.                 for (int i = inputStart; i < inputEnd; i++){
  36.                         if (max < array[i]){
  37.                                 max = array[i];
  38.                                int temp = array[inputEnd];
  39.                                 array[inputEnd] = max;
  40.  
  41.                         }
  42.                 }
  43.         }
  44. }
  45.  
Oct 31 '10 #1
0 1426

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tony Mascolo | last post by:
I am just wondering if dreamweaver still has a timeline or animations section. I was able to quickly do this with dreamweaver 2 or 3, not sure, and it was a bit like the flash timeline. Is there...
0
by: dinoo | last post by:
I have to crete a windows form within a thread. Can some post a code or direct me to some link to accomlish this? I have background service which needs to pop up this form which will be always on...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
4
by: Jacek Jurkowski | last post by:
I have to add a property to an object that is already allive. The problem is that I'm creating MainMenu from a definition file ... enyway , there is a need to add each menu item some extra...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
0
by: hugo | last post by:
I need urgent help with this program: Using C, write a calendar progam so that the user provides only a year. The program should print a calendar for the given year to the file calendar.dat. The...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
2
by: baroon | last post by:
Please if you can help me by solving the following question by >>using C++: >> >>One important function in word-processing system is typ >>justification the alignment of words to both the left...
2
by: Prime8 | last post by:
I've been researching so much about arrays and matrices and such, but it hasn't helped me at all on my program. Everything is either over my head or doesn't help with the specific program I have to...
4
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.