C programming help.

Post » Tue Aug 09, 2011 11:47 pm

The question I am working on is
"given a = 5, b = 1, x = 10, and y = 5, create a program that outputs the result of the formula f = (a - b )(x - y) using a single printf() function"

So far I have

#include

main()

{

intF = (5 - 1) * (10 - 5);

}


When I run it in cygwin I just get errors. I really don't know anything about programming but am forced to take this beginner course.

Any help with this would be really appreciated.
User avatar
James Baldwin
 
Posts: 3366
Joined: Tue Jun 05, 2007 11:11 am

Post » Tue Aug 09, 2011 3:04 pm


Uhhh, I decided to skip programming, so...

echo 20?
User avatar
Alan Cutler
 
Posts: 3163
Joined: Sun Jul 01, 2007 9:59 am

Post » Tue Aug 09, 2011 12:42 pm

The question I am working on is
"given a = 5, b = 1, x = 10, and y = 5, create a program that outputs the result of the formula f = (a - b )(x - y) using a single printf() function"

So far I have

#include

main()

{

intF = (5 - 1) * (10 - 5);

}


When I run it in cygwin I just get errors. I really don't know anything about programming but am forced to take this beginner course.

Any help with this would be really appreciated.
When declaring variables you have to set the type, for a number ( or more commonly referred to as an integer ) you do this
int myInt;
That will create the integer 'myInt' with no value. To add a value, after its declaration, you can do this.
myInt = 10;
So now myInt is declared and is 10. I'm assuming your teacher wants you to declare each variable and assign its value, so to save from doing something like
int myIntfoo = 10;int myIntbar = 5;int myIntn = 1;
So on and so forth to declare all your variables on a new line, you can save time by declaring the type then each variable separated by commas.
int myIntfoo = 10, myIntbar = 5, myIntn = 1;
To add variables together, following my examples so far, you simply write something like this
int foobar = myIntfoo + myIntbar;
Now foobar will be 15. You can also, in the interest of saving variables, write something like this
myIntfoo = myIntfoo + myIntbar;
Which will again be 15. The same goes with multiplying, dividing, and subtracting. I'm not familiar with printf, embarrassingly enough, but I know you'll need to write something like
printf("foo: %1 \nbar: %2\nn: %3", (myIntfoo, myIntbar, myIntn));
So it outputs the numbers given in their order or something, like I said I don't know how the flags work. Anyway, I hope I helped you a bit without doing all your homework for you. :D
Edit: I learned how to use printf() real quick and it's pretty simple. There may be a more concise way to print multiple variables, but I'll let someone else teach you how to do that.
Using the same variables, I'll write a working function to print them all out.
Your first problem was declaring main. Each function needs a type, just like a variable. main is a special function and must be an int.
int main(){}
I'm sure you already know how functions work after proper declaration. Now let's stick our variables inside.
int main(){	int myIntfoo = 10, myIntbar = 5, myIntn = 1, myIntsum;	myIntsum = myIntfoo * myIntbar + myIntn;}
As you can see, I added a new variable "myIntsum" and set it equal to a small equation using the other variables. Let's move onto using printf(), shall we? Let's run down how it works quickly.
printf(text, variable);
Text can be anything, and is where you include what are called "flags" which I'll show you how to use in a minute. Let's use printf() so it outputs myIntfoo.
printf("myIntfoo: %i", myIntfoo);
%i is a flag, and it basically says that the variable ( as we went over before is the thing after the comma ) is an integer and to output it. So, altogether, we have the text simply saying "myIntfoo: 10". You'll see I use \n a lot, and all this does is have any preceding text on a new line. One last thing, simply for ease of use and good habits. getchar() is a simple function which, in the current use, waits for you to press a key until continuing on in the program. So, in the code below it just waits for you to press a key before exiting.
#include // two forward slashes is a single line comment/* a forward slash and asterisk is a multiline comment */int main(){	int myIntfoo = 10, myIntbar = 5, myIntn = 1, myIntsum; // declare variables	myIntsum = myIntfoo * myIntbar + myIntn;	printf("foo:%i\n",myIntfoo); // output myIntfoo	printf("bar:%i\n",myIntbar); 	printf("n:%i\n",myIntn); 	printf("The sum:%i\n",myIntsum);	getchar(); // wait for a key press and exit	return 0;}

User avatar
rae.x
 
Posts: 3326
Joined: Wed Jun 14, 2006 2:13 pm

Post » Wed Aug 10, 2011 1:34 am

I'm not familiar with printf, embarrassingly enough, but I know you'll need to write something like
printf("foo: %1 \nbar: %2\nn: %3", (myIntfoo, myIntbar, myIntn));
So it outputs the numbers given in their order or something, like I said I don't know how the flags work. Anyway, I hope I helped you a bit without doing all your homework for you. :D

