0% found this document useful (0 votes)
17 views12 pages

New 14

The document contains C code that defines functions to manage an integer array including inputting/outputting array data, inserting/deleting elements, sorting, and searching. A main function provides a menu to call these array functions and switch between them.

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)
17 views12 pages

New 14

The document contains C code that defines functions to manage an integer array including inputting/outputting array data, inserting/deleting elements, sorting, and searching. A main function provides a menu to call these array functions and switch between them.

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/ 12

[Yesterday 9:27 PM] LINH TRAN

#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]);

[Yesterday 9:27 PM] LINH TRAN

file.h

[Yesterday 9:27 PM] LINH TRAN

#ifndef H_ARRAY_H

#define H_ARRAY_H

/*******************************************************************************

* Definitions

******************************************************************************/

#define N 100

typedef struct List

int array[N]; /* Array */

int NumMember; /* number of elements in the array */

}List;
/*******************************************************************************

* API

******************************************************************************/

void InputArray(List* ptr); /*Type the data for the array*/

void OutputArray(List* ptr); /*Print the data for the array*/

void InsertEND(List* ptr); /*Add 1 element to the end of the array*/

void DeleteIndex(List* ptr); /* Delete an element at position k*/

void SortArray(List* ptr); /*Sort array by ascending value*/

void SortArrayG(List* ptr); /* Sort array by descending value*/

void SearchElement(List* ptr); /*Searching for a number x in the array*/

#endif /*H_ARRAY_H*/

[Yesterday 9:27 PM] LINH TRAN

main.c

[Yesterday 9:27 PM] LINH TRAN

#include <stdio.h>

#include "Array.h"

#include <string.h>

/*******************************************************************************

* Definitions

******************************************************************************/

/*******************************************************************************

* Prototypes

******************************************************************************/

/*******************************************************************************

* Variables

******************************************************************************/
/*******************************************************************************

* Code

******************************************************************************/

int main() {

List array;

char choice[10];

int state =0;

do {

system("cls");

printf("========== Program to manage numbers using arrays ==========");

printf("\n\tEnter 'c': Type the data for the array");

printf("\n\tEnter 'p': Print the data for the array");

printf("\n\tEnter 'i': Add 1 element to the end of the array");

printf("\n\tEnter 'd': Delete an element at position k");

printf("\n\tEnter 's': Sort array by ascending value");

printf("\n\tEnter 'x': Sort array by descending value");

printf("\n\tEnter 't': Searching for a number x in the array");

printf("\n\tEnter 'e': Exit program");

printf("\n================================================");

printf("\nEnter selection: ");

do{

gets(choice);

if(1 == strlen(choice))

state =1;

else

printf("Please re-enter selection: ");

}
}while(state !=1);

state =0;

switch(choice[0]) {

case 'c':

printf("\t------- Type the data for the array ---------\n");

InputArray(&array);

fflush(stdin);

getch();

break;

case 'p':

printf("\t------- Print the data for the array ---------\n");

OutputArray(&array);

fflush(stdin);

getch();

break;

case 'i':

printf("\t---- Add 1 element to the end of the array ----\n");

InsertEND(&array);

fflush(stdin);

getch();

break;

case 'd':

printf("\t---- Delete an element at position k ---\n");

DeleteIndex(&array);

fflush(stdin);

getch();

break;

case 's':

printf("\t--- Sort array by ascending value ---\n");


SortArray(&array);

getch();

break;

case 'x':

printf("\t--- Sort array by descending value ---\n");

SortArrayG(&array);

getch();

break;

case 't':

printf("\t----- Searching for a number x in the array ----\n");

SearchElement(&array);

fflush(stdin);

getch();

break;

case 'e':

system("cls");

printf("\nExit program");

break;

default:

printf("Invalid choice");

getch();

break;

} while(choice[0] != 'e');

You might also like