In: Computer Science
Please write code for C language
Problem:
Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter.
display(): The function takes an int array and it’s size and prints the data in the array.
sumArray(): It takes an int array and size, and returns the sum of the elements of the array.
findMax(): It takes an int array and size, and returns the maximum value in the array.
getDouble(): it takes an int data (not an array) and returns the double of the data passed to it. For example, if you pass 5, it returns 10 (as 10 = 5*2)
In the main function:
#define SIZE 5 //do it in the global space after including the header file
Declare an array of size SIZE and take user inputs to store in the array.
Then call the display function with the array that prints the array
Then call sumArray() function and print the sum returned from the sumArray() function
Then call findMax() function and print the max value returned from the function
Then for each element of the array call the getDouble() function and print the returned value in the main function
Now, declare another array of the same size and put the data from your first array to the second array in reverse order.
Call all the functions above using the second array and they should also display the same information except the display function that display the data in reverse order.
Sample input/output:
Enter number 1: 50
Enter number 2: 40
Enter number 3: 30
Enter number 4: 20
Enter number 5: 10
50 40 30 20 10
The sum of myArray is: 150
The max value of myArray is: 50
Double of 50 is 100
Double of 40 is 80
Double of 30 is 60
Double of 20 is 40
Double of 10 is 20
Now the following data are from RevArray
10 20 30 40 50
The sum of revArray is: 150
The max value of revArray is: 50
Double of 10 is 20
Double of 20 is 40
Double of 30 is 60
Double of 40 is 80
Double of 50 is 100
#include <stdio.h>
#define SIZE 5
void display(int array[])
{
for(int i=0;i<SIZE;i++)
printf("%d ",array[i]);
}
int sumArray(int array[])
{
int sum = 0;
for(int i=0;i<SIZE;i++)
sum = sum+array[i];
return sum;
}
int findMax(int array[])
{
int max = 0;
for(int i=0;i<SIZE;i++)
{
if(max <
array[i])
max =
array[i];
}
return max;
}
int getDouble(int data)
{
return data*2;
}
int main(void) {
int array[SIZE];
for(int i=0;i<SIZE;i++)
{
printf("\nEnter number %d:",(i+1));
scanf("%d",&array[i]);
}
printf("\n");
display(array);
printf("\nThe sum of myArray is: %d",sumArray(array));
printf("\nThe max value of myArray is: %d",findMax(array));
for(int i=0;i<SIZE;i++)
{
printf("\nDouble of %d is %d",array[i],getDouble(array[i]));
}
int RevArray[SIZE];
for(int i=0;i<SIZE;i++)
{
RevArray[i] = array[SIZE-i-1];
}
printf("\nNow the following data are from RevArray\n");
display(RevArray);
printf("\nThe sum of myArray is: %d",sumArray(RevArray));
printf("\nThe max value of myArray is: %d",findMax(RevArray));
for(int i=0;i<SIZE;i++)
{
printf("\nDouble of %d is
%d",RevArray[i],getDouble(RevArray[i]));
}
return 0;
}
Output:
Enter number 1:50 Enter number 2:40 Enter number 3:30 Enter number 4:20 Enter number 5:10 50 40 30 20 10 The sum of myArray is: 150 The max value of myArray is: 50 Double of 50 is 100 Double of 40 is 80 Double of 30 is 60 Double of 20 is 40 Double of 10 is 20 Now the following data are from RevArray 10 20 30 40 50 The sum of myArray is: 150 The max value of myArray is: 50 Double of 10 is 20 Double of 20 is 40 Double of 30 is 60 Double of 40 is 80 Double of 50 is 100
Do ask if any doubt. Please up-vote.