Question

In: Computer Science

This assignment requires you to use at least one loop and an array. You can refer...

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

Solutions

Expert Solution

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:


Related Solutions

This assignment requires you to use at least one loop and an array. You can refer...
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...
FINANCE ASSIGNMENT **THIS ASSIGNMENT REQUIRES THE USE OF MICROSOFT EXCEL** Refer to information page on the...
FINANCE ASSIGNMENT **THIS ASSIGNMENT REQUIRES THE USE OF MICROSOFT EXCEL** Refer to information page on the use of some finance features in Excel. Understand financial functions @PMT, @PPMT, @IPMT, @PV and @FV. See here. 1. Assume you are employed at $60,000 per year. Consider such deductions as social security, income taxes (federal, state and county) and your payment on health benefits. Do the best you can to come to grips with what is a realistic monthly take home pay. 2....
USE VISUAL STUDIO/VB In this assignment, you will create an array of ten elements, one for...
USE VISUAL STUDIO/VB In this assignment, you will create an array of ten elements, one for name, one for hours worked,and the last for hourly rate. The arrays will receive the information from inputs from the screen.For example, you will ask the following three questions: a) Employee name: b) Hours worked: c) Hourly rate: After you have received all ten records and have inserted them into the array, you will then calculate the hourly rate times the hours worked to...
Please, write a loop to print odd numbers an array a with pointers backwards. You can...
Please, write a loop to print odd numbers an array a with pointers backwards. You can use a variable “size” for array size.
this assignment requires you to interview one person and requires an analysis of your interview experience....
this assignment requires you to interview one person and requires an analysis of your interview experience. Part I: Interview Select a patient, a family member, or a friend to interview. Be sure to focus on the interviewee's experience as a patient, regardless of whom you choose to interview. Review The Joint Commission resource which provides some guidelines for creating spiritual assessment tools for evaluating the spiritual needs of patients. Using this resource and any other guidelines/examples that you can find,...
The presentation slides This assignment requires you to look at one interface for a product of...
The presentation slides This assignment requires you to look at one interface for a product of your choice, identify what it was developed for, and then trace back its history (to the origin if possible) to look for similar solutions. This way you'll be clearer what the original problem is and how human through out history has managed it. You'll see that whole or part of the very early solutions seem to stay in most of the newer solutions, and...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters (a-z|A-Z) and store each in the array. If an invalid character is entered, re-prompt until a valid char is entered. After 10 characters are stored in the array, use a loop to print out all characters in the array from the first to the last element. Then, use another loop starting at the last element to print all characters in the array in reverse...
This assignment requires you to look at one interface for a product of your choice, identify...
This assignment requires you to look at one interface for a product of your choice, identify what it was developed for, and then trace back its history (to the origin if possible) to look for similar solutions. This way you'll be clearer what the original problem is and how human through out history has managed it. You'll see that whole or part of the very early solutions seem to stay in most of the newer solutions, and you'll be able...
1. Define "critical thinking" and briefly provide at least one example of you can use it...
1. Define "critical thinking" and briefly provide at least one example of you can use it outside of class. 2. Read Boss' discussion of "The Three-Tier Model of Thinking" and apply that analogy to an experience in your life. In effect, you will critically analyze an event/experience in your life.
VISUAL STUDIO CODE Use a for loop for to create "password". Request the password at least...
VISUAL STUDIO CODE Use a for loop for to create "password". Request the password at least 3 times. If the password is 12345, the procedure ends, if the correct password entered, close excel saving changes. THANKS
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT