Question

I need help writing these functions in C programming. Write a C function that checks if an array is sorted or not using an iterative approach. Write a C function that checks if an

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

int isSortedIter(int a[],int n)
{
int i;
for(i=0;i<n-1;i++)
{
if(a[i]>a[i+1])
return 0;
}
return 1;
}

b)

int isSortedRecur(int a[],int n,int ind)// pass initially ind as 0
{
if(ind==n-1)
{
return 1;
}
else
{
if(a[ind]>a[ind+1])
{
isSortedRecur(a,n,ind+1);
}
else
return 0;
}
}

c)

void reverse(int array[],int n)
{
int c,end=n-1;
for (c = 0; c < n/2; c++) {
int t = array[c];
array[c] = array[end];
array[end] = t;

end--;
}
}

d)

int sumElements(int a[],int n)
{
int i,sum=0;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
return sum;
}

Add a comment
Know the answer?
Add Answer to:
I need help writing these functions in C programming. Write a C function that checks if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Done in C++ using visual studio 1. Write a program with one additional function called int[]...

    Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...

  • USING C++: Write a function that given a 2D dynamic array of integers, will return the...

    USING C++: Write a function that given a 2D dynamic array of integers, will return the number of elements whose absolute value is smaller than a given value x. The function should take as arguments (in this order): a pointer to the array (i.e., 2D dynamic array) the number of rows the number of columns the given value The function should return the number of elements smaller in absolute value than epsilon E.g. if A={{0.2, -0.1, 3.4},{4.4, 0, -2}} and...

  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

  • I need help with a engineering problem solving with programming with C question, it says: Write...

    I need help with a engineering problem solving with programming with C question, it says: Write a function that fills a vector with zeros. Assume that the function prototype statement is: void zeros(int x[], int n); where x is a one-dimensional array with n elements. Give a reference to the function that fills positions 20 to 25 of an array with zeros.

  • Help me to write number 1 question using c programming 1. [10] Write three versions of...

    Help me to write number 1 question using c programming 1. [10] Write three versions of the function that computes f defined as follows: f(i) = f(i-1) + f(i-2) + f(i–3), and f(0) = 0, f(1) = 1, f(2)= 2, for i >=0, (1) purely recursive (2) iterative (3) a modified recursive function using recursion and an array to avoid redundant computation. 2. [15] Given a set I of n 16-bit ranges denoded by [bi, ei] for i = 0...

  • Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e....

    Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e. a two-dimensional array), number of rows and number of columns as input and prints the matrix after rotating it 90 degrees clockwise. Example: void rotate-matrix (int* matrix, int row, int column) Inputs: row 4, column-6 and the pointer to the matrix below: 2 3 6 5 8 0 Output:

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT