Question

In: Computer Science

Please write code for C language Problem: Write a couple of functions to process arrays. Note...

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.

  1. display(): The function takes an int array and it’s size and prints the data in the array.

  2. sumArray(): It takes an int array and size, and returns the sum of the elements of the array.

  3. findMax(): It takes an int array and size, and returns the maximum value in the array.

  4. 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

Solutions

Expert Solution

#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.


Related Solutions

Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking...
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking integers until the user enters -100. In the end, the program should display the count of positive, negative (excluding that -100) and zeros entered. Sample Input/Output 1: Input the number: 0 2 3 -9 -6 -4 -100 Number of positive numbers: 2 Number of Negative numbers: 3 Number of Zero: 1
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...
Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART A: (Minusculo Inn) Minusculo Inn has only eight guest rooms: Room Number Amenities 101 1 king size bed 102, 103,104 2 double beds 201, 202 1 queen size bed 203, 204 1 double bed & sofa bed Write a program that asks for the room number and displays the amenities. If the user enters a room number that does...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: ---the code can be general (can be tested with any int main() test function)--- Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT