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
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.
The data in ArrayV2.dat is:
24
-43
-63
-72
73
-72
7
-23
36
54
32
-97
7
9
82
5
4
38
27
8
-83
22
27
-64
7
-44
26
45
-66
83
-62
52
-7
-36
-62
78
-67
34
81
-3
2
27
11
-63
-11
2
-66
Hi, I have a problem with my code. A tutor was able to solve this and it computed just fine on her xCode, but when I tried to copy and paste her same exact code in my xCode, I got totally different results. Do you think you could help me straighten out what I am doing wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void printArray(int A[], int n)
{
for(int i = 0; i < n; i++)
{
cout<<setw(5)<<A[i]<< " ";
if((i+1) % 10 == 0)
cout<<endl;
}
}
double avgCalc(int A[], int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
sum += A[i];
return sum / n;
}
int aboveAvgCount(int A[], int n, double avg)
{
int count = 0;
for(int i = 0; i < n; i++)
if(A[i] > avg)
count++;
return count;
}
int evenValuesCount(int A[], int n)
{
int count = 0;
for(int i = 0; i < n; i++)
if(A[i] % 2 == 0)
count++;
return count;
}
void changeSign(int A[], int n)
{
for(int i = 0; i < n; i++)
A[i] *= -1;
}
void swapFirstAndLast(int A[], int n)
{
int temp = A[0];
A[0] = A[n-1];
A[n-1] = temp;
}
int main()
{
//1. Read in the data elements into the array
int Array[50];
//The input is found in arrayV2.dat
ifstream fin;
fin.open("ArrayV2.dat");
int count = 0;
while(!fin.eof() && count < 50)
fin>>Array[count++];
//2. Print out all the data elements in the array with no more than 10 numbers per output line.
cout<<"The elements in the array are: "<<endl;
printArray(Array, count);
cout<<endl;
//3. Print out the average of the array.
cout<<"The average of elements in the array is: "<<setprecision(2)<<avgCalc(Array, count)<<endl;
//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.
int numOfElementsAboveAvg = aboveAvgCount(Array, count, avgCalc(Array, count));
cout<<"The number of elements above average is: "<<numOfElementsAboveAvg<<endl;
cout<<"The number of elements below average is: "<<count - numOfElementsAboveAvg<<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.
int temp = 0;
for(int i = 0; i < count; i += 2)
{
cout<<setw(5)<<Array[i]<<" ";
temp++;
if(temp % 10 == 0)
{
temp = 0;
cout<<endl;
}
}
cout<<endl;
//6. Print out the number data elements in the array that are even values in the array (one value).
cout<<"The number of data elements in the array that are even values is: "<<evenValuesCount(Array, count)<<endl;
//7. Convert every negative data elements to a positive number and every positive data element to a negative number.
changeSign(Array, count);
//8. Print out the average of the new array.
cout<<"The average of elements in the new array is: "<<setprecision(2)<<avgCalc(Array, count)<<endl;
//9. Swap the first data element in the array with the last data element in the array. Print out the array.
swapFirstAndLast(Array, count);
cout<<"The elements in the new array are: "<<endl;
printArray(Array, count);
cout<<endl;
|
Here is the output I get:
The elements in the array are:
0 0 0 0 0 0 0 0 -86467113 -469714510
1606416560 32767 17 0 1 0 104 1 1606578991 32767
1606416304 32767 1606423158 32767 1606416328 32767 1606416328 32767 4 0
1606416352 32767 0 1 0 0 1606416392 32767 0 0
0 0 0 0 0 0 0 0 1606416336 32767
The average of elements in the array is: 2.8e+08
The number of elements above average is: 9
The number of elements below average is: 41
0 0 0 0 -86467113 1606416560 17 1 104 1606578991
1606416304 1606423158 1606416328 1606416328 4 1606416352 0 0 1606416392 0
0 0 0 0 1606416336
The number of data elements in the array that are even values is: 35
The average of elements in the new array is: -2.8e+08
The elements in the new array are:
-32767 0 0 0 0 0 0 0 86467113 469714510
-1606416560 -32767 -17 0 -1 0 -104 -1 -1606578991 -32767
-1606416304 -32767 -1606423158 -32767 -1606416328 -32767 -1606416328 -32767 -4 0
-1606416352 -32767 0 -1 0 0 -1606416392 -32767 0 0
0 0 0 0 0 0 0 0 -1606416336 0
Program ended with exit code: 0