Clouds
Home
Tutorial of C language
Computer Language
Introduction to C
Programming in C
Structure of C program
Variable in C
Datatypes in C
Token in C
Operator in C
Precedence of C Operator
Assignment Operator in C
Logical Operator in C
Bitwise Operator in C
Ternary Operator in C
C Input Output
Pointers in C
Storage Classes of C
Control Structure of C
C Decision Making Statement
C Looping Statement
C Jumping Statement
C Function
Function Call By Value
Function Call By Reference
C Input Output Function
C Structure
Array in C
Union in C

Array

An array is a subscripted or indexed variables created at contiguous memory location grouped under name and used to stores similar type of data, which can be manipulated during run-time. We can categorise array as follow
  • One dimensional array
  • Two dimensional array

    One Dimensional Array

  • One Dimensional Numeric Array
  • One Dimensional Character Array

    One Dimensional Numeric Array:

    An array declare with numeric data type are said to be numeric array

    Input Statement

    Input Statement:-Input in numeric array can be taken using two ways
  • Direct Initialisation: Is a technique which values can be assigned at the time of declaration.
  • Syntax: data type variable-name [Size of array] = {Value};

    Example:

    int num[5]  = {2,4,6,7,8};

    Input through Keyboard:

    An input in an one dimensional numeric can be taken using the following format.
  • Syntax: scanf("%format specified",&array-name[index]);

    Example:-

    				void  main()
    				{
    				 	int num[5],i;
    				 	clrscr();
    					for(i=0;i<5;i++)
    					{
    				 		printf("Enter the array Element: ")
    						scanf("%d", ,&num[i]);
    					}
    				}
    				

    Output Statement:

    Example:
    					For(i=0;i<5;i++)
    					{
    				 		Printf("%d",num[i]);
    					}
    				

    Question: WAP to assign 5 elements in one dimensional array then display the same element.

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    				  	int num[5]={1,2,3,4,5},i;
    				  	for(i=0;i<5;i++)
    				  	{
    				    	printf("%d",num[i]);
    				  	}
    				 	getch();
    				}
    				

    Question: WAP to Enter 5 elements in one dimensional array then display the same element.

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    				  	int num[5],i;
    				  	for(i=0;i<5;i++)
    				  	{
    				    	printf("Enter the array Element:");
    				    	scanf("%d",&num[i]);
    				  	}
    				  	for(i=0;i<5;i++)
    				  	{
    				    	printf("%d",num[i]);
    				  	}
    				 	getch();
    				}
    				

    Question: WAP to Enter 5 elements in one dimensional array then print the sum and Average .

    				#include<stdio.h>
    				#include<conio.h>
    				void  main()
    				{
    				  	int num[5],i,sum=0,avg;
    				  	for(i=0;i<5;i++)
    				  	{
    				    	printf("Enter the array Element:");
    				    	scanf("%d",&num[i]);
    				  	}
    				  	for(i=0;i<5;i++)
    				  	{
    				   		sum=sum+num[i];
    				  	}
    				  	Avg=sum/i;
    				  	printf("Sum No=%d",sum);
    				  	printf("Average value=%d",avg);
    				  	getch();
    				}
    				

    One Dimensional Character Array

    An array declare with character data type is refered to as character array. Input Statement
  • Direct Initialisation: Syntax:- datatype variable-name[size]={value};

    Example:-

    char str[5]={N,I,M,S};
    Or
    char str[5]={"JNU"};

    Input through Keyboard:-

    Example:-

    					Printf("Enter the String:");
    					Scanf("%s",str);
    				

    Output Statement:-

  • whole string at a time
  • Example:-
    printf("%s",str);
  • Character-by-Character
  • Example:-
    				for(i=0;str[i]!=\0;i++)
    				{
    				 	Printf("%c",str[i])
    				}
    				

    Question :- write a program to assign the string then print the same string.

    				#include<stdio.h>
    				#include<conio.h>
    				void  main()
    				{
    					char str[20]={'J','N','U'};
    					printf("%s",str);
    					getch();
    				}
    				

    Question :- write a program to assign the string then print the same string.

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    					char str[20]={"JNU"};
    					printf("%s",str);
    					getch();
    				}
    				

    Question :- write a program to enter the string then print the same string.

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    					char str[20];
    					clrscr();
    					printf("Enter the String: ");
    					scanf("%s",&str);
    					printf("%s",str);
    					getch();
    				}
    				

    Question :- write a program to enter the string then print the length of string

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    					char str[20];
    					int i;
    					clrscr();
    					printf("Enter the String: ");
    					scanf("%s",&str);
    					for(i=0;str[i]!='\0';i++);
    					printf("Length of string=%d",i);
    					getch();
    				}
    				

    Question :- write a program to enter the string then print the reverse of string

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    					char str[20];
    					int i;
    					clrscr();
    					printf("Enter the String: ");
    					scanf("%s",&str);
    					for(i=0;str[i]!='\0';i++);
    					for(--i;i>=0;i--)
    					{
    						printf("%c",str[i]);
    					}
    					getch();
    				}
    				

    Question :- write a program to enter the string then print the reverse of string.

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    					char str[20],rev[20];
    					int i,j;
    					clrscr();
    					printf("Enter the String: ");
    					scanf("%s",&str);
    					for(i=0;str[i]!='\0';i++);
    					for(--i,j=0;i>=0;i--,j++)
    					{
    						rev[j]=str[i];
    					}
    					printf("%s",rev);
    					getch();
    				}
    				

    Two Dimensional Array

    Input Statement

    Direct initialization:-

    Example:-

    					Int num[2][2]={1,2,3,4};
    					Input through keyboard
    					int r,c;
    					for(r=0;r<2;r++)
    					{
    						For(c=0;c<2;c++)
    				 		{
    				 			Printf("Enter the array Element:");
    							Scanf("%d",&num[r][c]);
    						}
    					}
    				
    Output Statement:-
    				for(r=0;r<2;r++)
    				{
    				 	for(c=0;c<2;c++)
    				 	{
    						printf("%d",&num[r][c]);}}
    					}
    				}
    				

    Question:- WAP to assign the 4 element in 2/2 matrix then print the same elements

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    					int num[2][2]={1,2,3,4},r,c;
    					clrscr();
    					for(r=0;r<2;r++)
    					{
    				 		for(c=0;c<2;c++)
    				 		{
    				  			printf("%d ",num[r][c]);
    				  			printf("\n");
    						}
    					getch();
    				}
    				

    Question:-WAP enter the 4 element in 2/2 matrix then print the same elements.

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    					int num[2][2],r,c;
    					for(r=0;r<2;r++)
    					{
    				 		for(c=0;c<2;c++)
    				 		{
    				  			scanf("%d ",&num[r][c]);
    				 		}
    					}
    					for(r=0;r<2;r++)
    					{
    				 		for(c=0;c<2;c++)
    				 		{
    				  			printf("%d ",num[r][c]);
    				 		}
    				 	printf("\n");
    					}
    					getch();
    				}
    				

    Question:-WAP enter the 4 element in 2/2 matrix then print the sum of elements.

    				#include<stdio.h>
    				#include<conio.h>
    				void main()
    				{
    					int num[2][2],r,c,sum=0;
    					for(r=0;r<2;r++)
    					{
    				 		for(c=0;c<2;c++)
    				 		{
    				  			Printf("Enter the array Element: ");
    				 	 		scanf("%d ",&num[r][c]);
    						}
    					}
    					for(r=0;r<2;r++)
    					{
    				 		for(c=0;c<2;c++)
    				 		{
    				 	 		Sum=sum+num[r][c]);
    				 		}
    					}
    					getch();
    				}
    				

    Union in C >>
    Computer Language >>
    Introduction to C >>
    Programming in C >>
    Structure of C program >>


    Home Clouds