Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8. Implement a method named pieces that accepts an array of integers named array

ID: 3864797 • Letter: 8

Question

8. Implement a method named pieces that accepts an array of integers named array. The method will sum each group of 5 integers in the array and place the sum into another array named result. The method will return the new array. If the last group in the array does not contain a full group of 5 it is not to be included.

Ex: if the array consists of : {3, 6, 3, 2, 8, 1, 0, 4, 9, 1, 0, 1, 1, 6, 7, 3, 8, l, 2}there are 3 groups of 5 and 4 remaining. The new array would be of size 3 and the sum of the last 4 digits would not be included.

9. Implement a method named init that accepts a char array named alpha. The method will initialize the array to the alphabet. If the array is greater than 26 elements, the additional elements will all be initialized to the dollar sign.

10. Given the following attributes of the class named Geometric, implement the equals method that accepts a Geometric parameter named geo and returns true if the two objects are identical. They are identical if each attribute is the same in both objects.

  

Explanation / Answer

int pieces (int array [ ], int n)    /* n is the size of array */

{ int a,b,c=0,d,x,y,z=0;

    a=sizeof(array);        /* Here we are finding the total number of elements in array named array*/

    b=a/5; int newarray[b];   /* here we are finding the total number of elements for new array to be returned */

    for(x=1; x<=b,x++) /* now we are starting our loop for all elements of new array to be returned*/

        {   d=0; /* we are initializing out counter d, which will add first 5 elements and then again set to zero*/

            for (y=1; y<=5;y++) /* we are repeating loop for 5 till 5 elements of array*/

               {    d=d+array[c]; /* Here we are adding 5 elements of array serially*/

                    c++; /* incrementing variable c to next element of array*/

                 }

             newarray[z]=d; /* inserting the sum of 5 elements of array into newarray*/

              z++; /* incrementing the variable to insert sum of next 5 elements of array into newarray*/

             }

return (newarray); /* returning newarray which contains only sum of 5 elements and discard the last elemnts if they are less than 5*/

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote