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

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++ 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:...
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
how to convert Sudo Code to C language of these two functions MsgEnv * request_msg_env( )...
how to convert Sudo Code to C language of these two functions MsgEnv * request_msg_env( ) { search for free memory block in the queue of the free blocks(shown in the data structure section ); if (no memory block is available) { Block invoking process(Process_switching(); } else { update the data structure; return a pointer to the memory block; } int release_msg_env( MsgEnv * msg_env_ptr ) : { if (memory block pointer is not valid) return ErrorCode; Add memory block...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT