Question

Hello, I need help with the function below, The language I am using is Ocaml open...

Hello, I need help with the function below, The language I am using is Ocaml

open Printf

let () =
   for i = 1 to Array.length Sys.argv - 1 do
if i + 1 <> Array.length Sys.argv then
(
   if Sys.argv.(i) > Sys.argv.(i+1) then
   (
--> printf "%s\n" Sys.argv.(i+1);

   )

       else printf "%s\n" Sys.argv.(i);
   )
  
   else printf "%s\n" Sys.argv.(i)
  
done;;

the function is taking command arguments and print them in order. It's taking sting. this is my kind solution but I have one problem. if I print args(i+1) then argue(i) will never get printed. I need a way to swap (i+1) with (i), where I point the arrow, if (i+1) printed first. Thank you. Please explain.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

I will try to answer this question with equivalent Java Code. I will explain the steps. In order to get the output in ascending order we have to sort the elements. You can solve the above question by using two for loops instead of one. Look at the following java code

String args[]={"a","c","d","b","e"};

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

for(int j=0;j<args.length-i-1;j++){

int result=args[j].compareTo(args[j+1]);//Here the elements will be compared with the next element. If args[j] >args[j+1] then result will be positive.If args[j] < args[j+1] then result will be negative. if they are equal then result will be 0.

if(result>0){ // for integer array you can use directly, if( args[j]> args[j+1]), then we need to swap these two

// logic to swap two variables

String temp=args[j];

args[j]=args[j+1];

args[j+1]=args[j];

}

}

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

System.out.println(args[i]);// printing the ascending ordered array i.e., a,b,c,d,e

}

Now, we have to apply the same logic in your question as well.

let () =
   for i = 1 to Array.length Sys.argv - 1 do

for j=1 to Array.length Sys.argv-i-1 do

if Sys.argv.(j) > Sys.argv.(j+1) then
   (

//Swaping the two variables

let temp=Sys.argv.(j);

Sys.argv.(j+1)=Sys.argv.(j);

Sys.argv.(j)=temp;
   )

  

for i=1 to Array.length Sys.argv-1 do

printf "%s\n" Sys.argv.(i)
  

Hope that answers the question. Thank You

Add a comment
Know the answer?
Add Answer to:
Hello, I need help with the function below, The language I am using is Ocaml open...
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
  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • The original code using the gets() function is written below. You need to do (a) change...

    The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • Hello, I am having trouble with a problem in my C language class. I am trying...

    Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct {     char name[MAX_SIZE1];     int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...

  • Hey, I really need help understanding the math get the answer that is given. Please help....

    Hey, I really need help understanding the math get the answer that is given. Please help. Thanks in advance. for (int k =0; k s Ign; k++) {for (int i =0; is n2; i++) Print “Hello World"; for (int j =0; j s n; j++) Print “Hello World"; } When n = 2, how many times will “Hello World” be printed?16 When n =4, how many times will “Hello World” be printed? 66 Assuming that the print is the basic...

  • Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include...

    Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <fcntl.h> static char* args[512]; pid_t pid; int command_pipe[2]; static void waiting(int n); static int command(int input, int first, int last) { int fd[2]; int flag=0; pipe( fd );   pid = fork(); if (pid == 0) { for(int i=0;args[i]!=0;i++) { if(args[i][0]=='>') { fd[1]=open(args[i+1], O_CREAT|O_TRUNC|O_WRONLY, 0644); flag=1; } if(args[i]=='>>' ) { fd[1]=open(args[i+1],...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • Hello! In this bash program, there is an error when i try to execute it. I am running it on Unix and I used emacs for the text editor it says: line 3: [0: command not found line 9: if[-f ]: command no...

    Hello! In this bash program, there is an error when i try to execute it. I am running it on Unix and I used emacs for the text editor it says: line 3: [0: command not found line 9: if[-f ]: command not found line 10: syntax error near unexpected token `then' line 10: `then' ------------------------------- #!/bin/bash if [ $# != 1 ] then echo "Usage: myScript.sh " exit 1 fi if [ -f $1 ] then awk ' BEGIN...

  • I need help with the following When you run it like the following: python3 decorator.py CrocodileLikesStrawberries...

    I need help with the following When you run it like the following: python3 decorator.py CrocodileLikesStrawberries The token "CrocodileLikesStrawberries" allows you to interact with the functions. This code is OK, but there is repetition with the checks for auth_token. We could pull this out into it's own function, but what is even better design is to use a decorator. Create an authorise decorator and use it with the refactored code given below. import sys MESSAGE_LIST = [] @authorise def get_messages():...

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
ADVERTISEMENT