I need some C++ help writing functions: 1. Write a complete function called lowe
ID: 3772858 • Letter: I
Question
I need some C++ help writing functions:
1. Write a complete function called lowestPosition() which takes as array (of double) and the number of elements used in the array (as an int) and returns the position of the element with the least value (as an int). You may assume the number of elements is >= 1.
Here's the prototype:
int lowestPosition(double a[], int size);
2. Write a function called setToMin() that will work with the following section of code. It works for any integer variable and will set it to the lowest of the subsequent three integer parameters. You don't need to put in the prototype, but you need to write a complete function. All the variables are integers.
int x, y, z;
setToMin(x, 1, 4, -6);
// the value of x is now -6
setToMin(y, 4, 2, 8);
// the value of y is now 2
setToMin(z, -9, -8, -7);
// the value of z is now -9
3. Write a function called printBackwards() that will work with a C++ string. The function will print any C++ string backwards. You don't need to put in the prototype, but you need to write a complete function.
string name="Oliver Twist";
printBackwards(name);
// tsiwT revilO is sent to cout
printBackwards("nothing");
// gnihtin is sent to cout
Explanation / Answer
c++ code:
#include <bits/stdc++.h>
using namespace std;
int lowestPosition(double arr[],int size){
double least=arr[0];
int ind=0;
for(int i=1;i<size;i++){
if(least>arr[i]){
ind=i;
least=arr[i];
}
}
return ind;
}
int min(int a,int b){
if(a<b){
return a;
}
return b;
}
void setToMin(int * n,int a,int b,int c){
*n=min(a,min(b,c));
}
void printBackwards(string s){
for(int i=s.length()-1;i>=0;i--){
cout<<s[i]<<endl;
}
cout<<endl;
}
int main(){
return 0;
}