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); }
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:
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:
#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:
#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:
#if you have any doubts comment below.....
Write the proposed program in Java, JavaScript, and Python. Run them and compare the results. Submit...
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 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, 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! 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...
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) 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 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 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...
Write and submit the source code for the following program. The program will use an integer array of size 10 to store the prices of smartphones. It will then determine and print the prices of the most expensive and cheapest phones. Use the following variables: int[] prices = new int[10]; // Array of smartphone prices Assignment Ask the user for the price of each smartphone (using a for loop) Sort the list of smartphones (once) from low to high price...
use PYTHON Write a program that will take an input file called input.txt which will contain a Java program and parse it and output an annotated version. Your program will track the nesting depth of the braces of the input file and will output an annotated version of the file. Your final program will 1. List the nesting depth of curly braces 2. Ignore braces inside quotes or comments Use Python Assume that all quoted strings begin and end on...