Question

==> Please In C Language <== Insert a value in a sorted array: Given an integer...

==> Please In C Language <==

Insert a value in a sorted array:

Given an integer array a[9] = { 2, 3, 5, 7, 11, 13, 17, 19 }, read in an integer k from the screen and insert k into the array so the new array remains sorted. Note that array a has size 9, but there are only 8 initial values. This ensures that no value will be lost after the insertion.

For example, when k=12 , the new array will be {2, 3, 5, 7, 11, 12, 13, 17, 19 };

when k = -5, the new array will be { -5, 2, 3, 5, 7, 11, 13, 17, 19 };

when k= 100, the new array will be { 2, 3, 5, 7, 11, 13, 17, 19, 100 }.

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


Given below is the code for the question. Please do rate the answer if it helped. Thank you.


#include <stdio.h>

void print(int a[], int n);

int main(){
   int a[9] = { 2, 3, 5, 7, 11, 13, 17, 19 };
   int n = 8;
   int index, i;
   int k;
  
   printf("The array elements are: \n");
   print(a, n);
   printf("Enter a value to insert: ");
   scanf("%d", &k);
   //find the correct index at which to insert
   for(index = 0; index < n; index++){
       if(k < a[index])
           break;
   }
  
   //shift all elements to right to make space for new value
   for(i = n-1; i >= index; i--){
       a[i+1] = a[i];
   }
  
   //now store new value
   a[index] = k;
   n++; //increment no. of elements
  
   printf("After inserting %d the array elements are: \n", k);
   print(a, n);
   return 0;
}

void print(int a[], int n){
   int i;
   for(i = 0; i < n; i++){
       printf("%d ", a[i]);
   }
   printf("\n");
}

Add a comment
Know the answer?
Add Answer to:
==> Please In C Language <== Insert a value in a sorted array: Given an integer...
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
  • Special array removal: Given an integer array a[8]= {2,3,5,7,11,13,17,19}, read in an integer k from the...

    Special array removal: Given an integer array a[8]= {2,3,5,7,11,13,17,19}, read in an integer k from the screen. When k is a valid array index (i.e., between 0 and 7), move a[k] to the end of the array and push the array elements after a[k] one position up front. Print out the elements in the array after this removal. If k is not a valid array index, print out an error message. For example, when k=2, the array will be changed...

  • Using C++ Insert elements into a partially filled array. a) Create a partially filled array of...

    Using C++ Insert elements into a partially filled array. a) Create a partially filled array of CAPACITY 100. b) Initialize the array with values 10,20,30,40,50,60,70,80,90,100 c) Create a print function for the array. d) Create an insert function which inserts an integer into the array in sorted position. e) Insert the numbers 5, 150 and 55. f) Print the array before and after each insertion. Output Example 10 20 30 40 50 60 70 80 90 100 5 10 20...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • You are given an array containing integers in the sorted order: [O] [1] [2] [3] [4]...

    You are given an array containing integers in the sorted order: [O] [1] [2] [3] [4] [5] [6] [7] 6 9 17 29 33 41 58 61 [8] [9] [10] [11] [12] [13] 67 87 93 112 118 145 We want to find 12 in the given array, using binary search algorithm. The first comparison will be made with ----- 6 58 61 None. 12 is not in the array.

  • Write a mips program that defines two integer array that are pre-sorted and the same size...

    Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6,...

  • You will be given an array of N elements sorted small to large. For the X...

    You will be given an array of N elements sorted small to large. For the X and Y values ​​that satisfy the X ≤ Y condition, draw the flow diagram of the algorithm that finds the start and end addresses of the region with the numbers greater than X and less than Y with the divide and manage approach and with O (logN) complexity. Also code the algorithm in c language. (not c++) Example: A[0…8] array 3, 5, 7, 9,...

  • Define a function named insert that takes an integer atom and a sorted list as parameters....

    Define a function named insert that takes an integer atom and a sorted list as parameters. The function should return a list with the integer in the correct position in the sorted list. For example: insert [3, (2 4 6 8)] = (2 3 4 6 8) insert 9, ()] = (9) insert[3, (2 3 4 5)] = (23 3 4 5) Note that in the case of that last example, it does not matter which 3 comes first in...

  • (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection...

    (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection Sort • Rotates the sorted array around a random point, e.g., 1 2 3 4 5 à 3 4 5 1 2 is obtained by rotation around 3. • Searches for a key point in the rotated array in O(logn) time, where the rotation point is known in advance. • Repeats above by first finding the unknown rotation point via sequential search and binary...

  • Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine...

    Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine library procedures Objectives: 1. using register indirect addressing 2. passing parameters 3. generating “random” numbers 4. working with arrays Description: Write and test a MASM program to perform the following tasks: 1. Introduce the program. 2. Generate ARRAYSIZE random integers in the range [LO = 10 .. HI = 29], storing them in consecutive elements of an array. ARRAYSIZE should be set to 200....

  • Please answer using assembly language Easy68k ONLY. I. Write a program called Sorted Primes to a....

    Please answer using assembly language Easy68k ONLY. I. Write a program called Sorted Primes to a. Find out the prime numbers in a method from the following list (15, 16, 17, 23, 2, 32, 3, 31, 13, 19, 12, 9) and push/store them in a stack and then PRINT. (15 points) b. Sort those prime numbers passed by reference (using PEA) to a second method that you found out from the above list in descending order and then PRINT. (20...

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