the question is
using pointer variable:
a)write a program that receives a sorted array of integer.then it will read an integer value and insert that integer value in correct place
(using pointer variable)
-is it how im write the code right.

b)improve the first program code by allowing the program to just read an integer and insert it in its correct place.the program will continue to read until the user exit the program(must be menu driven)(p/s: i dont know how to improve this program..please help me )

for question (a)
-is it how im write the code right.i mean the pointer variable

#include<stdio.h>

int main()
{
	const int arraySize = 5;
	int data[arraySize] = {2,3,5,4,1};
	int *insert;//temporary variable to hold element to insert
	int a;
	insert = &a;

	//original array
	printf("\nUnsorted Array = ");
	for(int i=0;i < arraySize;i++)
		printf("%3d", data[i]);

	//insertion sort
	//loop over the element of array
	for( int next = 1;next < arraySize;next++)
	{
		a = data[ next ];//store the value in current element
		int moveItem = next;//initialize location to place element

		//search for the location to in which to put current element
	while((moveItem > 0)&&(data[moveItem - 1] > a))
	{
		//shift element one slot to the right
		data[moveItem]=data[moveItem - 1];
		moveItem--;
	}//end while

	data[ moveItem ] = a;//place inserted element into the array
	}//end for


	//output sorted array
	printf("\nSorted array = ");
	for(i = 0;i< arraySize; i++)
	printf("%3d",data[i]);

	return 0;
	}

Recommended Answers

All 3 Replies

Your array size is 5 and you want to continuously read the user input till the user exits, how will there be sufficient space in the array ?

actually it not the problem what is size of my array i just want to know how how to write the code

Well suppose after the first sort, this is your input array

int arr[10]={1,2,3,7,8};

if the user enters 6, all you got to do is move the elements 8,7 one step behind. You see 8 is greater than 6 copy 8 to the 6th location. Same applies for 7. 3 is not greater than 6, so the loop ends there

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.