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: 3662850 • 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/JFqFZaTU)

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

Answer:

#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;

//2. Print out all the data elements in the array with no more than 10 numbers per output line.
void printArrayvalues(int intArrayLen,int* intArray)
{
   for(int k1=0;k1<intArrayLen;k1++)
   {
       if(k1%10==0)
           cout<<" "<<intArray[k1]<<" ";
       else
           cout<<intArray[k1]<<" ";
   }
}
//3. Print out the average of the array.
float averageArray(int intArrayLen,int* intArray)
{
  
   float arrayTotal=0;
   for(int k1=0;k1<intArrayLen;k1++)
   {
       arrayTotal+=intArray[k1];
   }
   return arrayTotal/intArrayLen;
}
//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.
void printAboveBelowAverage(int intArrayLen,float average,int* intArray)
{
  
   int aboveCount=0,belowCount=0;
   for(int k1=0;k1<intArrayLen;k1++)
   {
       if(intArray[k1]>average)
           aboveCount++;
       if(intArray[k1]<average)
           belowCount++;
   }
   cout<<"No of elements above average:"<<aboveCount<<endl;
   cout<<"No of elements below average:"<<belowCount<<endl;
}
//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.
void printOddElement(int intArrayLen,int* intArray)
{
  
   int c=0;
   cout<<endl;
   for(int k1=0;k1<intArrayLen;k1++)
   {
       if(k1%2==1)
       {
           if(c%10==0)
           {
               cout<<" "<<intArray[k1]<<" ";
               c++;
           }
           else
           {
               cout<<intArray[k1]<<" ";
               c++;
           }
       }
   }
}
//6. Print out the number data elements in the array that are even values in the array (one value).
void printEvenElementsCount(int intArrayLen,int* intArray)
{
  
   int evenElementCount=0;
   for(int k1=0;k1<intArrayLen;k1++)
   {
       if(intArray[k1]%2==0)
           evenElementCount++;
   }
   cout<<"No of Even elements "<<evenElementCount<<endl;
}  
//7. Convert every negative data elements to a positive number and every positive data element to a negative number.
int * convertArray(int intArrayLen,int* intArray)
{
  
       for(int k1=0;k1<intArrayLen;k1++)
   {
       if(intArray[k1]>0)
           intArray[k1]=-intArray[k1];
       else if(intArray[k1]<0)
           intArray[k1]=-intArray[k1];
       else if(intArray[k1]==0)
           intArray[k1]=intArray[k1];
   }
   return intArray;
}
/*8. Print out the average of the new array.*/
void printAverageNewArray(int intArrayLen,int* intArray)
{
   cout<<"Average of converted Array is"<<averageArray(intArrayLen,intArray)<<endl;
}

/*9. Swap the first data element in the array with the last data element in the array. Print out the array.*/
void swapFirstLast(int* intArray,int intArrayLen)
{
  
   int t=intArray[0];
   intArray[0]=intArray[intArrayLen-1];
   intArray[intArrayLen-1]=t;
}
/*10. How many data elements in the array are multiples of either 7 or 13?*/
int findMultiples(int intArrayLen,int *intArray)
{
  
   int intMultipleCount=0;
   for(int k1=0;k1<intArrayLen;k1++)
   {
       if((intArray[k1]%7==0) || (intArray[k1]%13==0))
           intMultipleCount++;
   }
   return intMultipleCount;
}

/*11. Find the largest data element and print out the data element and its array ‘position’.*/
void printLargestDataElement(int intArrayLen,int* intArray)
{
  
   int intLargestDataElement=0,k1;
   for( k1=0;k1<intArrayLen;k1++)
   {
       if(intArray[k1]>intLargestDataElement)
       {
           intLargestDataElement=intArray[k1];
           break;
       }
   }
   cout<<endl;
   cout<<"largest Data element:"<<intLargestDataElement<<"found at place "<<k1<<endl;
}
/* 12. Find the smallest data element and print out the data element and its array ‘position’. */
void printSmallestDataElement(int intArrayLen,int* intArray)
{
  
   int intSmallestDataElement=intArray[0],k1;
   for( k1=0;k1<intArrayLen;k1++)
   {
       if(intArray[k1]<intSmallestDataElement)
       {
           intSmallestDataElement=intArray[k1];
           break;
       }
   }
   cout<<endl;
   cout<<"Smallest Data element:"<<intSmallestDataElement<<"found at place "<<k1<<endl;
}

int main()
{
   int intArray[50];
   int k1=0;
   ifstream inReadFile("arrayV2.dat");
   if(inReadFile.is_open())
   {
       k1=0;
       while(inReadFile>>intArray[k1])
       {
           k1++;
       }
      
   }

   inReadFile.close(); //read array
   printArrayvalues(k1,intArray); //print array
   cout<<endl;
   float avgg=averageArray( k1,intArray);
   cout<<"Average Array value:"<<avgg<<endl; //print average
   printAboveBelowAverage(k1,avgg,intArray); //count of elements above & below average
   printOddElement(k1,intArray);//print odd place element
   printEvenElementsCount(k1,intArray); //count of even element
   //intArray=convertArray(intArray,k1); //convert array +ve to -ve and -ve to +ve
   printAverageNewArray(k1,intArray); //AVERAGE OF CONVERTED ARRAY
   swapFirstLast( intArray,k1);// swap first & last element
   cout<<"No of multip-les of 7 or 13 is:"<<findMultiples(k1,intArray)<<endl;
   printLargestDataElement(k1,intArray);
   printSmallestDataElement(k1,intArray);
   system("pause");
   return 0;
}