C Language Applet array operation sample Code

Source: Internet
Author: User
Tags int size printf
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;


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.