In: Computer Science
This assignment requires you to use at least one loop and an array. You can refer to our book and any programs you have written so far. Make sure that your work is your own.
Write a comment at the beginning of your program with your name and your birthday (month and day, does not need to include year). For example: John Doe, May 23
Create an array that will store whole numbers. The size of the array should be ten more than the month number of your birthday. Continuing the example, for a birthday in May, the array will be of size 15 since May is the fifth month and 5 + 10 = 15.
Then let the user enter each value to store in the array. Perform input validation that the user enters numbers between 1 and the day of the month that is your birthday. (That is, a value is invalid if is less than 1. Similarly, a value is invalid if it is greater than the day of the month that is your birthday.) Continuing the example, for a birthday on the 23 of the month, the valid values the user can enter are 1 through 23
Go through the array and collapse the array into a single representative number as follows:
i. First replace every three adjacent integers with the sum of the two numbers. For example, if the first three elements of the array are 1, 2, and 3, they become the single value of 6. For example [1, 2, 8, 9, 4, 13, 7, 1, 9, 10, 5] becomes [11, 26, 17, 15]
ii. Go through the new array and modify any number that is larger than 12 to be the difference between it and 12. (You can use either subtraction or modulus for this.) Continuing the example, [11, 26, 17, 15] becomes [11, 14, 5, 3]
Then iteratively continue collapsing the array until you have a single representative integer. Continuing the example: Add adjacent three integers in [11, 14, 5, 3] to get [30, 3] Then change it to become [18, 3] Add adjacent three integers in [18, 3] to get the single value of 21 which is modified to be 9 and the result is displayed to the user.
Some tips: Try this process with more than 1 array as input to make sure it really works! If you are finding this confusing or difficult, begin with getting input from user and performing the first iteration of adding adjacent pairs of integers.
Programming code should be written in Java Language thank you
CODE:
// Anonymous, Sept 18
import java.io.*;
import java.util.*;
class Test {
static int[] collapse(int[] ar, int n)
{ int len=n;
len=(len%3!=0)?(len/3)+1:(len/3);
int ret[]=new int[len];
int j=0,count=0,sum=0;
for(int i=0;i<n;i++)
{
sum+=ar[i];
count++;
if(count==3)
{
ret[j++]=sum;
sum=0;
count=0;
}
}
if(sum!=0)
ret[ret.length-1]=sum;
return ret;
}
public static void main (String[] args) {
Scanner sc=new Scanner (System.in);
int[] ar=new int[18];
System.out.println("Enter the values in array");
for(int i=0;i<ar.length; )
{ int temp=sc.nextInt();
if(temp>=1 && temp<=18)
{
ar[i]=temp;
i++;
}
else
{
System.out.println(temp+" is not a valid number.
Please input "+ (ar.length-i)+" more numbers.");
}
}
int len=ar.length;
int ans=0;
int tempar[]=ar.clone();
while(len!=1)
{
tempar= collapse(tempar,len);
for(int i=0;i<tempar.length;i++)
{
if(tempar[i]>12)
tempar[i]=Math.abs(tempar[i]-12);
}
len=(len%3!=0)?(len/3)+1:(len/3);
ans=tempar[0];
}
System.out.println("Your Ans is "+ans);
}
}
OUTPUT: