Question

In: Computer Science

Write a program to read in a collection of integer values, and find and print the...

Write a program to read in a collection of integer values, and find and print the index of the first occurrence and last occurence of the number 12. The program should print an index value of 0 if the number 12 is not found. The index is the sequence number of the data item 12. For example if the eighth data item is the only 12, then the index value 8 should be printed for the first and last occurrence.

The coding language required is Java.

Solutions

Expert Solution

import java.io.*;
class Main
{
    public static void main(String args[])throws IOException
    {
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter number of elements");
        int n=Integer.parseInt(in.readLine()); // taking size of array as input from user
        int arr[]=new int[n];
        System.out.println("Enter the elements"); // taking elements of array as input from user
        for(int i=0;i<n;i++)
          arr[i]=Integer.parseInt(in.readLine());
        int flag=0; //to check whether 12 is present in array or not
        
        //to determine first index of 12, run for loop from first to last index of array
        for(int i=0;i<n;i++)
        {
        if(arr[i]==12)
         {
            flag=1;  //if 12 is present,set flag to 1
            System.out.println("First index of 12="+(i+1)); // printing i+1 because array indexing starts from 0
            break; // break out from loop as soon as first index of 12 is found
         }
        }

        // to determine last index of 12, run for loop from last index to first index of array
        for(int i=n-1;i>=0;i--)
        {
            if(arr[i]==12)
            {
             flag=1;
             System.out.println("Last index of 12="+(i+1)); // printing i+1 because array indexing starts from 0
             break; // break out of loop as soon as last index of 12 is found
            }
        }
        // if 12 is present in array then value of flag will be 1,else 0.
        if(flag==0)
          System.out.println("First and Last index of 12= 0");
    }
}

An array is used to store the integer values. The inputs are taken dynamically from the user.

The first loop runs from 0th till (n-1)th index. As soon as 12 is encountered in the array, flag is set to 1 and the index position of first occurence of 12 is printed and the control breaks out of the first for loop.

The second loop runs in reverse order from (n-1)th index to the 0th index of array. As soon as 12 is encountered in the array, flag is set to 1 and the index position of the last occurence of 12 is printed and the control breaks out of the second for loop.

Lastly, we check the value of flag. If flag value is set to 1, it means that the element 12 is present in the array. If flag value is set to 0, it means that the element 12 is not present in the array and the first and last index of 12 is printed as 0 as mentioned in the question.


Related Solutions

1. Write a program that read a sequence of positive integer inputs and print the sum...
1. Write a program that read a sequence of positive integer inputs and print the sum and average of the inputs. Assume that the user always enters a positive number (integer) as an input value or an empty space as a sentinel value. Please use the below to test your code [45 points]: Enter an int value or empty string to end: 98 Enter an int value or empty string to end: 78 Enter an int value or empty string...
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and...
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and "multiplier", as user input. Create an array of integers with arraySize elements. Set each array element to the value i*multiplier, where i is the element's index. Next create two functions, called PrintForward() and PrintBackward(), that each accept two parameters: (a) the array to print, (b) the size of the array. The PrintForward() function should print each integer in the array, beginning with index 0....
Problem description Write a program that uses a loop to read integer values from the standard...
Problem description Write a program that uses a loop to read integer values from the standard input stream. Each non-zero value, x, is the first number of a sequence. Define a0 = x; an+1 = an / 2 if an is even; an+1 = 3 * an + 1 if an is odd. Then there exists an integer k such that ak = 1. For each non-zero value of x, the program outputs the integer k such that ak =...
Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Design a C program to print out the unsigned char (integer) values of the 4 bytes...
Design a C program to print out the unsigned char (integer) values of the 4 bytes in sequential order (i.e., the first byte is printed first). Using the code framework provided below, the printing is done by the function void byte_value(int *), in which only pointer variables can be declared and used. #include <stdio.h> ​ void byte_value(int *); ​ int main() { int n = 1; byte_value(&n); printf("Enter an integer: "); if (scanf("%d", &n) == 1) byte_value(&n); return 0; }...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it in horizontal order of the screen
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT