0% found this document useful (0 votes)
34 views3 pages

Assignment 5

Uploaded by

haladaher5555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Assignment 5

Uploaded by

haladaher5555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

American University of Beirut Fall 2023/2024

Department of Computer Science Asst 3


CMPS 201 – Introduction to
Due on Sunday Oct 8 at midnight
Programming

Exercise 1: Special Numbers


Special integer numbers are important part of Mathematics and different applied areas. Prime, Perfect,
and Triangular numbers are some examples.
• A prime number is a whole number greater than 1 that cannot be exactly divided by any whole
number other than itself and 1.
• A perfect number is a positive integer that is equal to the summation of its proper factors (the
numbers that can divide it except for itself).
• The triangular number sequence is the representation of the number of dots in each triangular
pattern. These numbers are in a sequence of 1, 3, 6, 10, 15, 21, 28, 36, 45, ... To get the nth
triangular number, the following formula can be used:

Implement the following methods:


1. isPrime(int n): it takes an integer and returns true if it is a prime number
and false otherwise
2. isPerfect(int n): it takes an integer and returns true if it is a perfect number
and false otherwise
3. isTriangularNumber(int n): it takes an integer and returns true if it is a term in the
triangular numbers sequence and false otherwise.
Write a main method that asks the user to enter a positive integer, and check if the entered number is a
special one as shown in the examples below.

Input Result

13 Enter a positive integer > 1:


13 is a prime number.

28 Enter a positive integer > 1:


28 is a perfect number and a triangular number.

496 Enter a positive integer > 1:


496 is a perfect number and a triangular number.

122 Enter a positive integer > 1:


Not a special number!

Exercise 2: Different Ways


Write a recursive method called differentWays() that takes two input integer parameters n and x, and
returns the number of ways x different values can be chosen out of n numbers. Note that,
choosing x elements out of n can be done using either one of the below:
• Choose all the x from the n elements excluding one
• Choose x-1 elements from the n elements excluding the last one and then adding the last
element.
Think thoroughly about the possible base cases. Assume that there exists only one way to
choose 0 elements out of any x number of elements, and one way to choose n out of n elements.
It is not allowed to use any formula that involves the computation of a factorial. Instead, the code that
generates such a result must be written from scratch following the above-described guidelines.
Sample runs:
System.out.println(differentWays(10, 4));
210
System.out.println(differentWays(6, 2));
15
System.out.println(differentWays(12, 1));
12
System.out.println(differentWays(6, 0));
1

Exercise 3: Pythagorean triplets


Pythagorean triplets are sets of 3 positive integers a; b; c satisfying the relationship: a2 + b2 = c2. The
smallest and best-known Pythagorean triple is (a, b, c) = (3, 4, 5). Write a program that reads an input n
and prints to the screen all Pythagorean triplets whose sum is less than n (i.e., a+b+c < n) and that are
not multiple of the (3, 4, 5) triplet. Your program will represent triplets as 3-tuples, and should consist of
three functions:
1. a function that takes an array of three integers and returns a boolean indicating whether the
Pythagorean relationship holds or not.
2. a function that takes an array of three integers and returns a boolean indicating whether the triplet
is a multiple of the smallest triplet or not.
3. a function that takes in an integer n and prints all the Pythagorean triplets, one triplet per line, as
specified above. It will use the previous two functions described.
4. The main portion of your program pythagore.py will read in the input, and call the last function
described above.

Exercise 4: Get Minimum in an Array


1. Write a method, getMin, that takes an integer array and returns the minimum value of the array.
2. Write another recursive version of the same method. Call it: getMinRecursive.

Exercise 5- Element Wise Sum in 1D Arrays


1. Create two integer arrays a and b of random sizes (between 3 and 8) and fill them with random
numbers between 1 and 10.
2. Print both arrays.
3. Create a third integer array c that has the size of the largest array.
4. Perform the element-wise sum of a and b meaning c[i] = a[i] + b[i]. Print the array.
5. Your program should work even if a and b are not of the same size. For example: If a =
[1,5,6,2] and b = [5,2] then c = [6,7,6,2]

Exercise 6: Remove Duplicates

Given a sorted array, write a java method int [] removeDuplicates(int[] array) that
removes all duplicates integers in the array. For example, an input of [0, 0, 0, 1, 1, 3, 4, 4, 4, 4, 6, 6]
should return [0, 1, 3, 4, 6]
Exercise 7: Array Character Counter
1. Create a 1D array of integers of size 26 called alphArray.
2. Ask the user to input a sentence s.
3. Write a case-insensitive function that receives the array alphArray and the sentence s and then fills
the array with the number of occurrences each English alphabet letter appears in s. Each element
in alphArray corresponds to such letter. Ignore all non-alphabet letters found in s.
4. In your solution, avoid using a set of nested if/else(s) to figure out for which alphabet letter a
certain character in s corresponds. A simple game of math does the job (can you guess what it’s?).
5. Once done, print the English alphabet letters (each on a separate line) and next to it how many
times it appears in s. Ignore any alphabet letter whose count is zero. In the example below, the
output ignores printing the alphabet letter f, j, k. p, q, u, v, x, and z since they don’t show up in s at
all.
Sample Run:
Please enter a sentence: Hello there! Today I went out to bring some groceries.
The letter count is:
a: 1
b: 1
c: 1
d: 1
e: 7
g: 2
h: 2
i: 2
l: 2
m: 1
n: 2
o: 6
r: 4
s: 2
t: 4
u: 1
w: 1
y: 1
Exercise 8- Exam Analytics
In this exercise, you will be helping professors in calculating the analytics of a given exam. The file
grades.txt stores the grades in different sections as follows: Each line represents respectively the scores
of all students in one section, for example, the first line stores the grades of section 1; the second line
contains the grades of section 2, and so on. The first number of each line represents the number of
students in the corresponding section.
1. Write a function, double calculateAverage(double[] grades), that takes an array of
doubles representing the grades, and returns the average.
2. Write a function, double[] lowestAndHighest(double[] grades), that takes an array
of doubles representing the grades, and returns the highest and the lowest grades as an array.
3. Write a function, void readGrades(String file), that takes a file name, read the file one section at a
time. For each section, the grades should be saved in an array. Then using the functions
implemented previously, it should calculate and print the analytics (average, min, and max) of every
section, in addition to the average, the lowest, and the highest grades of the whole class.
For example:
Section 1: average = 70.1, lowest = 23, highest = 91.
Section 2: average = 59.8, lowest = 32, highest =92.

Class: average = 68.6, lowest = 23, highest = 94

You might also like