Wednesday, 3 August 2016

"Arrays in C"

Arrays play very crucial role in processing of large amount of data. suppose i wish to add 25 numbers .so my steps will be to get 25 numbers from user, store them in some variable and then add them. Now number of variable i require here will be 25 (sort of manageable) but think of amount of work to be done if user wish to add 1000 number. So to sort out such problems array as developed in C.
Array is collection of large number of data each of same type(Yes you must have every element in collection of same data type).

To demonstrate further we will write some programs. Ok lets do some programming,
Now a Lift in our office can handle max. of 840 Kg of weight. So this may be good situation to utilised arrays. What we will do is we will take weight input from every labor entering into lift (probably ! stupid task but for use array it's fine) if sum weights of labor exceeds 840 we will give an indication and stop lift until some of labor left a lift. 

Program : 

 #include<stdio.h>
#include<stdlib.h>
int main(){
int number[100]={0};
int n,i,sum =0;
printf("Enter the number of labor inside Lift :\n");
scanf("%d", &n);

for(i = 0; i < n; i++){
printf("Enter the weight of %d labor \n", i+1);
scanf("%d", &number[i]);
sum += number[i];
}
if( sum < 840)
{


    printf("Total weight inside lift is : %d\n",sum);
    printf("You all can travel with lift safely :) \n");
}
else
    {

    printf("Total weight inside lift is : %d\n",sum);
    printf("Lift cannot handle this much of weight. Some of labor please come from neighboring lift ");

    }
return 0;


}



USE of  "TYPEDEF" in C-language.


typedef is used to assign user defined name to data types in c. for example if i am too lazy programmer and my team leader assign me a task of developing a program that consist of many variable with long unsigned data type. so in that case i can take help of typedef function available in C.

So i might defined unsigned long as ulong or ul in following way :
The proper format to use is as follow:

typedef   Previous_name  New_name ;

To demonstrate use of typedef function, i have prepared one program having two variable defined by inbuild data type and user defined data type. you will find size of both variable is same.  



#include<stdio.h>
#include<conio.h>

void main()
{

typedef unsigned long ulong;



unsigned long a = 0;
ulong b = 0;

printf("The size to store a inside memory is %d bytes \n ", sizeof(a) );
printf("The size to store b inside memory is %d bytes \n", sizeof(b));

}