Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
On line 4: I can not figure out how to make the anArray array add 100 to each element following index 0 in the for loop? The instructions are in the comment. Any suggestions?

public static void main(String[] args) {
        // LINE 1. DECLARE AN ARRAY OF INTEGERS
        int[] anArray; 
        
        // LINE 2. ALLOCATE MEMORY FOR 10 INTEGERS WITHIN THE ARRAY.
        anArray = new int[10];
        
        // Create a usable instance of an input device
        Scanner myInputScannerInstance = new Scanner(System.in); 
        
        // We will ask a user to type in an integer. Note that in this practice
        // code  WE ARE NOT VERIFYING WHETHER THE USER ACTUALLY
        // TYPES AN INTEGER OR NOT. In a production program, we would
        // need to verify this; for example, by including
        // exception handling code. (As-is, a user can type in XYZ
        // and that will cause an exception.)
        System.out.print("Enter an integer and hit Return: ");
        
        // Convert the user input (which comes in as a string even
        // though we ask the user for an integer) to an integer
        int myFirstArrayElement = Integer.parseInt(myInputScannerInstance.next());
        
        // LINE 3. INITIALIZE THE FIRST ARRAY ELEMENT WITH THE CONVERTED INTEGER myFirstArrayElement
        anArray[0] = myFirstArrayElement;
        
        // LINE 4. INITIALIZE THE SECOND THROUGH THE TENTH ELEMENTS BY ADDING 100 TO THE EACH PRECEDING VALUE.
        // EXAMPLE: THE VALUE OF THE SECOND ELEMENT IS THE VALUE OF THE FIRST PLUS 100;
        // THE VALUE OF THE THIRD ELEMENT IS THE VALUE OF THE SECOND PLUS 100; AND SO ON.
        for (int j=0; j < anArray.length; j++) {
            if (anArray[j]!= anArray[0]){
                anArray[j] += 100;
            }
           
        } 

        
        // LINE 5. DISPLAY THE VALUES OF EACH ELEMENT OF THE ARRAY IN ASCENDING ORDER BASED ON THE MODEL IN THE TOP-OF-CODE COMMENTS.
        Arrays.sort(anArray);		// Send our Anarray array to be sorted in ascending order by the sort() method
        int i;				// Define the counter for the loop. We can do it this way or inside
                                        // the for loop, as we did in the preceding for loop.
                                        
        for (i=0; i < anArray.length; i++) {	// Loop, starting at 0 and adding 1 each time, until counter is less than array length. 		
              System.out.println("Element at index " + i + ": " + anArray[i]);// print the numbers
        }

    }
    
}


What I have tried:

I have done everything else for the code. I cant figure out line 4. please help. Thanks.
Posted
Updated 6-Aug-18 21:13pm
Comments
Patrice T 7-Aug-18 1:30am    
Show input and output you want.
anArray[0] = myFirstArrayElement;
anArray[1] = ?
anArray[2] = ?
anArray[3] = ?

Something like below should do it. Start with 1 because the line 3 initialized the index 0 with the input value. Then based on your requirement, array[1] = index[0] + input or anArray[0] , and so on...

Java
for (int j=1; j < anArray.length; j++) {
    anArray[j] = anArray[j-1] +anArray[0];
}


Output:
Enter an integer and hit Return: 100
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
 
Share this answer
 
Java
for (int j=1; j < anArray.length; j++) 
{
  anArray[j] = anArray[j-1] + 100; // j is the current item, (j-1)  is the previous one
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900