Question

Write the proposed program in Java, JavaScript, and Python. Run them and compare the results. Submit...

Write the proposed program in Java, JavaScript, and Python. Run them and compare the results. Submit source code for 3 programs.

Let the function fun be defined as

int fun(int* k) {
 *k += 4;
 return 3 * (*k) - 1;
 }

Suppose fun is used in a program as follows:

void  main() {
  int i = 10, j = 10, sum1, sum2;
  sum1 = (i / 2) + fun(&i);
  sum2 = fun(&j) + (j / 2);
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

python,java,java script didn't have call by reference.they only have call by value.only c have call by reference.these are done through pointers.pointers concept is not there in python,javascript,java.

only c and cpp have pointers concept.

c code:source.c

#include<stdlib.h>
int fun(int* k){
   *k+=4;
   return 3*(*k)-1;
}
void main(){
   int i=10,j=10,sum1,sum2;
   sum1=(i/2)+fun(&i);
   sum2=fun(&j)+(j/2);
   printf("%d\n",sum1);
   printf("%d\n",sum2);
}

#output:

C:Users\vishnu\Desktop 1.exe 46 48 Process exited after 0.09085 seconds with return value 3 Press any key to continue

when passing &j to a function fun.if the reference variable is change the j value also change.this is call by reference concept.

the function return 41 and we need add(j/2).here j as 14 because in function  k+=4 it means k=10+4 so it was reference so

j value as 14 so 14/2=7

so the sum2 have 41+7=48

in sum1 initially (i/2) so i value is 10.that's why 10/2=5 and the function return 41 so 41+5=46

so output

sum1=46

sum2=48

But while we are using python,javascript,java didn't have call by reference that's why we get the same output

sum1=46

sum2=46

python source code: source.py

def fun(k):
   k+=4
   return (3*k)-1;
i=10;j=10;sum1=sum2=0
sum1=(i/2)+fun(i);
sum2=fun(j)+(j/2)
print(sum1)
print(sum2)

#output:

\Users\vishnu\Desktop>pyt hon 1.py 46.0 46.0 c: \Users\vishnu\Desktop>

#java source code : Source.java

import java.util.*;
import java.lang.*;
class Source{
   int fun(int k){
           k+=4;
           return (3*k)-1;
       }
   public static void main(String args[]){
       Source ob=new Source();
       int i=10;
       int j=10;
       int sum1=0;
       int sum2=0;
       sum1=(i/2)+ob.fun(i);
       sum2=ob.fun(j)+(j/2);
       System.out.println(sum1);
       System.out.println(sum2);
   }
}

#output:

C-Command Prompt EC: \Users\vishnu\Desktop>javac Source.java C: \Users\vishnu\Desktop>java Source 46 246 C: \Users\vishnu\Des

#java script source code: source.html

here i write javascript code within html tags to get the output display on browser

<html>
<body>
<script>
function fun(k){
k+=4;
return (3*k)-1;
}
var i=10;
var j=10;
var sum1=0;
var sum2=0;
sum1=(i/2)+fun(i);
sum2=fun(j)+(j/2);
alert(sum1+"\n"+sum2);
</script>
</body>
</html>

#output:

This page says 46 46 OK

#if you have any doubts comment below.....

Add a comment
Know the answer?
Add Answer to:
Write the proposed program in Java, JavaScript, and Python. Run them and compare the results. Submit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Let the function fun be defined as int fun(int *k) { *k - = 2; return...

    Let the function fun be defined as int fun(int *k) { *k - = 2; return 4 * (*k) +2 ; } Suppose fun is used in a program as follows: void main() { int i = 16, j = 16, sum1, sum2; sum1 = (i / 2) + fun(&i); sum2 = fun(&j) + (j / 2); } What is the values of sum1 and sum2 a. If the operands in the expressions are evaluated from left to right sum1...

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • Part 1: Python code; rewrite this code in C#, run the program and submit - include...

    Part 1: Python code; rewrite this code in C#, run the program and submit - include comments number= 4 guesscount=0 guess=int(input("Guess a number between 1 and 10: ")) while guess!=number: guesscount=guesscount+1 if guess<number: print("Your guess is too low") elif guess>number: print("Your guess is too high") else: print("You got it!!") guess=int(input("Guess again: ")) print("You figured it out in ",guesscount," guesses") Part 2: C++ code; rewrite the following code in C#. Run the program and submit. - include comments #include <iostream> using...

  • Using the following Java program, modify the code so that every time you run your program,...

    Using the following Java program, modify the code so that every time you run your program, it generates random numbers for your array, and then prints it (insertion sort) import java.awt.Graphics; import java.applet.Applet; public class SortingProg extends Applet { int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 }; public void paint(Graphics g)     {       print(g,"Data items in original order",a,25,25);       sort();       print(g,"Data items in ascending order",a,25,55);     } public void sort() {...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • JAVA CODE This program causes an error and crashes. Compile and give it a test run,...

    JAVA CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...

  • 1-Is it possible to run a program without a main() function? Yes No 2- How many...

    1-Is it possible to run a program without a main() function? Yes No 2- How many main() functions can one have in a program? 2 This depends on what compiler you use. As many as you like 1 3- What does the following code fragment leave in x? char a = 'A'; int x = sizeof (a); 1 Depends on what compiler you use. 4 2 4- True or false: In a C program I can have two functions with...

  • C++, Will Upvote: A) Try to get the following programs to run in your environment. B)...

    C++, Will Upvote: A) Try to get the following programs to run in your environment. B) If the programs will run, document each line of code with comments and describe any changes you had to make to the original code to get it to work. If the programs are unable to be modified to run with desirable results, explain why you think so. C) Description your solution and a synopsis of how your code is intended to work and the...

  • in java Part 1 In this section, we relook at the main method by examining passing...

    in java Part 1 In this section, we relook at the main method by examining passing array as parameters. Often we include options/flags when running programs. On command line, for example, we may do “java MyProgram -a -v". Depending on options, the program may do things differently. For example, "a" may do all things while "-v" display messages verbosely. You can provide options in Eclipse instead of command line: "Run ... Run Configurations ... Arguments". Create a Java class (Main.java)....

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT