Amazon

Wednesday, May 18, 2016

C language Arrays And Looping Statements



C'S ARRAYS AND LOOPING STATEMENTS



Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.

Title: C'S ARRAYS AND LOOPING STATEMENTS


Ø  Download the .Docx Format of this article. ClickHERE

Ø  Download the .Pdf Format of this article. ClickHERE

PART A - INTRODUCTION TO ARRAYS
§  What is an Array
o   An array is a collection of same type of elements which are sheltered under a common name.
         - Storing a string that contains series of characters, like storing a name in memory.
         - Storing multiple strings, like storing multiple names.
o   Array is a collection of same type elements under the same variable identifier referenced by index number.
o   Arrays are of two types single-dimensional array and multi-dimensional array.
§  Single-Dimensional Array
o   A single dimensional array will be useful for simple grouping of data that is relatively small in size. You can declare a single dimensional array as follows:
SYNTAX:
data_type array_name[size_of_array];

         data_type: It is the type of elements that an array stores. If array stores integer          elements then type of array is ‘int’.
         array_name: This is the name that is given to array. It can be any string but it is usually          suggested that some can of standard should be followed while naming arrays.
         [size_of_array]: This value in subscripts [] indicates the number of elements the array stores.
o   For example, an array of five characters can be defined as :

char arr[5];

o   How to Initialize an Array?
         - Initializing each element separately. For example:
                     int arr[10];
        int i = 0;
        for(i=0;i<sizeof(arr);i++)
             {
             arr[i] = i; // Initializing each element separately
             }
         - Initializing array at the time of declaration. For example:

                     int arr[] = {'1','2','3','4','5'};

         - Initializing array with a string (Method-Characters):

                     char arr[] = {'c','o','d','e','\0'};

         - Initializing array with a string (Method-String):

                     char arr[] = "code";

         - Accessing Values in an Array:

                     int arr[10];
        int i = 0;
        for(i=0;i<sizeof(arr);i++)
             {
             arr[i] = i; // Initializing each element separately
             }
        int j = arr[5]; // Accessing the 5th element of array

§  Multi-Dimensional Array (Two-Dimension)
o   A two-dimensional array can hold 2 indexes which can be represented by rows and columns.
o   Example:
                     int arr[2][3];

         - It can be represented like a 2X3 table.
0
1
2
1
1
2
         - Where the numbers are the index value

PART B - LOOPING STATEMENTS
§  What is a Loop?
o   Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping way.
§  for loop
o   for statements are often used to process lists such a range of numbers:
SYNTAX:
for(counter_initialization; condition; inc/dec)
{
   Single statement
   or
   Block of statements;
}

counter_initialization - Initialize counter variable.
condition - Conditional expression, as long as this condition is true, loop will keep executing.
inc/dec - It is the modifier which may be simple increment or decrement of a counter.

Example:
#include <stdio.h>
main()
{
    int i;
    int j = 5;

    for( i = 0; i <= j; i ++ )
    {
       printf("Hello %d\n", i );
    }
}

Sample Output:
         Hello 0
         Hello 1
         Hello 2
         Hello 3
         Hello 4
         Hello 5

§  While Loop
o   The most basic loop in C is the while loop.
o   A while statement is like a repeating if statement.
o   Like an If statement, if the test condition is true: the statements get executed.
o   The difference is that after the statements have been executed, the test condition is checked again.
o   If it is still true the statements get executed again. This cycle repeats until the test condition evaluates to false.
SYNTAX
while ( condition )
{
   Single statement
   or
   Block of statements;
}

Example:
#include <stdio.h>

main()
{
    int i = 5;

    while ( i > 0 )
    {
       printf("Hello %d\n", i );
       i = i -1;
    }
}

Sample Output:
         Hello 5
         Hello 4
         Hello 3
         Hello 2
         Hello 1
           
