Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts

Wednesday, June 13, 2012

Arrays in c

Arrays in c


Array tutorials in c programming language by examples


An array is derived data type in c programming language which can store similar type of data in continuous memory location. Data may be primitive type (int, char, float, double…), address of union, structure, pointer, function or another array.

Example of array declaration:

int arr[5];
char arr[5];
float arr[5];
long double arr[5];
char * arr[5];
int (arr[])();
double ** arr[5];
Array is useful when:

(a) We have to store large number of data of similar type. If we have large number of similar kind of variable then it is very difficult to remember name of all variables and write the program. For example:
//PROCESS ONE
#include
int main(){
    int ax=1;
    int b=2;
    int cg=5;
    int dff=7;
    int am=8;
    int raja=0;
    int rani=11;
    int xxx=5;
    int yyy=90;
    int p;
    int q;
    int r;
    int avg;
    avg=(ax+b+cg+dff+am+raja+rani+xxx+yyy+p+q+r)/12;
    printf("%d",avg);

    return 0;        
}
If we will use array then above program can be written as:
//PROCESS TWO
#include
int main(){
    int arr[]={1,2,5,7,8,0,11,5,50};
    int i,avg;
    for(int i=0;i<12;i++){
         avg=avg+arr[i];
    }
    printf("%d",avg/12);
    return 0;        
}
Question: Write a C program to find out average of 200 integer number using process one and two.
(b) We want to store large number of data in continuous memory location. Array always stores data in continuous memory location.
(q) What will be output when you will execute the following program?

#include
int main(){
int arr[]={0,10,20,30,40};
    char *ptr=arr;
    arr=arr+2;
    printf("%d",*arr);
    return 0;        
}
Advantage of using array:
1. An array provides singe name .So it easy to remember the name of all element of an array.

2. Array name gives base address of an array .So with the help increment operator we can visit one by one all the element of an array.

3. Array has many application data structure.
Array of pointers in c:

        Array whose content is address of another variable is known as array pointers.  For example:

#include
int main(){
float a=0.0f,b=1.0f,c=2.0f;
    float * arr[]={&a,&b,&c};
    b=a+c;
    printf("%f",arr[1]);
    return 0;        
}


Complex arrays in c

1. Declaration of an array of size five which can store address such functions whose parameter is void data type and return type is also void data type:
void ( arr[5] )( );
2. Declaration of an array of size five which can store address such function which has two parameter of int data type and return type is  float data type:
float ( arr[5] )(int, int);
3. Declaration of an array of size two which can store the address of printf or sacanf function:
int ( arr[2] )( const char *, … );
Note: prototype of printf function is:  int printf( const char *, … );
Different type of array in c:
(a) Array of integer
    An array which can hold integer data type is known as array of integer.

(b) Array of character
    An array which can hold character data type is known as array of character.
(c) Array of union
    An array which can hold address of union data type is known as union of integer.

For example:
(1) What will be output when you will execute the following program?

#include
union A{
char p;
float const * const q;
};
int main(){
    union A arr[10];
    printf("%d",sizeof arr);
   return 0;      
}
  
Output: 20
(2) What will be output when you will execute the following program?
#include
union A{
    char character;
    int ascii;
};
int main(){
    union A arr[2]={{65},{'a'}};
    printf("%c %c",arr[0],arr[1]);
       return 0;      
}
  
Output: A a
(d) Array of structure
 An array which can hold address of structure data type is known as array of structure. For example:
(1) What will be output when you will execute the following program?


#include
typedef struct stu{
    char * name;
    int roll;
}s;
int main(){
    s arr[2]={{"raja",10},{"rani",11}};
    printf("%s %d",arr[0]);
    return 0;        
}
Output: raja 10
(2) What will be output when you will execute the following program?


#include
struct A{
    int p;
    float q;
    long double *r;
};
int main(){
    struct A arr[10];
    printf("%d",sizeof arr);
   
    return 0;        
}
Output: 80
(e) Array of string
    An array which can hold integer data type is known as array of integer.
(f) Array of array
    An array which can hold address of another array is known as array of array.
(g) Array of address of integer
    An array which can hold address integer data type is known as array of address of integer.
Pointer to array

A pointer which holds base address of an array or address of any element of an array is known as pointer to array. For example:
(a)

