You will write a main that declares an array of size 100. In the main you will c
ID: 674487 • Letter: Y
Question
You will write a main that declares an array of size 100. In the main you will call three functions. They are
int readData (int aR[ ], int maxSize);
void SumsDigit (int aR[ ], int size);
double Average (int aR[ ], int size);
The first function readData is such that using a while loop will read integers from the keyboard and store them in the array aR (that is passed to the function). Our goal is to limit ourselves to two digit integers..
The while loop will be controlled by an integer identifier called flag which is initialized to be 1. While the flag is one we do the following:
Read an integer
For each read, which is performed by scanf, we will check to see if an integer was read.
If an integer is not read then we will break out of the loop.
If the integer read is >= 100 then we set the flag to 0 and do not insert the integer in the array.
If the integer is negative then we will skip the data and so we do not insert the integer in the array, but do continue the loop.
Remember to count the number of integers assigned to the array. Once we have exited the while loop we will return the number of integers assigned to the array to main.
In the second function SumsDigit we will compute the sum of digits based on their column position of the passed array. So we will have a total of tens and a total of ones (see below).
47
98
23
5
sum of ones is 23
sum of tens is 15
In this function you should output the sum of each digit in a formatted manner and return to main.
In the third function Average you compute the average of the integers in the array passed and return this average to the main.
In your main you should call each of the three functions, storing any returned value. After you call Average you should display the average in a formatted manner.
Above should be programmed in ONLY C lanuaguge
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int readData (int aR[ ], int maxSize);
void SumsDigit (int aR[ ], int size);
double Average (int aR[ ], int size);
int main()
{
int aR[100], size, maxSize = 100;
size = readData(aR, maxSize);
SumsDigit(aR, size);
printf("The average of the given array of numbers is: %.2lf ",Average(aR, size));
}
int readData (int aR[ ], int maxSize)
{
char str[10];
int flag = 1, num, count = 0;
while(flag == 1 && count < maxSize)
{
scanf("%s", str);
if((num = atoi(str)) == 0)
break;
if(num >= 100)
flag = 0;
else if(num < 0);
else
aR[count++] = num;
}
return count;
}
void SumsDigit (int aR[ ], int size)
{
int i, unitsSum = 0, tensSum = 0;
for(i = 0; i < size; i++)
{
if(aR[i] > 9)
unitsSum += aR[i] % 10;
else
unitsSum += aR[i];
tensSum += aR[i] / 10;
}
printf("Sum of ones is: %i ", unitsSum);
printf("Sum of tens is: %i ", tensSum);
}
double Average (int aR[ ], int size)
{
int i, sum = 0;
for(i = 0; i < size; i++)
sum += aR[i];
return sum/(double)size;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.