0% found this document useful (0 votes)
84 views

CSC 1513 Programming Fundamental Individual Project

The document contains details of an individual programming assignment submitted by a student. It includes the programme name, course code, course name, assignment name, submission date, student details like name, ID, semester etc. and is submitted to the faculty name and department.

Uploaded by

Achyut Neupane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

CSC 1513 Programming Fundamental Individual Project

The document contains details of an individual programming assignment submitted by a student. It includes the programme name, course code, course name, assignment name, submission date, student details like name, ID, semester etc. and is submitted to the faculty name and department.

Uploaded by

Achyut Neupane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programme Name: BCS

Course Code: CSC 1513


Course Name: Programming Fundamental
Assignment: Individual Project
Date of Submission: 12 May 2022

Submitted By: Submitted To:


Student Name: Achyut Neupane Faculty Name: Rupak Koirala
IUKL ID: 042103900002 Department: PO
Semester: 1st Semester
Intake: September 2021
Q. 1. Write a method named isDivisible that takes an integer array and a divisor and returns
1 if all its elements are divided by the divisor with no remainder. Otherwise, it returns 0.
A This class CheckDivisible is used to check if the array is divisible. Being a divisible array
means all its elements are divided by the divisor with no remainder.
There is a method in this class called isDivisible() that is used to check if the array is divisible
or not. If the array is divisible, it returns `1`. If the array is not divisible, it returns `0`.

Logic behind divisible array


There is variable isDivisible initially set to 1 and is used to check if the array is divisible or
not. If the array is not divisible, it is set to 0. For loop is used to iterate through the array and
check if the element is divisible by the divisor.
For each element, if the reminder of the element divided by the divisor is not 0, isDivisible is
set to 0 and loop is terminated.
Test Cases:
For the test cases given in the question, following is the test code:

Test Output:
Q. 2. Write a method called `isVanilla` that returns `1` if its argument is a vanilla array.
Otherwise it returns `0`.
A This class CheckVanilla is used to check if the array is vanilla or not. Being a vanilla array
means all its elements are made up of the same digit.
There are three methods in this class:
• isVanilla()
• isVanillaElement()
• setVanillaWith()
There is an attribute vanillaWith that is used to store the digit that the array is supposed to be
made up of for being a vanilla array. The isVanillaElement() method is used to check if the
element in the parameter is made up of the same digit as the vanillaWith attribute. The
setVanillaWith() method is used to set the vanillaWith attribute. The isVanilla() method is
used to check if the array is vanilla or not. If the array is vanilla, it returns 1. If the array is not
vanilla, it returns 0.
Logic behind vanilla element
To check if an element is vanilla element, we need to check if the element is made up of the
same digit as the vanillaWith attribute. The modulus by 10 is used to get all the digits in the
element. While loop is executed and if either of digit in the element is not the same as the
vanillaWith attribute, it returns 0.

Test Cases:
For the test cases given in the question, following is the test code:

Test Output:
Q. 3. Write a function named isZeroBalanced that returns 1 if its argument is a zero-balanced
array. Otherwise it returns 0.
A This class CheckZeroBalanced is used to check if the array is zero-balanced or not. Being a
zero-balanced array means all its elements sum to 0 and for each positive element n, there
exists another element that is the negative of n. This class has two methods:
• isZeroBalanced()
• existsInArray()
isZeroBalanced() is used to check if the array is zero-balanced or not. If the array is zero-
balanced, it returns 1. If the array is not zero-balanced, it returns 0. existsInArray() is used to
check if the element in the parameter is the negative of another element in the array. If the
negative of the element is found in the array, it returns the index of the negative element. If
the negative of the element is not found in the array, it returns -1. Here, negative is for the
positive number as the element. If the element is negative, it returns the index of the positive
element. If the positive of the negative element is not found in the array, it returns -1.

Logic behind the zero-balanced array


To check if an element is zero-balanced element, for loop is executed in all the element of
array. If the element is negative, it checks if the positive of the element is also in the array. If
the positive of the element is not found in the array, the variable isZeroBalanced is set to 0. If
the positive of the element is found in the array, the variable isZeroBalanced is set to 1. If the
element is positive, it checks if the negative of the element is also in the array. If the negative
of the element is not found in the array, the variable isZeroBalanced is set to 0. If the
negative of the element is found in the array, the variable isZeroBalanced is set to 1.
There is an extra variable called balance that is used to store the sum of all the element.
Initially, balance is set to 0. On every loop, the value of the element is added to the balance
variable. After the loop is ended, the balance variable is checked. If the balance variable is not
0, it returns 0. If the balance variable is 0, it returns 1.

Test Cases:
For the test cases given in the question, following is the test code:
Test Output:
Q. 4. Write a function named isDaphne that returns `1` if its array argument is a Daphne
array. Otherwise it returns `0`.
A This class `CheckDaphneArray` is used to check if the array is Daphne or not. Being a
Daphne array means all its elements are either all odd numbers or all even numbers.

There are three methods in this class:


• `isEven()`
• `isOdd()`
• `isDaphne()`

`isEven()` is used to check if the element in the parameter is even.


`isOdd()` is used to check if the element in the parameter is odd.
`isDaphne()` is used to check if the array is Daphne or not. If the array is Daphne, it returns
`1`. If the array is not Daphne, it returns `0`.

Logic behind Daphne Array


For loop is used to iterate through the array and check if the element is even or odd. There are
two variables `isAllEven` and `isAllOdd` which are used to check if all the elements are even
or odd. There is also a variable `isDaphne` which is initially set to `1`.
First element is checked and if it is even, `isAllEven` is set to `true` and `isAllOdd` is set to
`false`. If it is odd, `isAllEven` is set to `false` and `isAllOdd` is set to `true`. After that loop
is executed and for each element, if the element is even and `isAllEven` is `false`, `isDaphne`
variable is set to `0`. Also, if the element is odd and `isAllOdd` is `false`, `isDaphne` variable
is set to `0`.

Test Cases:
For the test cases given in the question, following is the test code:

Test Result:
Q. 5. Write a function named isBalanced that accepts an array of integers and returns 1 if the
array is balanced, otherwise it returns 0.
A This class CheckBalanced is used to check if even indexed element is even and odd indexed
element is odd.
There are three methods in this class:
• isBalanced()
• isEven()
• isOdd()
isEven() is used to check if the element in the parameter is even. isOdd() is used to check if
the element in the parameter is odd. isBalanced() is used to check if the array is balanced or
not. If the array is balanced, it returns 1. If the array is not balanced, it returns 0.

Logic behind Balanced array


For loop is used to iterate through the array and check if both index and element are even or
odd. If the index is even and the element is even, it returns 1. If the index is odd and the
element is odd, it returns 1. If the index is even and the element is odd, it returns 0. If the
index is odd and the element is even, it returns 0. When the isBalanced variable is 0, it breaks
the loop and return the value.

Test Cases:
For the test cases given in the question, following is the test code:
Test Output:

You might also like