Sunday, 14 October 2012

FLOWCHART-IZZHAM AND SYAMIN


                                                                

FLOWCHART : IF


FLOWCHART :  NESTED IF



FLOWCHART : IF… ELSE



FLOWCHART : SWITCH




FLOWCHART :  WHILE LOOP



FLOWCHART :  DO..WHILE LOOP















FLOWCHART :  FOR LOOP


PROGRAM LIFE CYCLE-IZZHAM AND SYAMIN



PROGRAM LIFE CYCLE






Arrays-IZZHAM AND SYAMIN


Arrays
 
Array is data structure used to share multiple data elements under a single name declaration. 
It's important that every single element of data, we wish to assign to array, belongs to the same data type. 
Arrayâ's elements are easily accessed - 
we use index; a number that must be nonnegative integer (constant, variable, integer expression). 
Element's index is a number between 0 and the number of elements minus one, including. In short: Index ( [0, NrOfElements-1].
 
 
Declaration of an array:
 
data_type array_name[index];
 
 
 
Array's definition in C:
 
int x[20] - array consisted of 20 integer numbers
char symbols[2] - array consisted of 2 characters
float sequence[MAX] - MAX is constant
 
 
 
Assigning element's values definition:
 
int array[ ] = {1, 2, 3};
array[0] = 1
array[1] = 2
array[2] = 3
 
int array [4] = {1, 2};
array[0] = 1
array[1] = 2
array[2] = 0
array[3] = 0
 
 
 
Accessing array's elements:
 
x[0] - first element of an array
sequence[i] -i. element of an array, 0 <= i <= NrOfElements-1
sequence[MAX-1] last element of an array
 
 
 
Common Wrong access to array's elements:
 
int array[10] = {0};
int x = sequence[10];
 
float a = 1.;
int x = array[a];
int a = 1, b = 0, c = 1;
int x = array[(a && b) - c];
 
 
 
 
Example:
 
Write your own program that asks user to input sequence of numbers, afterwards it calculates arithmetic middle of the given sequence. 
Program also prints numbers smaller than arithmetic middle, and afterwards prints numbers bigger than arithmetic middle.
 
 
 
#include <stdio.h>
#define DIMENSION 10
 
int main(void) {
 
int i;
  float sum = 0., arit_midd = 0., sequence[DIMENSION]={0};
  
  for (i = 0; i < DIMENSION; i++) {
 
    printf("Input number: ");
    scanf("%f",&sequence[i]);
    sum += sequence[i];
 
  } 
 
  arit_midd = sum / DIMENSION;
  printf("Arithmetic middle of the sequence is %6.2f.\n", arit_midd);
  for (i = 0; i < DIMENSION; i++) {
 
    if (sequence[i] < arit_midd) {
 
      printf("%6.2f is smaller than arithmetic middle.\n", sequence[i]);
 
    }
  }
 
  for (i = 0; i < DIMENSION; i++) {
 
    if (sequence[i] > arit_midd) {
 
      printf("%6.2f is bigger than arithmetic middle.\n", sequence[i]);
 
    }
  }
     
  // What happens if the number is equal to arithmetic middle?
 
}
 
 
 
 
 
 
Example:
 
Write your own program that asks for input of sequence of numbers. After the program reads given numbers, 
it divides every number with the biggest sequence element and shows them in a way relative to the biggest element.
 
 
#include <stdio.h>
#define DIMENSION 10
 
int main(void) {
 
  int i;
  float max, array[DIMENSION]; 
 
  for (i = 0; i < DIMENSION; i++) {
 
    printf("array[%d] = ", i);
    scanf("%f",& array[i]);    
    if (i == 0) {
 
      max = array[i];
 
    }
    if (max < array[i]) {
 
      max = array[i];
 
    }
  } 
 
  printf("Biggest element in array is %f.\n\n", max);
  for (i = 0; i < DIMENSION; i++) {
 
    array[i] /= max;
    printf("array[%d] = %f\n", i, array[i]);
 
  }
}
 
 
 
 
 
 
Example:
 
Compose your own program that reads given natural numbers that belong in [10, 99] interval and counts how many times each number showed up. 
Program stops reading numbers when element that doesn̢۪t belong to interval is given. Af
afterwards, program prints each number from the interval that has showed at least once, and number of times it has really been given.
 
 
#include <stdio.h>
 
#define LL 10          /* lower limit of the interval */
#define UL 99          /* upper limit of the interval */
 
