0
Java program
Good evening all, Any one help me to solve this question 🥺? Write a JAVA program and do the following a) Read a single dimensional array. It contains 10 integer numbers. . b) Find the positive, negative and zeros c) Display the numbers in reverse order
8 odpowiedzi
+ 2
Hello Azza, the Q&A section is reserved for specific coding related questions and answers.
If your question is a challenge, you can add it in your feed instead.
If it is some task you have to do, such as school work, please attempt to do it yourself, and only ask specific questions when you are stuck.
Happy coding!
+ 2
Azza
You can copy your code in the playground. Save it and share the link here.
static int n = 0;
static int array[] = new int[n];
When you do this at the beginning, then you create an array of size 0.
Your description says you need 10 elements:
static int[] arr = new int[10];
If you want you can leave n. Maybe better than to use array.length
static int n = 10;
Remove the other variables. You can create them inside the method. You don't need them outside the methods.
in your main:
remove int[] array = new int[n];
You already have your static one. Same for n.
Remove also the rev array.
The loop for getting user input is okay.
in public void findPNZ():
What do you need here?
Count the elements?
Or show the elements?
Or show the index of the elements?
in static void reverse():
You don't need a second array if you just print it:
for(int i = n - 1; i >= 0; i--){
System.out.print(array[i] + " ");
}
+ 1
Thank you @AKR @ifl for your advice and notes
this is my attempt to solve the question .. I will be grateful to you if you correct my mistakes .
and I am sorry I do not know how I send as file
import java.util.Scanner;
public class Labwork
{
static int n, res,i,j=0;
static int array[] = new int[n];
static int rev[] = new int[n];
static int countp=0, countn=0, countz=0;
public static void main(String [] args)
{
read();
find_P_N_Z();
reverse();
}
static void read()
{
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int array[] = new int[n];
int rev[] = new int[n];
System.out.println("Enter "+n+" elements ");
for( i=0; i < n; i++)
{
array[i] = s.nextInt();
}
}
static void find_P_N_Z()
{
{
for(i=0; i<n; i++)
{
if(array[i] < 0)
{
countn++;
}
else if(array[i] == 0)
{
countz++;
}
else
{
countp++;
}
}
}
}
static void reverse()
{
System.out.println("Reverse of an array is :");
for( i=n;i>0 ; i--,j++)
{
rev[j] = array[i-1];
System.out.println(rev[j]);
}
}
}
+ 1
Azza if you dont know what (b) means, you can try and implement several versions, eg
- one method dislaying the count
- one method showing the indexes
After all, this is all a chance to practice and show your skills!
0
Thanks Denise Roßberg
I am really stuck here
in static void findPNZ()
I don't understand the part (b) of question ?🥺
if mean count or show !!
0
Ok ifl I will try
thank you so much