3.) Answer the following questions on arraying the numbers in specific order: A)
ID: 3587691 • Letter: 3
Question
3.) Answer the following questions on arraying the numbers in specific order:
A)
Write a function to print all the values in an array, one per line. Your function will take two parameters, the array and the array length. Use the function declaration: void printValues(int myArray[], int length);
Example:
int myArray[3] = {2, 5, 24};
Output:
2
5
24
B)
Write a function to print all the values in an array in reverse order, one per line. Your function will take two parameters, the array and the array length. Use the function declaration: void reversePrintValues(int myArray[], int length);
Example:
int myArray[3] = {2, 5, 24};
Output:
24
5
2
Explanation / Answer
I am providing this solution in JAVA language as you haven't mentioned in which language you want the solution.
3(A) : Please find below the method you have asked for:
void printValues(int[] myArray, int length) {
System.out.println("Output:");
for(int i =0;i< length; i++){
System.out.println(myArray[i]);
}
}
3(B) : Please find below method to print array element in reverse order:
void reversePrintValues(int[] myArray, int length) {
//Iterating loop from its length-1 to zero to print it in reverse order.
System.out.println("Output:");
for(int i = length-1;i >= 0; i--){
System.out.println(myArray[i]);
}
}
Below is the common code that i have used to check the working of both the methods :
import java.util.Scanner;
public class ArrayingNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the size of array");
int length = sc.nextInt();
//declaring an array with the input size
int [] myArray = new int [length];
//Taking the input of elements of array from user
System.out.println("Please eneter the elements of array");
for(int i=0; i< length ; i++){
myArray[i] = sc.nextInt();
}
//calling method printValues to print the elements of array one per line
printValues(myArray, length);
//calling method reversePrintValues to print the elements of array in reverse order one per line
reversePrintValues(myArray, length);
}
//This method is static because we are calling it from static method
static void reversePrintValues(int[] myArray, int length) {
//Iterating loop from its length-1 to zero to print it in reverse order.
System.out.println("Output:");
for(int i = length-1;i >= 0; i--){
System.out.println(myArray[i]);
}
}
//This method is static because we are calling it from static method
static void printValues(int[] myArray, int length) {
// TODO Auto-generated method stub
System.out.println("Output:");
for(int i =0;i< length; i++){
System.out.println(myArray[i]);
}
}
}
End of solution. Please revert to me if you face any issue. Thanks :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.