Thursday, September 30, 2010

while / do while loop

#include <stdio.h>
main()
{
    int i = 5;
    int j = 5;
    printf("%s\n"," --------------While loop ----------------------------");
    while(i < 5)
    {
        printf("\n%s%d","value -  ",i);
        i = i + 1;   
    }



        printf("%s\n"," ----------------do while loop -------------  ");
        do    {
        printf(" \n Value  - %d \n " , j);
        j = j + 1;    
            }while(j < 5);
}

SUM 1 to 2o

#include <stdio.h>
main()
{
    int i = 1;
    int s = 0;
    while(i <=20)
    {
        s = s + i;
        printf("%d%s",i, "+");
        i = i + 1;   
    }

    printf("\n%s%d \n\n","SUM IS  -  ",s );
}

Numbers devide by 2 and 3

# include <stdio.h>
main()
{
int remainder = 0;
int startNumber = 500;
while(startNumber <750 )
    {
    startNumber = startNumber + 1;
    if((startNumber % 2 == 0) &&(startNumber % 3 == 0) )
    printf("%d\n",startNumber);
    }

}

Find Max In a array

# include <stdio.h>
main()
{
    int mx = 0;
    int array[5];
   
    array[0] = 32;
    array[1] = 33;
    array[2] = 34;
    array[3] = 28;
    array[4] = 28;

    int i;
        mx = array[0] ;
        for (i=0 ; i<5 ; i++)
        {
           
            if(mx < array[i])
            {
                mx =  array[i];
            }
                       
        }
       
    printf("%s%d\n","MAX = " , mx);
}

Wednesday, September 29, 2010

Question 2

1.      # include <stdio.h>
2.      main()
3.      {
4.      }




\








   Explain the purpose of line 1 (# include <stdio.h>) of the above source program.
  (4 marks)
stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations.

http://en.wikipedia.org/wiki/Stdio.h

Question 1

Question 1

Human languages are not understood by the computer. The instructions the computers are understood called machine codes or machine language. And programming languages are a vocabulary and set of grammatical rules for instructing a computer to perform specific tasks.

a.      What do you mean by a source program?  (4 marks)

In computer science, source code is any collection of statements or declarations written in some human-readable computer programming language. Source code is the means most often used by programmers to specify the actions to be performed by a computer.

Program is written in the form of a number of text files using a screen editor. This form of the program is called the source program. It is not possible to execute this file directly.

b.      What do you mean by an Object/executable Program? (4 marks)

A Program which contain instructions in computer understandable way.

What is a compiler? (4 marks)

Program is made by running a compiler which takes the typed source program and converts it into an object file that the computer can execute. A compiler usually operates in two or more phases (and each phase may have stages within it). These phases must be executed one after the other. 

http://www.iu.hio.no/~mark/CTutorial/img/1.4.jpg

d.      What is the difference between compiler and interpreter? (4 marks)

A compiler, in general, reads higher level language computer code and converts it to native machine code. An interpreter runs directly from source code or an interpreted code such as Basic or Lisp. Typically, compiled code runs much faster, is more compact and has already found all of the syntax errors and many of the illegal reference errors. Interpreted code only finds such errors after the application attempts to interpret the affected code. Interpreted code is often good for simple applications that will only be used once or at most a couple times, or maybe even for prototyping. 

Are upper and lower case equivalent in C? (4 marks)
YES

Thursday, September 23, 2010

++ / --

# include <stdio.h>

main()
{
    int i;
    int j;
    i = 3 ;

    printf("%s%d\n" , "Value i " , i);
   
     i++; /* i = i + 1*/

    printf("%s%d\n" , "Value i++ " , i);

    i--;/* i = i - 1*/

    printf("%s%d\n" , "Value i-- " , i);

    ++i; /* i = i + 1*/

    printf("%s%d\n" , "Value ++i " , i);

    --i;/* i = i - 1*/

    printf("%s%d\n" , "Value ++i " , i);

    printf("%s" , "------------------------------------------------\n");

      j = 10;
    printf("%s%d\n" , "Value j++ " , j++);
    printf("%s%d\n" , "Value j =  " , j);
    printf("%s%d\n" , "Value ++j " , ++j);

    printf("%s%d\n" , "Value j-- " , j--);
    printf("%s%d\n" , "Value j =  " , j);
    printf("%s%d\n" , "Value --j " , --j);
    }

Nested Loops (2010 09 23)

# include <stdio.h>
main()
{
    int i;
    int j;
    for(i = 1 ; i<=12 ; i++)
    {
        printf("%s%d\n"," ------------------ " , i);
        for(j=1 ; j <=12 ;j++)
        {       
            printf("%d%s%d%s%d\n",i," X ",j ," = " , i*j );
        }
    }   
}

Arrays (2010 09 23)

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

Ref Links -
http://www.idleloop.com/tutorials/introC/introC-10.php

Samples -

----------------------------------------------------------------------------------------------
# include <stdio.h>
main()
{
    int array[5];
   
    array[0] = 32;
    array[1] = 33;
    array[2] = 34;
    array[3] = 28;
    array[4] = 28;

    printf("%s%d\n" , "array[0]  - " , array[0] );
    printf("%s%d\n" , "array[1]  - " , array[1] );
    printf("%s%d\n" , "array[2]  - " , array[2] );
    printf("%s%d\n" , "array[3]  - " , array[3] );
    printf("%s%d\n" , "array[4]  - " , array[4] );
printf("----------------------------------------\n");
        int i;
       
        for (i=0 ; i<5 ; i++)
        {
            printf("%s%d%s%d\n" , "array[ " , i , "]  = " ,array[i]);
           
        }
printf("---------------array2[] -----------------\n");

 int array2[] = {32,33,34,28,28};

    for (i=0 ; i<5 ; i++)
        {
            printf("%s%d%s%d\n" , "array2[ " , i , "]  = " ,array2[i]);
           
        }

printf("---------------array3[] -----------------\n");
            int array3[10];
array3[5] = 100;
    for (i=0 ; i<10 ; i++)
        {
            printf("%s%d%s%d\n" , "array3[ " , i , "]  = " ,array3[i]);
           
        }

printf("---------------array4[] -----------------\n");
            double array4[10];

}

-----------------------------------------------------------------------------------------------


TO DO - find the maximum stored in a array 

# include
main()
{
int array[5];

array[0] = 32;
array[1] = 33;
array[2] = 34;
array[3] = 28;
array[4] = 28;

int i;

for (i=0 ; i<5 ; i++)
{
printf("%s%d%s%d\n" , "array[ " , i , "] = " ,array[i]);

}


}