int main(void) {
 
  int number, i;
  int counter[UL – LL + 1] = { 0 };     
 
  do {
 
    printf("\nInput number from interval [%d, %d]: ", LL, UL);
    scanf("%d", &number);
    if (number >= LL && number <= UL) {
 
      counter[number - LL]++;
 
    }
 
  } while (number >= LL && number <= UL); 
 
  for (i = DG; i <= UL; i++) {
 
    if (counter[i - LL] > 0) {
 
      printf("\nNumber %d showed up %d times", i, counter [i - LL]);
 
    }
  }
}

 

 

 

 

 

 

 

 

 

Multi dimensional Arrays:

Often there is a need to store and manipulate two dimensional data structure such as matrices & tables. Here the array has two subscripts. One subscript denotes the row & the other the column.


The declaration of two dimension arrays is as follows:
data_type array_name[row_size][column_size];
int m[10][20]

Here m is declared as a matrix having 10 rows( numbered from 0 to 9) and 20 columns(numbered 0 through 19). The first element of the matrix is m[0][0] and the last row last column is m[9][19]

Elements of multi dimension arrays:

A 2 dimensional array marks [4][3] is shown below figure. The first element is given by marks [0][0] contains 35.5 & second element is marks [0][1] and contains 40.5 and so on.

marks [0][0]
35.5

marks [0][1]
40.5

marks [0][2]
45.5

marks [1][0]
50.5

marks [1][1]
55.5

marks [1][2]
60.5

marks [2][0]

marks [2][1]

marks [2][2]

marks [3][0]

marks [3][1]

marks [3][2]


Initialization of multidimensional arrays:

Like the one dimension arrays, 2 dimension arrays may be initialized by following their declaration with a list of initial values enclosed in braces
Example:
int table[2][3]={0,0,0,1,1,1};
Initializes the elements of first row to zero and second row to 1. The initialization is done row by row. The above statement can be equivalently written as
int table[2][3]={{0,0,0},{1,1,1}}
By surrounding the elements of each row by braces.
C allows arrays of three or more dimensions. The compiler determines the maximum number of dimension. The general form of a multidimensional array declaration is:

date_type array_name[s1][s2][s3]…..[sn];
Where s is the size of the ith dimension. Some examples are:
int survey[3][5][12];
float table[5][4][5][3];

Survey is a 3 dimensional array declared to contain 180 integer elements. Similarly table is a four dimensional array containing 300 elements of floating point type.

/* example program to add two matrices & store the results in the 3rd matrix */
#include< stdio.h >

