Learn C programming in 11 days

Day 5 Arrays

Simple array

  

/*********************************************************/

/*  array1.cpp - VC5.0                                   */

/*  Simple array                                         */

/*  J Luo, 20-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

 

void main()

{

    float x[3];

    float sum;       

    float mean;

 

    printf("Input your data, please ... \n");

sum = 0;

    for (int i=0; i<3; i++)

    {

       printf("x(%d) = ", i);

       scanf("%f", &x[i]);

       printf("x(%d) = \t %f \n", i, x[i]);

       sum = sum + x[i];

    }

 

    mean = sum/3;

 

    printf("Sum  = %f\n", sum);

    printf("Mean = %f\n", mean);

}

 

 

Output

 

Input your data, please ...

x(0) = 12.3

x(1) = 45.6

x(2) = 78.9

 12.30 45.60 78.90

Sum  = 136.80

Mean = 45.600

 

/*********************************************************/

/*  array1.cpp - VC5.0                                   */

/*  With a constant                                      */

/*  J Luo, 20-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

 

void main()

{

    const n=3;

    float x[n];

    float sum;       

    float mean;

 

    int i;

 

    printf("Input your data, please ... \n");

    sum = 0;

    for (i=0; i<n; i++)

    {

       printf("x(%d) = ", i);

       scanf("%f", &x[i]);

       sum = sum + x[i];

    }

    mean = sum/n;

 

    for (i=0; i<n; i++) printf("%6.2f\t", x[i]);

    printf("\n");

 

    printf("Sum  = %6.2f\n", sum);

    printf("Mean = %6.3f\n", mean);

}

 

N and n

 

/*********************************************************/

/*  array1.cpp - VC5.0                                   */

/*  With constant                                        */

/*  J Luo, 20-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

 

void main()

{

    const int N=100;

    float x[N];

    float sum;       

    float mean;

    int n;

 

    int i;

 

    printf("No of data n = ");

    scanf("%d", &n);

    printf("%d", n);

 

    sum = 0;

    printf("Input your data, please ... \n");

    for (i=0; i<n; i++)

    {

       printf("x(%d) = ", i);

       scanf("%f", &x[i]);

       sum = sum + x[i];

    }

    mean = sum/n;

 

    for (i=0; i<n; i++) printf("%6.2f\t", x[i]);

    printf("\n");

 

    printf("Sum  = %6.2f\n", sum);

    printf("Mean = %6.3f\n", mean);

}

 

Mean and SD

 

/*********************************************************/

/*  mean_sd.cpp - VC5.0                                  */

/*  Mean and SD                                          */

/*  My FIRST useful C program                            */

/*  J Luo, 14-Feb-2001                                   */

/*  Modified, J Luo, 20-Mar-2001                         */

/*********************************************************/

 

#include <stdio.h>      

#include <math.h>   

 

void main()

{

    const int N=1000;

    float x[N];

    int n;

    float sumX1, sumX2;        

    float mean;      

    float SD;

 

    int i;

 

    printf("No of data n = ");

    scanf("%d", &n);

    printf("%d", n);

 

    printf("Input your data, please ... \n");

    sumX1 = 0;

    sumX2 = 0;

    for (i=0; i<n; i++)

    {

       printf("x(%d) = ", i);

       scanf("%f", &x[i]);

       sumX1 = sumX1 + x[i];

       sumX2 = sumX2 + x[i]*x[i];

    }

 

    mean = sumX1/n;

    SD = (float)sqrt( (sumX2 - sumX1*sumX1/n) / (n-1) );

 

    for (i=0; i<n; i++) printf("%6.2f\t", x[i]);

    printf("\n");

    printf("Mean = %6.3f\n", mean);

    printf("  SD = %6.3f\n", SD);

}

 

T-test

 

/*********************************************************/

/*  sort_swap.cpp - VC5.0                                */

/*  Swap sorting                                         */

/*  J Luo, 28-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

 

void main()

{

    int a[] = {5,6,2,9,10,3,4,7,8,1};

    int n;

    int i, j;

    int temp;

 

    n = sizeof(a) / sizeof(int);

 

    printf("Before sorting ... \n");

    for (i=0; i<n; i++) printf("%d\t", a[i] );

    printf("\n");

 

    for (i=0; i<n; i++)

    {  

       for (j=i+1; j<n; j++)

       {

           if ( a[j] < a[i] )

           {

              temp = a[i];

              a[i] = a[j];

              a[j] = temp;

           }

       }

    }

 

    printf("After sorting ... \n");

    for (i=0; i<n; i++) printf("%d\t", a[i] );

    printf("\n");

}

 

Select sorting

 

/*********************************************************/

/*  sort_select.cpp - VC5.0                              */

/*  Select sorting                                       */

/*  J Luo, 28-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

 

void main()

{

    int a[] = {5,6,2,9,10,3,4,7,8,1};

    int n;

    int i, j, k;

    int temp;

 

    n = sizeof(a) / sizeof(int);

 

    printf("Before sorting ... \n");

    for (i=0; i<n; i++) printf("%d\t", a[i] );

    printf("\n");

 

    for (i=0; i<n; i++)

    {  

       k = i;

       for (j=i+1; j<n; j++)

       {

           if ( a[j] < a[k] ) k = j;

       }

       temp = a[i];

       a[i] = a[k];

       a[k] = temp;

    }

 

    printf("After sorting ... \n");

    for (i=0; i<n; i++) printf("%d\t", a[i] );

    printf("\n");

}

 

Bubble sort

 

/*********************************************************/

