Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) Describe a non-recursive algorithm that takes a list of distinct integers a_1

ID: 3843721 • Letter: A

Question

a) Describe a non-recursive algorithm that takes a list of distinct integers a_1, a_2, ..., a_n and returns the number of positive integers in the list. Write your answer in pseudo-code or any well-known procedural language like Python, Java, C++, .... E.g. For the list -2, 9, -5, 3, -1 your program should return 2, because the list contains two positive integers. procedure Number_of_Positives (a_1, a_2, ..., a_n: integers) b) Describe a recursive algorithm that takes a list L of distinct integers and returns the the smallest positive number in the list. Write your answer in pseudo-code or any well-known procedural language like Python, Java, C++, .... You may assume that your language has the following built in functions to manipulate lists. i) "empty?" which returns TRUE if a given list is empty, FALSE otherwise ii) "first" which returns the first element of a given nonempty list. iii) "rest" which returns a list containing all but the first element of a given nonempty list. Note that if the list has only one element, "rest" will return the empty list. procedure Number_of_Positives (L: list of integers)

Explanation / Answer

import java.util.*;

public class allmain {
  

public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
  
   System.out.println("Enter numbers count:");
   int numcount=sc.nextInt();
   int arr[]=new int[numcount];
   for(int i=0;i<arr.length;i++){
       System.out.println("enter number:"+i);
       arr[i]=sc.nextInt();
   }
int count=   Number_of_postives(arr);
System.out.println("total number of postive integers are="+count);


}
public static int Number_of_postives(int arr[]){
   int count=0;
   for(int i=0;i<arr.length;i++){
       if(arr[i]>=0){
           count++;
      
       }
   }
   return count;
  
}

  
}

output:
Enter numbers count:
6
enter number:0
1
enter number:1
-3
enter number:2
-2
enter number:3
-1
enter number:4
3
enter number:5
2
total number of postive integers are=3

//iam writing second program also with same class name called allmain.javaprogramm 2

import java.util.*;
public class allmain {
   static int small=1000;

public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
  
   System.out.println("Enter numbers count:");
   int numcount=sc.nextInt();
   int arr[]=new int[numcount];
   for(int i=0;i<arr.length;i++){
       System.out.println("enter number:"+i);
       arr[i]=sc.nextInt();
   }
   Number_of_postives(arr);
  


}
public static void Number_of_postives(int arr[]){
  
   boolean empty=isempty(arr);
   if(empty==false){
       int first= check_if_first(arr);
       System.out.println("first element is :"+first);
       if(first!=small){
           int arr3[]= check_if_rest(arr);
           for(int i=0;i<arr3.length;i++)
               System.out.print(arr3[i]+" ");
       }
   }
  
  
  
}
private static int[] check_if_rest(int[] arr) {
  
   int listsize=arr.length-1;
   int[] arr2=new int[listsize];
   int size=arr.length-1;
       if(arr.length==1){
           arr[size]--;
         
       }else{
           for(int i=1;i<arr.length;i++){
               int j=i-1;
                   arr2[j]=arr[i];
              
              
           }
       }
      
       return arr2;
      
          
      
   }
   private static int check_if_first(int[] arr) {
   int first=arr[0];
   for(int i=0;i<arr.length;i++){
       if(arr[i]>=0){
       if(arr[i]<small){
           small=arr[i];
       }
       }
   }
   System.out.println("smallest element in list is:"+small);
   if(small==arr[0])
       System.out.println("first element is itself a small element in list:");
   else
       System.out.println("first elements is not a small element in list:");
       return first;
      
   }
   public static boolean isempty(int arr[]){
   if(arr.length>0)
       return false;
   else
       return true;
  
}
  
}

ouput:
Enter numbers count:
4
enter number:0
-1
enter number:1
-3
enter number:2
6
enter number:3
9
smallest element in list is:6
first elements is not a small element in list:
first element is :-1
-3 6 9