void main()
{
      int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;

      printf(“enter the order of the matrix\n”);
      scanf(“%d%d”,&p,&q);

      if(m==p && n==q)
      {
            printf(“matrix can be added\nn”);
            printf(“enter the elements of the matrix a”);
            for(i=0;i < m;i++)
                  for(j=0;j < n;j++)
                  scanf(“%d”,&a[i][j]);

            printf(“enter the elements of the matrix b”);

            for(i=0;i < p;i++)
                  for(j=0;j < q;j++)
                  scanf(“%d”,&b[i][j]);
     
printf(“the sum of the matrix a and b is”);
for(i=0;i < m;i++)
      for(j=0;j < n;j++)
            c[i][j]=a[i][j]+b[i][j];

for(i=0;i < m;i++)
{
      for(j=0;j < n;j++)
      printf(“%dt”,&a[i][j]);
      printf(“n”);
      }
}

Basic Structure of C program-IZZHAM AND SYAMIN


 Basic Structure of C program

Text Box: preprocessor directive

main()
{
statements;
}

All C  programs consist of the following elements:

Explanations
preprocessor         Refer to next page
main()                    Every program must have a main() function and it is a first function that will be called.
                               The main() function is the one to which control is passes when the program is executed.
{ }                           These are known as braces or curly brackets.  The statements  of the functions are enclosed in these curly brackets. The opening curly brackets indicates that a block of statements is about to begin. the closing curly brackets terminates the block of the statements.

Example
// A first C  program
#include <stdio.h>

main()
{
  printf( “Welcome  to C\n”);
  printf( “Thank you for using me!\n”);
  return 0;
}

 
 






 


Explanation

//A first .......                It is a comments.
#include <stdio.h>  It is one of the preprocessor directives. Line begins with # are processed by the preprocessor before the program is compiled.  This specific line tells the preprocessor to include in the program the contents of the standard input/output stream header file
printf                          Also called console output, instruct the computer to print on the screen the string of characters contained between the quotation marks.
\                                  This is called escape character.Some common escape sequences ie \n.
\n                               The escape sequence means newline.
return 0                      This statements terminates the function containing it and return the specified value to that function caller. In this example, it will terminate main and return 0 to its caller.
Console Output (printf)
printf is an object of the stdio class and is said to be “tied to” or connected to the standard output device, normally the display screen. 

// another example display data on screen
#include <stdio.h>
main()
{             
  printf( “45 plus 50 is  %d“ ,45 + 50);
  return 0;
}
 
Example


            




 

Header File

Standard libraries (and most C libraries) typically come with what are called “Header files”. These files provide information to the C compiler about the inputs and results produce by each function in the libraries.  Header files usually accessed by the #include directive.

Preprocessor Directives

Preprocessing occurs before a program is compiled. Some possible actions are : inclusion of other files in the file being compiled, definition of symbolic constants and macros, conditional compilation of program code and conditional execution of preprocessor directives.                 Preprocessing refer to the first step in translating.  All preprocesssor directives begin with #.  Preprocessor directives are not C statements,  so they do not end in (;).  It is processed fully before compilation begins.  You can utilize three major capabilities of the preprocessor to make your programs modular,  more readable and easier to customize:
1.             #include
                This directive is to insert the contents of a file into your program. With this, you can place common declarations in one location and use them in all source file through file inclusion.  The result is a reduced risk of mismatches between declarations of variables and functions in separate program modules. If the file is enclosed in angle brackets ( < and > ) - used for standard header files.
               
Examples:
                #include <stdio.h>

2.             #define
                This directive define macros that enable you to replace one string with another.  You can use it to give meaningful names to numeric constants, thus improving constants,  thus improving the readability of your source files.
                Examples:
                #define PI 3.149
3.                   With directives such as #if,  #ifdef,  #else and #endif,  you can compile only selected portion of your program.  You can use this feature to write source files with code for two nor more systems,  but compile only those parts that apply to the computer system on which you compile the program.  With this strategy ,  you can maintain multiple versions of a program using a single set of source files.

Text Box: /* C program that illustrates the usage of #define  */

#include <stdio.h>
#define X1  b+c
#define X2  X1+X1
#define X3  X2* c+X1-d
#define X4  2*X1+3*X2+4* X3

main()
{
  int b=2;
  int c=3;
  int d=4;
  int e=X4;
  printf(" %d  %d  %d  %d  %d ", e, X1, X2, X3, X4);
  return 0;
}

Example

Escape Sequence

An escape sequences always begins with the backslash \ and is followed by one or more special characters. 

Escape Sequence
Description
\n
Newline
\t
Horizontal tab
\a
Alert. Sound the system bell
\\
Backslash. Used to print backslash character.
\”
Double quote. Used to print double quote character.

Example

// A first C  program
#include <stdio.h>

void main(void)
{
  printf( “Welcome  to C \a \n”);
  printf( “Thank you for using me! \a \n”);
}
 
 








Commenting of C  programs

C programs can include comments that provide information to a person reading the program. Comments in C are preceded by the two characters  / and * and are followed by the same characters * and / in reversed order.  A most common feature of most programs written by professional programmers is that there is a program heading as shown below:

/**************************************************************
                PROGRAMMER NAME : yosof kadase
                DATE :  January 17, 2011
                PURPOSE OF PROGRAM : To Print Hello
****************************************************************/
#include <stdio.h>
main()
{
                  printf( “Welcome  to C \n”);
                  printf( “Thank you for using me! \n”);
                  return 0;
}


 
 





























< Programming Questions 


PQ1     Write a C  program to print your name, address and telephone number in the following format:

            Name...
                     Address...
                                 Telephone

PQ2     Write a C  program to print your name, address and telephone number in the following format:

                                 Name...., Address...., Telephone.....

 PQ3    Write a C program to print your initials in a large size type using the letter of your initials as components of the output. Adjust the initial by fully used of escape sequences and save your program as Initial.c.
            Example: if your initial is ADL

                           AAA            DDDDDD         LL
                        AA   AA         DDDDDDD      LL
                      AA      AA        DD      DDD      LL
                      AAAAAA        DD       DDD   LL
                      AAAAAA        DD       DDD   LL
                      AA      AA       DD      DDD      LL
                      AA      AA       DDDDDDD       LLLLLLLL
                      AA       AA       DDDDDD         LLLLLLLL