Aquí usamos el switch para que si metemos el valor 1 nos de un resultado y si usamos el 2 nos dé otro.
]]>int main (){
string Read;
string mid_price;
string city_mpg;
string high_mpg;
int n = 57;
float city;
float avg_city = 0.0;
float high;
float avg_high = 0.0;
float price;
float avg_price = 0.0;
ifstream read_file(“93cars.dat.txt”);
if (read_file.is_open()){
for (int i =0; i < 93; i++){
getline(read_file, Read);
mid_price = Read.substr(42,4);
istringstream buffer(mid_price);
buffer >> price;
city_mpg = Read.substr(52,2);
istringstream buffer2(city_mpg);
buffer2 >> city;
high_mpg = Read.substr(55,2);
istringstream buffer3(high_mpg);
buffer3 >> high;
avg_high = avg_high + high;
avg_price = avg_price + price;
avg_city = avg_city + city;
getline(read_file, Read);
}
read_file.close();
}else{
cout << “Failed opening the file” << endl;
}
avg_price = avg_price/93;
avg_city = avg_city/93;
avg_high = avg_high/93;
cout << “Average midprice: ” << avg_price << endl;
cout << “Average City MPG: ” << avg_city << endl;
cout << “Average Highway MPG: ” << avg_high << endl;
return 0;
}
This one was a easy but a little tricky I think but at the end I could make it work.
]]>]]>
long double precision( int n, long double e)
{
if (n == 0){
return ceil (e);
} else if (n == 1){
return floor ((e * 10 ) + 0.5) / 10;
}else if ( n ==2){
return ceil((e * 100 ) + 0.05) / 100;
} else if ( n == 3){
return floor ((e * 1000 ) + 0.005) / 1000;
} else if (n == 4){
return floor ((e * 10000 ) + 0.0005) / 10000;
}else {
return e;
}
}
long double calculate_e (){
long double e = 2.5;
long double c = 2.0;
long double d, factorial;
for (int i=0;i<5;i++){
c = c + 1.0;
d = c;
factorial = c;
while (d>1){
d = d – 1.0;
factorial = factorial * d;
}
e = e + (1.0/factorial);
}
return e;
}
int main(){
int n;
cout << “numero de decimales que desees entre 0 y 5″<< endl;
cin>> n;
long double e = calculate_e ();
long double e2 = precision(n, e);
cout << e2 << endl;
return 0;
}
This one was really difficult, at first I didn’t understand what to do but I read the link that was on the wsq and read other post to understand what to do.
]]>float raiz (float a){
float x = a / 2;
for (int i=0; i <20; i++){
x = .5*(x+(a/x));
}
return x;
}
int main (){
float a, r;
cout << “Escribe un numero al que quieras sacarle su raiz cuadrada “;
cin >> a;
float s = raiz (a);
cout << “la raiz cuadrada de ” << a << ” = “<< s << endl;
return 0;
}
This one was a bit hard but I research some information on the internet and I could make it.
]]>This one was really easy.
]]>