Thread: Help an ambitious 16 year old learn C

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Help an ambitious 16 year old learn C

    This is not a question anyway related a single homework assignment, but rather a request for someone to help someone learn C on the side, without them taking a class. This post starts out with some general information on my skill level, and works its way to down to a list of things you could do to help me learn C. It's a fairly long read so if you aren't interested in helping this is a forewarning that it might be a waste of time.

    Hello, I am a Junior in High School, and recently I decided to take a class called "AP Computer Science I" which required me to skip "Intro to Computer Science" (a class which does not cover much) and "Windows Programming" (a dual-enrollment class which mainly works with Visual Basic? I think... I didn't take it lol). At first, I had a real hard time understanding programming terminology, and I got off to a rough start. Over the first 2 weeks or so, we learned some scheme. The programs that we made were extremely low level and after this brief intro into programming (for me at least), we switched to Java. After more struggling, I began to get the gist of programming at a low level.

    Currently, we are working on a program called StringUtil, in which we have to create functions that manipulate strings. Such as: Reverse, Palindrome Checker, Pig Latin Converter, and Short Hand Converter. Each of these must be done recursively, and since we have not learned loops or arrays, neither can be used, along with StringBuffer and additional useful methods that can be found in the Java API. I have already written: Reverse and the Palindrome Checker. I had them checked and they work as intended and were done recursively (the right way).

    As far as Programming in C, I have only read the "C Made Easy" Beginner Tutorials on this site from the first lesson "Intro to C" through "C-Style Strings." I read this tutorials today, and I do not fully understand them, but I have learned somewhat the basics. While coming from Java Background (though a very incomplete one), the lesson on pointers was the most confusing and difficult lesson.

    I do understand some basics of pointers vaguely, such as:

    - How to declare a pointer
    - How to assign the address of a variable to a pointer
    - How to initialize pointers using malloc
    - How to free pointers and set them to become null pointers

    But, I lack the understanding of when to use them and ultimately, why to use them? This is a very important subject to learn and fully understand, and anyway in which you could help me understand them would be greatly appreciated.

    Even though we have not learned loops, I decided to read ahead in our Java Lessons, and I have a basic understanding of loops in Java (and some in C via the Tutorials on this site). After reading this I decided to go ahead and do one of the assignments which we may do or may not. This assignment included finding magic squares and writing a method to find the Least Common Multiple or LCM of two numbers. I found this not too hard in Java. I wrote a program which included the two methods which worked.

    Now, I am trying to write the magic square portion of that program in C as an attempt to grasp what I have read because without any practice I will not learn anything about C. This is one of the major problems I face. I have no assignments which are at my level and no person to help me through this process.

    I have a fair amount of desire to learn C, but I am somewhat lazy, and I would like to ask if anyone (or a number of people) would like to help me get the hang of C by:

    1) Taking the first step by helping me complete and understand this magic square program which I have attempted writing in C

    2) Possibly take a step back to review the very basics of C by providing me a couple of simple programs to write and for you (or a number of people to check)

    3) Continue to move forward by giving me additional programs to write increasing in difficulty provided with help to complete them and obtain the optimal solution

    I know this a lot to ask, but any help would be much appreciated. I am considering programming as a career.


    Now, back to the magic square program which i am trying to duplicate in C. Here is a little bit of information that was provided to help me understand what a magic square was:

    Magic square problem:

    1. Some perfect squares have unique mathematical properties. For example, 36 is:

    - A perfect square, 6^2
    - And the sum of the integers 1 to 8 (1+2+3+4+5+6+7+8 = 36)

    So let us call a "magic square" any number that is both a perfect square AND equal to the sum of consecutive integers beginning with 1.

    2. The next magic square is 1225:
    352 = 1225
    1225 = sum of 1 to 49

    3. Write a method that prints the first n magic squares.

    Here is my version of this completed in Java:

    Code:
    public class FunLoops
    {
        public void magicSquare(int n)
        {
            int ms = 0; //Variable for storing the amount of magic squares found
            long num = 1, num2 = 1;
            long ps = 1, cia = 1; //Ps stands for Perfect Square, and Cia stands for Consecutive Integer Addition
            
            while( ms < n)
            {
                 while( ps < cia)
                 {
                     num++;
                     ps = num*num;
                 }
                 
                    while( ps > cia)
                    {
                        num2++;
                        cia += num2;
                    }   
                        if( ps == cia)
                        {
                            System.out.println(ps);
                            ms++;
                            num++;
                            num2++;
                            ps = num*num;
                            cia += num2;
                        }              
            } 
        }
    Here is my attempt in C:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        int ms = 0;
        int num1 = 1;
        int num2 = 1;
        int ps = 1;
        int cia = 1;
    
        printf( "Please enter the number of magic squares\n" );
        printf( "to be found and displayed:\n\n" );
        printf( "(Remember these values will be found in\n" );
        printf( " order, from the lowest possible magic\n" );
        printf( " square to the highest possible magic square)\n\n" );
        printf( " Input: " );
        scanf( "&d\n", &n );
    
        while ( ms < n )
        {
            while ( ps < cia )
            {
                num1++;
                ps = num1*num1;
            }
    
            while ( ps > cia )
            {
                num2++;
                cia += num2;
            }
    
            if ( ps == cia )
            {
                printf( "\n&#37;d\n", ps );
                ms++;
                num1++;
                num2++;
                ps = num1*num1;
                cia += num2;
            }
        }
    }
    It does compile and it gives me the first 2 magic squares (1 and 36) but for some reason stops right there.

    I know that this was a horrible attempt, and I basically just copied the code, but I did learn a couple of things by this attempt. Remember, that I don't know when to use pointers and anything I do know was learned in a 4 hour reading of half of the Tutorials on this site.
    Last edited by Xlayer; 10-06-2007 at 10:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little help with leap years
    By Jayhawk_26 in forum C Programming
    Replies: 17
    Last Post: 10-05-2006, 02:03 PM
  2. Constructive criticism/suggestions
    By LineOFire in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 09:32 AM
  3. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM
  4. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM
  5. Help Me Out!! Pls
    By Joanna in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2001, 05:08 AM