필터 지우기
필터 지우기

c++ to matlab

조회 수: 1 (최근 30일)
Faisal Al-Wazir
Faisal Al-Wazir 2022년 3월 7일
편집: Walter Roberson 2022년 3월 8일
i need somone to convert c++ to matlab
#include<iostream>
using namespace std;
main(){
int num,length,n;
cout<<"Please enter a number between 0 and 255: ";
cin>> num; // Taking input
n=num; //Copy of input number for operations so that input number is stored intact
if(num>255 || num<0){ // Validating range
cout<<"Number out of range";
exit(0);
}
string binary=""; // Initializing Null (empty) String
while(n>0){ // Process of converting decimal to binary
binary=to_string(n%2)+binary; // Concatenating Strings (each new bit in front of other bits)
n=n/2;
}
length=binary.length(); // If length of binary is less than 8 then convert add trailing zeros in front
if(length<8){
for(int i=length+1;i<=8;i++)
binary="0"+binary;
}
cout<<"The binary equivalent of "<<num<<" is "<<binary;
}
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 3월 8일
Your assignment requires that you create a flowchart first and work from that.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2022년 3월 8일
You'll learn more if you do it yourself than if someone else does it for you. Here are some tips:
  1. Get rid of the first 4 lines of your code.
  2. Get rid of any { that starts a block of statements.
  3. Replace any } that ends a block of statements with "end"
  4. Replace comment starter // with %.
  5. Replace string binary="" with binary=""
  6. Replace exit(0) with return;
  7. Replace "length=binary.length()" with "bLength = length(binary)"
  8. Replace "for(int i=length+1;i<=8;i++)" with "for i = bLength+1 : 8"
Handle remaining errors as you encounter them. Post resulting code that you can't fix here.
  댓글 수: 4
Image Analyst
Image Analyst 2022년 3월 8일
Two hours -- that will be plenty of time. I'm sure you're not allowed to turn in my work as your own anyway. Here's a start (almost 90% done for you):
num = input("Please enter a number between 0 and 255: ");
n=num; %Copy of input number for operations so that input number is stored intact
if(num>255 || num<0) % Validating range
uiwait(errordlg("Number out of range"));
return;
end
binary=""; % Initializing Null (empty) String
loopCounter = 1;
maxIterations = 1000;
while(n>0) && loopCounter < maxIterations % Process of converting decimal to binary
binary = to_string(n%2)+binary; % Concatenating Strings (each new bit in front of other bits)
n=n/2;
loopCounter = loopCounter + 1;
end
bLength = length(binary); % If length of binary is less than 8 then convert add trailing zeros in front
if (bLength < 8)
for i= bLength + 1 : 8
binary="0" + binary;
end
fprintf("The binary equivalent of %d is %s\n", num, binary);
end
See if you can finish it.
Faisal Al-Wazir
Faisal Al-Wazir 2022년 3월 8일
thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by