Tutorials
C LanguageIn this tutorial you will learn about C Programming - Arrays - Declaration of arrays, Initialization of arrays, Multi dimensional Arrays, Elements of multi dimension arrays and Initialization of multidimensional arrays.
The C language provides a capability that enables the user to define a set of ordered data items known as an array.
Suppose we had a set of grades that we wished to read into the computer and suppose we wished to perform some operations on these grades, we will quickly realize that we cannot perform such an operation until each and every grade has been entered since it would be quite a tedious task to declare each and every student grade as a variable especially since there may be a very large number.
In C we can define variable called grades, which represents not a single value of grade but a entire set of grades. Each element of the set can then be referenced by means of a number called as index number or subscript.
Like any other variable arrays must be declared before they are used. The general form of declaration is:
type variable-name[50];
The type specifies the type of the elements that will be contained in the array, such as int float or char and the size indicates the maximum number of elements that can be stored inside the array for ex:
float height[50];
Declares the height to be an array containing 50 real elements. Any subscripts 0 to 49 are valid. In C the array elements index or subscript begins with number zero. So height [0] refers to the first element of the array. (For this reason, it is easier to think of it as referring to element number zero, rather than as referring to the first element).
As individual array element can be used anywhere that a normal variable with a statement such as
G = grade [50];
The statement assigns the value stored in the 50th index of the array to the variable g.
More generally if I is declared to be an integer variable, then the statement g=grades [I];
Will take the value contained in the element number I of the grades array to assign it to g. so if I were equal to 7 when the above statement is executed, then the value of grades [7] would get assigned to g.
A value stored into an element in the array simply by specifying the array element on the left hand side of the equals sign. In the statement
grades [100]=95;
The value 95 is stored into the element number 100 of the grades array.
The ability to represent a collection of related data items by a single array enables us to develop concise and efficient programs. For example we can very easily sequence through the elements in the array by varying the value of the variable that is used as a subscript into the array. So the for loop
for(i=0;i < 100;++i);
sum = sum + grades [i];
Will sequence through the first 100 elements of the array grades (elements 0 to 99) and will add the values of each grade into sum. When the for loop is finished, the variable sum will then contain the total of first 100 values of the grades array (Assuming sum were set to zero before the loop was entered)
In addition to integer constants, integer valued expressions can also be inside the brackets to reference a particular element of the array. So if low and high were defined as integer variables, then the statement
next_value=sorted_data[(low+high)/2]; would assign to the variable next_value indexed by evaluating the expression (low+high)/2. If low is equal to 1 and high were equal to 9, then the value of sorted_data[5] would be assigned to the next_value and if low were equal to 1 and high were equal to 10 then the value of sorted_data[5] would also be referenced.
Just as variables arrays must also be declared before they are used. The declaration of an array involves the type of the element that will be contained in the array such as int, float, char as well as maximum number of elements that will be stored inside the array. The C system needs this latter information in order to determine how much memory space to reserve for the particular array.
The declaration int values[10]; would reserve enough space for an array called values that could hold up to 10 integers. Refer to the below given picture to conceptualize the reserved storage space.
|
values[0] |
|
|
values[1] |
|
|
values[2] |
|
|
values[3] |
|
|
values[4] |
|
|
values[5] |
|
|
values[6] |
|
|
values[7] |
|
|
values[8] |
|
|
values[9] |
|
The array values stored in the memory.
We can initialize the elements in the array in the same way as the ordinary variables when they are declared. The general form of initialization off arrays is:
type array_name[size]={list of values};
The values in the list care separated by commas, for example the statement
int number[3]={0,0,0};
Will declare the array size as a array of size 3 and will assign zero to each element if the number of values in the list is less than the number of elements, then only that many elements are initialized. The remaining elements will be set to zero automatically.
In the declaration of an array the size may be omitted, in such cases the compiler allocates enough space for all initialized elements. For example the statement
int counter[]={1,1,1,1};
Will declare the array to contain four elements with initial values 1. this approach works fine as long as we initialize every element in the array.
The initialization of arrays in c suffers two draw backs
1. There is no convenient way to initialize only selected elements.
2. There is no shortcut method to initialize large number of elements.
| /* Program to count the no of positive and negative numbers*/ #include< stdio.h > void main( ) { int a[50],n,count_neg=0,count_pos=0,I; printf(“Enter the size of the array\n”); scanf(“%d”,&n); printf(“Enter the elements of the array\n”); for I=0;I < n;I++) scanf(“%d”,&a[I]); for(I=0;I < n;I++) { if(a[I] < 0) count_neg++; else count_pos++; } printf(“There are %d negative numbers in the array\n”,count_neg); printf(“There are %d positive numbers in the array\n”,count_pos); } |
Often there is a need to store and manipulate two dimensional data structure such as matrices & tables. Here the array has two subscripts. One subscript denotes the row & the other the column.
The declaration of two dimension arrays is as follows:
data_type array_name[row_size][column_size];
int m[10][20]
Here m is declared as a matrix having 10 rows( numbered from 0 to 9) and 20 columns(numbered 0 through 19). The first element of the matrix is m[0][0] and the last row last column is m[9][19]
A 2 dimensional array marks [4][3] is shown below figure. The first element is given by marks [0][0] contains 35.5 & second element is marks [0][1] and contains 40.5 and so on.
|
marks [0][0] |
Marks [0][1] |
Marks [0][2] |
|
marks [1][0] |
Marks [1][1] |
Marks [1][2] |
|
marks [2][0] |
Marks [2][1] |
Marks [2][2] |
|
marks [3][0] |
Marks [3][1] |
Marks [3][2] |
Like the one dimension arrays, 2 dimension arrays may be initialized by following their declaration with a list of initial values enclosed in braces
Example:
int table[2][3]={0,0,01,1,1};
Initializes the elements of first row to zero and second row to 1. The initialization is done row by row. The above statement can be equivalently written as
int table[2][3]={{0,0,0},{1,1,1}}
By surrounding the elements of each row by braces.
C allows arrays of three or more dimensions. The compiler determines the maximum number of dimension. The general form of a multidimensional array declaration is:
date_type array_name[s1][s2][s3]…..[sn];
Where s is the size of the ith dimension. Some examples are:
int survey[3][5][12];
float table[5][4][5][3];
Survey is a 3 dimensional array declared to contain 180 integer elements. Similarly table is a four dimensional array containing 300 elements of floating point type.
| /* example program to add two matrices & store the results in the 3rd matrix */ #include< stdio.h > #include< conio.h > void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q; clrscr(); printf(“enter the order of the matrix\n”); scanf(“%d%d”,&p,&q); if(m==p && n==q) { printf(“matrix can be added\n”); printf(“enter the elements of the matrix a”); for(i=0;i < m;i++) for(j=0;j < n;j++) scanf(“%d”,&a[i][j]); printf(“enter the elements of the matrix b”); for(i=0;i < p;i++) for(j=0;j < q;j++) scanf(“%d”,&b[i][j]); printf(“the sum of the matrix a and b is”); for(i=0;i < m;i++) for(j=0;j < n;j++) c[i][j]=a[i][j]+b[i][j]; for(i=0;i < m;i++) { for(j=0;j < n;j++) printf(“%d\t”,&a[i][j]); printf(“\n”); } } |