That would need to be
printf("foo: %d\nbar: %d\nn: %d\n", myIntfoo, myIntbar, myIntn);

The usual substitution for an integer is "%d", and the integers will be printed in the order in which they are presented in the argument list. And just in case it hasn't already been mentioned, those "\n"s are newlines...
User avatar
Liv Brown
 
Posts: 3358
Joined: Wed Jan 31, 2007 11:44 pm

Post » Tue Aug 09, 2011 6:43 pm

What the heck is myintfoo and myintbar? I have not had those commands yet in my reading.
User avatar
David John Hunter
 
Posts: 3376
Joined: Sun May 13, 2007 8:24 am

Post » Wed Aug 10, 2011 12:36 am

What the heck is myintfoo and myintbar? I have not had those commands yet in my reading.


they are variable names
User avatar
Yama Pi
 
Posts: 3384
Joined: Wed Apr 18, 2007 3:51 am

Post » Tue Aug 09, 2011 6:18 pm

yeah, theyre just arbitrary names. Rename them as you wish (just be consistent)
User avatar
Connie Thomas
 
Posts: 3362
Joined: Sun Nov 19, 2006 9:58 am

Post » Tue Aug 09, 2011 4:45 pm

I'll go with Capital on that one since I only know C++ not C programming. Only thing I might change is the naming of the int functions to A, B, X, and Y so it goes in line with the requirements of the program instead of naming it something different. Don't know if your Teacher/Instructor is a stickler for that type of attention to detail or not.
User avatar
Darren Chandler
 
Posts: 3361
Joined: Mon Jun 25, 2007 9:03 am

Post » Tue Aug 09, 2011 7:09 pm

Very helpful posts, thanks you. But when I try to change the names of the variables it ends up messing it up. I can get a few to change right but not all of them. Also there are more variables in my project then was in the example. I tried adding another but it just won't read it. I tried replacing foo with X and what not, but it just errors.
User avatar
Honey Suckle
 
Posts: 3425
Joined: Wed Sep 27, 2006 4:22 pm

Post » Wed Aug 10, 2011 1:00 am

What's the error? Something about undeclared variable?
User avatar
Nikki Lawrence
 
Posts: 3317
Joined: Sat Jul 01, 2006 2:27 am

Post » Tue Aug 09, 2011 3:41 pm

yep, pretty much. And some other mixed in there too, I think.
User avatar
joannARRGH
 
Posts: 3431
Joined: Mon Mar 05, 2007 6:09 am

Post » Tue Aug 09, 2011 12:12 pm

Ok I think I got it besides one problem. I can only use a single printf function, so I need to combine all these printfs into one printf

printf("X:%i\n",myIntX);
printf("A:%i\n",myIntA);
printf("B:%i\n",myIntB);
printf("Y:%i\n",myIntY);
User avatar
Nikki Morse
 
Posts: 3494
Joined: Fri Aug 25, 2006 12:08 pm

Post » Tue Aug 09, 2011 11:18 pm

int is one variable type. It has to be followed by variable name that can be anything but a reserved name. Then you can use the variable to assign it a value. If you are trying to do it in another order like assigning a value to before introducing the variable first, the compiler will give you an error because it goes "the heck is this". :) My guess is something like that is happening.
User avatar
Chica Cheve
 
Posts: 3411
Joined: Sun Aug 27, 2006 10:42 pm

Post » Tue Aug 09, 2011 9:08 pm

Ok I think I got it besides one problem. I can only use a single printf function, so I need to combine all these printfs into one printf

printf("X:%i\n",myIntX);
printf("A:%i\n",myIntA);
printf("B:%i\n",myIntB);
printf("Y:%i\n",myIntY);

I'm not sure if you missed vometia's post.
That would need to be
printf("foo: %d\nbar: %d\nn: %d\n", myIntfoo, myIntbar, myIntn);

The usual substitution for an integer is "%d", and the integers will be printed in the order in which they are presented in the argument list. And just in case it hasn't already been mentioned, those "\n"s are newlines...

User avatar
lillian luna
 
Posts: 3432
Joined: Thu Aug 31, 2006 9:43 pm

Post » Tue Aug 09, 2011 10:19 pm

From the wording of the assignment, I got the idea that you were supposed print out a single number that was the result of the equation f with the given input..
User avatar
Charity Hughes
 
Posts: 3408
Joined: Sat Mar 17, 2007 3:22 pm

Post » Tue Aug 09, 2011 11:34 pm

Yes I think you are right, I am totally lost again.
User avatar
Adrian Powers
 
Posts: 3368
Joined: Fri Oct 26, 2007 4:44 pm

Post » Tue Aug 09, 2011 1:24 pm

The solution would be along the lines of:

