ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I see you've been getting pretty good help with your homework from LQ so far. But you would learn more if you listened in class.
I don't think the actual assignment said anything about "octal variable" or "octal type".
I expect most of the programmers here could guess what the assignment really was (despite the bad description you just posted) and do it for you. But I hope no one will.
Representing a number in a specific numeric base means representing it as a series of digits. In C, the data type you would use to store a series of digits is an array of char.
/*To Convert a Decimal number to Octal*/
extern int printf(const char *fmt , ...);
void main()
{
int m,i,n,z,x,b,c,d[100];
printf("Enter a Number : ");
scanf("%d",&n);
z=n;
b=1;
while(n>0)
{
c=n%8;
b=b*10+c;
n=n/8;
}
n=b;
b=0;
while(n>0)
{
c=n%10;
b=b*10+c;
n=n/10;
}
b=b/10;
printf("Octal Value for the given number is : %d ",b);
}
alex1983-0112: thanks alot ! btw, why did u initialized so many unused variables ?
In this code?
Code:
/*To Convert a Decimal number to Octal*/
extern int printf(const char *fmt , ...);
void main()
{
int m,i,n,z,x,b,c,d[100];
printf("Enter a Number : ");
scanf("%d",&n);
z=n;
b=1;
while(n>0)
{
c=n%8;
b=b*10+c;
n=n/8;
}
n=b;
b=0;
while(n>0)
{
c=n%10;
b=b*10+c;
n=n/10;
}
b=b/10;
printf("Octal Value for the given number is : %d ",b);
}
There are many more things wrong with this code. Since this is a homework assignment, you'll have plenty of opportunity to clean them up so you can transform this from a C- to an A.
I won't point out the defects, because it's your homework assignment. I don't normally just slam someone's work without being specific, but I felt a warning was necessary.
Three weeks from today (if I remember and if the moderator doesn't lock the thread, please please) I will come back and explain.
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
Quote:
bgeddy: I know that, but I need to save the different representations of the value in a data sheet, not to print it to the screen ...
Well the point is the printf family of functions (fprintf,sprintf,etc) provide ready to use easy format conversions. The integer variable itself I used is constant but it's representation changes via the format specifier as desired.
alex1983-0112: thanks alot ! btw, why did u initialized so many unused variables ?
I cut out and have inserted this piece of a code from the project which wrote at studying of programming language C. It is simply indicative example. I would consider that he has helped you to understand a principle and on the basis of this principle already to realise that what you want. I have made something not so?
Yes and no. The other things I wish to say about the code are not of vital importance, and I intend to address them in three weeks, past the likely homework due date.
Homework is something a teacher gives you, and that YOU should complete by yourself, and not have us do it for you. Why ? Because it's actually bad for you, what are you gonna do on the test ? We're not gonna be there with you.
Either way for this I agree with johnsfine, use printf or sprintf.
Actually, your English is pretty good. My Russian is nonexistent, so you're way ahead of me!
And the only way the homework issue affects you is that we try to avoid giving source examples to people like SpLaSh212 who are completing homework assignments, because they should write the code themselves.
Once they've done that, if they have questions about why their code doesn't work, then we jump in and help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.