--Originally published at The Talking Chalk
Here it is!
Unfortunately I will not be able to include the format I wanted to use for the blogs until next partial; however I hope these codes help you. There are not many complicated processes on the excercise; maybe the use of templates to determine that an array will conserve its values when used as a parameter of a fucntion is the most complicated part.
Have a nice thursday!
E1.
#include <iostream>
#include <math.h>
using namespace std;
float distancia (float x1, float y1, float x2, float y2)
{
float distancia= sqrt(pow((x2-x1),2)+pow((y2-y1),2));
return distancia;
}
int main()
{
cout<<distancia(1, 6, 8, 9);
}
E2.
#include <iostream>
using namespace std;
string triangulo(int size)
{
int counter=1, counter2=0;
for(counter; counter<size; counter++)
{
do
{
cout<<“T”;
counter2= counter2+1;
}
while (counter2<counter);
cout<<endl;
counter2=0;
}
counter=counter-1;
counter2=counter;
for(counter; counter>=0; counter–)
{
do
{
cout<<“T”;
counter2=counter2-1;
}
while(counter2>=0);
cout<<endl;
counter2=counter;
}
return “T”;
}
int main()
{
cout<<triangulo(6)<<endl;
return 0;
}
E3.
#include <iostream>
using namespace std;
int factorial(int x)
{
int store=1;
for(int count=1; count<=x; count++)
{
store*=count;
}
return store;
}
int main()
{
cout<<factorial(5)<<endl;;
return 0;
}
E4. TEMPLATE!!!!
#include <iostream>
using namespace std;
template <typename T, int N>
float promedio_lista (T (&lista)[N])
{
float sum=0;
size_t size= sizeof(lista)/sizeof(lista[0]);
for(int counter=0; counter<size; counter++)
{
sum+=lista[counter];
}
float avr= sum/size;
return avr;
}
int main()
{
float x[] = {1,2,3,4,4,3,2,1};
cout<<promedio_lista(x)<<endl;
}
E5.
#include <iostream>
using namespace std;
float smallest_of_four (float w, float x, float y, float z)
{
if(w<=x && w<=y && w<=z)
{
return w;
}
else if(x<=w && x<=y && x<=z)
{
return x;
}
else if(y<=w && y<=x && y<=z)
{
return y;
}
else
{
return z;
}
}
int main()
{
cout<<smallest_of_four(2,3,4,5)<<endl;
cout<<smallest_of_four(8,7,6,5)<<endl;
cout<<smallest_of_four(7,8,4,5)<<endl;
cout<<smallest_of_four(2,2,2,2)<<endl;
return 0;
}
E6.
#include <iostream>
using namespace std;
int fibonnaci
Continue reading "Quiz 11 – Lack of bananas" →