1. Become a Developer for CrazyEngineers - Apply NOW!

Beginners Tutorial : The C-language.

Discussion in 'Computer Science | IT | Networking' started by simplycoder, Aug 19, 2011.

  1. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science

    and yeah! the loop should be common to take inputs arrange values and to display output!
     
  2. simplycoder

    simplycoder Ace

    Engineering Discipline:
    Electronics & Communications
    I would take a try to crack this,
    PHP:
     
    #include "stdio.h"
     
    int main(int argc,char**argv)
    {
    int index=0// Variable 1
    int temp_index=0// Varible 2
     
    // Size considered as 6 hardcoded :(
    for(index=1,temp_index=1;index<=6||temp_index<=6/2;index++) // One Loop.
    {
    // Print the odd numbers first.
    if((index&1)&&(index<=6))
    {
    printf("%d",index);
    }
    // Print 0 for the even number later.
    else if(index>6)
    {
    temp_index++;
    printf("0");
    }
     
    }
    printf("\n");
    return 
    0;
    }
    Didn't utilize array part as well I have hard coded the values.
     
    • Agree Agree x 1
  3. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    and my code for the question is this... kinda similar! and for n values!

    Code:
    #include<stdio.h>
    int ar[100],i = 0; //array and 1 var
     
    int main() {
        int n; //2nd var
        printf("enter range : ");
        scanf("%d",&n);
        printf("enter the numbers : ");
        while(i < n) {
            if(i >= n/2) {
                printf("%d, ",ar[i]);
                i++;
                continue;
            }
            scanf("%d",&ar[i]);
            if(ar[i]%2 == 0) {
            }
            else {
                ar[i] = ar[i];
                printf("%d, ",ar[i]);
                i++;
            }
        }
        return 0;
    }
     
  4. simplycoder

    simplycoder Ace

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-27
    Pointers in C
    I would break this main tutorials in many sub tutorials in order to highlight each of the concept related to pointers.
    In this tutorial I would be covering the basic definition,usage,need and the Address Of Operator in C POINTERS.
    ---------------------------------------------------------------------------------------------------
    If I have to point out the feature in C language, which is most widely misunderstood by begineers in C, then I wouldnot have second thoughts to say its POINTERS.
    Basically begineers have difficulty in understanding the pointer terminology implemented by C.
    There is no programing language, which doesnot use pointers. However, very few (C being one of them) gives programmers a chance to use it in day to day coding.

    Pointers are like double edge swords and programmer must take care to use it properly.

    • #What are pointers:
    A pointer is a variable which has address of the memory location of another variable.
    eg. Suppose I declare an integer 'A' and compile the program. The compiler would store a finite memory for this integer at some location. Every memory location has unique address. So its safe to say that a memory location is like a house and like every house has unique address, every memory location would have unique address.

    Now I would like to store the address of the memory location where integer 'A' has been stored in a variable 'X'.
    This variable 'X' is called as pointer.

    • Basic Pointers:

    1. Address Of Pointer:
    Assume a declaration like
    int x=4;
    When we compile the program, compiler does the following tasks:

    ->Reserve space in memory to hold the integer value.
    ->Associate the name x with the above memory location.
    ->Save value 4 in this memory location.

    Lets say that compiler chose the memory location with address '1000' to store the variable x.
    Then the pointer of x would have value 1000 in it.

    Let us write a sample code for this.

    Code:
    //Code
    
    #include <iostream>
    int main(int argc,char**argv)
    {
      
    int x=4;
    printf(" Value of x : %d\n",x);
    printf(" Address of x : %d\n",&x);
       system("PAUSE");
       return 0;
    }
    
    
    //Output :
    Value of x : 4
    Address of x : 2686788
    Press any key to continue . . .
    
    The '&' operator that is being used here, is called as the 'Address Of' operator.
    This operator, returns the address of the memory location where in the value of the variable is stored.
    Please note the output of the address may(and would in most of the cases) differ from the output given here.

    I leave the task of reading further about Address Of operator and linking it to the scanf functionality, to the reader.
    In the next tutorial, I shall write on Value At Address (*) operator.

    Until the next time,take care.
    Ciao.

     
    • Like Like x 1
  5. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    I thought of sharing this with everyone. A good way to program and test your programs. Most of beginners generally code every logic inside the main() and to test their program they execute the code n number of times (n test cases) each time entering a test case. That's really annoying for the person who has written the code and they may skip most of the test cases thinking they'll get a correct output for all of them. But, this can be done in an interesting way where the evaluation of the code is done by the code itself i.e. no need of executing the code again n again.

    A programmer needs to understand that it is always a good practice to divide the problem in as many functions as possible, keeping in mind that each function performs only a particular task.
    I'll not make the post much longer so we'll have a quick look on an example.
    Let us pick up a small program like, Sum of the elements of an array. Now how to start? Well, just define a structure first (I'll explain why we are doing it)

    Code:
    struct test
    {
        int input[10];    // involves your test case arrays
        int terms;         // the number of term in your array
        int output;        // the output which you are expecting
    } testVals[4] = {   // these are your test cases you can add as many you like (4 here)
                                {{1,2,3,4,5,6,7,8,9,10} , 10, 55},  // initializing input, terms, expected output
                                {{2,4,6,8} , 4, 20},
                                {{3,6,9} , 3,  18},
                                {{1,2,3,4,5} , 5, 15}
                    };
    
    I've added comments and it's self explanatory. Once we are ready with the structure which holds our inputs, we need to have a function which will solve our base problem that is to find the sum of elements of an array. So, lets code that as well

    Code:
    int sumFinder(int *input, int terms)
    {
        int sum = 0;
        for(int i = 0 ; i < terms ; i++)
             sum += input[i];
        return sum;
    }
    
    So, our base function is ready which takes the input of an array and the number of terms in the array, finds sum and returns the sum obtained.
    We still don't have a function which passes test cases to our base function. Let's code that as well.

    Code:
    void testCases()
    {
        int result;
        for(int i = 0 ; i < 4 ; i++)      // for passing 4 test cases
        {
             result = sumFinder(testVals[i].input,testVals[i].terms);  // sending the values
             if(result == testVals[i].output)    // checking the obtained result
                 printf("PASSED\n");
             else
                 printf("FAILED\n");
        }
    }
    
    so our testCases() is pretty powerful which passes all the test cases 1 by 1 and even checks whether we have arrived to a correct solution or not, hence all we need to do is to fill in the structure with as many test cases you like and remaining processing is done by the code.
    Yeah, we are missing our main function. Lets see the complete code together,

    Code:
    #include "stdio.h"
    
    struct test
    {
        int input[10];    // involves your test case arrays
        int terms;         // the number of term in your array
        int output;        // the output which you are expecting
    } testVals[4] = {   // these are your test cases you can add as many you like (4 here)
                                {{1,2,3,4,5,6,7,8,9,10} , 10, 55},  // initializing input, terms, expected output
                                {{2,4,6,8} , 4, 20},
                                {{3,6,9} , 3,  18},
                                {{1,2,3,4,5} , 5, 15}
                           };
    
    int sumFinder(int *input, int terms)
    {
        int sum = 0;
        for(int i = 0 ; i < terms ; i++)
             sum += input[i];
        return sum;
    }
    
    void testCases()
    {
        int result;
        for(int i = 0 ; i < 4 ; i++)      // for passing 4 test cases
        {
             result = sumFinder(testVals[i].input,testVals[i].terms);  // sending the values
             if(result == testVals[i].output)    // checking the obtained result
                 printf("PASSED\n");
             else
                 printf("FAILED\n");
        }
    }
    
    int main()
    {
       testCases();
       return 0;
    }
    
    So that's the complete code which looks huge but as I said its very good practice as you just need to add more test specs and every other thing is dealt. The same practice can be used for other codes as well, you just need to change the things you pass from structure, your result checker. (you can use a separate function for checking as well) that's all for now! If you have any questions, add comments :)
     
    • Informative Informative x 1

Share This Page