About Ruby to C Language Conversion

Asked 2 years ago, Updated 2 years ago, 45 views

I'm not good at C language, so please help me.
I would like to convert the source code created in Ruby below into C language.

n=gets.to_i

an = 0
i = 1
US>n.times{
  a=gets.to_i
  an + = (a-i).abs
  i = a
}

putans
#include<stdio.h>

int main(void){

int n [100];
intans, i;

    scanf("%d", & n);

    if((1<=n)&(n<=100)){
        for(i=1;i<=n;i++){
               an=ans+(n-i);
        }
    }
    return 0;
}

Thank you for your cooperation.

ruby c

2022-09-30 12:07

2 Answers

First of all, the following parts are unnatural as code in C language.

  • scanf("%d", & n);, so n is naturally declared as the int type, but it is actually an array of int.

Ruby's code also shows that n should be replaced by n by reading only one number from input first, so it seems unnatural to be unnatural to arrange n.

Next, the following parts compare Ruby's code with C's code, which is unnatural.

  • On the C language side, there is an increasing number of "n is 1 to 100?" if statements that are not on the Ruby side.
  • Ruby reads the input further in the code nRepeat times and substitutes a, but there is no code.
  • Similarly, there are no codes in the loop saying "add the absolute value of a-i to ans" or "subscribe a to i.However, Ruby uses the variable i as i=a, while C uses the word counting the number of for statements repeated, so you may need to fix it from there.
  • Similarly, there is no code for ans.

Based on the above, please try to fix the program on the C language side.The absolute value function abs in C language can be used like ans+=abs(a-i) by doing #include<stdlib.h>.

Also, I will link the program that I created to here, so please use it as a reference after fixing it yourself.


2022-09-30 12:07

First, check the specifications of the functions used in the Ruby program.

module function Kernel.#gets

Read a line from the ARGF and return it.

object ARGF

An object that represents a single virtual file that considers the arguments specified in the script (see Object::ARGV) as file names and concatenates those files. If the ARGV is empty, it is intended for standard input.

instance method String #to_i

Interpret the string as a decimal represented integer and convert it to an integer.

class Integer

Integer class.Starting with 2.4.0, Fixnum and Bignum have been integrated into Integer.

instance method Integer #times

Repeat self only times. If self is not a positive integer, do nothing.

instance method Integer#abs

Returns its own absolute value.

module function Kernel.#puts

Outputs arguments and line breaks in order to the standard output $stdout. If there are no arguments, print only new lines.
If the argument is an array, print the elements and line breaks in order. If an object other than an array or string is given as an argument, try to convert the object to an array first by to_ary and then to_s method to a string. For arguments and array elements ending with new lines, puts themselves will not print new lines.

instance method Integer#inspect

Converts an integer to a decimal string expression.

Then, guess what the Ruby program is about.If specifications are specified, use them.
It will probably look like this:

  • Reads a single line of characters (from standard or specified input files), considers them as decimal numbers, converts them to integers, and stores them in the integer variable n

  • Initialize the integer variable ans to 0 and i to 1

  • n Repeat the following actions for minutes

    • Reads a single line of characters (from standard input or specified input file) and converts them into integers by considering them as decimal numbers and stores them in the integer variable a
    • Convert
    • a minus i to an absolute value and add it to ans
    • a to i
  • Convert the ans of the above results to a decimal string and output it to standard output

Reads a single line of string (from standard input or specified input file), considers it a decimal number, converts it to an integer, and stores it in the integer variable n

Initialize integer variable ans to 0 and i to 1

n Repeat the following actions for minutes

  • Reads a single line of characters (from standard input or specified input file) and converts them into integers by considering them as decimal numbers and stores them in the integer variable a
  • Convert
  • a minus i to an absolute value and add it to ans
  • a to i

Convert the ans of the above results to a decimal string and output it to the standard output

Then look at the equivalent function in C language.
If not, replace it with a similar specification or a combination of actions.
The following is not a C language but a VC++ description.
Ruby's Integer is replaced by long because the maximum value cannot be handled by VC++.

memset, wmemset
gets_s,_getws_s
Range of data types
atoll, _atoll_l, _wtoll, _wtoll_l
abs, labs, labs, _abs64
_itoa_s, _ltoa_s, _ultoa_s, _i64toa_s, _ui64toa_s, _itow_s, _ultow_s, _i64tow_s, _ui64tow_s
puts,_putws

Examples of conversions will be as follows:
(However, it's the MBCS version.Unicode and definition versions need to be considered and addressed a little more.)
It is not completely equivalent, but it is considered almost identical.

#include "pch.h" // For VC++ console applications: Not required for C language version

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

#defineWORK_BUFFER_SIZE24
#define MAX_INPUT_COLUMN20

int main()
{
    char buffer [WORK_BUFFER_SIZE] = {0};
    gets_s(buffer,MAX_INPUT_COLUMN);
    long long n = atoll(buffer);

    long longans = 0;
    long long i = 1;

    for (long long counter=0; counter<n;counter++) {
        memset(buffer,0,WORK_BUFFER_SIZE);
        gets_s(buffer,MAX_INPUT_COLUMN);
        long long a = atoll(buffer);

        an + = llabs(a-i);

        i = a;
    }

    memset(buffer,0,WORK_BUFFER_SIZE);
    _i64toa_s(ans, buffer, work_BUFFER_SIZE, 10);
    puts(buffer);

    return 0;
}


2022-09-30 12:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.