Reputation: 283
I am trying to use the following remove() function in the main() function. Please let me know what is wrong with my current code.
void remove(float a[], int& n, int i);
int main() {
float a[]={56.8, 14.2, 0.2, 22.3, 3.3, 54.02, 543.33, 456,333, 1.1};
int size, del;
size =sizeof(a)/sizeof(a[0]);
cout<<"Enter the element to be deleted!";
cin >>del;
cout << "Removed= "<< remove(a, size, del) << "\n";
}
void remove(float a[], int& n, int i){
for (int j=i+1; j<n; j++)
a[j-1] = a[j];
--n;
}
Upvotes: 2
Views: 144
Reputation: 95
float remove(float a[], int& n, int i){
float deleted = a[i];
for (int j=i+1; j<n; j++)
a[j-1] = a[j];
--n;
return deleted;
}
Upvotes: 1
Reputation: 1069
So your question is
a) let me know what is wrong with my code
and
b) isn't there another way to do the same without altering the function code
to a)
I learned recently not to use namespace if possible because of the high chance to get avoidable errors due to variable naming that actually happened very often - so std::
is recommended instead of using namespace std;
sizeof()
may use internally double not float so beside it works my warning: implicit conversion loses floating-point precision: 'double' to 'float'
A for - slope with more than one line in the body should be written in brackets else my warning: this ‘for’ clause does not guard... [-Wmisleading-indentation] 20 | for (int j=i+1; j
If you use std::cin
always should be checking for wrong input e.g. like I did with the input()
function.
Since int main(){return 0;}
is a function and the "int" indicates a return value, always close the function with a "return 0" for a stop signal to your OS.
Declaring the variables at the beginning instead between the lines makes the code more readable
Also please don't forget check your Question as solved if you got a proper answer to avoid it showing up in the "unsolved" section of stackoverflow.com ;)
to b)
yes, it is - just besides using the { } - brackets like so:
#include <iostream> //std, std::cin
#include <limits> //std::numeric_limits<std::streamsize>::max()
int input(int size); //int signals return
void remove(double a[], int& n, int i); //void signals no return
int main() {
int size, del;
double rem;
double a[]={56.8, 14.2, 0.2, 22.3, 3.3, 54.02, 543.33, 456,333, 1.1};
size =sizeof(a)/sizeof(a[0]);
std::cout<<"Which element do you want to remove: ";
del = input(size); //check the input
rem = a[del]; //filling rem to print out the value to be removed to not change the function like you asked for
std::cout << "Removed= " << rem << std::endl;
remove(a, size, del);
return 0;
}
void remove(double a[], int& n, int i)
{
for (int j=i+1; j<n; j++)
{
a[j-1] = a[j];
--n;
}
}
int input(int size)
{
int input;
while (!(std::cin >> input) || input < 0 || input >= size) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "invalid input ignored; try again and enter a valid move between -1 and " << size << "\n";
}
return input;
}
My intention is for everyone including myself to learn even from seemingly easy questions. I hope I reached my goal for you :)
Upvotes: 2
Reputation: 78
float remove(float a[], int& n, int i);
int main() {
float a[]={56.8, 14.2, 0.2, 22.3, 3.3, 54.02, 543.33, 456,333, 1.1};
int size, del;
size =sizeof(a)/sizeof(a[0]);
cout<<"Which element do you want to remove: ";
cin >>del;
remove (a, size, del);
}
float remove(float a[], int& n, int i){
cout << "Removed= "<< a[i] << "\n";
for (int j=i+1; j<n; j++)
a[j-1] = a[j];
--n;
return i;
}
Upvotes: 2
Reputation: 11
Add return statement in remove function and change return type accordingly.
Upvotes: -1