Pseudo Arithmetic ISC Computer Science 2011 Theory

You are given a sequence of N integers, which are called as pseudo arithmetic sequences (sequences that are in arithmetic progression).

Sequence of N integers: 2, 5, 6, 8, 9, 12
We observe that 2 + 12 = 5 + 9 = 6 + 8 = 14.
The sum of the above sequence can be calculated as 14 × 3 = 42.

For sequence containing an odd number of elements, the rule is to double the middle element. For example: 2, 5, 7, 9, 12 = 2 + 12 = 5 + 9 = 7 + 7 = 14.
14 
× 3 = 42 (middle element = 7)

A class Pseudoarithmetic determines whether a given sequence is a pseudo-arithmetic sequence.

The details of the class are given below:

Class name: Pseudoarithmetic

Data members/instance variables:
n: to store the size of the sequence
a[]: integer array to store the sequence of numbers
flag: store the status
sum: store the sum of sequence of numbers
r: store the sum of the two numbers

Member functions:
Pseudoarithmetic(): default constructor
void accept(int num): to assign num to n and to create an integer array. Fill in the elements of the array.
boolean check(): return true if the sequence is a pseudo-arithmetic sequence otherwise returns false

Specify the class Pseudoarithmetic, giving details of the constructor, void accept(int) and boolean check(). Also define the main() function to create an object and call the member functions accordingly to enable the task.

import java.util.Scanner;
class Pseudoarithmetic{
    int n;
    int a[];
    boolean flag;
    int sum;
    int r;
    public Pseudoarithmetic(){
        n = 0;
        flag = true;
        sum = 0;
        r = 0;
    }
    public void accept(int num){
        n = num;
        a = new int[n];
        Scanner in = new Scanner(System.in);
        System.out.println("Enter elements:");
        for(int i = 0; i < n; i++)
            a[i] = Integer.parseInt(in.nextLine());
    }
    public boolean check(){
        int limit;
        int i;
        if(n % 2 == 0)
            limit = n / 2;
        else
            limit = n / 2 - 1;
        for(i = 0; i <= limit; i++){
            r = a[i] + a[n - 1 - i];
            sum += r;
            if(i == 0 && r != sum)
                flag = false;
            else if(sum % r != 0)
                flag = false;
        }
        if(n % 2 == 1 && (sum % (a[i] * 2) != 0))
            flag = false;
        return flag;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("N = ");
        int num = Integer.parseInt(in.nextLine());
        Pseudoarithmetic obj = new Pseudoarithmetic();
        obj.accept(num);
        if(obj.check())
            System.out.println("Pseudo-arithmetic sequence!");
        else
            System.out.println("Not a Pseudo-arithmetic sequence.");
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory