l. Create a function definition that converts an array of characters into a stri
ID: 3836591 • Letter: L
Question
l. Create a function definition that converts an array of characters into a string. The function should not change the original array. The function will take the character array and the number of characters as input. The function returns a string containing the characters in the array. See the example below. is converted to "EXAM" and returned. M 2. Create a function definition that converts each element of any array of double to an integer (its fractional part will 0) and return the value lost from the conversion. The value of the lost fractional parts should be returned. See prototype and example below. 10.5 double doublet oint(float arrayin0, const int& SIZE) 10.0 7.43 7.0 15.25 The fractional part of each element in the passed array (on left) 15.0 is 0 shown on the and the removed fractional part 7.65 7.0 of each is added together and and returned. The return value of the example is 1.83. 3. What is the value of klijand k[3] if 2,0, l are entered for each cin (in that order? int k[6] 10, 0, 0, 0, 0, 0) Data: 2 0 1 int i, n for (i 3; i 6; i++) cin n; k[n]Explanation / Answer
I will help you solve them here.
Problem 1. We have a char array and we need to convert it to string. This is very simple. We will start with empty string and traverse the array using for loop and keep adding character to the string. The corresponding code will be given at last with output.
Problem 2. Its same as of first question, we have a double array, We will traverse the array, each element we will type case to int and then take the difference of the both. This different we will add for all the elements and return.
Problem 3: -
The program has two things to be taken care of…
So, if Data entered is 2 0 1. Then firstly at k[2], 3 will be inserted then at k[0] , 4 will be inserted , then at k[1] , 5 will be inserted.
So value at k[1] will be 5. Value of k[3] will be unchanged and will be 0.
#include<iostream>
using namespace std;
string arrToString(char arr[],int n) {
string x = "";
for(int i = 0;i < n;i++) { //traversing the for loop and adding each character to the string
x = x + arr[i];
}
//returning the formed string
return x;
}
double doubletoint(float arr1[],const int& n) {
double x = 0.0;
int c;
//traversing the for loop and casting each element to int first then getting the difference of both
//and adding the difference to double variable which returned
for(int i = 0;i < n;i++) {
c = (int) arr1[i]; //casting element to int.
x = x + (arr1[i] - c); // adding difference
}
return x; //returning total difference
}
int main() {
char arr[] = {'E','X','A','M'}; //Initializing the char array
int size = sizeof(arr) / sizeof(arr[0]) ; // getting the size of array
string convertedStr = arrToString(arr,size); // calling the conversion method
//printing the string
cout<<"Converted String:"<<convertedStr;
float arr1[] = {10.5,7.43,15.25,7.65}; // Initializing the double array
int size1 = sizeof(arr1) / sizeof(arr1[0]) ; // Getting the size of array
double diff = doubletoint(arr1,size1);// calling the function to get the difference
cout<<" Total Precision Lost: "<<diff;
return 0;
}
Output: -
Converted String:EXAM
Total Precision Lost: 1.83
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.