array insertion | Sololearn: Learn to code for FREE!

+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?

4/23/2021 9:42:57 AM

Ramisa Fariha

13 Answers

New Answer

+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.

+1

We are here to help you learn C++. The C++ standard library has functions and collection classes to help you solve problems. This library was developed by the C++ standards committee to address common programming problems. Before the C++ standard template classes existed (a very long time ago) it was necessary to create your own data structures, such as linked lists, or use dynamic memory allocation to address problems such as this. That is no longer the case with modern C++. Because an array has a fixed size, in your example 8, you cannot simply add another entry past the end of the array. In some cases this can lead to a segmentation fault and the program aborts. In other cases it may result in corrupting other variables.

+1

Would you care to provide the books instructions because it is not possible to alter the size of an array declared using a constant as you have done, though it can be done using pointers and dynamic memory allocation. Which may be what is intended by the exercise. It sounds like you are being taught a very old style of C++ (pre STL). This sort of thing is just not usually done these days but it's still common in C and a useful technique to know.

+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

+1

OK. Now that I know where you are coming from I am not surprised that you are confused. That book was first published in 1996 and the second edition was published in 2000. The book is at least 20 years out of date and has poor quality code that contains potential sequence point errors. It does not conform to the current C++ standards. The question relates to using fixed size arrays. The code keeps track of the number of entries currently used in the array using the variable n. This is NOT the same as the allocated size of the array. The size of the array must have been declared as being larger than the current number of entries used. Hence the statement in the exercise "Note that this requires the array to have at least n+1 elements allocated". In your code the array is declared as having 8 elements. It is not possible to "insert" a new value into the array since the array does not have any "spare" elements. All 8 elements are already in use. Your while loop is also broken. In the book Hubbard does not use brackets around the loop. While this is legal most modern compilers may issue a warning about this, it depends on the compiler settings. You have placed the brackets in the wrong place. The worst part of Hubbard's code is the potential sequence point error caused by the statement a[j--] = a[j-1]. This is undefined behaviour as it is unclear whether a[j--] will be auto decremented before or after a[j-1]. This is just poor quality code (not your fault). The use of single letter variable names is also a poor coding practice, apart from loop counters and indexes, as it makes the code hard to read. Here is an example of using this method for you to study. It is heavily commented. https://code.sololearn.com/cA140A224a25 Note that the array is declared with 10 elements but only 8 of these are actually initialised and in use. The insert function does not change the size of the array it simply increases the number of elements in use. One further element could be inserted but no more after that.

0

ok thank you

0

@HATEcoding How many times do you need to be told you cannot insert or remove elements from an array declared with a fixed size. https://www.sololearn.com/Discuss/2763382 https://www.sololearn.com/Discuss/2763362 You should either use dynamically allocated memory with a pointer or use a list container class. You are having problems due to your fundamental missunderstanding of the use of arrays. "C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type." Note the use of the term "fixed-size". If you need to change the size of a dynamically allocated array you create a new array dynamically and copy over the required data from the original array. Just read the documentation on the list class in the C++ Standard Template Library (STL) and make your life easier. http://www.cplusplus.com/reference/list/list/ I'm not surprised you hate coding if you are doing your own head in torturing the C++ language in this fashion.

0

you don't have to be rude @Martin Taylor..

0

@HATEcoding I wasn't being rude. You fail to understand the concept of an array. You are posting questions asking how to insert and remove elements from an array. This is like asking how to use a hammer to fasten a screw. You are using the wrong tool for the job. If you are going to take offence when someone tries to correct your mistakes you will learn nothing. Josh Greig recommend using a Vector, another STL container class. You could also use a stack, queue, forward_list or list. You will constantly find yourself frustrated if you continue to attempt to use inappropriate data structures to solve problems.

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]<<","; } }

0

HATEcoding, Here is an example of using a list. https://code.sololearn.com/cA3A3a22a23a/#cpp The push_back() method adds an element at the end. The iterator that is used can be thought of as a special class for a pointer that can be used without all of the fuss of dereferencing things with * and setting addresses with &. Notice how simple it is to sort, find and delete items from this list.

0

thank you for helping but I need to do it using array following the book's instruction

0

thanks a lot