10 Replies - 247 Views - Last Post: 20 February 2013 - 12:22 PM Rate Topic: -----

#1 MattDN93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 13

Syntax-Code Streamlining? Also RE: Printing Chars

Posted 19 February 2013 - 10:57 AM

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?

/*A program to read and perform operations on integers */
#include <stdio.h>

/* declaring the operations will be on integers*/
int divide(int,int); 
int remain(int,int);

int main()
{
	int whole_1, whole_2, answer, rem;
	printf("Input your first value:"); /* tells user to enter first number*/
	scanf("%d", &whole_1);
	
	printf("now input the next value:"); /* tells user to enter second number*/
	scanf("%d", &whole_2);
		
	answer = divide(whole_1, whole_2); /* divides the first by second value*/
	printf("Dividing %d by %d is %d \n", whole_1, whole_2, answer); 
	
	rem = remain(whole_1, whole_2); /* also calculates a remainder*/
	printf("Also, the remainder is %d \n", rem);
	
	return 0;
}

/* below informs what maths to do on the values*/

int divide(int a, int B)/>/>
{
return a / b;	
}

int remain(int a, int B)/>/>
{
	return a % b;
}




Secondly, we needed to use C to print:

+ + +  + + + + + +  + + + + + +  + + + + + +
  +    +            +            +
  +    +            +            +
  +    + + + +      + + + +      + + + +  
  +    +            +            +
  +    +            +            +
+ + +  + + + + + +  + + + + + +  + + + + + +





I realize the policy about not doing the homework assignments, but I was hoping for a step in the right direction. Could I theoretically use


#include <stdio.h>
int main()

{
	char i = '+' ;
	printf("%c%c%c%c%c",i,i,i,i,i);
	return 0;
}



And repeat per line? Searched a lot on the topic but none seem to discuss line space omission and such.

Thanks again in advance!

Is This A Good Question/Topic? 0
  • +

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

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

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.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

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.
int divide(int numerator , int divisor );

int divide(int numerator , int divisor )
{
   return numerator / divisor;	
}



Then when you look at your prototype or implementation you know at a glance the purpose of each parameter.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,493
  • Joined: 19-February 09

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

Posted 19 February 2013 - 02:53 PM

With printf you can print a part of a string as well as pad with blanks. If that is an option you are looking at.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

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

Posted 19 February 2013 - 04:34 PM

Code looks fine.

Personally, I don't like to use more variables than I need. I also don't like to bother with user input until I've got things sorted. I'd have a function like:
void process(int whole_1, int whole_2) {
	printf("Dividing %d by %d is %d \n", whole_1, whole_2, divide(whole_1, whole_2)); 
	printf("Also, the remainder is %d \n", remain(whole_1, whole_2));
}



This way I can plug in testing values until I'm ready to accept user input.

For your banner...

Perhaps:
#define LETTER_HEIGHT 7

void printI(int line);
void printE(int line);

void printIEEE() {
	int i;
	for(i=0; i<LETTER_HEIGHT; i++) {
		printI(i); printE(i); printE(i); printE(i);
		printf("\n");
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 MattDN93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 13

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

Posted 20 February 2013 - 10:12 AM

Thanks for all the responses guys!

@Skydiver How would you suggest division by zero check? Something like this:


void function(int x)
{
    float fx;

    if(x == 0) {
        printf("division by zero is not allowed");
    } else {
        fx = 10/x;
        printf("f(x) is: %.5f",fx);
    }



Also I think the best way will probably be a string of printf() commands, this is only an intro to the module at this point, looping and such will probably be required later anyhow.

@jimblumberg Thanks Jim, I'll keep that in mind, helps me learn what I'm doing!

@#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!
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

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

Posted 20 February 2013 - 10:35 AM

That would be a good approach for this particular program.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

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

Posted 20 February 2013 - 10:39 AM

Quote

How would you suggest division by zero check? Something like this:

Looks correct for testing for division by zero but there is a problem.
        fx = 10/x;



Since both 10 and and x are integers this calculation will probably not produce the results you desire. Remember integer math has no fractions so something like 10/5 would yield zero.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

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.

Here's a working example:
#include <stdio.h>

#define LETTER_HEIGHT 7

void printI(int line);
void printE(int line);
void printIEEE();


int main() {
	printIEEE();
	return 0;
}

void printI(int line) { printf("II%dII ", line); }
void printE(int line) { printf("EE%dEE ", line); }

void printIEEE() {
	int i;
	for(i=0; i<LETTER_HEIGHT; i++) {
		printI(i); printE(i); printE(i); printE(i);
		printf("\n");
	}
}



Now, all you have to do is put useful code in those functions. Have fun.

This post has been edited by baavgai: 20 February 2013 - 10:43 AM

Was This Post Helpful? 1
  • +
  • -

#10 MattDN93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 13

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

Posted 20 February 2013 - 12:07 PM

Quote

Declaring a function with no body is called a prototype.


Thanks! Didn't know that. Will complete the code now :)

@Jim redefining as a float or adding a decimal point to the math would work, correct?
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

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

Posted 20 February 2013 - 12:22 PM

Yes if one element of the calculation is a floating point type it forces floating point math so: 10/5.0, 10.0/5, 10.0/5.0 or (double)10/5 would invoke floating point math.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1