Thursday, August 16, 2012

Pointers


Pointers. 


A pointer is a variable whose value is an address.

                  int thing;            /* define a thing */
int *thing_ptr;   /* define a pointer to a thing */ 


There are things and pointers to things. Knowing the difference between the two is very important.
The address of thing is 0x1000. Addresses are automatically assigned by the C compiler to every variable. Normally, you don't have to worry about the addresses of variables, but you should understand that they're there.
Our pointer (thing_ptr) points to the variable thing. Pointers are also called address variables because they contain the addresses of other variables. In this case, our pointer contains the address 0x1000.




Pointer Operator Syntax. 


The operator ampersand (&) returns the address of a thing which is a pointer. The operator asterisk (*) returns the object to which a pointer points. These operators can easily cause confusion. 













Monday, July 30, 2012

Assignment 01-cmis1223


Assignment 01-cmis1223


1). School Teacher of a Primary School want a software program to calculate the average marks for
Maths, English, Science and Art for a Student.
1. Use the first three steps of the Development Cycle(Define the
problem:input/output/processing, Outline the solution:main Computation/input
variables/intermediate results, Develop an algorithm) to come up with an algorithm.


Inputs : Maths mark , Englsh mark , Science mark
Process :  Calculate average marks
Output  :   Average mark

  calAverage
1.sum <-  (math_mark + English_mark+Science_marrk)
2.average <- sum / 3


2. Write a C program for the Problem.

#include<stdio.h>

main(){

float M_marks , E_marks , Sc_marks;
float Average;

printf("Please enter Maths marks \n");
scanf("%f \n"&M_marks);
printf("Please enter English marks \n");
scanf("%f \n"&E_marks);
printf("Please enter English marks \n");
scanf("%f \n"&Sc_marks);

Average = (M_marks+E_marks+Sc_marks)/3;

printf("Average marks for three subject is %f \n", Average);
}



2). This program is developed to calculate the Age of a living person.

#include <stdio.h>
main() {
int day,month,year;
int bday,bmonth,byear;
printf("\n********Quiz01!********\n");
printf("\nEnter the Todays Date:\f");
scanf("%d/%d/%d",&day,&month,&year);
printf("\nEnter your Birth Day:\f");
scanf("%d/%d/%d",&bday,&bmonth,&byear);
printf("Your Age for Today: %d\n",year-byear);
}


1. Explain the Execution of this program.

#include <stdio.h>

main() {

int day,month,year;     // create memory locations in the main memory

int bday,bmonth,byear; // create memory locations in the main memory

printf("\n********Quiz01!********\n"); // print the text on screen  ********Quiz01!********

printf("\nEnter the Todays Date:\f"); // prin the text Enter the Todays Date

scanf("%d/%d/%d",&day,&month,&year); // blinking curser , values are assign to the relavant memory locations

printf("\nEnter your Birth Day:\f"); // print the text Enter your Birth Day

scanf("%d/%d/%d",&bday,&bmonth,&byear);  //blinking curser , values are assign to the relavant memory locations


printf("Your Age for Today: %d\n",year-byear); // your age for tody year-byear

}




2. Does this program correctly calculates the age of a person? If not explain why?

NO , there is unused variables in this program , day , bday , month , bmonth . Tocalulate the age precisely  have to use this variables as well .

right code to calulate the age precisely -
printf("Your Age for Today: %d  yaers %d  months %d days \n",year-byear , month - bmonth , day-bday);


Sunday, July 15, 2012

Arrays

Hi friends..... ,

We have already talk about simple data types, simple variable can hold only one value at any time during program execution ,although that value is change. We use data structures to save multiple values at the same time.The array is a one kind of data structure.

Lets see about Arrays.........


Arrays of any type can be formed in C. The syntax is simple:
type name[dim];
An array is a group of related data items that all have the same name and the same data type.
Arrays can be of any data type we choose.
An array’s data items are stored contiguously in memory.
Each of the data items is known as an element of the array.  Each element can be accessed individually.


