I'm trying to get the index of the nearest upper bound (unless equivalent value found) in the array size
using the value in variable sum
as upper bound, and then find the value at the same index in the array value
.
For example: if the value in sum
is 270, my program should find the value 280 located at index 6 in size
and output the value at corresponding value[6]
.
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
double x = 0;
double y = 0;
double sum = 0;
double size[27] = {24, 28, 32, 38, 48, 240, 280, 320, 360, 380,
420, 480, 560, 600, 640, 700, 720, 800, 840,
960, 980, 1120, 1200, 1280, 1440, 1680, 1920};
double value[27] = {.0022, .0026, .0029, .0035, .0044, .0219,
.0256, .0292, .0328, .0384, .0438, .0513,
.0547, .0584, .0641,.0656, .073, .0766,
.0875, .0877, .0897, .1023, .1094, .1169,
.1313, .1531, .175};
cout << "Enter width: " << endl;
cin >> x;
cout << "Enter height: " << endl;
cin >> y;
x = ceil(x) + 3;
y = ceil(y) + 3;
sum = x * y;
}
Change your code to this -
double x = 0;
double y = 0;
double sum = 0;
int size[27] = {24, 28, 32, 38, 48, 240, 280, 320, 360, 380,
420, 480, 560, 600, 640, 700, 720, 800, 840, 960, 980, 1120, 1200, 1280, 1440, 1680, 1920};
double value[27] = {.0022, .0026, .0029, .0035, .0044, .0219,
.0256, .0292, .0328, .0384, .0438, .0513, .0547, .0584, .0641,.0656, .073, .0766, .0875, .0877, .0897, .1023, .1094, .1169, .1313, .1531, .175};
cout << "Enter width: " << endl;
cin >> x;
cout << "Enter height: " << endl;
cin >> y;
x = ceil(x) + 3;
y = ceil(y) + 3;
sum = x * y;
for (int i=0;i<27;i++)
{
if (size[i]>=sum)
{
cout<<value[i]<<endl;
break;
}
else if(i==26)
{
cout<<"No upper Bound find\n";
}
}
There are other ways to solve this. But as you said you are a beginner. I have given the simple bruteforce solution. :)