Header Ads

Header ADS

Array

                                                           Array[ ]
What is array?
Array is one kind of constant that can store multiple value of same kind. Same kind means if I declare  a integer array it will take all integer value neither char type or float type. Array helps us to store multiple value in one constant. If we want to store 100 students mark in their exam we dont need to declare 100 variable we can store it using only one array.

Classification:
There is three kind of array.

  1. One dimensional  array [ ]
  2. Two dimensional array[ ][ ]
  3. Three dimensional array[ ][ ] [ ] 
One Dimensional array:
syntax,

     arrayType  arrayName[ arraySize ] ;

arrayType: one array can carry one kind of value. if you declare integer array it will carry all integer type value. a array can be integer(int) float double char type.

narrayName: array name same as a constant name.

[arraySize]: How many data you want to store. if you want to store 100 student mark you have to input 100 as a array size.

Ex.
float mark[100];

Initialization: 
Method One: int mark[5] = {19, 10, 8, 17, 9};

Method Two: int mark[] = {19, 10, 8, 17, 9};

Method Three: you can initialization using loop or odd loop.

for(i=0;i<5;i++)
            {
          scanf("%d",&arr1[i]);
        }
1   2     3
↓ ↓   
&arr1[i]

1-& means memory address of an array. Because value store in a memory address.
2-arr1 array name.   
3-[i] means index. array index start with 0.
    EX.
  int mark[] = {19108179};
mark[0]=19
mark[1]=10
mark[2]=8
mark[3]=17
mark[4]=9

Lets Solve some problem:


* Input n number of variable and print them. 
code:

#include<stdio.h>
int main(){
    int s,a[100],i;
    printf("Give The Size Of An Array:\n");
    scanf("%d",&s);
    printf("Enter Value:");
    for(i=0;i<s;i++){
        scanf("%d",&a[i]);
    }
printf("Value of array:\n")
for(i=0;i<s;i++){
        printf("%d ",a[i]);
    }
return 0;
}

*Input n number of variable and print their sum. 
code:

#include<stdio.h>
int main(){
    int s,a[100],i,sum=0;
    printf("Give The Size Of An Array:\n");
    scanf("%d",&s);
    printf("Enter Value:");
    for(i=0;i<s;i++){
        scanf("%d",&a[i]);
    }
printf("Value of array:\n")
for(i=0;i<s;i++){
        sum=sum+a[i];
    }
printf("sum = %d\n");

return 0;
}

*Input n number of variable and print them as a ascending order.
Code:
#include <stdio.h>
int main()
{
    int arr1[100];
    int n, i, j, tmp,suma=0,sumb=0,term,count=0;
    scanf("%d",&term);
    while(count<term){
            count++;
    scanf("%d", &n);

       for(i=0;i<n;i++)
            {
          scanf("%d",&arr1[i]);
        }

    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr1[j] <arr1[i])
            {
                tmp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = tmp;
            }
        }
    }
printf("Value of array according to ascending order:\n")
for(i=0;i<n;i++){
        printf("%d ",arr1[i]);
    }
return 0;
}

or, using define function.

#include<stdio.h>
int ascending(int a[],int s);
int main(){
    int s,a[100],i,j,temp;
    printf("Give The Size:\n");
    scanf("%d",&s);
    printf("Enter Value:");
    for(i=0;i<s;i++){
        scanf("%d",&a[i]);
    }
    ascending(a,s);
}

int ascending(int a[],int s){
    int i,j,temp;
    for(i=0; i<s; i++)
    {
        for(j=i+1; j<s; j++)
        {
            if(a[j] <a[i])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
    for(i=0;i<s;i++){
        printf("%d ",a[i]);

        }
}


*Input n (must be even number) number of variable and print their maximum difference.
like:
if you input 1 2 3 4 5 9
in here total 6 number is taken. maximum difference will be (4+5+9)-(1+2+3)=12

code:

#include <stdio.h>
int main()
{
    int arr1[100];
    int n, i, j, tmp,suma=0,sumb=0,term,count=0;
    scanf("%d",&term);
    while(count<term){
            count++;
    scanf("%d", &n);

       for(i=0;i<n;i++)
            {
          scanf("%d",&arr1[i]);
        }

    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr1[j] <arr1[i])
            {
                tmp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = tmp;
            }
        }
    }
    for(i=0; i<n/2; i++)
    {
        suma=suma+arr1[i];
    }
    for(i=n/2; i<n; i++)
    {
        sumb=sumb+arr1[i];
    }

            printf("%d\n",sumb-suma);
            suma=0;
            sumb=0;

    }
    return 0;
}

How to delete a value of an array??