/*  sort_bubble1.cpp - VC5.0                             */

/*  Bubble sort                                          */

/*  J Luo, 28-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

 

void main()

{

    int a[] = {5,6,2,9,10,3,4,7,8,1};

    int n;

    int i, j;

    int temp, flag;

 

    n = sizeof(a) / sizeof(int);

 

    printf("Before sorting ... \n");

    for (i=0; i<n; i++) printf("%d\t", a[i] );

    printf("\n");

 

    for (i=0; i<n; i++)

    {  

       flag=0;

       for (j=n; j>i; j--)

       {

           if ( a[j] < a[j-1] )

           {  

              temp = a[j];

              a[j] = a[j-1];

              a[j-1] = temp;

              flag=1;

           }

       }

       if (flag==0) break;

    }

 

    printf("After sorting ... \n");

    for (i=0; i<n; i++) printf("%d\t", a[i] );

    printf("\n");

}

 

Insert sorting

 

/*********************************************************/

/*  sort_insert.cpp - VC5.0                              */

/*  Insert sorting                                       */

/*  J Luo, 28-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

 

void main()

{

    int a[] = {5,6,2,9,10,3,4,7,8,1};

    int n;

    int i, j;

    int temp;

 

    n = sizeof(a) / sizeof(int);

 

    printf("Before sorting ... \n");

    for (i=0; i<n; i++) printf("%d\t", a[i] );

    printf("\n");

 

    for (i=1; i<n; i++)

    {  

       temp = a[i];

       for (j=i-1; j>=0; j--)

       {

           if ( temp < a[j] ) a[j+1] = a[j];

           else break;

       }

       a[j+1] = temp;

    }

 

    printf("After sorting ... \n");

    for (i=0; i<n; i++) printf("%d\t", a[i] );

    printf("\n");

}

 

Matrix multiplication

 

/*********************************************************/

/*  matrix.cpp - VC5.0                                   */

/*  Matrix multiplication                                */

/* Swap sorting of string                                */

/*  J Luo, 28-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

#include <string.h>

 

void main()

{

    int a[2][3] =

    {

       {1, 2, 3},

       {3, 2, 1}

    };

    int b[3][4] =

    {

       {0, 0, 0, 1},

       {0, 1, 0, 1},

       {1, 1, 0, 0}

    };

    int c[2][4];

 

    int i, j, k;

 

    for (i=0; i<2; i++)

    {

       for (j=0; j<3; j++)

       {

           printf("%d\t", a[i][j]);

       }

       printf("\n");    

    }

    printf("\n");    

 

    for (i=0; i<3; i++)

    {

       for (j=0; j<4; j++)

       {

           printf("%d\t", b[i][j]);

       }

       printf("\n");    

    }

    printf("\n");    

 

    for (i=0; i<2; i++)

    {

       for (j=0; j<4; j++)

       {

           c[i][j] = 0;

           for (k=0; k<3; k++)

           {

              c[i][j] = c[i][j] + a[i][k] * b[k][j];

           }

       }

    }

 

    for (i=0; i<2; i++)

    {

       for (j=0; j<4; j++)

       {

           printf("%d\t", c[i][j]);

       }

       printf("\n");    

    }

    printf("\n");    

}

 

1   2   3  

3   2   1  

 

0   0   0   1  

0   1   0   1  

1   1   0   0  

 

3   5   0   3  

1   3   0   5  

 

Sine Curve

 

/*********************************************************/

/*  sine.cpp - VC5.0                                     */

/*  Print out since curve                                */

/*  J Luo, 28-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

#include <math.h>

 

void main()

{

    const float pi = 3.1415927F;

    const int N = 51;

    char plot[N][N];

    int x, y;

    int np, nc;

    float r;

 

    for (x=0; x<N; x++)

    {

       for (y=0; y<N; y++)

       {

           plot[x][y] = ' ';

       }

    }

 

    r = 2*pi / (N-1);    // 2*pi/360

    np = (N-1) / 2;

    nc = np + 1;

    for (x=0; x<N; x++)

    {

       y = (int) ( np * sin(x*r) + nc );

       plot[x][nc]  = '|';

       plot[x][y] = '*';

    }

 

    for (x=0; x<N; x++)

    {

       for (y=0; y<N; y++)

       {

           printf("%c", plot[x][y]);

       }

       printf("\n");

    }

}

 

Output

 

                          *                       

                          |  *                    

                          |     *                 

                          |        *              

                          |           *           

                          |             *         

                          |                *      

                          |                  *    

                          |                    *  

                          |                     * 

                          |                      *

                          |                       *

                          |                       *

                          |                       *

                          |                       *

                          |                      *

                          |                     * 

                          |                    *  

                          |                  *    

                          |                *      

                          |             *         

                          |           *           

                          |        *              

                          |     *                 

                          |  *                    

                         *|                       

                      *   |                       

                   *      |                       

                *         |                       

             *            |                       

           *              |                       

        *                 |                       

      *                   |                        

    *                     |                       

   *                      |                       

  *                       |                       

 *                        |                       

 *                        |                        

 *                        |                       

 *                        |                       

  *                       |                       

   *                      |                       

    *                     |                        

      *                   |                       

        *                 |                       

           *              |                       

             *            |                       

                *         |                        

                   *      |                       

                      *   |                       

                          *