Home > Back-end >  In the C language array of basic operations (insert element, remove elements, positioning elements)
In the C language array of basic operations (insert element, remove elements, positioning elements)

Time:10-27

my question in the end, you can directly to the final , it's the repetitive words as some kind of fun!
The beginning of the thing is like this, my teacher assigned homework to write "of" an array of basic operation code, because the teacher spoke nothing about last semester, the small white decided to baidu, find a "insert element" of the code, I research and write "remove elements" of the code, the "element positioning" last semester the teacher is speaking, so four hours finished (middle countless mistakes, of course, several collapse, thanks to the help of a teacher here), but when I finished my brainwave, if I delete or positioning is not the elements in the array? So I quickly finished the insert element code changes, and then modify the "remove elements" and "positioning elements", and then modified many times, many failures,
Code is (of course now is wrong)
 
#include
# define N 6
Void bubble_sort (int a [], int n, int k)
{
Int I, t;

If (k> A/n - 2)
A [n - 1]=k;
The else {
for(i=0; iIf (a [I] & gt; K)

{
T=I;
break;
}
For (I=n - 1; i>=t; I -)
{
A [I + 1)=a, [I].

}
A [t]=k;
}
}
Void delete_sort (int a [], int n, int k)
{
Int I, t;

for(i=0; iIf (a [I]==k)
{
{
T=I;
break;
}
For (I=t + 1; i{
A [I - 1)=a, [I].

}
A/n - 1=0;
} else {
Printf (" can't find the element \ n ");
break;
}
}
Int locate_sort (int a [], int n, int k)
{
Int I, t;
for(i=0; i
If (a [I]==k)
{
{
T=I;
break;
}
return t;
}

Else if (a [I]!=k)
{the continue;
Return 10;
}

}
Void print (int a [], int n)
{
int i;
for(i=0; iPrintf (" % 3 d ", a [I]);
printf("\n");
}

Void main ()
{
Int a [6]=,3,5,7,9 {1};
int i;
Int n, j, w;
Int s=N;

Int z;
Printf (" array data before operation is: ");
Print (a, s);
Printf (" please enter the need to insert elements: ");
The scanf (" % d ", & amp; n);
Bubble_sort (a, s, n);
Printf (" array is: after insert % d ", n);
Print (a, s);
Printf (" please enter the need to delete the element: ");
The scanf (" % d ", & amp; J);
Delete_sort (a, s, j);
Printf (" array is: after delete % d ", j);
Print (a, s);
Printf (" please input needs to locate elements: ");
The scanf (" % d ", & amp; W);
Z=locate_sort (a, s, w);
If (z=10)
Printf (" can't find the element ");
The else
Subscript for elements of printf (" % d: % d ", w, z);
}

Run screenshot:



I wanted to know how to write "element to delete" and "element positioning" part of the code, can be achieved if delete or position, not the elements in the array print "can't find the element"; If print the corresponding array or the elements of an array subscript!



Maybe my question is very simple, want to please bosses fraternity, answer it! Thank you very much!

CodePudding user response:

Local_sort function
If (a [I]==k)
{
{
T=I;
break;
}
return t;
}

So write, found the break out of the cycle, after the return t to perform less than the year after the horse;
Delete the break statement

CodePudding user response:

Upstairs positive solution, all break, return t doesn't work, direct:
for(i=0; iIf (a==k) [I] {
return i;
}
}

return -1;//said can't find the

Remove elements, deleted, you still have to move forward to a position at the back of the element,

CodePudding user response:

Thank you very much! Positioning element part of the problem already solved
I delete elements code part of the problem how to solve? Years,
  • Related