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

1-D Array - C++ You are to write a program that will manipulate an array. Your p

ID: 3662806 • Letter: 1

Question

1-D Array - C++

You are to write a program that will manipulate an array. Your program should handle up to 50 integer numbers. You have to count how many values you read in and you cannot hard code the number of values in the data file.

Label the results for each print 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). Pass back the values from functions through the return statement (return value) and not through a function parameter.

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

1. Read in the data elements into the array. The input is found in     arrayV2.dat (found here: http://pastebin.com/FdFNHJUM)

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

3. Print out the average of the array.

4. Print out how many data elements are larger than the average (one value) and how many are smaller than the average (one value). Pass average as a parameter.

5. Print out the data elements in the array that are in the odd position of the array.   That is printout ary[1], ary[3], ary[5] …. Print only 10 numbers per line.

6. Print out the number data elements in the array that are even values in the array (one value).

7. Convert every negative data elements to a positive number and every positive data element to a negative number.
8. Print out the average of the new array.

9. Swap the first data element in the array with the last data element in the array. Print out the array.

10. How many data elements in the array are multiples of either 7 or 13?

11. Find the largest data element and print out the data element and its array ‘position’.

12. Find the smallest data element and print out the data element and its array ‘position’.

13. How many values in the array are less than 30?

14. How many values in the array are greater than 75?

15. How many data elements in the array are greater than the 15th data element in the array?

16. Create a second array. Copy all the data elements from the first array to the second array. Print out the second array

17. Now copy all the data elements from the first array to the second array in reverse order. Print out the second array.

Explanation / Answer

/* C++ Program - One Dimensional Array Program with all the required operations*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

int arr[50], n, new_arr[50];
void create_array(int arr[50]);
void print_arr(int arr[50]);
int average(int arr[50]);
int smaller_count(int avg);
int larger_count(int avg);
void odd_position(void);
void even_val_num(void);
void pos_neg_convert(void);
void swap(void);
int multiple_7_13(void);
void largest_smallest(void);
void less30(void);
void greater75(void);
void create_new(void);

void create_array(int arr[50])
{
   //int n;
   cout<<" How many element you want to store in the array ? ";
   cin>>n;
   cout<<" Enter "<<n<<" element to store in the array : ";
   for(int i=0; i<n; i++)
   {
       cin>>arr[i];
   }
}

void create_new()
{
for(int i=0; i<n; i++)
   {
       new_arr[i]=arr[i];
   }
}

void print_arr(int arr[50])
{
   cout<<" The Elements in the Array is : ";
   for(int i=0; i<n; i++)
   {
       if (i%10 == 0)
           cout<<" ";

       cout<<arr[i]<<" ";
   }
   //return arr;
}

int average(int arr[50])
{
int sum=0;
int avg=0;
for(int i=0; i<n; i++)
   {
       sum=sum+arr[i];
cout<<" "<<sum;
   }
avg=sum/n;
return avg;
}

int smaller_count(int avg)
{
   int count=0;
   for(int i=0; i<n; i++)
   {
       if(arr[i]<avg)
           count++;
   }
   return count;
}

int larger_count(int avg)
{
   int count=0;
   for(int i=0; i<n; i++)
   {
       if(arr[i]>avg)
           count++;
   }
   return count;
}

void odd_position()
{
   int count=0;
   for(int i=0; i<n; i++)
   {
       if(count%10 == 0)
           cout<<" ";
       if(i%2 == 0)
       {
           cout<<arr[i]<<" ";
           count++;
       }
   }
   return;
}

void even_val_num()
{
   int count = 0;
   for(int i=0; i<n; i++)
   {
       if(arr[i]%2 == 0)
           count++;
   }
cout<<" The number of even value array elements is::"<<count;
   return;
}

void pos_neg_convert()
{
   for(int i=0; i<n; i++)
   {
           arr[i]=arr[i] * (-1);
   }
   return;
}

void swap()
{
int temp=0;
temp=arr[0];
arr[0]=arr[n-1];
arr[n-1]=temp;
return;
}

int multiple_7_13()
{
int count = 0;
for(int i=0; i<n; i++)
{
   if((arr[i]%7 == 0) || (arr[i]%13 == 0))
       count++;
}
return count;
}

void largest_smallest()
{
int l=arr[0];
int s=arr[0];
int l_pos=0;
int s_pos=0;
for(int i=1; i<n; i++)
{
   if(arr[i]>l)
   {
       l = arr[i];
       l_pos=i;
   }
   if(arr[i]<s)
   {
       s = arr[i];
       s_pos=i;
   }
}
cout<<" The largest element in the array is::"<<l<<" in the position::"<<l_pos;
cout<<" The smallest element in the array is::"<<s<<" in the position::"<<s_pos;
return;
}

void less30()
{
int count = 0;
for(int i=0; i<n; i++)
{
   if(arr[i] < 30)
       count++;
}
cout<<" Number of elements less than 30 is::"<<count;
return;
}

void greater75()
{
int count = 0;
int p15_count=0;
int p_15_elem=arr[14];
for(int i=0; i<n; i++)
{
   if(arr[i] > 75)
       count++;
   if(n>15)
   {
       if(arr[i] > p_15_elem)
       p15_count++;
   }
}
cout<<" Number of elements greater than 30 is::"<<count;
if(n > 15)
   cout<<" Number of elements greater than 15th position element is::"<<p15_count;
return;
}

void reverse()
{
   int count=0;
   for(int i=n-1; i<=0; i--)
   {
       new_arr[count]=arr[i];
   }
   print_arr(new_arr);
   return;
}

int main()
{
   int avg=0;
   int s_count=0;
   int l_count=0;
   int m_count=0;
   create_array(arr);
   print_arr(arr);
   avg=average(arr);
   cout<<" The average of the array is::"<<avg;
   s_count=smaller_count(avg);
   cout<<" Number of array elements smaller than average is::"<<s_count;
   l_count=larger_count(avg);
   cout<<" Number of array elements larger than average is::"<<l_count;
   odd_position();
   even_val_num();
   pos_neg_convert();
avg=average(arr);
   cout<<" The average of the new array is::"<<avg;
   swap();
   print_arr(arr);
   m_count=multiple_7_13();
   cout<<" Number of array elements which are multiple of 7 or 13 is::"<<m_count;
   largest_smallest();
   less30();
   greater75();
   create_new();
   print_arr(new_arr);
   reverse();

   return 0;
}