|
/* example program to add two matrices & store the results in the 3rd matrix */ #include #include void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q; clrscr(); printf(“enter the order of the matrix\n”); scanf(“%d%d”,&p,&q); if(m==p && n==q) { printf(“matrix can be added\n”); printf(“enter the elements of the matrix a”); for(i=0;i [i][j] b[i][j]; for(i=0;i j ) printf(“%d\t”,&a[i][j]); printf(“\n”); } } |
|
int table[2][3]={0,0,01,1,1}; :: Here is a typo:: CORRET: int table[2][3]={0,0,0,1,1,1}; --comma missing |
| Hi can u plz help me out to write a program for the mul;tipication of twp matrixes. |
| Create a program that will compute for the sum of all odd numbers between 0 to 10. Store the values in a array and compute for the sum of all the odd numbers by looping through each element in the array. |
|
give me a program in c that would count the number of similar inputs and display it in descending order. |
| give me a program in c for matrix multiplication |
|
hi buddy i tried the above programe bt got sme problem there should nt be printf(“%dt”,&a[i][j]); but it is printf(“%dt”,c[i][j]); thanx |
| can u please give a program that would give the time of the program when it was lastly accessed. also please tell how to compare the time of two programs |
|
write a programe to add two matrices |
| Hi can u plz write a program to define an array, ask the user to assign values and check if an element is present in the array and its position in it? |
| can u help write a program which finds whether a number is palindromic prime or not? |
| I was wondering how would you go about writing an Array program that allows the user to input a string of data for a 3 x 3 table. Calculates the sum or each row and column, outputs the array, sum of rows and columns and than calculated the diagonal sum of the table, corner to corner. |
| can u help me write a program which u wil enter name and the vowels wil become asterisk... please help me... |
| can u teach me how to write a program that will insert a value on an array between two indexes? for example..I will insert the value 23 between index 1 and index 2...and the value on those indexes will not be replaced...how? |
| how can i create an m,x,n array that will find the sum of diagonal elements.. thanks |
| can we use heterogeneous declarations in arrays? if they are not declared what is the reason |
| can u please give me some examples of multidimensional array with using functions? |
| Hi can u plz help me out to write a program for the mul;tipication of twp matrixes. |
| can u pls give me a prog with combination of function,looping,recursion,and array...i nid it asap...tnx u xo mch... |
| In the initialization arrays part example there is a open bracket missing in the first for statement please mention about that thank you. |
| write a program for checking a symmetric matrix |
|
pls help me application of one dimensional and two dimensional arreys. |
|
plz help me to solve the following problems as soon as possible: (1) Input n numbers and find the summation of the digits of the prime numbers among those n numbers.? (2) Input some students age & CGPA in two different arrays .Display the age of the highest CGPA students from those array? (3)Input n numbers and find how many numbers are greater than the 1st number,how many numbers are greater than the 2nd number and how many numbers are greater than the 3rd number.? now please please please give some hints or give a full solve those questions to help me.If u do so i will be ever greatful to u. |
|
given int x[3][3]={{5,2,4},{7,8,6},{10,12,11}}; assist me to write a program quickly that would display the smallest value in the array |
|
can you give an example on how to get the highest value in the array and the index where it is being pu.... |
|
what will be the code if I would create an array,the size is 25.Initialized the first ten elements and display it without the array list.Then delete an element and display again the list....thank you!! it's so easy dvahh?? so answer now!! if you can answer this quickly then you'll might be the most genius in the field of programming!! |
| Iam a professor in the cologe " William France Univesity" in France iam now in malasia i want some more information in ARRAYS IN C LANGUAGE as i want to know . Basically iam a science professor and also wants to become a good computer professor |
| can u write a program for checking whether a given number is prime number or not |
|
hi this is rahul i am very satisfify for this programe and i would like in fiuture everybody will help me thankyou thankyou very much |
| can we define an array with dynamic size? |
| Can u write a program to sort a list of numbers and to determine the median of the number....(the size of the array shud b input by the user i knw the whole prog but i actually dont knw hw the user can input the size of the array )hope u got my question! |
| hi, plz tell me, why we start array index from 0, not 1 |
|
can you help me solve this problem. Q. to find the sum of individuals rows & column 5/5 matrix |
|
Hi! How to disply this partern 12345 6789 123 45 6 |
| 2 d array sorting using c |
| Sam: Think it as an offset from the base of the array. You can address an item in array this way: array[n] or this way: *(array+n). It's equivalent, so if you used n=1, you'd be working with the fist item after the base of array - the array's second item. |
| its nice |
| its so good & nice |
| please help me to create a program that calculates and stores 10 random numbers between 1 and 100.output the random numbers to the screen using a loop and then output them again in reverse using a second loop.thnx |
|
using array concepts,How to display the month when we enter the number of month? I need it soon. please.... |
|
program of ohm * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #include<stdio.h> #include<conio.h> void main() { int i,j,k,n,s=21; clrscr(); textcolor(500); printf("enter the value of n:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(k=1;k<s;k++) printf(" "); for(j=1;j<=n;j++) { if(i==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { printf(" "); } for(j=1;j<=n;j++) { if(i==1 || i==n ||j==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { if(i==n) cprintf("*"); else printf(" "); } printf(" "); for(j=1;j<=n;j++) { if(j==1 || i==n) cprintf("*"); else if(i==(n+1)/2 && j==(n+1)/2) printf("*"); else printf(" "); } s=s-0; printf("n"); } for(i=1;i<=n;i++) { for(k=1;k<s;k++) printf(" "); for(j=1;j<=n;j++) { if(j==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { if(i==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { if(i==n ||j==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { printf(" "); } for(j=1;j<=n;j++) { if(j==1 || i==n) cprintf("*"); else printf(" "); } s=s-0; printf("n"); } getch(); } |
|
program of ohm #include<stdio.h> #include<conio.h> void main() { int i,j,k,n,s=21; clrscr(); textcolor(500); printf("enter the value of n:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(k=1;k<s;k++) printf(" "); for(j=1;j<=n;j++) { if(i==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { printf(" "); } for(j=1;j<=n;j++) { if(i==1 || i==n ||j==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { if(i==n) cprintf("*"); else printf(" "); } printf(" "); for(j=1;j<=n;j++) { if(j==1 || i==n) cprintf("*"); else if(i==(n+1)/2 && j==(n+1)/2) printf("*"); else printf(" "); } s=s-0; printf("n"); } for(i=1;i<=n;i++) { for(k=1;k<s;k++) printf(" "); for(j=1;j<=n;j++) { if(j==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { if(i==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { if(i==n ||j==n) cprintf("*"); else printf(" "); } for(j=1;j<=n;j++) { printf(" "); } for(j=1;j<=n;j++) { if(j==1 || i==n) cprintf("*"); else printf(" "); } s=s-0; printf("n"); } getch(); } |
|
how can i print series 0,1,1,2,3,5,8,........ please give me solution on below Email I'd. umangp81@gmail.com |
|
i want program for interchange biggest & second biggest position in c using arrays..... urgent plz |
| i want a C program to find the sum of the diagonal elements of a matrix using arrays. |
| It's very nice! |
| i want a program that uses arrays in its most simplest form but does a lot of tasks. |
|
hi this is viswanadh.it's very nice |
|
Hi, This is Habeeb, Hyderabad INDIA. I am basically Graphics and Web Designer. Can you give me more information arrays Habeeb |
|
tell me how can i create a c program to print armstrong number.please inform me on c_jk@rediffmail.com |
| how to initialize a three dimensional array and an example to pointer to a 2 dimensional array |
| I wants the program for addition of two matrix in c-language |
|
please give me c program for finding largest and smallest value in a matrix |
| write a programme to check whether the matrix is diagnal or not |
|
please give me c program for finding biggest and smallest elements in a array urgent pls. |
| hello....i am rocky. can you help me.? I have a problem about program.... program to sort the element of the matrix. |
| hallo plz help me in data stracture and How can creait an logic in mind for c programs in C laguage... |
| can u give me how to write a to copy one string to another string |
| please give me a program to find the greatest value among three given values with nested_if without using operators... |
|
i want to generate bar code using c language please give example program with explanation |
|
it is very nice |
| write a c program to find the sum of all prime numbers in an array? if any one can help me to do this question |
|
i need RECURSIVE c program to reverse input number. also i need algorithm for selection sort and implementing the same. also i need algorithm and program for finding ITERATIVE factorial. |
| Can u please tell me the advantages and disadvantages of using arrays |
|
hi I want programme to accept n numbers in range 1to 25 and count the frequency of occurence of each number. |
| can u plz explain how to do a programme for multiplication of sparse matrices? |
|
#include<stdio.h> void main() { int m,i,j; printf("enter d no of rows"); scanf("%d",&m); int a[m][m]; printf("fill d first matrixn"); for(i=0;i<m;i++) { for(j=0;j<m;j++) { scanf("%d",&a[i][j]); } printf("n"); } for(i=0;i<m;i++) { for(j=0;j<m;j++) { printf("%d",a[i][j]); printf(" "); } printf("n"); } } |
| Please show me how to write a program involving elements of main diagonal of matrix and find their sum and matrix involving elements of upper diagonal and their sum. |
| In what way array is different from an ordinary variable |
|
this is program to find the largest and smallest number in arrays #include<stdio.h> void main() { int a[20]; int i,n,max,min; print f("enter no of elements:"); scan f("%d",&n); print f("reading array elements:"); for(i=0;i<n;i++) { print f("enter a[%d]elements:"); scan f("%d",&a[i]); } print f("array elements:"); for(i=0;i<n;i++) { print f("%dt",a[i]); } max=a[0]; min=a[0]; for(i=0;i<n;i++) { if(max<a[i]) { max=a[i]; } if(min>a[i]) { min=a[i]; } } print f("n max=%dt min=%d",max,min); } |
|
Hey, Lets say I have 2 tables of data: table1: 5,2,5,7 table2: 7,1.5,6,4 How can I write a C prog. that outputs the lowest number and from which table? Thanks. |
|
How to write a 'C' program to read an array of 10 numbers and print the prime nums? this is what i wrote could someone tell me where is my mistake? can i use here flag at all? #include <stdio.h> void main () { int arr[10], i, j, flag; for (i=0;i<10;i++) scanf ("%d", &arr[i]); for (i=0;i<10;i++) { flag=1; for (j=2;j<=(arr[i]/2); j++) { if (arr[i]%j==0) { flag=0; break; } } } for (i=0;i<10;i++) { if (flag=1) printf ("%d", arr[i]); } } |
| Write a function int isit ( int mat[][], int n) to return TRUE if the n x n array mat is left upper triangular matrix or a right lower triangular matrix, and FALSE otherwise. The array should be read in the main function. An array is left upper triangular if all the elements below the right-to-left diagonal elements are zero. Similarly, in a right lower triangular array, all the elements above the right-to-left diagonal are zero. |
|
please help us in doing the following lab assessment Write a program that reads in an integer and prints out the given integer in binary, octal and hexadecimal formats. your program should do the following tasks: * it should accept an integer number from the keyboard * change this number to binary, octal and hexadecimal formats * it should finally print these three formats to the screen |
|
I'm new to C programming and i'm a bit confused about how to scan numbers into an array from a format such as this: 1 2 3 4 5 6 7 8 9 i understand how to write a program to print that array however it's when i have to ask the user to input their own numbers in that form that i don't know how to scan it and then print it. Any help would be great |

