• 01-07-2013
Moody Barrawi
Okai here is the Code so far, It's not bad :p But the teacher told me to add a Main function that calls my sum function. WTF? wouldn't the compiler go crazy if I change the 'main' function because its the MAIN! function. And what does he mine by a function that calls the other. Here is the program it works, but I need someone to just organize it with a function that calls this sum function. So this is simple basic C-free program so no complex things please
Code:

#include <stdio.h>

int sum(int x, int y);

int
main(void)
{
int x,y;
x=7;y=2;

printf("    x  y  result\n\n");

printf("%4d%4d%4d\n",x,y,sum(x,y));

printf("%4d%4d%4d\n",x,y,sum(y,x));

printf("%4d%4d%4d\n",sum(sum(y,x),y),y,sum(x,y));

printf("%4d%4d%4d\n",(sum(sum(y,x),sum(y,x))),y,sum(x,y));

printf("%4d%4d%4d\n",(sum(sum(y,x),sum(y,x))),sum(y,y),sum(x,y));

return(0);
}

int sum(int x,int y)
{
int result;
result=x+y;

return(result);
}

• 01-07-2013
grumpy
That main() function does call the sum() function.

I'm guessing your teacher wants to see things that are more readable and clear, rather than aspiring to do multiple things in single statements.
Code:

some_variable = sum(x,y);
/*  lots of other similar but distinct statements */

and then separate
Code:

printf("%4d%4d%4d\n", x,y, some_variable);
/*  other similar statements to print out other variables */

Simply replace "some_variable" with variable names that are meaningful to your program.

Bear in mind that, if you write multiple effects into single statements, the code is harder to read, header to get right, and harder for someone else (such as a teacher marking your homework) to understand.

Teachers are often unwilling to put effort into understanding code that is unnecessarily hard to read, such as yours. View that as a preparation for real-life programming - I once sacked a programmer who insisted on writing code that was hard to read.
• 01-07-2013
Moody Barrawi
This program when run is supposed to make these answers... So if I do what your saying it would give me these answers?
 x y z 7 2 9 7 2 9 11 2 9 18 2 9 18 4 9
• 01-07-2013
grumpy
Sure, if you do it right. Not if you do it wrong, obviously.

My answer is generic. You need to flesh things out with some specifics - based on whatever is required based on the homework question you have been asked.
• 01-07-2013
Moody Barrawi
It's no homework. we have this book here in UNI that we can do these exercises in preparation for our exams next week. And I'm training to write programs so that I won't screw up in the exam. So I want my program to exactly match the questions in the book and actually be prefect flawless ones :p
• 01-07-2013
c99tutorial
Quote:

Originally Posted by Moody Barrawi
This program when run is supposed to make these answers...
 x y z 7 2 9 7 2 9 11 2 9 18 2 9 18 4 9

If that's true, why not do

printf("%d %d %d\n", 7, 2, sum(7,2));

You can place similar statements to get the other results. And they are all called from main, fulfilling the requirements. It's not necessary to nest the function calls as in sum(sum(sum(...
• 01-07-2013
Moody Barrawi
OOh I actually get what you mean... But can I like do a *x in the function to change the value so it would change in the main program so I don't have to change it again and again over and over. So basically I can change it from there and like this.. So in the end how would it look like>?
• 01-07-2013
c99tutorial
Quote:

Originally Posted by Moody Barrawi
But can I like do a *x in the function to change the value so it would change in the main program so I don't have to change it again and again over and over.

The &x while calling the function introduces a pointer, which can be useful sometimes, but if the functions only returns a single value you probably don't need it.

If you want an example, suppose you want to find both the sum and the product of two numbers a and b using a single function. You might write the following function:

Code:

void find_sum_prod(int *sum, int *prod, int a, int b);
This function should calculate a+b and place it into the integer that `sum' points to. Then it should calculate a*b and place it into the integer that `prod' points to.

Functions that accept pointers generally follow this principle. But in your sum(a,b) example it makes more sense to return the sum using the return value, not to return it via a pointer.
• 01-07-2013
Moody Barrawi
I see thanks I will try that now