first step:
we have to find an index of that number we want to delete.
second step: 
we have to swap its place with its forward value and its forward value to another forward value until its reach its last value. if an array have 5 value(index 0 to 4) called a[5] and we want to delete a[2]=3 then we have to swap a[2] value with a[3] and a[3] with a[4]. As after a[4] there is no value so we cant swap. so condition will be i<5-1:

Third step:
 we have to print array less than its one size.
if its index 0 to 4 then we have to print 0 to 3. Have fun!!

Code: 
#include<stdio.h>
int cheack(int array[],int size,int value);
int del(int array[],int size,int value);
int main(){
    int array[100],i,size,value,index;
    int *a = &array;
    printf("Enter Array size:\n");
    scanf("%d",&size);
    printf("Enter Array value:\n");
    for(i=0;i<size;i++){
        scanf("%d",&array[i]);
    }
    printf("Enter a value you want to delete\n");
    scanf("%d",&value);
    index=cheack(array,size,value);
    printf("Deleted Index No = %d\n",index);
    if(size!=0){
        printf("New array: \n");
        del(a,size,index);
    }
}
int cheack(int array[],int size,int value){
    int i,index;
    for(i=0;i<size;i++){

        if(array[i]==value){
                index=i;
                return index;
                break;
        }
    }
    printf("No Value Found\n");
    return size =0;

}
int del(int *a, int size, int index){

    int i;
    for(i=index;i<size-1;i++){
        a[i]=a[i+1];
    }
    for(i=0;i<size-1;i++){
        printf("%d ",a[i]);
    }

}




Two Dimensional Array:
Syntax,
       arrayType  arrayName[ rowSize][ColumnSize] ;




Two dimensional array can hold more value than one dimensional array. Two dimensional array same as matrix.  
Ex.
int array[3][4] this array can hold (3 x 4)=12 value. 

Initialization of a 2d array

// Different ways to initialize two-dimensional array

int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
         
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
                
int c[2][3] = {1, 3, 0, -1, 5, 9};

Example 1: Two-dimensional array to store and print values

// C program to store temperature of two cities of a week and display it.
#include <stdio.h>
const int CITY = 2;
const int WEEK = 7;
int main()
{
  int temperature[CITY][WEEK];

  // Using nested loop to store values in a 2d array
  for (int i = 0; i < CITY; ++i)
  {
    for (int j = 0; j < WEEK; ++j)
    {
      printf("City %d, Day %d: ", i + 1, j + 1);
      scanf("%d", &temperature[i][j]);
    }
  }
  printf("\nDisplaying values: \n\n");

  // Using nested loop to display vlues of a 2d array
  for (int i = 0; i < CITY; ++i)
  {
    for (int j = 0; j < WEEK; ++j)
    {
      printf("City %d, Day %d = %d\n", i + 1, j + 1, temperature[i][j]);
    }
  }
  return 0;
}
Output
City 1, Day 1: 33
City 1, Day 2: 34
City 1, Day 3: 35
City 1, Day 4: 33
City 1, Day 5: 32
City 1, Day 6: 31
City 1, Day 7: 30
City 2, Day 1: 23
City 2, Day 2: 22
City 2, Day 3: 21
City 2, Day 4: 24
City 2, Day 5: 22
City 2, Day 6: 25
City 2, Day 7: 26

Displaying values: 

City 1, Day 1 = 33
City 1, Day 2 = 34
City 1, Day 3 = 35
City 1, Day 4 = 33
City 1, Day 5 = 32
City 1, Day 6 = 31
City 1, Day 7 = 30
City 2, Day 1 = 23
City 2, Day 2 = 22
City 2, Day 3 = 21
City 2, Day 4 = 24
City 2, Day 5 = 22
City 2, Day 6 = 25
City 2, Day 7 = 26
Three Dimensional Array:

Syntax,
arrayType  arrayName[ arraySize ][arraySize][arraySize] ;

Three dimensional array can hold more value than one or two  dimensional array.  
Ex.
int array[3][4][4] this array can hold (3 x 4 x 4)=48 value. 

Initialization of a 3d array

You can initialize a three-dimensional array in a similar way like a two-dimensional array. Here's an example,
int test[2][3][4] = {
    {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
    {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};

Example 3: Three-dimensional array

// C Program to store and print 12 values entered by the user

#include <stdio.h>
int main()
{
  int test[2][3][2];

  printf("Enter 12 values: \n");

  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {
      for (int k = 0; k < 2; ++k)
      {
        scanf("%d", &test[i][j][k]);
      }
    }
  }

  // Printing values with proper index.

  printf("\nDisplaying values:\n");
  for (int i = 0; i < 2; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {
      for (int k = 0; k < 2; ++k)
      {
        printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
      }
    }
  }

  return 0;
}
Output
Enter 12 values: 
1
2
3
4
5
6
7
8
9
10
11
12

Displaying Values:
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12

 




No comments

Powered by Blogger.