Help someone translate code from pascal to c # | Sololearn: Learn to code for FREE!

+1

Help someone translate code from pascal to c #

uses crt,math; var b:array [1..255] of integer; k,a,i,n:integer; p:real; begin clrscr; write('Введите двочное число (цифры через пробел): '); p:=0; k:=0; while not eoln do begin read(b[k]); k:=k+1; end; n:=k; for i:=n-1 downto 0 do begin p:=p+power(2,i)*b[i]; end; writeln('Полученное десятичное число: ',p:8:0); p:=p+1; a:=round(p); writeln('Полученное двоичное число: ',a); i:=0; while a>=1 do begin i:=i+1; b[i]:=a mod 2; a:=a div 2; end; n:=i; writeln; for i:=n downto 1 do write(b[i]); readln; end. The code for task: There are given a positive integer n, integers a0, ..., an such that each ai is zero or one and an ≠ 0. The sequence a0, ..., an defines the binary representation of some integer p = an * 2 ^ n + ... + a1 * 2 + a0. There is need to get a sequence of zeros and ones defining a binary representation: the number p - 1.

12/23/2019 9:59:58 AM

Mary_d9

21 Answers

New Answer

+2

i think it's better for you to tell what the task is and how to do it than asking to translate the program here

+2

Mary_d9 I made this, within the code you will find hyperlinks where I got the idea from, through a web search of course. The plan is, get the binary digit sequence as string then convert to int. Next just subtract 1 from the result, and convert back to binary. Unless you are explicitly required to make your own implementation I guess this is okay, it uses built-in support which is probably more optimised. I think the challenge lies in working with negative values, if you choose to try your own implementation. https://code.sololearn.com/cmpkpWlwImzS/?ref=app

+2

Ipang Why in the task did you need a formula for p if you did not use it? And there it says in the condition that the binary number does not end in zero

+2

I copied the pascal code to the playground. It does not run, but it was a way to add comments. https://code.sololearn.com/cyKzgwgVWRoS Have a look at my comments.

+1

Ipang If I understand something. I can translate the output lines with text and that's it. I don’t know pascal at all.

+1

Ipang I need to solve the task, I found a solution on the Internet, it is in Pascal. I study programming the second month and I study only c#.

+1

Ipang I indicated the task after the code

+1

Ipang yes

+1

Ipang I don't have example(

+1

Mary_d9 That code isn't the answer to the task, it was just to show an alternative way for reading the values in, as string rather than an array of int. The built-in conversion algorithm does the job, that's why there's no need to use the formula. It is also possible to check whether the string input does not end with a '0' character, in various ways. To fully comply to the task requirement, means to write your own implementation I suppose.

+1

Mary_d9 Let's wait for others' opinion on this topic. Maybe someone can help to actually port the Pascal code into C#, or even has better idea for custom implementation to follow the task requirements.

+1

Ipang Thank you for all the explanations and for the time you spent, I understand how much time and effort it took you to solve this, thanks to you it has become much clearer to me😊

+1

Mary_d9 No problem at all, it's okay 👌 Maybe I'll get back to this tomorrow, but I hope by then someone could have helped further and a solution is there 👍

0

Can you show what you have tried and how far you had gone through this? Share the C# code in the Description of your original question above, so people can review it, offer suggestion or correction. Follow this guide for sharing links 👍 https://www.sololearn.com/post/74857/?ref=app

0

Mary_d9 Is this a task or assignment in college or something? I thought you know at least aa little about Pascal. It's not right to tell someone to makethe conversion with no prior knowledge about it : )

0

Mary_d9 I'll see if I can do something. It's been forever since I did Pascal and I only slightly remember. Not making any promises though.

0

Mary_d9 An example maybe? I'm not understanding your task description. Particularly this part here -> "There is need to get a sequence of zeros and ones defining a binary representation: the numbera p - 1." So here's what I'm getting from reading your description (cmiiw) we are given an int array containing bits that represents a number <p> in base 2 (binary). And there is a need to find out the binary representation of the number <p> subtracted by one. Am I interpreting it correctly?

0

sneeze thanks a lot😊

-2

-2

Hello