| Need help with writing a program that: Lets Arr be an array of 20 integers. The program should first fill the array with up to 20 input values and then find and display both the subscript of the largest item in arr and the value of the largest item. |
|
void main() { in a[1],i; for(i=0;i<=9;i++) scanf("%d",&a[i]); for(i=0;i<=9;i++) printf("%d",a[i]); getch(); } how can we store 10 elements in one element size array then what is use of array.without giving 10 size we are able to store 10 elemetns. |

| can u plz help me out to write a program in which user inputs a name and a password match it with some stored names and passwords in a variable and if it matches print "welcome to my program" using arrays |
| How can I print the length of the string, the number of spaces and special character? Please help me. |
| can array be defined as float a[2.5][2.5],so that at various positions such as a[1][1.5] a[1.5][1.5] a[2][1.5] a[2.5][2.5] values can be stored? |

|
can u help me to write a complete C program to perform Matrix operation such as Addition,Subtraction,Multiplication and Transpose according to the user's choice.Define functions to perform these various operations and call them from the main function whenever needed.The program should display the following menu to the user for them to select one of the five options listed. MAIN MENU 1. MATRIX ADD. 2. MATRIX SUB. 3. MATRIX MULTI. 4. MATRIX TRANSPOSE 5. EXIT this program is an infinte loop and it will return if enter wrong number. thak you. |
|
//progam factorial of givan pogistion.... #include<conio.h> #include<stdio.h> void main() { int n=0,f=1: printf("n enter the any no"); scanf("%d",&n); while(n>=1) { f=f*n; printf("*%d",n); n--; } printf("%d",f); getch(); } |