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

Objectives: To better understand how simple operations work in the new language

ID: 3808362 • Letter: O

Question

Objectives: To better understand how simple operations work in the new language Go, developed by some of the designers of the Clanguage Assignment: You need to write two Go functions, mcompute() and lcompute(). When used, such as by the program below: package main import fmt Your code for mcompute and lcompute go here func main fmt. Println (mcomputel ("sayhello", 30 20) fmt. Println (mcompute2 ("add", mcompute2 ("sub 70 400), 20 fmt. Println (mcompute2 ("sub 30 20) fmt. Println (mcompute3 ("equalorLarger 10 5)) fmt. Println (mcompute3 ("equalorLarger", 5, 5)) fmt. Println (mcompute3 ("equalorLarger 5, 15)) lst: [3 jint{1, 2, 3) fmt. Println(lcomputel ("find 1, lst)) fmt. Println (lcomputel ("find", 3, lst)) fmt. Println(lcompute2 ("append 7, lst)) fmt. Println (lcompute2 ("insert, 7, lst)) The result is hello 30 and 20 nice to meet you 50 10 true true false 1, 2, 3, 7] [7, 1, 2, 3]

Explanation / Answer

Program

------------

#include <stdio.h>
#include <string.h>

int mcompute1(char *opr, int operand1, int opearnd2) {
   char addition[4] = { 'A', 'D', 'D', '' };
   char subtraction[4] = { 'S', 'U', 'B', '' };
   char sayHello[9] = { 'S', 'A', 'Y', 'H', 'E', 'L', 'L', 'O', '' };
   if (strcmp(opr, addition) == 0) {
       return (operand1 + opearnd2);
   } else if (strcmp(opr, subtraction) == 0) {
       return (operand1 - opearnd2);
   } else if (strcmp(opr, sayHello) == 0) {
       printf("hello %d and %d, how are you ", operand1, opearnd2);
       return 0;
   }
}

void icompute1(char *opr, int operand1, int *array) {
   char append[7] = { 'A', 'P', 'P', 'E', 'N', 'D', '' };
   char find[5] = { 'F', 'I', 'N', 'D', '' };
   char insert[7] = { 'I', 'N', 'S', 'E', 'R', 'T', '' };

   if (strcmp(opr, find) == 0) {
       int value = array[0];
       int i = 0;
       while (value != '') {
           value = array[i];
           if (value == operand1) {
               printf("True ");
               break;
           }
           i++;
       }
   } else if (strcmp(opr, append) == 0) {
       int i = 0;
       while (array[i] != '') {
           printf("%d", array[i]);
           i++;
       }
       array[i--] = operand1;
       printf("%d ", array[i + 1]);
   } else if (strcmp(opr, insert) == 0) {
       int i = 0;
       while (array[i] != '') {
           i++;
       }
       i--;
       for (; i >= 0; i--) {
       if(array[i] == ''){
       break;
       }else{
           array[i+1] = array[i];
       }
       }
       array[0] = operand1;
       int j = 0;
       while(array[j] != ''){
       printf("%d ", array[j]);
       j++;
       }
      
   }
}

int main() {
   char addition[4] = { 'A', 'D', 'D', '' };
   char subtraction[4] = { 'S', 'U', 'B', '' };
   char sayHello[9] = { 'S', 'A', 'Y', 'H', 'E', 'L', 'L', 'O', '' };
   int result = mcompute1(addition, 10, 20);
   printf("%s Operation result : %d ", addition,result);
   result = mcompute1(subtraction, 10, 20);
   printf("%s Operation result : %d ", subtraction,result);
   mcompute1(sayHello, 10, 20);
   printf("%d ", result);

   char append[7] = { 'A', 'P', 'P', 'E', 'N', 'D', '' };
   char find[5] = { 'F', 'I', 'N', 'D', '' };
   char insert[7] = { 'I', 'N', 'S', 'E', 'R', 'T', '' };
   int values[10] = { 1, 2, 3, '' };
   icompute1(find, 1, values);
   icompute1(append, 99, values);
   icompute1(insert, 100, values);
   return 0;
}

Output

----------

ADD Operation result :  30                                                                                                                                          

SUB Operation result :  -10                                                                                                                                         

hello 10 and 20, how are you -10                                                                                                                                    

True                                                                                                                                                                

12399                                                                                                                                                               

100                                                                                                                                                                 

1                                                                                                                                                                   

2                                                                                                                                                                   

3                                                                                                                                                                   

99