Answer to Question #272708 in Electrical Engineering for Neerav Gupta

Question #272708

Looking at Run time error, My main field is not c++ computer programming. so mistakes can be occurred.

#include <iostream>

using namespace std;


const int MaxSize = 10;


// Implementation of functions

void insert(float a[], int& n, float x)

{

if (n == 0 || a[n - 1] <= x)

{

a[n] = x;

}

else

{

a[n] = a[n - 1];

int i = n - 1;

insert(a, i, x);

}


if (n < MaxSize)

{

n++;

}

}


int main()

{


float a[MaxSize];

int n = 0;


// Test

float testArray[] = { 20.0, 5.0, 30.0, 50.0, 2.0, 50.5, 25.5, 40.0, 100.0, 80.0, 70.0 };

for (int i = 0; i < 11; i++)

{

insert(a, n, testArray[i]);

}


for (int i = 0; i < n; i++)

{

cout << a[i] << "\n";

}


// To pause

cin.get();

cin.get();


return 0;

}

On my Machine This Program Executes Where the Prototype Occurs and definition occurs.

 regards;

so how to remove run time error.

regards;


1
Expert's answer
2021-11-30T14:55:02-0500

/*Looking at Run time error, My main field is not c++ computer programming. so mistakes can be occurred*/


#include <iostream>


using namespace std;




const int MaxSize = 10;




// Implementation of functions


void insert(float a[], int& n, float x)


{


if (n == 0 || a[n - 1] <= x)


{


a[n] = x;


}


else


{


a[n] = a[n - 1];


int i = n - 1;


insert(a, i, x);


}




if (n < MaxSize)


{


n++;


}


}




int main()


{




float a[MaxSize];


int n = 0;




// Test


float testArray[] = { 20.0, 5.0, 30.0, 50.0, 2.0, 50.5, 25.5, 40.0, 100.0, 80.0, 70.0 };


for (int i = 0; i < 11; i++)


{


insert(a, n, testArray[i]);


}




for (int i = 0; i < n; i++)


{


cout << a[i] << "\n";


}




// To pause


cin.get();


cin.get();




return 0;


}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS