0% found this document useful (0 votes)
13 views

Bài CH A

This C code defines functions for manipulating arrays: - InputArray() prompts the user to enter the number of elements and values for a new array - OutputArray() prints the contents of an array - InsertEND() adds a new element to the end of an array - DeleteIndex() removes the element at a given index from an array - SortArray() and SortArrayG() sort an array in ascending and descending order respectively - SearchElement() searches an array for a given element and prints its indices

Uploaded by

Cương Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Bài CH A

This C code defines functions for manipulating arrays: - InputArray() prompts the user to enter the number of elements and values for a new array - OutputArray() prints the contents of an array - InsertEND() adds a new element to the end of an array - DeleteIndex() removes the element at a given index from an array - SortArray() and SortArrayG() sort an array in ascending and descending order respectively - SearchElement() searches an array for a given element and prints its indices

Uploaded by

Cương Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include "Array.

h"
#include <stdio.h>
/*******************************************************************************
* Definitions
******************************************************************************/

/*******************************************************************************
* Prototypes
******************************************************************************/

/*******************************************************************************
* Variables
******************************************************************************/

/*******************************************************************************
* Code
******************************************************************************/
void InputArray(List *ptr)
{
int i;
char c;
int state=0;
ptr->array[0]='\0';
/* Type number of elements in the array*/
printf("\nNhap so luong phan tu cua mang ( 1-> %d): ",N);
do{
fflush(stdin);
if(scanf("%d%c", &ptr->NumMember, &c) != 2 || c != '\n' || ptr->NumMember
>100 || ptr->NumMember <=0)
{
printf("The input value is not valid! Please re-enter: ");
}
else
{
state =1;
}

}while(state != 1);
state = 0;
/* Type the data for the array*/
for(i=0;i < ptr->NumMember; i++)
{
printf("Enter the element value %d: ",i);
do{
fflush(stdin);
if(scanf("%d%c", &ptr->array[i], &c) != 2 || c != '\n')
{
printf("Invalid element value %d! Please re-enter: ",i);
}
else
{
state =1;
}
}while(state != 1);
state =0;
}
printf("Enter array data successfully!");
}
void OutputArray(List* ptr)
{
int i;
if(0 == ptr->NumMember) /*check empty array ? */
{
printf("\nEmpty array!");
}
else
{
for (i=0; i < ptr->NumMember; i++)
{
printf("\nElement value %d: %d", i, ptr->array[i]);
}
}
}
void InsertEND(List* ptr)
{
char ch;
int state =0;
if(N == ptr->NumMember) /* check full array ? */
{
printf("\nArray is full, cannot be inserted!");
}
else{
printf("\nEnter the number to insert: ");
do{
fflush(stdin);
if(scanf("%d%c", &ptr->array[ptr->NumMember], &ch) != 2 || ch != '\n')
{
printf("The input value is not valid! Please re-enter: ");
}
else
{
state = 1;
}
}while(state != 1);
ptr->NumMember++;
printf("Add value successfully!");
}
}
void DeleteIndex(List* ptr)
{
int Num = ptr->NumMember;
int i;
int val;
int k;
int state = 0;
char ch;
if(0== ptr->NumMember) /*check empty array ?*/
{
printf("\nEmpty array!");
}
else
{
/*Enter the position to delete*/
printf("\nEnter the position to delete (0 -> %d): ",Num-1);
do{
fflush(stdin);
if(scanf("%d%c", &k, &ch) != 2 || ch != '\n')
{
printf("The input value is not valid! Please re-enter: ");
}
else
{
state = 1;
}
}while(state != 1);
/* Delete element*/
if(k>=0 && k<= Num-1)
{
for(i=k;i<=Num-2;i++)
{
ptr->array[i]=ptr->array[i+1];
}
ptr->NumMember = Num-1;
printf("Delete element %d successfully!",k);
}
else
{
printf("Array has no element %d!",k);
}
}
}
void SortArray(List* ptr)
{
int i;
int j;
int val;
for (i=0; i< ptr->NumMember-1; i++)
{
for(j=i+1; j< ptr->NumMember; j++)
{
if(ptr->array[i]> ptr->array[j] )
{
val= ptr->array[i];
ptr->array[i]=ptr->array[j];
ptr->array[j]=val;
}
}
}
printf("\nSort array by ascending value successfully!");
}
void SortArrayG(List* ptr)
{
int i;
int j;
int val;
for (i=0; i< ptr->NumMember-1; i++)
{
for(j=i+1; j< ptr->NumMember; j++)
{
if(ptr->array[i]< ptr->array[j] )
{
val= ptr->array[i];
ptr->array[i]=ptr->array[j];
ptr->array[j]=val;
}
}
}
printf("\nSort array by descending value successfully!");
}
void SearchElement(List* ptr)
{
int i;
int Index[ptr->NumMember];
int count=0;
int Elem;
int state = 0;
char ch;
/* Enter the number to search */
printf("\nEnter the number to search: ");
do{
fflush(stdin);
if(scanf("%d%c", &Elem, &ch) != 2 || ch != '\n')
{
printf("The input value is not valid! Please re-enter: ");
}
else
{
state = 1;
}
}while(state != 1);
/* Find the position and print to the screen*/
for(i=0; i<ptr->NumMember;i++)
{
if(ptr->array[i] == Elem)
{
Index[count]=i;
count++;
}
}
if(0 == count)
{
printf("Element %d not found!", Elem);
}
else
{
printf("The position of the number %d in the array is: ", Elem);
for(i=0;i<count;i++)
{
printf("%d\t",Index[i]);
}
}
}

You might also like