Quiz on Best Programming Practices
Overview
Teaching: 15 min
Exercises: 0 minQuestions
Is programming your forte?
Objectives
Validate your understanding by working through some examples.
Video Session
The session was captured for your asynchronous review.
Quiz Time
Question 01
In the following two code snippets, which example is more efficient, A or B?
Code Example (A)
Code Example (B)
double sum = 0;
double f = TMath::Sqrt(0.5);
for (size_t i=0; i<n_channels; ++i)
{
sum += result.at(i)*f;
}
double f = TMath::Sqrt(0.5);
for (size_t i=0; i<n_channels; ++i)
{
sum += result.at(i)*f;
}
double sum = 0;
for (size_t i=0; i<n_channels; ++i)
{
sum += result.at(i)/TMath::Sqrt(2.0);
}
for (size_t i=0; i<n_channels; ++i)
{
sum += result.at(i)/TMath::Sqrt(2.0);
}
Question 02
In the following two code snippets, which example is more efficient, A or B?
Code Example (A)
Code Example (B)
double r = TMath::Sqrt( x*x + y*y );
double r = TMath::Power( TMath::Power(x,2) + TMath::Power(y,2), 0.5);
Question 03
In the following two code snippets, which example is more efficient, A or B?
Code Example (A)
Code Example (B)
double r = TMath::Sqrt( slow_function_calculating_x()*
slow_function_calculating_x() +
slow_function_calculating_y()*
slow_function_calculating_y() );
slow_function_calculating_x() +
slow_function_calculating_y()*
slow_function_calculating_y() );
double r = TMath::Sqrt( TMath::Sq(slow_function_calculating_x()) +
TMath::Sq(slow_function_calculating_y()));
TMath::Sq(slow_function_calculating_y()));
Question 04
In the following two code snippets, which example is more efficient, A or B?
Code Example (A)
Code Example (B)
if (TMath::Sqrt( x*x + y*y ) < rcut )
{
do_something();
}
{
do_something();
}
double rcutsq = rcut*rcut;
if (x*x + y*y < rcutsq)
{
do_something();
}
if (x*x + y*y < rcutsq)
{
do_something();
}
Question 05
In the following two code snippets, which example is more efficient, A or B?
Code Example (A)
Code Example (B)
double sum = 0;
std::vector <double> results;
(fill lots of results)
for (size_t i=0; i<results.size(); ++i)
{
sum += TMath::Sq(results.at(i));
}
std::vector <double> results;
(fill lots of results)
for (size_t i=0; i<results.size(); ++i)
{
sum += TMath::Sq(results.at(i));
}
double sum = 0;
std::vector <double> results;
(fill lots of results)
for (size_t i=0; i<results.size(); ++i)
{
float rsq = results.at(i)*result.at(i);
sum += rsq;
}
std::vector <double> results;
(fill lots of results)
for (size_t i=0; i<results.size(); ++i)
{
float rsq = results.at(i)*result.at(i);
sum += rsq;
}
Question 06
In the following two code snippets, which example is more efficient, A or B?
Code Example (A)
Code Example (B)
int dune::VDColdboxChannelMapService:: getOfflChanFromWIBConnectorInfo (int wib, int wibconnector, int cechan)
{
int r = -1;
auto fm1 = infotochanmap.find(wib);
if (fm1 == infotochanmap.end()) return r;
auto m1 = fm1->second;
auto fm2 = m1.find(wibconnector);
if (fm2 == m1.end()) return r;
auto m2 = fm2->second;
auto fm3 = m2.find(cechan);
if (fm3 == m2.end()) return r;
r = fm3->second;
return r;
{
int r = -1;
auto fm1 = infotochanmap.find(wib);
if (fm1 == infotochanmap.end()) return r;
auto m1 = fm1->second;
auto fm2 = m1.find(wibconnector);
if (fm2 == m1.end()) return r;
auto m2 = fm2->second;
auto fm3 = m2.find(cechan);
if (fm3 == m2.end()) return r;
r = fm3->second;
return r;
int dune::VDColdboxChannelMapService:: getOfflChaFromWIBConnectorInfo (int wib, int wibconnector, int cechan)
{
int r = -1;
auto fm1 = infotochanmap.find(wib);
if (fm1 == infotochanmap.end()) return r;
auto& m1 = fm1->second;
auto fm2 = m1.find(wibconnector);
if (fm2 == m1.end()) return r;
auto& m2 = fm2->second;
auto fm3 = m2.find(cechan);
if (fm3 == m2.end()) return r;
r = fm3->second;
return r;
}
{
int r = -1;
auto fm1 = infotochanmap.find(wib);
if (fm1 == infotochanmap.end()) return r;
auto& m1 = fm1->second;
auto fm2 = m1.find(wibconnector);
if (fm2 == m1.end()) return r;
auto& m2 = fm2->second;
auto fm3 = m2.find(cechan);
if (fm3 == m2.end()) return r;
r = fm3->second;
return r;
}
Question 07
Can you point out the errors in the following code blocks?
Code Example (A)
Code Example (B)
#include <iostream>
int main(int argc, char ∗∗argv)
{
for (int i=0; i< 10000; ++i)
{
int ∗j = new int[1000]
}
int kt;
std::cout << kt << std::endl;
int p[100];
p[100] = 5;
std::cout << p[100] << std::endl;
int ∗k;
k= 0;
∗k = 6;
std::cout << ∗k << std::endl;
}
int main(int argc, char ∗∗argv)
{
for (int i=0; i< 10000; ++i)
{
int ∗j = new int[1000]
}
int kt;
std::cout << kt << std::endl;
int p[100];
p[100] = 5;
std::cout << p[100] << std::endl;
int ∗k;
k= 0;
∗k = 6;
std::cout << ∗k << std::endl;
}
#include <iostream>
int main(int argc, char ∗∗argv)
{
for (int i=0; i< 10000; ++i)
{
int ∗j = new int[1000]
}
int kt= 0;
std::cout << kt << std::endl;
int p[100];
p[99] = 5;
std::cout << p[99] << std::endl;
int ∗k=k;
k= 0;
∗k = 6;
std::cout << ∗k << std::endl;
}
int main(int argc, char ∗∗argv)
{
for (int i=0; i< 10000; ++i)
{
int ∗j = new int[1000]
}
int kt= 0;
std::cout << kt << std::endl;
int p[100];
p[99] = 5;
std::cout << p[99] << std::endl;
int ∗k=k;
k= 0;
∗k = 6;
std::cout << ∗k << std::endl;
}
Key Points
Practice makes perfect.