You are to write a program that will manipulate an array. Your program should ha
ID: 3782878 • 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 and11?
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.
Numbers for the array
24
43
63
-72
73
72
77
23
36
94
22
58
-55
-34
-41
28
93
9
92
-8
83
-36
73
5
38
56
95
96
11
3
6
-5
17
93
80
99
26
19
62
72
8
4
48
21
54
84
32
97
74
94
82
57
4
38
27
83
83
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
#include <stdio.h>
#include <stdlib.h>
void print(int *array, int size)
{
int i = 0;
for(i = 0; i < size; i++)
{
printf("%d ", array[i]);
if ((i+1) % 10 == 0)
{
printf(" ");
}
}
}
void printDouble(double value)
{
printf("Average: %f ", value);
}
void printInt(int value)
{
printf("%d ", value);
}
double computeAndPrintAverage(int *array, int size)
{
double sum = 0;
int i = 0;
for(i = 0; i < size; i++)
{
sum += array[i];
}
double average = sum/size;
printDouble(average);
return average;
}
void printLargeSmallCount(int *array, int size, double average)
{
int i = 0;
int large = 0;
int small = 0;
for(i = 0; i < size; i++)
{
if (array[i] < average)
{
small++;
}
if(array[i] > average)
{
large++;
}
}
printInt(small);
printInt(large);
}
void convertNegativeToPositive(int *array, int size)
{
int i = 0;
for(i = 0; i < size; i++)
{
if(array[i] < 0)
{
array[i] = -1*array[i];
}
}
print(array, size);
}
void printMultiple(int *array, int size)
{
int i = 0;
int count = 0;
for(i = 0; i < size; i++)
{
if (((array[i] % 4) == 0 )|| ((array[i] % 11) == 0 ))
{
count++;
}
}
printInt(count);
}
void printlargest(int *array, int size)
{
int i = 0;
int largest = array[0];
int index = 0;
for(i = 1; i < size; i++)
{
if (array[i] > largest)
{
largest = i;
index = i;
}
}
printInt(i);
printInt(index);
}
void printsmallest(int *array, int size)
{
int i = 0;
int smallest = array[0];
int index = 0;
for(i = 1; i < size; i++)
{
if (array[i] < smallest)
{
smallest = i;
index = i;
}
}
printInt(i);
printInt(index);
}
void madeEvenZero(int *array, int size)
{
int i = 0;
for(i = 1; i < size; i++)
{
if (array[i] % 2 == 0)
{
array[i] = 0;
}
}
print(array, size);
}
int main()
{
int array[100];
FILE *fp = fopen("array.data", "r");
int i = 0;
while(!feof(fp))
{
int value;
fscanf(fp, "%d", &value);
if (value == 0)
{
break;
}
array[i] = value;
i++;
}
int size = i;
print(array, size);
double average = computeAndPrintAverage(array, size);
printLargeSmallCount(array, size, average);
convertNegativeToPositive(array, size);
average = computeAndPrintAverage(array, size);
printMultiple(array, size);
printlargest(array, size);
printsmallest(array, size);
madeEvenZero(array, size);
average = computeAndPrintAverage(array, size);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.