Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I am having difficulty trying to figure out how to write these two proble

ID: 2247021 • Letter: H

Question

Hello, I am having difficulty trying to figure out how to write these two problems in C++. They can both be separate.

4. The 4h function will process a string in the format of left operand operator right operand. It will return the computational result of the converted string. For example: calculate("54321+222") will return 54543 calculate("120*20") will return 2400 calculate("235/3") will return 78 calculate("356-32") will return 324 The Sth function will compute the average of a 2 dimensional vector. (please introduce a 2D vector, where # row != # column.

Explanation / Answer

Q1)Please run the code, i have created the function calculate go through it and if you have any doubt feel free to contact me.

#include <iostream>

#include <iostream>

#include <sstream>

using namespace std;

int calculate(string s)

{

int index=0;

for(int i=0;i<s.length();i++)

{

if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/')

{

index=i;

break;

}

}

int left_operand = 0;

int right_operand=0;

  

string left=s.substr(0,index);

string right=s.substr(index+1,s.length()-index-1);

  

stringstream left_s(left);

stringstream right_s(right);

left_s >> left_operand;

right_s>>right_operand;

  

if(s[index]=='+')

{

return left_operand+right_operand;

}

if(s[index]=='-')

{

return left_operand-right_operand;

}

if(s[index]=='*')

{

return left_operand*right_operand;

}

if(s[index]=='/')

{

return left_operand/right_operand;

}

}

int main() {

// your code goes here

cout<<calculate("12+34");

cout<<endl;

cout<<calculate("235/3");

return 0;

}

Q2) i have created the function average, please go through it.

#include <iostream>

#include <vector>

using namespace std;

double average(vector <vector <int > > v)

{

int sum=0;

int cnt=0;

for(int i=0;i<v.size();i++)

{

for(int j=0;j<v[i].size();j++)

{

sum=sum+v[i][j];

cnt=cnt+1;

}

}

double average=(double)sum/cnt;

return average;

}

int main() {

// your code goes here

vector<vector<int> > v(10, vector<int>(20,1));

cout<<v.size()<<endl;

cout<<v[0].size()<<endl;

v[0][0]=1;

v[0][1]=2;

cout<<average(v);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote