- #1
Hypnos_16
- 153
- 1
Homework Statement
Input a list of n integer values into a 1-D array and do the following:
• determine and output the number of positive values and the number of negative values
• update the array values as follow
• if the value is located at an even-numbered subscript, replace the value by its square • if the value is located at an odd-numbered subscript, replace the value by its cube
• input a list of search values and, using the Sequential Search, search for each value and report its location in the list (if found); otherwise output a message
• using the Bubble Sort algorithm, sort the list into descending order.
Homework Equations
okay so i have most of this done, I'm just looking or some troubleshooting because something has gone wrong. The code will be below.
The Attempt at a Solution
Code:
import java.util.Scanner;
public class Assignment6Question1{
public static void main(String[] args){
int index = 0;
int PosCount = 0;
int NegCount = 0;
int SearchKey = 0;
boolean found = false;
Scanner in = new Scanner(System.in);
System.out.print("Enter number of values you would like to input: ");
int SIZE = in.nextInt();
int [] Values = new int [SIZE];
for (int NUM = 0; NUM < SIZE; NUM++)
{
System.out.print("Enter value of number: ");
index = in.nextInt();
if (index < 0){
NegCount++;
} else {
PosCount++;
}
if ((NUM % 2 != 0)||(NUM == 0)){
index = (index * index * index);
} else {
index = (index * index);
}
Values[NUM] = index;
}
int NUM = 0;
System.out.print("Would you like to search for a value? (Y / N): ");
String ANS = in.next().toUpperCase();
while (ANS.equals("Y")){
System.out.print("Enter value you would like to search for: ");
SearchKey = in.nextInt();
while ((!found)&&(NUM < SIZE))
if (SearchKey == Values[NUM])
found = true;
else
NUM = NUM + 1;
if (found == true){
System.out.print("Search for value " + SearchKey + " was Successful! It was found at location " + (NUM + 1) + " Would you like to search for another value? (Y / N): ");
ANS = in.next().toUpperCase();
} else {
System.out.print("Search for value " + SearchKey + " was UnSuccessful. Would you like to search for another value? (Y / N): ");
ANS = in.next().toUpperCase();
}
}
System.out.println("Number of Negative Values = " + NegCount);
System.out.println("Number of Positive Values = " + PosCount);
}
}
Any suggestions what's wrong?
Code:
Enter number of values you would like to input: 2
Enter value of number: 1
Enter value of number: 2
Would you like to search for a value? (Y / N): y
Enter value you would like to search for: 1
Search for value 1 was Successful! It was found at location 1 Would you like to search for another value? (Y / N): y
Enter value you would like to search for: 2
Search for value 2 was Successful! It was found at location 1 Would you like to search for another value? (Y / N):
a read of the typescript in case what i was saying got tangly.
Last edited: