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 AMMary_d9
21 AnswersNew Answer
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
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
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
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.
Ipang If I understand something. I can translate the output lines with text and that's it. I don’t know pascal at all.
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#.
Ipang I indicated the task after the code
Ipang I don't have example(
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.
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.
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😊
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 👍
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
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 : )
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.
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?
sneeze thanks a lot😊