Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jbailey8734

macrumors newbie
Original poster
Jan 31, 2008
7
0
So i'm currently taking a C for programmers online class at my college, and my teacher has given suggestions on c compilers to download for windows trouble is I just got a macbook for christmas and still getting used to it (i used to use windows). I downloaded Xcode 3.0 but can't figure out how to build simple C programs for the homework assignments. Is there a simple way for me to use Xcode to do my work or is there a less complicated compiler i could use. Thanks for the help
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
So i'm currently taking a C for programmers online class at my college, and my teacher has given suggestions on c compilers to download for windows trouble is I just got a macbook for christmas and still getting used to it (i used to use windows). I downloaded Xcode 3.0 but can't figure out how to build simple C programs for the homework assignments. Is there a simple way for me to use Xcode to do my work or is there a less complicated compiler i could use. Thanks for the help

Xcode isn't a compiler, it's an IDE that includes the GCC compiler (unlike VisualStudio, you don't need Xcode to get gcc). Once installed (by Xcode, fink, macports, etc.), you can access gcc from the terminal like you can any other UNIX utility.

For small scale, simple homework assignments, don't worry about the IDE. Just do it from the command line:

gcc -g -o hello_world hello_world.c
 

asciimov

macrumors newbie
Jan 31, 2008
10
0
Start Xcode

in finder File -> New Project...

in Assistant, find bolded Command Line Utility

click the triangle to display its options

select Standard Tool

click next

give name... and project directory

click finish
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
So i'm currently taking a C for programmers online class at my college, and my teacher has given suggestions on c compilers to download for windows trouble is I just got a macbook for christmas and still getting used to it (i used to use windows). I downloaded Xcode 3.0 but can't figure out how to build simple C programs for the homework assignments. Is there a simple way for me to use Xcode to do my work or is there a less complicated compiler i could use. Thanks for the help

Start XCode, "New Project...", then choose "Standard Tool" under "Command Line Utility". To see output, press Command-Shift-R ("Show Console" in the "Run" menu).
 

jbailey8734

macrumors newbie
Original poster
Jan 31, 2008
7
0
Thanks for the responses it helped, but so I fully understand can someone explain doing this example assignment with the explained xcode guide.

Write a C program, speed.c, which does the following (be sure to give all numeric output to two decimal places):

• assigns a value (425.5) representing the distance in miles traveled by a car
• assigns a value (7.5) representing the number of hours taken to travel that distance
•computes the speed of the car in miles/hour (speed = distance / time)
•computes the speed of the car in meters/second (1 mile = 1600 meters)


Use the following guidelines to develop your program:

• declare your variables with appropriate data types
• assign values to your variables
• perform your calculations
• generate appropriate output


Points to remember:

Make sure you are creating a C program and not a C++ program. The .c suffix to your source code will invoke the C compiler while the .cpp suffix will invoke the C++ compiler (which is the default). As this is a class in C and not C++, please make sure your source code uses the .c suffix.

You should not be using any global variables in your program. A global variable is a variable declared outside of main().

Be sure to read the document on Capturing Program Output. It can be found in:

Course Documents | 1 - Microsoft Visual C++ Express Edition Tutorial | Capturing Program Output

Be sure to include the output file, csis.dat, in your zip archive.
 

aross99

macrumors 68000
Dec 17, 2006
1,541
1
East Lansing, MI
I'm going to take a stab in the dark here, and say that for simple programs like this assignment, you would be better off using gcc from the command line.

I'm a bit of an old school C programer from a Unix background, but this should work...

Create your speed.c program using a text editor, like TextEdit, and then compile it (from a Terminal Window) like this:

gcc -o speed speed.c

This command runs the "gcc" compiler, and outputs a program (-o speed) from your source program (speed.c). Make sure your Terminal window is in the same directory as your source file (use the "cd" command to change to the directory you want).

If you get any errors, go back and edit speed.c and then fix them.
Recompile again until you don't get any errors.

When you are done, run your program, by typing this command:

speed

If you use standard printf statements for output, you should see it on the screen.

If you need to capture the output in a file, then run your program like this:

speed > csis.dat

This will put the output of your program into the file called csis.dat (that is what I assumeyou want from the assignment description). You can open this with Textedit, and see the output. I would run it the first way without the redirect first, and make sure it works with the output to your screen. Then for the final run, redirect into the file.

If you might start with a simple "hello world" program like this:

#include <stdio.h>
main ()
{
printf("Hello World!\n");
}

I don't have gcc loaded, but I think that would work. Compile it and you should get "Hello World" when you run it. Once you have this working, make some other minor changes, and you can convert this into your speed.c program assignment...
 

numero

macrumors regular
Jul 23, 2002
106
3
OR
I'd go with Xcode on this one. I started school in 2001. I did most of my assignments in Xcode. It's friendlier than using gcc on the command line. Why use two tools (a text edit and a command line compiler) when you can use one integrated tool?

This assignment sounds easy to us, but it could be challenging to someone not familiar with the process. As the assignments get harder you will want to step through the code line by line. Again, I like Xcode better than gdb.

If you decide to go the Xcode way then setup you project as gnasher has said. Show the console to see the program's output. If you like the output (it is correct) then you can copy and paste the text from the console to a file to save for turning in.

Good luck with your studies. It will be a series of thinking that the current assignment is the hardest thing you have ever done and the last one you finished (that you thought was hard at the time) wasn't that bad. Things just keep stair-stepping up, but you get through it.
 

ChrisA

macrumors G5
Jan 5, 2006
12,915
2,161
Redondo Beach, California
Is there a simple way for me to use Xcode to do my work or is there a less complicated compiler i could use. Thanks for the help

gcc is the compiler included with Xcode. Open up a terminal window. Assume your code is in a file caled "hello.c" then type
gcc hello.c
then to run to program type
./a.out
That is about as simple as it gets

Ok, I little nicers. try "gcc -o hello hello.c" and then rather then "a.out" your program is called "hello". soyou can run i by typing "./hello".

Note that you xcan recall commands in the terminal beusing the up arrow key. So you will only have to type once.
 

jbailey8734

macrumors newbie
Original poster
Jan 31, 2008
7
0
Thanks for all the responses, I was able to complete 2 out of 4 assignments with some ease. The one assignment I posted earlier is the one I just can't figure out and my internet teacher won't answer my emails. So if someone can help me get how to complete this on I think it'll help me with the last assignment I have to do. Also his description on how he wants a speed.c, speed.exe, and a csis.dat file included in a zip archive, I really have no idea how to go about doing this and all the "helpful" tutorials they give for the class are all for windows not Mac. Thanks you guys are life savers

-Jon
 

aross99

macrumors 68000
Dec 17, 2006
1,541
1
East Lansing, MI
Thanks for all the responses, I was able to complete 2 out of 4 assignments with some ease. The one assignment I posted earlier is the one I just can't figure out and my internet teacher won't answer my emails. So if someone can help me get how to complete this on I think it'll help me with the last assignment I have to do. Also his description on how he wants a speed.c, speed.exe, and a csis.dat file included in a zip archive, I really have no idea how to go about doing this and all the "helpful" tutorials they give for the class are all for windows not Mac. Thanks you guys are life savers

-Jon

It sounds like he wants you to send in your source code (speed.c) the compiled executable (speed.exe), and the output (csis.dat) in a zip file. You can do that my puttting the files in a folder, then control-clicking on the folder and selecting "Create Archive".

One problem though: speed.exe is a Windows executable, and you aren't going to be able to create that on your mac. Best thing you can hope for is to send him your Mac executable (speed), and see if he will accept that.

Better talk with hjim first and explain that you are working on a Mac and that you can't send him a Windows executable...
 

jbailey8734

macrumors newbie
Original poster
Jan 31, 2008
7
0
Thanks i'll do that. Anyone have a clue how to do this assignment though:

Write a C program, speed.c, which does the following (be sure to give all numeric output to two decimal places):

• assigns a value (425.5) representing the distance in miles traveled by a car
• assigns a value (7.5) representing the number of hours taken to travel that distance
•computes the speed of the car in miles/hour (speed = distance / time)
•computes the speed of the car in meters/second (1 mile = 1600 meters)


Use the following guidelines to develop your program:

• declare your variables with appropriate data types
• assign values to your variables
• perform your calculations
• generate appropriate output


#include <stdio.h>
int main (void)
{?
}

Thats all I have so far, haven't had a problem like this with my other assignments so really don't even know how to start ha. Couldn't find anything helpful for this type in the book either. Thanks for the help
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Code:
#include <stdio.h>
int main (void)
{?
}
Thats all I have so far, haven't had a problem like this with my other assignments so really don't even know how to start ha. Couldn't find anything helpful for this type in the book either. Thanks for the help

So, some questions. If you've done other assignments, surely you know the answer to these questions.
  • What are the basic C data types and which one would be appropriate to hold values with fractions?
  • What variables do you need? What would you name them to be meaningful?
  • How do you declare a variable?
  • How do you assign a variable a value?
  • How do you output information to the user?
 

aaronw1986

macrumors 68030
Oct 31, 2006
2,622
10
I hope you don't expect people to do your work for you. You won't learn anything that way. How about you post some specific questions, or have some code we could help you debug.

To get you started, do you know what #define does? If so, can you see how this could maybe help you?
 

jbailey8734

macrumors newbie
Original poster
Jan 31, 2008
7
0
actually the other assignments had me multiplying other variables which seemed easy such as:

//heart.c -- determines how many times a heart beats in 50 years
#include <stdio.h>
int main (void)
{
int years, days, hours, minutes, seconds;

years=50;
days=365;
hours=24;
minutes=1440;
seconds=1*minutes*hours*days*years;
printf("There are %d days in a year.\n", days);
printf("Therefore a heart beats %d times in the course of %d years!\n", seconds, years);

return 0;
}

but with the speed.c one as I said I have no idea how to even start it, hopefully someone can help me see the difference between the assignment I completed above and the one i'm stuck on thanks. And sorry to the guy who thinks i'm getting people to do my work, but when you don't even know where to start on something its hard to do anything so please don't accuse me of anything. I just started the class and don't know much about C so yeah like I said. Thanks
 

aaronw1986

macrumors 68030
Oct 31, 2006
2,622
10
It's pretty much the same. Just assign things to miles traveled, time, etc...then divide instead of multiplying. You have the formulas there. You may want to look into the various types you can store numbers as: floats, long, int, etc..
 

jbailey8734

macrumors newbie
Original poster
Jan 31, 2008
7
0
Yeah thats my problem I don't know how to divide the miles and time to get the speed, don't know if its just a simple problem but couldn't see anything in the book to show how to divide.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
Yeah thats my problem I don't know how to divide the miles and time to get the speed, don't know if its just a simple problem but couldn't see anything in the book to show how to divide.

Your book doesn't explain how to divide?!! What book are you using?

Division is not that common in C-code, but still, it's one of the fundamental arithmetic operations.

The division operator is '/', so you'd have:

int speed, distance, time;

speed = distance/time; /*assuming time !=0 */

It really couldn't be any simpler.
 

aross99

macrumors 68000
Dec 17, 2006
1,541
1
East Lansing, MI
Yeah thats my problem I don't know how to divide the miles and time to get the speed, don't know if its just a simple problem but couldn't see anything in the book to show how to divide.

So is it the math that you are having a hard time with, or how to implement that math in your program?

You seem to be saying that you don't know how to compute the speed, yet you don't have anything (even the basic variable assignment) listed in your starting program...

If you don't know how to divide in C, then the previous poster gave you what you need: Use a "/" to indicate division, just like you use a "*" to indicate multiplication. "A divided by B" would be written as "A / B".

If you don't understand the math, then Speed = the distance you traveled divided by the time it takes you to travel. The units for speed will be based on the the units for the distance and the units for time in your formula.

The first result will be in miles per Hour (miles/hour). The second one you will have to convert into meters per second.

You will need to know how to convert miles to meters, and hours into seconds in order to be able to convert "miles per hour" into "meters per second".

I hope this helps...
 

jbailey8734

macrumors newbie
Original poster
Jan 31, 2008
7
0
Cool I used the / for division but i got huge errors when building, this is my first time writing a code with float and decimals types so besides what I wrote I have no clue. If anyone can help me understand what i was trying to write and where I went wrong so I can understand this stuff for myself. Thanks here's what I wrote:

#include <stdio.h>

int main (void) {
float miles;
float hours;
float speed;
float meters;

scanf("%f", &hours);

miles=425.5
hours=7.5
speed=miles/hours;
meters=1600*miles;

printf("If we travel "%f" miles in "%f" hours.\n", miles, hours);
printf("Then they must be going "%.2f" MPH.\n", speed)
printf("So what about meters?\n")
printf("Well there are about "%f" meters in this whole trip!!!\n", meters)
return 0;
}
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
Comment out the scanf (since for now you're hardcoding the value anyway), get rid of the quotes in printf around the %f, and don't forget to terminate your expressions with a semicolon.
 

itickings

macrumors 6502a
Apr 14, 2007
947
185
Cool I used the / for division but i got huge errors when building, this is my first time writing a code with float and decimals types so besides what I wrote I have no clue. If anyone can help me understand what i was trying to write and where I went wrong so I can understand this stuff for myself.

The errors are your friends, make sure you read what the compiler actually says when you encounter errors and warnings. They can be a bit cryptic, but usually points in a helpful direction.

For example, pasting your code into a file namned "test.c" results in (among others) the following errors:

test.c:12: error: syntax error before 'hours'
Something is wrong before 'hours', which is your declaration of 'miles'. The statement is lacking a ;

test.c:16: error: 'f' undeclared (first use in this function)
Not as straightforward, but the line number points to a printf-line, and talks about 'f'. The only 'f' is the %f. A string starts with ", and ends with ". If you want to have " inside a string, you must write \" instead to make it clear you want the character, and not to end the string.

Always fix errors from the top and down until you get the hang of it. Fix the first error, then recompile. A error in the beginning can cause errors further down which aren't really errors if the first error is fixed.

Code:
#include <stdio.h>

int main (void) {
  float miles;
  float hours;
  float speed;
  float meters;

  scanf("%f", &hours);

  miles=425.5;  /* Added ; after the statement */
  hours=7.5;  /* Added ; after the statement */
  speed=miles/hours;
  meters=1600*miles;

  printf("If we travel \"%f\" miles in \"%f\" hours.\n", miles, hours);  /* \" inside the string */
  printf("Then they must be going \"%.2f\" MPH.\n", speed);  /* Added ; after the statement and \" inside the string */
  printf("So what about meters?\n");  /* Added ; after the statement */
  printf("Well there are about \"%f\" meters in this whole trip!!!\n", meters);  /* Added ; after the statement and \" inside the \
string */
   return 0;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.