-
is there a cleaner way
howdy,
this code does basically what i want except it seems pretty ugly.
the idea is to get a value from a function, look through the array and return vales baaed on the ranges set up in the loops
this seems pretty messy to me. is there a cleaner way of establishing the ranges and going through the arrays?
~~~~~~~~~~~~~~~~~code~~~~~~~~~~~~~~~~~~~~~~~~
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main (void)
{
system ("clear");
start:
cout<<"\n\n";
double a[29] ={3160, 2940, 2390, 2250,2100, 1670, 1580, 1280, 1190, 926, 804, 486, 447, 305, 272, 229, 218, 147, 124, 64.9, 57.6, 42.4, 36.7, 26.3, 22.1, 15.2, 12.3, 6.79, 6.08};
char b[29][9]={"S24x121","S24X106","S24x100","S24x90","S24x80", "S20x96","S20x86","S20x75","S20x66","S18x70","S18x 54.7","S15x50","S15x42.9",
"S12x50","S12x40.8","S12x35","S12x31.8","S10x35"," S10x25.4","S8x23","S8x18.4","S7x20","S7x15.3","S6x 17.25","S6x12.5","S5x14.75","S5x10","S4x9.5","S4x7 .7"};
double k = 0;
cout<<" Enter a value: ";
cin>>k;
int i;
double d=0.0;
if(k>3160 || k<.1){cout<<"Value is out of range!\n";}
if(k>=449){
for(i = 0; i < 29; i++){
if( a[i] >=k && a[i] < ( k +550) ){
cout<<a[i]<<" is in the range >>-<< ";
cout<<b[i]<<" is the shape\n";
}
}
}
if(k<=448 && k>201){
for(i = 0; i < 29; i++){
if( a[i] >=k && a[i] < ( k +175) ){
cout<<a[i]<<" is in the range >>-<< ";
cout<<b[i]<<" is the shape\n";
}
}
}
if(k<=201 && k>41){
for(i = 0; i < 29; i++){
if( a[i] >=k && a[i] < ( k +75) ){
cout<<a[i]<<" is in the range >>-<< ";
cout<<b[i]<<" is the shape\n";
}
}
}
if(k<=41 && k>=.1){
for(i = 0; i < 29; i++){
if( a[i] >=k && a[i] < ( k +10) ){
cout<<a[i]<<" is in the range >>-<< ";
cout<<b[i]<<" is the shape\n";
}
}
}
cout<<"Press any key to continue.\n";
goto start;
//menu();
}
~~~~~~~~~~~~~~~~~code~~~~~~~~~~~~~~~~~~~~~~~
BTW the goto :o is in place so i could test the code without restarting it and will be removed when i get it finished.
thanks
M.R.
-
How about
Code:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
const int SIZE = 29;
double a[SIZE] = {
3160, 2940, 2390, 2250, 2100,
1670, 1580, 1280, 1190, 926,
804, 486, 447, 305, 272,
229, 218, 147, 124, 64.9,
57.6, 42.4, 36.7, 26.3, 22.1,
15.2, 12.3, 6.79, 6.08
};
char *b[SIZE] = {
"S24x121","S24X106","S24x100","S24x90","S24x80",
"S20x96","S20x86","S20x75","S20x66","S18x70",
"S18x54.7","S15x50","S15x42.9","S12x50","S12x40.8",
"S12x35","S12x31.8","S10x35","S10x25.4","S8x23",
"S8x18.4","S7x20","S7x15.3","S6x17.25","S6x12.5",
"S5x14.75","S5x10","S4x9.5","S4x7.7"
};
void checkit( double lower, double upper ) {
for( int i = 0; i < SIZE; i++){
if( a[i] >= lower && a[i] < upper ){
cout<<a[i]<<" is in the range >>-<< ";
cout<<b[i]<<" is the shape\n";
}
}
}
int main(void) {
start:
cout<<"\n\n";
double k = 0;
cout<<" Enter a value: ";
cin>>k;
if ( k > 3160 || k < 0.1 ) {
cout << "Value is out of range!\n";
} else
if ( k >= 449 ) {
checkit( k, k+550 );
} else
if ( k <= 448 && k > 201 ) {
checkit( k, k+175 );
} else
if ( k <= 201 && k > 41 ) {
checkit( k, k+75 );
} else
if ( k <= 41 && k >= 0.1 ) {
checkit( k, k+10 );
}
cout<<"Press any key to continue.\n";
goto start;
return 0;
}
-
VERY Slick
howdy salem,
very coooool. you real programer types are amazing. thanks for your time!
M.R.