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

need help in computer science!! 1. Write a program to : Declare an integer array

ID: 3682351 • Letter: N

Question

need help in computer science!!

1.

Write a program to :

Declare an integer array a of size 10

Assign it values using the following formula:

Print the contents of array a.

Copy all elements divisible by 3 into another array (say b). Print array b.

Create a new array (say c) with the first half of c as the second half of a and the second half of c as the first half of a. Print array c.

Create a new array(say d) that contains all elements of array a in reverse. Print array d.

Contents of each array must be printed on a new line and array elements in each line should be separated by a space.

AND

2.

Write a program to : 1. Create an integer array of size 20

Assign it values using loops such that the first element is 3 and every element after that is the determined by the following formula

Print array elements with each element separated by a space

Compute and print the following (with formats given in double quotes):

Explanation / Answer

1).

#include <iostream>
#include <string>

using namespace std;


int main()
{
   // creating an array of 10 integers
   int arr[10] = {0}; // initializing with 0
  
   // aasigning value to each entry
   for(int i=0; i<10; i++)
       arr[i] = 2*i+1;
      
   // printing content of array
   for(int i=0; i<10; i++)
       cout<<arr[i]<<" ";
   cout<<endl;
  
   // counting number of elements in array that is divisible by 3
   int count =0;
   for(int i=0; i<10; i++)
       if(arr[i]%3==0)
           count++;
          
   // creating an array of size 3
   int arr2[count];
   int j=0;
   // copying all elemetns from arr to arr2 those are divisible by 2
   for(int i=0; i<10; i++)
       if(arr[i]%3==0)
           arr2[j++] = arr[i];
          
   // printing arr2
   for(int i=0; i<count; i++)
       cout<<arr2[i]<<" ";
   cout<<endl;
  
   // creating array c
   int c[10];
   // assigning first half c with second half of a
   for(int i=0; i<5; i++)
       c[i] = arr[i+5];
   // assiging second half of c with first half of a
   for(int i=0; i<5; i++)
       c[i+5] = arr[i];
   // printing array c
   for(int i=0; i<10; i++)
       cout<<c[i]<<" ";
   cout<<endl;
  
   // creating new array d
   int d[10];
  
   // storing elements of arr into d in reverse order
   for(int i=0; i<10; i++)
       d[i] = arr[9-i];
      
   // printing array d
   for(int i=0; i<10; i++)
       cout<<d[i]<<" ";
   cout<<endl;
   return 0;

}


/*

Output:

1 3 5 7 9 11 13 15 17 19
3 9 15
11 13 15 17 19 1 3 5 7 9
19 17 15 13 11 9 7 5 3 1

*/

2).

#include<iostream>

using namespace std;

int main(){
  
   // creating an integer array of size 20
   int arr[20] = {0}; // initializing with 0
  
   // assigining first element of array to 3
   arr[0] = 3;
  
   for(int i=1; i<20; i++){
       if(arr[i-1]%2 == 0)
           arr[i] = arr[i-1] + (3*i); // if arr[i-1] is even
       else
           arr[i] = arr[i-1] - (2*i); // if arr[i-1] is odd
   }
  
   // printing array
   for(int i=0; i<20; i++)
       cout<<arr[i]<<" ";
   cout<<endl;
  
   // calculating maximum, minimum, average, number of elemetns divisible by 5
   int sum=0, min = arr[0], max = arr[0], divBy5 = 0;
   for(int i=1; i<20; i++){
       sum = sum+arr[i];
      
       if(max < arr[i])
           max = arr[i];
       if(min > arr[i])
           min = arr[i];
          
       if(arr[i]%5==0)
           divBy5++;
   }
  
   // printing max, min and average
   cout<<"Average: "<<sum/20.0<<endl;
   cout<<"Maximum: "<<max<<endl;
   cout<<"Minimum: "<<min<<endl;
   cout<<"Number of elements divisible by 5: "<<divBy5<<endl;
  
   return 0;
}


/*

Output:

3 1 -3 -9 -17 -27 -39 -53 -69 -87 -107 -129 -153 -179 -207 -237 -269 -303 -339 -377
Average: -130.15
Maximum: 3
Minimum: -377
Number of elements divisible by 5: 0

*/