§  Do...while loop
o   do ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start.
o   This has the effect that the content of the loop are always executed at least once.
SYNTAX:
do
{
   Single statement
   or
   Block of statements;
}while(condition);

Example:
#include <stdio.h>

main()
{
    int i = 5;

    do{
       printf("Hello %d\n", i );
       i = i -1;
    }while ( i > 0 );
}
Sample Output:
            Hello 5
            Hello 4
            Hello 3
            Hello 2
            Hello 1

§  Continue statements
o   Continue -- skip 1 iteration of loop.
Example:
for( i = 0; i <= j; i ++ )
    {
       if( i == 5 )
       {
          continue;
       }

PART C - SORTING ALGORITHMS
§  Bubble Sort
o   Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
o   The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
Step by Step Example:

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.

First Pass:
 ( 5 1 4 2 8 )  ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
 ( 1 5 4 2 8 )  ( 1 4 5 2 8 ), Swap since 5 > 4
 ( 1 4 5 2 8 )  ( 1 4 2 5 8 ), Swap since 5 > 2
 ( 1 4 2 5 8 )  ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
 ( 1 4 2 5 8 )  ( 1 4 2 5 8 )
 ( 1 4 2 5 8 )  ( 1 2 4 5 8 ), Swap since 4 > 2
 ( 1 2 4 5 8 )  ( 1 2 4 5 8 )
 ( 1 2 4 5 8 )  ( 1 2 4 5 8 )
 Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
 ( 1 2 4 5 8 )  ( 1 2 4 5 8 )
 ( 1 2 4 5 8 )  ( 1 2 4 5 8 )
 ( 1 2 4 5 8 )  ( 1 2 4 5 8 )
 ( 1 2 4 5 8 )  ( 1 2 4 5 8 )




§  Insertion Sort
o   Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time.
Example: The following table shows the steps for sorting the sequence {3, 7, 4, 9, 5, 2, 6, 1}. In each step, the item under consideration is underlined. The item that was moved (or left in place because it was biggest yet considered) in the previous step is shown in bold.

3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 5 7 9 2 6 1
2 3 4 5 7 9 6 1
2 3 4 5 6 7 9 1
1 2 3 4 5 6 7 9

§  Selection Sort
o   In computer science, a selection sort is a sorting algorithm, specifically an in-place comparison sort.
o   Algorithm:
            1) Find the minimum value in the list
            2) Swap it with the value in the first position
            3) Repeat the steps above for the remainder of the list (starting at the second position and advancing each time)
o   Here is an example of this sort algorithm sorting five elements:

            64 25 12 22 11
     11 25 12 22 64
     11 12 25 22 64
     11 12 22 25 64
               11 12 22 25 64   


Workshop

This Workshop provides quiz questions to help you solidify your understanding of the material covered, and exercises to provide you with experience in using what you've learned.
1. Write a for statement to count from 1 to 100 by 3s.
Answer:



         
2. Write a while statement to count from 1 to 100 by 3s.
Answer:




3. Write a do...while statement to count from 1 to 100 by 3s.
Answer:




4. BUG BUSTER: What is wrong with the following code fragment?
        record = 0;
        while (record < 100)
               {
                printf( "\nRecord %d ", record );
               printf( "\nGetting next number..." );
               }
Answer:




5. BUG BUSTER: What is wrong with the following code fragment? (MAXVALUES is not           the problem!)
        for (counter = 1; counter < MAXVALUES; counter++);
               printf("\nCounter = %d", counter );

        Answer:


PART D - PROGRAM DEVELOPMENT

·         Sorting Program (30pts)
           Create a C program that will accept 10 integer numbers and sort it in:
                       Set01
                                   Ascending Order using Insertion Sort
                       Set02
                                    Descending Order using Selection Sort




Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.

Title: C'S ARRAYS AND LOOPING STATEMENTS
Ø  Download the .Docx Format of this article. ClickHERE

Ø  Download the .Pdf Format of this article. ClickHERE

No comments:

Post a Comment