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.
I have a question about
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;
{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.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images