Pętla for
Ostatnio zmodyfikowano 2017-02-03 23:57
czaffik |
» 2017-02-03 22:01:28 Nie do końca wiadomo czy w tym zadaniu odcinki są liczone od 0 czy od 1. Jeżeli od 1 to wywołanie funkcji get_time i get_length z parametrem 0 może dać jakieś wartości które nie powinny być uwzględnione w wyniku. @jankowalski25 - przy sumowaniu intów jako float raczej nie powinien stracić precyzji, a na pewno nie tyle żeby aż tak się różniło. |
|
jankowalski25 |
» 2017-02-03 22:03:15 for( int i = 0; i <= check_points; ++i ) |
Pętla wykonuje się o jeden raz za dużo. |
|
milmega Temat założony przez niniejszego użytkownika |
» 2017-02-03 22:31:34 UWAGA! Dziękuje bardzo wszystkim zaangażowanym! Zmieniłem inicjalizajce iteratora z 0 na 1 i się udało. Wklejam jeszcze dla innych użytkowników cały kod. #include <rajdy.h>
float average_speed( int check_points ) { float total_length = 0.0; float total_time = 0.0; for( int i = 1; i <= check_points; ++i ) { total_length += get_length( i ); total_time += get_time( i ); } return( total_length / total_time ) * 3.6; } |
|
jankowalski25 |
» 2017-02-03 23:57:59 @jankowalski25 - przy sumowaniu intów jako float raczej nie powinien stracić precyzji, a na pewno nie tyle żeby aż tak się różniło. |
Przy słabszych testach tego nie widać. Przykład: #ifndef RAJDY_H #define RAJDY_H
int get_length( int check_point ); int get_time( int check_point );
#endif
#include "rajdy.h" #include <cstdlib> #include <ctime> #include <iostream> #include <sstream> #include <string>
constexpr const int get_tab_size() { return 150; }
int get_length( int check_point ) { static const int TAB_SIZE = get_tab_size(); static int tab_length[ TAB_SIZE ]; static bool init = false; if( !init ) { for( int i = 0; i < TAB_SIZE; i++ ) tab_length[ i ] = 1073741824 +(( rand() % 127 ) + 1 ) +( 128 *( rand() % TAB_SIZE ) ); init = true; } return tab_length[ check_point - 1 ]; }
int get_time( int check_point ) { static const int TAB_SIZE = get_tab_size(); static int tab_time[ TAB_SIZE ]; static bool init = false; if( !init ) { for( int i = 0; i < TAB_SIZE; i++ ) tab_time[ i ] = 1; init = true; } return tab_time[ check_point - 1 ]; }
float average_speed( int check_points ); float my_average_speed( int check_points );
int main() { srand( time( NULL ) ); int tests[] = { 5, 10, 15, 20, 50, 100, 130 }; const int NUMBER_OF_TESTS =( sizeof( tests ) / sizeof( * tests ) ); bool isOk = true; for( int i = 0; i < NUMBER_OF_TESTS; i++ ) { float myResult = my_average_speed( tests[ i ] ); float yourResult = average_speed( tests[ i ] ); std::ostringstream out; out.precision( 2 ); out.setf( std::ios_base::fixed ); out << myResult; std::string strMyResult = out.str(); out.str( "" ); out << yourResult; std::string strYourResult = out.str(); bool equals =( strMyResult == strYourResult ); if( !equals ) std::cout << "FAILED average_speed( " << tests[ i ] << " ) " << "{ return " << strYourResult << "; } == " << strMyResult << '\n'; isOk =( isOk && equals ); } if( isOk ) return EXIT_SUCCESS; return EXIT_FAILURE; }
#include <rajdy.h>
float average_speed( int check_points ) { float total_length = 0.0; float total_time = 0.0; for( int i = 1; i <= check_points; ++i ) { total_length += get_length( i ); total_time += get_time( i ); } return( total_length / total_time ) * 3.6; }
#include <rajdy.h>
float my_average_speed( int check_points ) { }
Przykładowe standardowe wyjście programu: FAILED average_speed( 10 ) { return 3865499648.00; } == 3865499392.00 FAILED average_speed( 15 ) { return 3865504768.00; } == 3865504512.00 FAILED average_speed( 50 ) { return 3865503744.00; } == 3865504000.00 FAILED average_speed( 100 ) { return 3865504256.00; } == 3865504768.00 FAILED average_speed( 130 ) { return 3865503232.00; } == 3865503488.00
Process returned 1 (0x1) execution time : 0.007 s Press ENTER to continue. |
|
1 2 « 3 » |