For each pairwise comparison, where do you think your program is better? Why? Wh
ID: 3866178 • Letter: F
Question
For each pairwise comparison, where do you think your program is better? Why? Where do you think the other program is better? Why? (be detailed)
What have you learned from looking at other people's code, and how can you apply it in future assignments? (be detailed)
OP's code:
/*
* *****stDev.cpp*****
* */
#include<iostream>
#include<string>
#include<person.hpp>
#include<math>
using
namespace
std;
double
stdDev(Person
p_arr[],
int
N){
double
mean=0,
variance=0,
std;
for(int
i=0;i<N;i++){
mean
+= p_arr[i].getAge();
}
mean
= mean/N;
for(int
i=0;i<N;i++){
variance
= pow((p_arr[i].getAge()-mean),2);
}
variance
= variance/N;
std
= sqrt(variance);
return
std;
}
User 1's code:
/*************************************************************************
**
Description:
Program
contains
funtion
that
returns
standard
deviation
*
of all
ages
found
in an array
of class
Objects
containing
ages.
**************************************************************************
**/
#include
<cmath>
double
stdDev(array[],int
size)
{
double
sum
= 0, average,
stdDeviation
= 0.0;
int
i;
for
(i = 0, i < size,
++i)
{
sum
+= array[i].getAge();
}
average
= sum/size;
for
(i = 0, i < size,
++i)
{
stdDeviation
+= pow(array[i].getAge()
- average,2);
}
return
sqrt(stdDeviation
/ 10);
}
User 2's code:
/*********************************************************************
** Author:
** Date:
21 July
2017
** Description:
Function
that
takes
two
parameters and
returns the
**
standard
deviation
of all
the ages.
*********************************************************************/
#include
<iostream>
#include
<string>
#include
<cmath>
#include
"Person.hpp"
using
namespace
std;
/*********************************************************************
** Description:
Function
takes
two
parameters
and calculates
and
**
returns
standard
deviation
of all the
ages.
*********************************************************************/
double
stdDev(Person
persons[],
int
N)
{
double
avg
= 0.0,
sum2
= 0.0,
sum
= 0.0, stdDev;
for
(int
i = 0; i < N; i++)
{
sum
+= persons[i].getAge();
}
avg
= sum
/ N;
for
(int
i = 0; i < N; i++)
{
sum2
+= pow((persons[i].getAge()
- avg),
2);
}
stdDev
= sqrt((sum2)
/ (N));
return
stdDev;
}
Explanation / Answer
Op's Code VS user1's code:
If we compare the time complexity of both programs, both of them are having same time complexity O(N), where N is size of the array.
But Op's Code is better than user1's code, because users1 have assumed that the size of the array is 10 as he has writtent this return statement " return sqrt(stdDeviation/ 10);". If the size of the array is changed then user1's code will give wrong anwer.
Op's Code VS User2's code:
If we compare the time complexity of both programs, both of them are having same time complexity O(N), where N is size of the array.
The only plus point of Op's code is that he has used only 3 local variables double mean=0, variance=0, std; where as user2 has used 4 local variables double avg = 0.0, sum2 = 0.0, sum = 0.0, stdDev;
User2's Code VS user1's code:
If we compare the time complexity of both programs, both of them are having same time complexity O(N), where N is size of the array.
The only plus point of user1's code is that he has used only 3 local variables double sum = 0, average, stdDeviation = 0.0; where as user2 has used 4 local variables double avg = 0.0, sum2 = 0.0, sum = 0.0, stdDev;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.