#include /* * the exact declaration of main varies from * system to system and should ideally follow * whatever your tutor suggested.  "void" indicates * that it has no return value. */void main(){    /* forward declarations - may or may not want to use const here */    int a = 5;    int b = 1;    int x = 10;    int y = 5;    /* workspace for result */    int f;    /* perform the calculation */    f = (a - b ) * (x - y);    /* and print the result */    printf("f = %d\n", f);    /* finished */}

Now the trick is to make sure you understand what it's doing. ;)


Edit: removed a stray capital that the autoformatter had inserted: some versions of C are case-sensitive.
User avatar
lilmissparty
 
Posts: 3469
Joined: Sun Jul 23, 2006 7:51 pm

Post » Tue Aug 09, 2011 4:32 pm

That one seems to work. I had to change void to int but it is working now. Thank you very much for your time helping me out.
User avatar
Alyce Argabright
 
Posts: 3403
Joined: Mon Aug 20, 2007 8:11 pm

Post » Tue Aug 09, 2011 7:24 pm

#include void main(){   \snip }


May I ask, why did you make "main" a void?

I learned that we should make "main" an int, and end it with "return 0".
(I am not sure why though)
User avatar
Dylan Markese
 
Posts: 3513
Joined: Sat Dec 01, 2007 11:58 am

Post » Wed Aug 10, 2011 3:20 am

May I ask, why did you make "main" a void?

I learned that we should make "main" an int, and end it with "return 0".
(I am not sure why though)

that is mostly programmers flavor of choice, since main doesn't need to return anything
User avatar
Yonah
 
Posts: 3462
Joined: Thu Aug 02, 2007 4:42 am

Post » Wed Aug 10, 2011 12:24 am

May I ask, why did you make "main" a void?

I learned that we should make "main" an int, and end it with "return 0".
(I am not sure why though)

It's largely system-dependent: some of them don't expect main() to return anything, in which case it gets declared as void, others do expect a return value (whether or not they actually use it: often exit(2) is what actually provides a useful return status) in which case it would normally be an int. I forget which is which, though I suspect most actually want the int declaration nowadays, in which case it should probably finish with a return statement to stop the compiler moaning... unless it makes a special exception for main, that is. As with Unix, its history is all a bit frayed and messy!
User avatar
Lisa
 
Posts: 3473
Joined: Thu Jul 13, 2006 3:57 am

Post » Wed Aug 10, 2011 2:30 am

I guess the c++ (?) standard requires the main function to return int. ISO C does not specifically state the return value. Even then it's up to the compilers to complain about it or not and follow the standards, so it kinda varies. Some do, some don't, like vometia said. If the return value is used, it is passed to the environment where it was called from. This would only be meaningful on command line though, I think. The last time I had anything to do with trying to interpret the return value from the main function was about 15 years ago in either DOS or Unix/Linux, I forgot. return 0 would be "success", anything else an error that is up to the caller to interpret, the error level, or exit status. Usually just 0 or 1 though.

Edit: Hmm, now I got confused while pondering the difference between the explicit call to exit(0) in main function instead of just returning 0....
User avatar
Hannah Whitlock
 
Posts: 3485
Joined: Sat Oct 07, 2006 12:21 am

Post » Tue Aug 09, 2011 12:08 pm

Edit: Hmm, now I got confused while pondering the difference between the explicit call to exit(0) in main function instead of just returning 0....

exit() will immediately abort the program and return whatever status code is provided to the caller's wait() system call; return will do pretty much as expected: on some systems the routine that starts up main() is called _start() or something along those lines, which if I had to hazard a guess will pass whatever value is returned to an exit() call of its own. But that's guesswork on my part!
User avatar
Tanika O'Connell
 
Posts: 3412
Joined: Fri Jan 26, 2007 1:34 am

Post » Tue Aug 09, 2011 7:28 pm

I guess the c++ (?) standard requires the main function to return int. ISO C does not specifically state the return value..


From the C99 (Draft, I can't afford to buy the full one) standard:
5.1.2.2.3 Program termination
If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified


In other words, main with a void return type is unspecified. However there is no need to write "return 0", as this will happen automatically.

Most compilers have an flag to enable all errors (-Wall on gcc if I remember rightly). It is worth running this flag.
User avatar
kyle pinchen
 
Posts: 3475
Joined: Thu May 17, 2007 9:01 pm

Post » Tue Aug 09, 2011 11:07 am

#include int main(void){     printf("Answer: %d", (5-1)*(10-5));    //computes (5-1)*(10-5) amd prints the answer. Output should be (Answer: 20)     return 0;}


One single printf.
User avatar
Stephanie Kemp
 
Posts: 3329
Joined: Sun Jun 25, 2006 12:39 am

Next

Return to Othor Games