Syntax-Code Streamlining? Also RE: Printing Chars

Hey everyone, obviously quite a noob, studying Computer Engineering at university, I'm quite advanced when it comes to general PC hardware and software but programming is new for me. We've been required to write code in C using Bloodshed.net's Orwell Update C++ application. I've got 2 queries:

We were informed to "write a program in C that reads two whole numbers into two variables of type int, and then outputs both the whole number part and the remainder of the division of the first number by the second. This can be done by using the operators = and %."

I've completed the code, but since I'm new at this, is there a better way of writing this in C?

Replies To: Syntax-Code Streamlining? Also RE: Printing Chars

Re: Syntax-Code Streamlining? Also RE: Printing Chars

Posted 19 February 2013 - 01:51 PM

That's pretty good code for displaying the quotient and remainder. The only thing I would add is a check for division by zero.

As for printing out the banner, what is wrong with a brute force approach of using 7 printf()'s? If you aren't allowed to have 7 printf()'s use a single one with a super long string with linefeeds embedded. If a super long string isn't allowed, only then resort to the more elegant data driven loop that prints out each raster line one at a time.

The harder approach is to attempt to create the characters one at a time. It's doable, but a little harder, and in the end, you'll still need to loop to print out each raster line.

A way to avoid printing each raster line is to use the curses library or whatever native console APIs are available on your target system.

Re: Syntax-Code Streamlining? Also RE: Printing Chars

Posted 19 February 2013 - 01:51 PM

Quote

I've completed the code, but since I'm new at this, is there a better way of writing this in C?

To me the program looks okay and is a good try and using the functions, while not really necessary for this problem is a good way to familiarize yourself with using functions. The only suggestion I have deals with your function prototypes. Normally I recommend that you include the variable names in the prototype, if you use meaningful variable names you will help document your code.

@#define As Skydiver mentioned, would it be multiple printf() commands with lots of whitespace then I guess?

@baavgai Yeah one of the later questions required we do the operations inside the printf() instruction. So you've voided the process instead of returning a value from another function written below int main() correct?

As for the banner that code throws me multiple Linker errors in function "printIEEE" is says "undefined reference to printI" and "undefined reference to printE" etc. Do I need to change anything? Compiling after saving a C and C++ file gave me those errors.

***
Thanks again for the really helpful advice everyone, much appreciated!

Re: Syntax-Code Streamlining? Also RE: Printing Chars

Posted 20 February 2013 - 10:42 AM

It's kind of your job to write printI and printE. Declaring a function with no body is called a prototype. You're expected to provide the body somewhere else. If you don't provide the body, you'll get an error at link time, though it will usually compile just fine.