C Language Applet array operation sample Code
Last Update:2017-02-28
Source: Internet
Author: User
An array of operations, find, INSERT, delete
copy code code as follows:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int size = 0;
int flag = 0;
void output (int *arry)
{
int i = 0;
for (i=0 i<size; i++)
{
printf ("Arry[%d]=%dt", I,arry[i]);
if ((i+1)%5 = = 0)
printf ("n");
}
printf ("n");
}
void Getarry (int *arry)
{
int i = 0;
Srand (Time (NULL));
for (i=0 i<size; i++)
{
Arry[i] = rand ()% 100;
}
}
void Add (int *arry, int pos, int num)
{
int i = 0;
if (pos>=0 && pos<=size)
{
if (POS < size)//insert
in the middle
{
for (i=size i>pos; i--)
{
Arry[i] = arry[i-1];
}
Arry[pos] = num;
}
else//insert
at last position
{
arry[size] = num;
}
size++;
}
Else
printf ("can only be inserted at the 0-%d location.) n ", size);
}
int search (int *arry, int num)
{
static int pos = 0;
if (flag)
pos++;
for (; pos<size; pos++)
{
if (arry[pos] = = num)
{
flag = 0;
return POS;
}
}
return-1;
}
void mod (int *arry, int pos, int num)
{
if (pos>=0 && pos<size)
{
Arry[pos] = num;
}
Else
{
printf ("Input location error.") n ");
}
}
int del (int *arry, int num)
{
int count = 0;
int pos = 0;
int i = 0;
pos=search (Arry, num);
while (pos+1)
{
for (I=pos i<size; i++)
{
Arry[i] = arry[i+1];
}
count++;
pos=search (Arry, num);
}
return count;
}
int main ()
{
//pos 0 to Size-1
int *arry = NULL;
int count = 0;
int pos = 0;
int num = 0;
printf ("Enter how many random numbers to generate:");
scanf ("%d", &size);
Arry = malloc (2*size*sizeof (int));
Getarry (Arry);
output (Arry);
printf ("Enter the location to add (0-%d):", size);
scanf ("%d", &pos);
printf ("Enter the number you want to add:");
scanf ("%d", &num);
Add (Arry, POS, num);
output (Arry);
printf ("Enter the number you want to find:");
scanf ("%d", &num);
pos=search (Arry, num);
while (pos+1)
{
flag = 1;
count++;
printf ("Arry[%d]=%dn", POS, num);
pos=search (Arry, num);
}
printf ("found%d matching digits n", count);
printf ("Enter the location to modify:");
scanf ("%d", &pos);
printf ("Input to be modified to number:");
scanf ("%d", &num);
mod (Arry, POS, num);
output (Arry);
printf ("Enter the number to delete:");
scanf ("%d", &num);
del (Arry, num);
output (Arry);
free (Arry);
Arry = NULL;
return 0;
}