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

You are to write a program that will manipulate an array. Your program should ha

ID: 3781618 • Letter: Y

Question

You are to write a program that will manipulate an array. Your program should handle up to 100 integer numbers.

Label the results for each printed out below. You will have several different outputs below. Print your results in the same order given below.

For each part below, write one or more functions (there are a number of functions). Restriction: A function that does any of the calculations listed below can NOT print out the results in that function but must print out results in either the ‘main’ routine or call a different function that only prints a results. A function does one task and only one task.

------------------------------------

1. Read in the values into the array. The last value is a 0 and is NOT one of the values to be used in any of the calculation below. The input is found in     array.data

2. Print out all the values in the array with no more than 10 numbers per output line.

3. Print out the average of the set of numbers.

4. Print out how many values are larger than the average and how many are smaller than the average. Pass average as a parameter.

5. Convert every negative number to a positive number. Print out the array.

6. Print out the average of the set of numbers.

7. How many numbers in the array are multiples of 4 and 11?

8. Find the largest number and print out the number and its position.

9. Find the smallest number and print out the number and its position.

10. Zero out all even value numbers in the array and print out the array.

11. Print out the new average for the new array.


The array.data file looks like:

22
27
64
7
44
26
45
66
83
62
52
-7
-36
62
78
-67
34
73
93
8
-3
-2
2
27
1
11
12
-73
83
77
37
72
82
23
0

Explanation / Answer

//solution of the given problem

#include <iostream>
using namespace std;

//define the array
int arr[100];
int arr_length;

//function prototype
void read_array();
void print_array();
double array_average();
int avg_greater(double avg);
int avg_smaller(double avg);
void neg_to_pos();
int no_of_multiple(int k);
int largest();
int smallest();
void zero_out();

int main()
{
   // your code goes here
  
   //task 1: read the values in the array
   read_array();
  
   //task 2: print array
   print_array();
  
   //task 3: print out the average of array
   double avg = array_average();
   cout<<"Average of array is: "<<avg<<endl;
  
   //task 4(a): find out the count of numbers which are greater than avg
   int max_no = avg_greater(avg);
   cout<<"No. of values larger than average is: "<<max_no<<endl;
  
   //task 4(b): find out the count of numbers which are greater than avg
   int min_no = avg_smaller(avg);
   cout<<"No. of values smaller than average is: "<<min_no<<endl;
  
   //task 5: convert negative number to positive number
   neg_to_pos();
  
   //task 6: print out the average of array
   avg = array_average();
   cout<<"Average of array is:"<<avg<<endl;
  
   //task 7(a): number of multiples of 4 and 11
   int count1 = no_of_multiple(4);
   int count2 = no_of_multiple(11);
   cout<<"No. of multiples of 4 and 11 in array are "<<count1<<" and "<<count2<<",respectively"<<endl;
  
   //task 8: print out the largest no. and its position
   int pos = largest();
   cout<<"Largest no. in the array is "<<arr[pos]<<" and it's position (starting from 1) is "<<pos+1<<endl;
  
   //task 9: print out the smallest no. and its position
   pos = smallest();
   cout<<"Smallest no. in the array is "<<arr[pos]<<" and it's position (starting from 1) is "<<pos+1<<endl;
  
   //task 10: zero out all the even values and print the array
   zero_out();
  
   //task 11: print out the new average of new array
   avg = array_average();
   cout<<"Average of new array is:"<<avg<<endl;
  
   return 0;
}//end of main function

void read_array()
{
   int i = 0, n ;
   while(true)
   {
       cin>>n;
       if(n==0)
           break;
       arr[i++]=n;
   }
   arr_length = i;
}//end of read_array function


void print_array()
{
   int count=0;
   cout<<"Values of array are:"<<endl;
   for(int j=0; j<arr_length; j++)
   {
      
       cout<<arr[j]<<" ";
       count++;
       //in order to print 10 values at each line
       if(count==10)
       {
           cout<<endl;
           count=0;
       }
   }
   cout<<endl;
}//end of print_array function

double array_average()
{
   double sum = 0;
   for (int j=0; j<arr_length ; j++)
   {
       sum += arr[j];
   }
   return (sum/arr_length);
}//end of average function

int avg_greater(double avg)
{
   int count =0 ;
   for(int j=0;j<arr_length; j++)
   {
       if(arr[j]>avg)
           count++;
   }
   return count;
}//end of avg_greater function

int avg_smaller(double avg)
{
   int count =0 ;
   for(int j=0;j<arr_length; j++)
   {
       if(arr[j]<avg)
           count++;
   }
   return count;
}//end of avg_smaller function

void neg_to_pos()
{
   for(int j=0; j<arr_length; j++)
   {
       if(arr[j]<0)
       {
           arr[j] = 0-arr[j];
       }
   }
   print_array();
}//end of negative to positive converter

int no_of_multiple(int k)
{
   int count = 0;
   for(int j=0;j<arr_length; j++)
   {
       if(arr[j]%k==0)
           count++;
          
   }
   return count;
}//end of no. of multiples function

int largest()
{
   int max=arr[0],pos=0;
   for(int j=1; j<arr_length; j++)
   {
       if(arr[j]>max)
           pos = j;
   }
   return pos;
}//end of calculating largest value function

int smallest()
{
   int min=arr[0],pos=0;
   for(int j=1; j<arr_length; j++)
   {
       if(arr[j]<min)
           pos = j;
   }
   return pos;
}//end of calculating smallest value function

void zero_out()
{
   for(int j=0; j<arr_length; j++)
   {
       if(arr[j]%2==0)
           arr[j]=0;
   }
   print_array();
}//end of zero_out the array function

Output of the given code is:

Input:

22

27
64
7
44
26
45
66
83
62
52
-7
-36
62
78
-67
34
73
93
8
-3
-2
2
27
1
11
12
-73
83
77
37
72
82
23
0

Output:

Values of array are:
22 27 64 7 44 26 45 66 83 62
52 -7 -36 62 78 -67 34 73 93 8
-3 -2 2 27 1 11 12 -73 83 77
37 72 82 23
Average of array is: 31.9118
No. of values larger than average is: 17
No. of values smaller than average is: 17
Values of array are:
22 27 64 7 44 26 45 66 83 62
52 7 36 62 78 67 34 73 93 8
3 2 2 27 1 11 12 73 83 77
37 72 82 23
Average of array is:42.9706
No. of multiples of 4 and 11 in array are 7 and 5,respectively
Largest no. in the array is 23 and it's position (starting from 1) is 34
Smallest no. in the array is 12 and it's position (starting from 1) is 27
Values of array are:
0 27 0 7 0 0 45 0 83 0
0 7 0 0 0 67 0 73 93 0
3 0 0 27 1 11 0 73 83 77
37 0 0 23
Average of new array is:21.6765

For implementation of code, you can see this url: http://ideone.com/eIAQbV

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