c++ to matlab
조회 수: 1 (최근 30일)
이전 댓글 표시
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
2022년 3월 8일
Your assignment requires that you create a flowchart first and work from that.
채택된 답변
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:
- Get rid of the first 4 lines of your code.
- Get rid of any { that starts a block of statements.
- Replace any } that ends a block of statements with "end"
- Replace comment starter // with %.
- Replace string binary="" with binary=""
- Replace exit(0) with return;
- Replace "length=binary.length()" with "bLength = length(binary)"
- 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
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!