I have a question about programming in C(89) C. Could someone please show me how to make a program that uses a custom function that rearranges array elements from one array of size n into a new larger second array of size (n+1) and places a new element into the new second larger array at a particular index location? For example, the user would be prompted for a size like say, 7. Then the user would be prompted for the seven array element values, which could be like {1, 7, 4, 6, 3, 9, 8}. Then the user gets prompted again for a new element value to add to the new larger second array. Let's say the new value is entered as 5. All of this would be done in main and no pointers are required. Then main calls a custom function called rearrange. the prototype for rearrange must be; void rearrange(int *a, int n, int insert, int *b);   After rearrange is done, main would display the contents of the new larger second array like so;   {1, 4, 3, 5, 7, 9, 8} Here's the trick though. Please notice how all the numbers lower than the new element 5 are still in the same order as the first array and that all the elements with values higher than 5 are in the same order as the original array. Here are my feelings about the logic of the custom function rearrange. I feel like a counter variable counts the number of elements lower than the new value (say 5) so that the new value (5) can be placed at the correct index location (3) in the new array named b. I think that a while loop with a conditional if could be used with a pointer to guide the elements of the first array a that are less than or equal to the new value (the 5) into the new larger second array named b. And finally, another while loop with a conditional if walks through the original array a again, guiding the elements with values that greater than the newly placed element (the 5) into the last part of the new larger second array named b. The contents of the new array b would then be displayed back in the main function.  That's the logic I would like to see if it could work, but I can't juggle pointers. I just need to see this one in action. If I could see this work it would really help me wrap my head around the relationship between array names and pointers that use *p++ style of coding in C. Many, many thanks for reading this far and to the ones who can offer help. I would greatly appreciate it.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 44SA
icon
Related questions
Question
100%

I have a question about programming in C(89) C.

Could someone please show me how to make a program that uses a custom function that rearranges array elements from one array of size n into a new larger second array of size (n+1) and places a new element into the new second larger array at a particular index location?

For example, the user would be prompted for a size like say, 7. Then the user would be prompted for the seven array element values, which could be like {1, 7, 4, 6, 3, 9, 8}.

Then the user gets prompted again for a new element value to add to the new larger second array. Let's say the new value is entered as 5. All of this would be done in main and no pointers are required.

Then main calls a custom function called rearrange. the prototype for rearrange must be;

void rearrange(int *a, int n, int insert, int *b);
 
After rearrange is done, main would display the contents of the new larger second array like so;  

{1, 4, 3, 5, 7, 9, 8}

Here's the trick though. Please notice how all the numbers lower than the new element 5 are still in
the same order as the first array and that all the elements with values higher than 5 are in the same order as the original array.

Here are my feelings about the logic of the custom function rearrange.

I feel like a counter variable counts the number of elements lower than the new value (say 5) so that the new value (5) can be placed at the correct index location (3) in the new array named b.

I think that a while loop with a conditional if could be used with a pointer to guide the elements of the first array a that are less than or equal to the new value (the 5) into the new larger second array named b.

And finally, another while loop with a conditional if walks through the original array a again, guiding the elements with values that greater than the newly placed element (the 5) into the last part of the new larger second array named b.

The contents of the new array b would then be displayed back in the main function. 

That's the logic I would like to see if it could work, but I can't juggle pointers. I just need to see this one in action. If I could see this work it would really help me wrap my head around the relationship between array names and pointers that use *p++ style of coding in C.

Many, many thanks for reading this far and to the ones who can offer help. I would greatly appreciate it.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr