Jump to content
Objectivism Online Forum

Software: C Programming

Rate this topic


FaSheezy

Recommended Posts

So it's loops within loops..

"Loops within loops" is one concrete way to look at it. Speaking more broadly, a typical algorithm is best tackled by breaking it down into component tasks that need to be done. You keep breaking it down further and further, until each sub-task is "trivial" enough to be expressed as a line of code.

I learnt with flowcharts too. I think (but am not sure about this) that flowcharts were a bad tool. After my first few years of programming I never used them and never will. Looking back, I think they did more harm than good.

I think that the best way is to break down the problem using a "structured" english-like approach, commonly referred to as "pseudo code". The snippet from ramKatori's post is a good starting point.

I suppose one could take it further and say that "Print a Week" required a loop where we print a max of 7 days (assuming as that snippet does, that a "week" is defined so that it does not go across months).

BTW: The "code" button is cool, formats stuff nicely.

Question: Have you learnt how to write functions in C? Or are you just writing everything in main() ?

Let me start by exploring the snippet from a few posts ago, and expanding on it. I am going to use a slightly more C-like pseudo code.

printAyear()
{
   for ( m = 1 ... 12 )
   {
      printAmonth (year,  m )
   }
}

printAmonth (yearNum, monthNum )
{
   w = 1;
   do
   {
      printAweek(yearNum, monthNum, w)
   }
   until no-more-weeks
}

printAweek(yearNum, monthNum, week)
{
   Figure out the number(1,2,3...) and day(Sun, Mon...)
       of the first day in this week

   for (Sun ... Sat)
   {
       print the date (or a blank if there is no date for this month)
   }
}[/code]

I could never be a programmer.

I'm sure there were times when Edison thought to himself that he would never be an inventor... don't remember how many materials he tried as a filament for the light bulb before he hit on the right one. So... don't worry about it.

... if you have a variable that you declare at the beginning of the program, you could use it within nested-loops, right?

A C program can have global variables that can be used anywhere. If you know how to use functions, then is it "safer" to use variables that are passed as parameters to the function and variables that are local to that function.

Suggest it is now your turn to refine the program to the next stage of detail and post it here for comments. (Use the "Code" button while posting.)

All the best. Enjoy!

Link to comment
Share on other sites

I see that this thread was re-opened. FaSheezy and I had continued the discussion elsewhere. To provide continuity, I will post FaSheezy's final post from our parallel discussion and also my final response.

FaSheezy: We were given an algorithm to produce the years.

It looks like this

int find_jan_first (int year) 
{
    int firstDayInJan;
    firstDayInJan = ((5 + (year - MIN_YEAR) + count_leap_years(year)) % DAYS_PER_WEEK);

return (firstDayInJan);
}[/code]
FaSheezy: so I need to declare MIN_YEAR, count_leap_years, and DAYS_PER_WEEK right?  like..    MIN_YEAR = 1582; DAYS_PER_WEEK = 7; she also gave us the algorithm for leap years cause it's something crazy
[code]int count_leap_years  (int year)   /*<--- I dont understand why that is there*/
{
 int leapYears;
 int hundreds;
 int fourHundreds;

 leapYears = (year - (MIN_YEAR - 1)) / 4;
 hundreds = (year - 1501) / 100;
 leapYears - = hundreds;

 fourHundreds = (year - 1201) / 400;
 leapYears += fourHundreds;

 return (leapYears);
}

FaSheezy: so So, is all of that within int main(); or outside?  And I should declare  count_leap_years before it is used the first time in the first equation, right?

To which, I replied:

Yup. Declare those three. One is a function. The other two can be declared globally. They are CONSTANTS (convention is to use CAPS) that are unchanging and are used in various functions.

Treat the two functions she has given you as "blackboxes". They work, and you can use them, but you do not need to understand the internals for the purpose of this exercise.

You can call the first to get the starting day for the year. Then print Jan. With that done, you now know the last day of Jan and thereofre the first day of Feb. So, print Feb. And so on.

The only complication is whether to print 28 or 29 days in Feb. I suppose you can write a little function that can have a passed in and can tell whether it is a leap year or not.

Link to comment
Share on other sites

C is a pretty bad language to start learning in, do you have to use it or can you pick your own language? You'd be better off using a high level language to get used to algorithms and general programming concepts, without having to mess around needlessly with low-level things such as pointers and memory allocation. I don't understand why C gets taught these days other than in classes specifically geared towards low-level programming; probably just convention and laziness on the part of the teachers.

Edited by Hal
Link to comment
Share on other sites

I don't understand why C gets taught these days other than in classes specifically geared towards low-level programming; probably just convention and laziness on the part of the teachers.

Agreed, especially with the big push towards .net in the industry. All the universities around here begin with teaching Java, much more user friendly. I think C does have its place in giving one a better core understanding of lower level concepts.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...