In C, arrays starts at position 0. The elements of the array occupy adjacent locations in memory. C treats the name of the array as if it were a pointer to the first element--this is important in understanding how to do arithmetic with arrays. Thus, if v is an array, *v is the same thing as v[0], *(v+1) is the same thing as v[1], and so on:



int a[5];
An array as a whole has the following properties:
Its elements are contiguous in memory.


Friday, July 13, 2012

Repetition and Looping (for loop , while loop)


Repetition means performing the same set of statements over and over.
The most common way to perform repetition is via looping.
A loop is a sequence of statements to be executed, in order, over and over, as long as some condition continues to be true.
while Loop
C has a loop construct known as a while loop:
        while (condition) {
            statement1;
            statement2;
            ...
        }
The condition of a while loop is a Boolean expression completely enclosed in parentheses – just like in an if block.
The sequence of statements between the while statement’s block open and block close is known as the loop body.



while Loop Behavior

while (condition) {
            statement1;
            statement2;
            ...
        }
A while loop has to the following behavior:
The condition is evaluated, resulting in a value of either true (1) or false (0).
If the condition evaluates to false (0), then the statements inside the loop body are skipped, and control is passed to the statement that is immediately after the while loop’s block close.
If the condition evaluates to true (1), then:
the statements inside the loop body are executed in sequence.
When the while loop’s block close is encountered, the program jumps back up to the associated while statement and starts over with Step 1.

while Loop Flowchart



               statement_before;
                      while (condition) {
                              statement_inside1;
                              statement_inside2;
                                  ...
                       }
               statement_after;









for Loop




                        for (counter = initial_value; counter <= final value; counter++) {
                                 statement1;
                                 statement2;
                                  ...
                        } /* for counter */
                        statement_after

Variables and Arithmetic Expressions.


Variables in C have the same meaning as variables in algebra.  That is, they represent some unknown, or variable, value.
x = a + b
Variables in C may be given representations containing multiple characters.  But there are rules for these representations.
Variable names (identifiers) in C
May only consist of letters, digits, and  underscores
May be as long as you like, but only the first 31 characters are significant
May not begin with a digit
May not be a C reserved word (keyword)

Reserved Words (Keywords) in C

auto     break        float     for                        sizeof                 static
case     char          goto        if
const                continue           int      long            struct                 switch
Default                 do                     register      return
double                else               short                 signed            typedef      union
enum                   extern            unsigned      void                     volatile      while

C Simple program further

stdio.h and int main ()


When we write our programs, there are libraries of functions to help us so that we do not have to write the same code over and over.
Some of the functions are very complex and long.  Not having to write them ourselves make it easier and faster to write programs.
Using the functions will also make it easier to learn to program.
Every program must have a function called main.  This is where program execution begins.
main() is placed in the source code file as the first function for readability.  There must be a function with this name or gcc can not successful compile and link your program.
The reserved word “int” indicates that main() returns an integer value.
The parentheses following the reserved word “main” indicate that it is a function.


The Function Body


A left brace (curly bracket) --  {  -- begins the body of every function.  A corresponding right brace --  }  -- ends the function body.

printf (“Hello, World!\n”) ;

This line is a C statement.
It is a call to the function printf ( ) with a single argument (parameter), namely the string “Hello, World!\n”.
Even though a string may contain many characters, the string itself should be thought of as a single quantity.  
Notice that this line ends with a semicolon.  All statements in C end with a semicolon.


return 0 ;

Because function main() returns an integer value, there must be a statement that indicates what this value is.
The statement
return 0 ;

indicates that main() returns a value of zero to
the operating system.
A value of 0 indicates that the program successfully terminated execution.
Do not worry about this concept now.  Just remember to use the statement


Simple C Program


#include  <stdio.h>
int main ()
{
     printf ( “Hello, World!\n” ) ;
     return 0 ;
}



Program Header Comment and Preprocessor Directives

A comment is descriptive text used to help a reader of the program understand its content.
All comments must begin with the characters  /*  and end with the characters  */
These are called comment delimiters
The program header comment always comes first.

Lines that begin with a # in column 1 are called preprocessor directives (commands).
Example:  the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code.
This header file was included because it contains information about the printf ( ) function that is used in this 
program.