WRITE THE FULL FLEDGE C++ PROGRAM WITH EACH PARAMETER DEFINED FOR THE FOLLOWING
ID: 3740143 • Letter: W
Question
WRITE THE FULL FLEDGE C++ PROGRAM WITH EACH PARAMETER DEFINED FOR THE FOLLOWING PARTS OF QUESTION WITH OUTPUT DISPLAYED WITH THE HELP OF SNAPSHOT AND THE PROGRAM SHOULD RUN ON ANY COMPILER
Write the C++ code of a function that takes as inputs, a number, position, and digit. The function should return a new number with the digit at position 'position' changed to 'digit'. Start counting the digits from the left means left most digit will be at position zero.
For example:
number = 127856, position = 3, digit = 9: the function returns 127956
number = 2431, position = 0, digit = 5: the function returns 5431
B) Write C++ code of a function named dot_Product( ) that returns either the product of two numbers x1 and y1 or the dot product of a pair of two-dimensional vectors (x1, y1) and (x2, y2) or the dot product of a pair of three-dimensional vectors (x1, y1, z1) and (x2, y2, z2), according to whether the function is passed 2, 4, or 6 parameters. For example, with four arguments (3.0, 8.0, -4. 0, 6.0), the call dot_Product would return (3.0) (-4.0) + (8.0) (6.0) = 36.0.
NOTE YOU CANNOT USE ANY break, continue or goto statement IN THIS
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
//Answer for A)
int change(int num,int pos,int digit)
{
int temp = num;
int len = 0;
while(temp>0)
{
temp/=10;
len++;
}
if(pos<=len)
{
int pow_val = pow(10,len-pos-1); //Store pow(10,len-pos-1) to calculate number before and after given position (ex: 1234 pow_val will be 100 if pos=1)
int num_after_pos = num%pow_val; //(ex: num_after_pos = 34)
int num_before_pos = num - num%(pow_val*10); //substract number after postion including given position (Ex: num_before_pos = 1000)
int change_pos_val = digit*pow_val; //calculate new value //(Ex: change_pos_val = 900 if digit = 9
num = num_before_pos + change_pos_val + num_after_pos; // num = sum of all the values we calculated above
return num; //finally return modified digit number
}
else
{
cout << "Position should be lessthan the digit length "<<endl;
return 0;
}
}
int main()
{
int num ,digit,pos;
int result ;
int choice = 1;
while(choice !='0')
{
cout << "enter Number , Position and Digit to change"<<endl;
cin >>num>>pos>>digit;
if(digit<10)
{
result = change(num,pos,digit);
if(result !=0)
{
cout<<"Number "<<num<<" after change at pos "<<pos <<" ==> "<< result<<endl;
}
}
else
{
cout << "Digit Should be between 0 to 10"<<endl;
}
cout<<"press 1 to continue or 0 to quit : ";
cin>>choice;
cout<<endl;
}
}
######################################
//Answer for B)
#include<iostream>
#include<cmath>
using namespace std;
float dot_Product(float x1,float y1)
{
return x1*y1;
}
float dot_Product(float x1,float y1,float x2,float y2)
{
return x1*x2+y1*y2;
}
float dot_Product(float x1,float y1,float z1,float x2,float y2,float z2)
{
return x1*x2+y1*y2+z1*z2;
}
int main()
{
cout << dot_Product(10,2)<<" ";
cout << dot_Product(3.0,8.0,-4.0,6.0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.