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

Algoritmi de Cautare Intr Un Array

The document discusses various operations that can be performed on arrays in C programming, including insertion, deletion, searching, and updating of elements. It provides code examples to insert elements at different positions in an array, delete an element from an array, search for an element in an array, and update an element in an array. The code examples use basic for and while loops to shift elements within the array to make space for insertions or deletions.

Uploaded by

Raluca Ioana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Algoritmi de Cautare Intr Un Array

The document discusses various operations that can be performed on arrays in C programming, including insertion, deletion, searching, and updating of elements. It provides code examples to insert elements at different positions in an array, delete an element from an array, search for an element in an array, and update an element in an array. The code examples use basic for and while loops to shift elements within the array to make space for insertions or deletions.

Uploaded by

Raluca Ioana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <stdio.

h>

main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}

n = n + 1;
//j=5, k=3
while( j >= k) {
LA[j+1] = LA[j]; // LA[6]= LA[5]: se muta elementele sirului
cu o pozitie la dreapta
j = j - 1;
}

LA[k] = item;

printf("The array elements after insertion :\n");

for(i = 0; i<n; i++) {


printf("LA[%d] = %d \n", i, LA[i]);
}
}
INSERAREA UNUI ELEMENT
INSERAREA A 2 ELEMENTE

#include <stdio.h>

main() {

int LA[] = {1,3,5,7,8,};

int item = 10, k = 1, n = 5;

int item1=11, k1=3;

int i = 0, j = n;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

n = n +1;

//j=5, k=1

while( j >= k) {

LA[j+1] = LA[j]; // LA[6]= LA[5]: se muta elementele sirului cu o pozitie la dreapta

j = j-1;

LA[k] = item;

j=n; // j=6

n=n+1;

//j=6, k1=3

while( j >= k1) {

LA[j+1] = LA[j];// LA[7]=LA[6] , se muta elementele sirului cu o pozitie la dreapta

j = j - 1;

LA[k1] = item1; // k1=11

printf("The array elements after insertion :\n");


for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

INSERAREA UNUL ELEMENT LA INCEPUTUL UNUI TABLOU(ARRAY)

VARIANTA 1:

#include <stdio.h>

main() {

int LA[] = {1,3,5,7,8};

int item = 10, k = 0, n = 5;

int i = 0, j = n;

printf("The original array elements are :\n");

for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

n = n + 1;

//j=5, k=3

while( j >= k) {

LA[j+1] = LA[j]; // LA[6]= LA[5]: se muta elementele sirului cu o pozitie la dreapta

j = j - 1;

LA[k] = item;
printf("The array elements after insertion :\n");

for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

VARIANTA 2:

#include <stdio.h>

#define MAX 5

void main() {

int array[MAX] = {2, 3, 4, 5};

int N = 4; // number of elements in array

int i = 0; // loop variable

int value = 1; // new data element to be stored in array

// print array before insertion

printf("Printing array before insertion −\n");

for(i = 0; i < N; i++) {

printf("array[%d] = %d \n", i, array[i]);

// now shift rest of the elements downwards

for(i = N; i >= 0; i--) {

array[i+1] = array[i];

// add new element at first position


array[0] = value;

// increase N to reflect number of elements

N++;

// print to confirm

printf("Printing array after insertion −\n");

for(i = 0; i < N; i++) {

printf("array[%d] = %d\n", i, array[i]);

INSERAREA UNUI ELEMENT DUPA INDEXUL DAT

VARIANTA 1 : while

#include <stdio.h>

void main() {

int array[] = {1, 2, 4, 5};

int N = 4; // number of elements in array

int i = 0; // loop variable

int index = 2; // index location to insert new value

int value = 3; // new data element to be inserted

for(i = 0; i < N; i++) {

printf("Printeaza array[%d] = %d \n", i, array[i]);

}
while ( i >= index ) {

array[i+1] = array[i];

i--;

array[index] = value;

N++;

for(i = 0; i < N; i++) {

printf("Printeaza array dupa un index dat [%d] = %d\n", i, array[i]);

}
VARIANTA 2 : for

#include <stdio.h>

void main() {

int array[] = {1, 2, 4, 5};

int N = 4; // number of elements in array

int i = 0; // loop variable

int index = 2; // index location to insert new value

int value = 3; // new data element to be inserted

for(i = 0; i < N; i++) {

printf("Printeaza array[%d] = %d \n", i, array[i]);

for ( i=N; i >= index; i--) {

array[i+1] = array[i];

array[index] = value;

N++;

for(i = 0; i < N; i++) {

printf("Printeaza array dupa un index dat [%d] = %d\n", i, array[i]);

}
STERGEREA YNUI ELEMENT:

#include <stdio.h>

void main() {

int LA[] = {1,3,5,7,8};

int k = 3, n = 5;

int i, j;

for(i = 0; i<n; i++) {

printf("The original array elements are: LA[%d] = %d \n", i, LA[i]);

j = k;

while( j < n) {

LA[j-1] = LA[j]; // LA[2] = LA[3];

j = j + 1; // j= 4;

n = n -1; // n=4

for(i = 0; i<n; i++) {

printf("The array elements after deletion: LA[%d] = %d \n", i, LA[i]);

}
CAUTAREA UNUI ELEMENT

#include <stdio.h>

void main() {

int LA[] = {1,3,5,7,8};

int item = 5, n = 5;

int i = 0, j = 0;

for(i = 0; i<n; i++) {

printf("The original array elements are: LA[%d] = %d \n", i, LA[i]);

while( j < n){

if( LA[j] == item )

break;

j = j + 1;

printf("Found element %d at position %d\n", item, j+1);

While( 0 < 5) {

If(LA[0:index, la indexul 0 se afla valoarea 1] == 5

Break;

J= j+1 // j=1
ACTUALIZAREA(INLOCUIREA) UNUI ELEMENT

#include <stdio.h>

void main() {

int LA[] = {1,3,5,7,8};

int k = 3, n = 5, item = 10;

int i, j;

for(i = 0; i<n; i++) {

printf("The original array elements are: LA[%d] = %d \n", i, LA[i]);

LA[k-1] = item;

for(i = 0; i<n; i++) {

printf("The array elements after updation: LA[%d] = %d \n", i, LA[i]);

You might also like