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: 3662716 • 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/6a8ZGJix)

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

#include <iostream>
#include <fstream>

using namespace std;
int countLine = 0;
int intArr[50];
int *originalArr;

void readData()
{
   fstream file("d:\ArrData.txt");
  

   while (!file.eof())
   {
       file >> intArr[countLine];
       countLine++;
   }
  
   originalArr = new int[countLine];
   for (int i = 0; i < countLine; i++)
   {
       originalArr[i] = intArr[i];
   }
}

void printAllData()
{
   int line=0;
   cout << endl << "Array Data : " << endl;
   for (int i = 0; i < countLine; i++)
   {
       line++;
       cout << originalArr[i]<<" ";
       if(line%10==0)
       cout<<endl;
   }
}

double average()
{
   double sum=0;
   for (int i = 0; i < countLine; i++)
   {
       sum=sum+originalArr[i];
   }
   return (sum/countLine);
}

int countLessThanAvg()
{
   double avg=average();
   int count=0;
   for (int i = 0; i < countLine; i++)
   {
       if(originalArr[i]<avg)
       {
           count++;
       }
   }
   return count;
}

int countGreaterThanAvg()
{
   double avg=average();
   int count=0;
   for (int i = 0; i < countLine; i++)
   {
       if(originalArr[i]>avg)
       {
           count++;
       }
   }
   return count;
}

void printDataOdd()
{
   int line=0;
   cout << endl << "Array Data at Odd : " << endl;
   for (int i = 0; i < countLine; i=i+2)
   {      
           line++;
           cout << originalArr[i]<<" ";
           if(line%10==0)
               cout<<endl;
   }
}

int countEvenValues()
{
   int count=0;
  
   for (int i = 0; i < countLine; i++)
   {  
   if(originalArr[i]%2==0)
       count++;
   }
  
   return count;
}

int main(void)
{
  
   readData();           //Q.1 Input Data from file.

   printAllData();       //Q.2 Print All Data.
  
   cout<<"Average : "<<average()<<endl;    //Q.3. Calculate Average of Array.
  
   cout<<"Numbers Less Than Average : "<<countLessThanAvg()<<endl;       //Q.4. Count Less than Average

   cout<<"Numbers Greater Than Average : "<<countGreaterThanAvg()<<endl;       //Q.4. Count Greater than Average  
  
   printDataOdd();       //Q.5 Print Data At odd position
  
   cout<<" Count Even Values : "<<countEvenValues()<<endl;   //Q.6. Count Even Values
   return 0;
}