+ 2

array insertion

void insert(float a[], int n, float x); void print(float a[],int n); int main() { float m; float a[8]= {22,33,44,55,66,77,88,99}; cout<<"The given array is: "<<endl; for(int i=0; i<8; i++) { cout<<a[i]<<","; } cout<<"\n Enter the value inserted into the array: "; cin>>m; insert(a,8,m); } void insert(float a[], int n, float x) { int j=n; while( j>0 && a[j-1]>x) { a[j--] = a[j-1]; a[j] = x; } ++n; cout<<"After inserting,the array: "<<endl; print(a,n+1); } void print(float a[],int n) { for(int i=0; i<n; i++) { cout<<a[i]<<","; } } the code works fine but how can I remove that 0 at last?

23rd Apr 2021, 9:42 AM
Ramisa Fariha
Ramisa Fariha - avatar
7 ответов
+ 3
You could have a function like this if you want to remove at index 0. This assumes you have an int variable for length of the array. // a prototype for the top void removeAtZero(float [], int&); void removeAtZero(float a[], int& len) { for (int i=1;i<len;i++) a[i - 1] = a[i]; len--; // 1 less element in array so decrement length. } Note that you need a variable for the length parameter. Passing 8 instead of an int holding the value of 8 won't compile. Notice the function tries to decrement the length parameter. That won't work if you pass a constant value like 8 into it. You could change the function above to not use references and not decrement but then the caller will need to remember that the array length changed some other way. On a side note, your code will be more understandable if you have a variable named len in your main function and pass that around to your functions. n isn't as clear a variable or parameter name. Consider using vector. vector is a kind of dynamic array and will store your length and the values together in one object.
23rd Apr 2021, 9:57 AM
Josh Greig
Josh Greig - avatar
+ 1
Write and test the function void insert(float a[],int& n,float x) This function inserts the item x into the sorted array a of n elements and increments n. The new item is inserted at the location that maintains the sorted order of the array. This requires shifting elements forward to make room for the new x. (Note that this requires the array to have at least n+1 elements allocated.) book is programming with C++ by john hubbard
23rd Apr 2021, 8:34 PM
Ramisa Fariha
Ramisa Fariha - avatar
0
ok thank you
23rd Apr 2021, 10:12 AM
Ramisa Fariha
Ramisa Fariha - avatar
0
you don't have to be rude @Martin Taylor..
23rd Apr 2021, 1:14 PM
Ramisa Fariha
Ramisa Fariha - avatar
0
@Martin Taylor I am a beginner in learning c++ and honestly I don't find interest in learning c++ ,it's something I can't do by myself..Infact I sit for hours in solving a problem...I am doing all these codes with the help of a book...trying to understand the concept....sometimes I take help from a friend...Learning C++ is not something that I want to learn but I have to because I have this course in this semester and I don't want to fail in this course..I am helpless here,you see I am not torturing c++ rather it's c++ that is torturing me..jk and the code that I had given here was the first one , I corrected the code later. void insert(float a[], int& n, float x); void read(float a[],int& n); void print(float a[],int& n); const int MAXSIZE=100; int main() { float a[MAXSIZE],m; int size; cout<<"Enter the array size: "; cin >> size; //3 read(a,size); cout<<"the array is: "; //23,45,67, print(a,size); cout<<"\n Enter the value inserted into the array: "; cin>>m; //39 insert(a,size,m); int new_size = size; cout<<"After inserting, the array is: "<<endl; print(a,new_size); } void insert(float a[], int& n, float x) // a 3 39 { int j=n; //j=3 while( j>0 && a[j-1]>x) //3>0 && a[2]=67>39 { a[j--] = a[j-1]; //a[3] = a[2] i.e 67 goes to a[3] moving forward // a[2] = a[1] i.e 45 goes to a[2] a[j] = x;// a[2] = 39 // a[1] =39 } ++n; //4 } void read(float a[],int& n) { for(int i = 0; i < n; i++) { cout<<"a["<<i<<"]="<<endl; cin>>a[i]; } } void print(float a[],int& n) { for(int i=0; i<n; i++) { cout<<a[i]<<","; } }
23rd Apr 2021, 4:04 PM
Ramisa Fariha
Ramisa Fariha - avatar
0
thank you for helping but I need to do it using array following the book's instruction
23rd Apr 2021, 6:53 PM
Ramisa Fariha
Ramisa Fariha - avatar
0
thanks a lot
24th Apr 2021, 6:35 AM
Ramisa Fariha
Ramisa Fariha - avatar