So I was bored and thought giving C a try again, and well, I have to admit, it was fun! I tried learning C the same way I learned php; when I learned php I was already working on a web site, I had a goal in mind and features I wanted to create, I had a project. In C, it's very difficult to imagine your self creating a program to be able to do anything useful; so I just thought up a random simple objective that I wanted to complete with a program written in C.
So first I created a new Generic C project in Anjuta and went straight to the readme file and basically wrote what my program was going to do.
[1] Program Objective
This will be a simple program to determine how long it takes you to arrive at your destination at
any given speed.
[2] Processing Steps
1. We must determine the distance from the starting point to the finish point.
2. We must determine the average speed at which the vehicle is going.
3. Using data from steps 1 and 2, create a calculation using the formula
Distance / Km/h = ETA in Hours
4. Use the result from step 4 to give the answer in minutes if the ETA is less than 1 hour
using the formula ETA * 60.
[3] Variables
int speed
float eta_hours, eta_minutes, float, eta
After that, it was much easier to just get on with programming without having to think of what I want to do with the program. I could more focused and I could really concentrate on writing the code. Although the code I wrote was really basic, and not hard at all to write, I learned much from it. If you want to write your own program from scratch to fulfil that objective feel free. Here's my code if you want to use it to compare.
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.c
* Copyright (C) Yiu Ming Patrick Ma 2008
*
*/
#include
float eta_minutes ( float eta );
float eta_hours ( float distance, int speed );
int main () {
int speed;
float distance, eta;
printf ( "Please enter the distance to your target destination: \t" );
scanf( "%f", &distance );
printf ( "\nEnter your average driving speed in km/h: \t" );
scanf( "%d", &speed );
eta = eta_hours( distance, speed );
if ( eta >= 1.0 )
printf ( "Your estimated time of arrival is %.1f hours.\n" , eta);
else
printf ( "Your estimated time of arrival is %.1f minutes.\n", eta_minutes( eta ) );
getchar();
return 0;
}
float eta_hours ( float distance, int speed ) {
return distance / speed;
}
float eta_minutes ( float eta ) {
return eta * 60;
}