#include
int main(){
    int arr[5]={100,200,300};
    int *ptr1=arr;
    char *ptr2=(char *)arr;
    printf("%d   %d",*(ptr1+2),*(ptr2+4));

       return 0;       
}
Output: 300   44

(b)

#include
int main(){
    static int a=11,b=22,c=33;
    int * arr[5]={&a,&b,&c};
    int const * const *ptr=&arr[1];
    --ptr;
    printf("%d ",**ptr);
        return 0;        
}
Output: 11

C Command line argument Q & A

C Command line argument questions with solution


Command line arguments in c example 




(1)What will be output of following c code?
#include
int main(int count,char *argv[]){
    int i=0;
    for(i=0;i
         printf("\n%s",argv[i]);
    return 0;
}
//save file as arg.c
In command line
C:\tc\bin>arg c question bank
Output:
c
question
bank




(2) What will be output of following c code?
#include
#include
int main(){
    printf("%d",_argc);
    return 0;
}
//save file as countarg.c
In command line
C:\tc\bin>countarg a1 a2 b1 b2 (press enter)
Output:
5
Explanation:
Here _argc is global identifier which has defined in dos.h.it count toal number of argument in command line.




(3)Reverse any string while string is passed throw command line?
Answer:
#include
#include
int main(int count,char *str[]){
    printf("%s",strrev(str[1]));
    return 0;
}


(4) What will be output following c code?
#include
#include
int main(){
    int i=0;
    for(i=0;i<_argc;i++)
         printf("\n%s",_argv[i]);
    return 0;
}
//save file as arg.c
In command line
C:\tc\bin>arg usa india japan
Output:
Usa
India
japan

Explanation:
Here _argc,_argv is global identifier which has defined in dos.h._arg count total number of argument in command line while _argv is array of string which store all the argument in command line.

(5) Write a c program to create dos command type.
Answer:
#include
int main(int count,char * argv[]){
    int i;
    FILE *ptr;
    char *str;
    char ch;
    if(count==1){
         printf("The syntax of the command is incorrect.\n");
    }
    for(i=1;i
         ptr=fopen(argv[i],"r");
         if(ptr==NULL){
             printf("The system cannot find the file specified.");
             if(count>2)
                 printf("\nError occurred while procesing : %s.\n",argv[i]);
         }
         else{
             if(count>2){
                 printf("%s\n\n",argv[i]);
             }
             while((ch=getc(ptr))!=-1)
                 printf("%c",ch);
         }
         fclose(ptr);
    }
    return 0;
}
Save the above file as open.c, compile and execute the go to command mode (current working directory) and write: open xy.c (xy.c any file present in that directory)
To run the open command in all directories and drive you will have to give the path of current working directory in command mode. Write:
C:tc\bin>PATH c:\tc\bin
Now press enter key. Now your open command will work in all directory and drive.

(6) Write a c program to create dos command dir.
Answer:
#include
#include
int main(int count,char *argv[]){
    struct find_t q ;
    int a;
    if(count==1)
         argv[1]="*.*";
    a = _dos_findfirst(argv[1],1,&q);
    if(a==0){
         while (!a){
             printf(" %s\n", q.name);
             a = _dos_findnext(&q);
         }
    }
    else{
         printf("File not found");
    }
    return 0;
}
Save the above file as list.c, compile and execute the go to command mode (current working directory) and write: list *.c
To run the list command in all directories and drive you will have to give the path of current working directory in command mode. Write:
C:tc\bin>PATH c:\tc\bin
Now press enter key. Now your list command will work in all directory and drive.
Image list

(7)How can we display the entire environments vector by c program?
Answer:
#include
int main(int count,char *arg[],char *argvect[]){
    int i=0;
    while(argvect[i]) {
         printf("\n%s",argvect[i]);
         i++;
    }
    return 0;
}


(8)Write a c program which takes a string from command line with main function has no parameter and convert the string in uppercase?
Answer:
#include
#include
#include
int main(){
    char str[15];
    int i=0;
    strcpy(str,_argv[1]);
    for(i=0;i<=strlen(str);i++){
         if(str[i]>=97&&str[i]<=122)
             str[i]=str[i]-32;
    }
    printf("\nstring in uppercase : %s",str);
    return 0;
}


If you have any queries or suggestions in above C Command line argument questions with solution, please share it.