Uploaded by han xia

CISC1001 Assignment7

advertisement
Assignment:
Need to put source code into a word file and upload it to UMMOODLE.
A[Max] is defined as an array of integers. Suppose the integers are
sorted in increasing order and Num is the number of integers. Write a function
SortedInsert that inserts one integer X into the array and increases Num by 1.
Example: A={1,3,5,7,9}, Max=10, Num=5, X=6,
after insert A={1,3,5,6,7,9}, Num=6
#include <stdio.h>
int SortedInsert(int Num,int MAX,int A[MAX], int X);
int main()
{
int Num,MAX,X;
int A[MAX];
printf("MAX:");
scanf("%d",&MAX);
printf("Num:");
scanf("%d",&Num);
printf("X:");
scanf("%d",&X);
printf("Enter A:");
for(int i=0;i<Num;i++)
{
scanf("%d",&A[i]);
}
SortedInsert(Num,MAX,A,X);
}
int SortedInsert(int Num, int MAX,int A[MAX], int X)
{
for(int i = 0;i < Num; i++)
{
if(X <= A[i])
{
for(int j=Num; j >= i+1 ;j--)
{
A[j] = A[j-1];
}
A[i] = X;
Num +=1;
break;
}
}
for(int i=0;i<= Num;i++)
{
printf("%d",A[i]);
}
}
Download