developer
developer

Reputation: 283

How to call this function in main() in cpp

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

Answers (4)

Naveen Mittal
Naveen Mittal

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

Ingo Mi
Ingo Mi

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)

  1. 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;

  2. sizeof() may use internally double not float so beside it works my warning: implicit conversion loses floating-point precision: 'double' to 'float'

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

  4. If you use std::cin always should be checking for wrong input e.g. like I did with the input() function.

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

  6. Declaring the variables at the beginning instead between the lines makes the code more readable

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

ajax20
ajax20

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

Kalyani
Kalyani

Reputation: 11

Add return statement in remove function and change return type accordingly.

Upvotes: -1

Related Questions