SlideShare a Scribd company logo
C-ARRAYS
By: Prashant Dwivedy
ARRAYS
ď‚§ An array is a collection of elements of the same type that are
referenced by a common name.
ď‚§ Compared to the basic data type (int, float) it is an
aggregate or derived data type.
ď‚§ All the elements of an array occupy a set of contiguous
memory locations.
ď‚§ Why need to use array type?
ď‚§ Consider the following issue:
 "We have a list of 1000 students' marks of an
integer type. If using the basic data type
(int), we will declare something like the
following…"
 int studMark0, studMark1, ...studMark999
ď‚§ Can you imagine how long we have to
write the declaration part by using
normal variable declaration?
int main(void)
{
int studMark1, studMark2, studMark3,
studMark4, …, …, studMark998, stuMark999,
studMark1000;
…
…
return 0;
}
ď‚§ By using an array, we just declare like this,
 int studMark[1000];
ď‚§ This will reserve 1000 contiguous memory locations for
storing the students’ marks.
ď‚§ Graphically, this can be depicted as in the following
figure.
ď‚§ This absolutely has simplified our declaration of the
variables.
ď‚§ We can use index or subscript to identify each
element or location in the memory.
ď‚§ Hence, if we have an index of j, studMark[j]
would refer to the jth element in the array of
studMark.
ď‚§ For example, studMark[0] will refer to the first
element of the array.
ď‚§ Thus by changing the value of j, we could refer to
any element in the array.
ď‚§ So, array has simplified our declaration and of
course, manipulation of the data.
ď‚§ A single or one dimensional array declaration
has the following form,
âš« array_element_data_type array_name[array_
size];
ď‚§ Here, array_element_data_type defines the base
type of the array, which is the type of each
element in the array.
ď‚§ array_name is any valid C identifier name that
obeys the same rule for the identifier naming.
ď‚§ array_size defines how many elements the array
will hold.
ď‚§ For example, to declare an array of 30
characters, that construct a people
name, we could declare,
âš« char cName[30];
ď‚§ Which can be depicted as follows,
ď‚§ In this statement, the array character
can store up to 30 characters with the
first character occupying location
cName[0] and the last character
occupying cName[29].
ď‚§ Note that the index runs from 0 to
29. In C, an index always starts
from 0
and ends with array's (size-1).
ď‚§ So, take note the difference between the
array size and subscript/index terms.
ď‚§ Examples of the one-dimensional array declarations,
 int
 float
 char
xNum[20], yNum[50];
fPrice[10], fYield;
chLetter[70];
ď‚§ The first example declares two arrays named xNum and yNum
of type int. Array xNum can store up to 20 integer
numbers while yNum can store up to 50 numbers.
ď‚§ The second line declares the array fPrice of type float.
It
can store up to 10 floating-point values.
ď‚§ fYield is basic variable which shows array type can be
declared together with basic type provided the type is
similar.
ď‚§ The third line declares the array chLetter of type char.
It can store a string up to 69 characters.
ď‚§ Why 69 instead of 70? Remember, a string has a null
terminating character (0) at the end, so we must reserve for it.
ARRAY
INITIALIZATION
ď‚§ An array may be initialized at the time of declaration.
ď‚§ Initialization of an array may take the following form,
 type array_name[size] =
{a_list_of_value};
ď‚§ For example:


idNum[7] = {1, 2, 3, 4, 5, 6, 7};
fFloatNum[5] = {5.6, 5.7, 5.8, 5.9, 6.1};
chVowel[6] = {'a', 'e', 'i', 'o', 'u',
 int
float
char
'0'};
ď‚§ The first line declares an integer array idNum and it immediately
assigns the values 1, 2, 3, ..., 7 to idNum[0], idNum[1],
idNum[2],..., idNum[6] respectively.
ď‚§ The second line assigns the values 5.6 to
fFloatNum[0], 5.7 to fFloatNum[1], and so on.
ď‚§ Similarly the third line assigns the characters 'a' to chVowel[0],
'e' to chVowel[1], and so on. Note again, for characters we must
use the single apostrophe/quote (') to enclose them.
ď‚§ Also, the last character in chVowel is NULL character ('0').
ď‚§ Initialization of an array of type char for holding strings
may take the following form,
âš« char array_name[size] =
"string_lateral_constant";
ď‚§ For example, the array chVowel
in the previous example
could have been written more
compactly as follows,
âš« char chVowel[6] =
"aeiou";
ď‚§ When the value assigned to a character array is a string
(which must be enclosed in double quotes), the compiler
automatically supplies the NULL character but we still
have to reserve one extra place for the NULL.
ď‚§ For unsized array (variable sized), we can declare as
follow,
âš« char chName[ ] = "Mr. Dracula";
ď‚§ C compiler automatically creates an array which is big
enough to hold all the initializer.
RETRIEVING ARRAY ELEMENTS
 If you want to retrieve specific element then then
you have to specify not only the array or variable
name but also the index number of interest.
 For example:
 int Arr[]={1,3,5,6,8};
 printf(“%dt%dn”,Arr[1],Arr[2]);
 Output: 3 5
ARRAY EXAMPLE
 Take 10 integer input from user and store then in an
array and find the sum of all numbers stored in array.
#include<stdio.h>
int main(){
int i,sum=0,arr[10];
for(i=0;i<10;i++)
scanf(“%d”,&arr[i]);
for(i=0;i<10;i++)
sum+=arr[i];
printf(“Sum of
input integers is
%dn”,sum);
return 0;
Summarize the response of a survey.
Input: Response of the survey, can be in the range
between 0 and 10. Assume the population size to be
40.
Output: Frequency of each response.
 #include<stdio.h>
 #define SIZE 40
 #define ANS 11
 int main(void) {
 int response[SIZE];
 int freq[ANS] = {0};
 int i;
 for(i=0; i< SIZE; i++){
 scanf(“%d”,&response[i]);
 ++freq[response[i]];
 }


 }
for(i=0;i<ANS;i++)
printf("Frequency of %d is %dn",i,freq[i]);
ASSIGNMENT
 Read from user ages of all students in class and
save them in an array which can store floating
point and find average, minimum and maximum
age.
 A six faced die is rolled 600 times. Find the
frequency of the occurrence of each face?
 int rand(void): returns a pseudo-random
number in the range of 0 to RAND_MAX.
 RAND_MAX: is a constant whose default value
may vary between implementations but it is
granted to be at least 32767.
 Issue: If we generate a sequence of random
number with rand() function, it will create the
same sequence again and again every time
program runs.
 The srand() function sets the starting point for
producing a series of pseudo-random integers. If
srand() is not called, the rand() seed is set as if
srand(1) were called at program start.
 The pseudo-random number generator should
only be seeded once, before any calls to rand(),
and the start of the program.
 Standard practice is to use the result of a call
to srand(time(0)) as the seed. However, time()
returns a time_t value which vary everytime and
hence the pseudo-random number vary for every
program call.
#include<stdio.h>
int main(){
int i,j,arr[7]={0};
srand(time(0));
for (i=0;i<600;i++)
{ j=rand()%6;
j=j+1;
arr[j]++;
}
for(i=1;i<=6;i++)
printf("%d came
out for %d
times
n",i,arr[i]);
 [sourav@gaya]$ ./a.out
1 came out for 113 times
2 came out for 114 times
3 came out for 102 times
4 came out for 86 times
5 came out for 99 times
6 came out for 86 times
ASSIGNMENT
 Store marks obtained by students in an array. Find
if there is more than one student who scored same
marks. Assume minimum marks obtained is 30
and maximum marks obtained is 85.
#include<stdio.h>
int main(){
int i,j,x,arr[10];
printf("Enter 10 integer numbern");
for(i=0;i<10;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<9;i++){
x=arr[i];
for(j=i+1;j<10;j++){
if(x==arr[j])
printf("%
d
number
appeared
more
than
once
[sourav@gaya]$ ./a.out
Enter 10 integer number 1
2
3
4
5
6
2
4
8
9
2 number appeared more
than once
4 number appeared more
than once
TWO DIMENSIONAL/2D
ARRAYS
ď‚§ A two dimensional array has two subscripts/indexes.
ď‚§ The first subscript refers to the row, and the second, to the
column.
ď‚§ Its declaration has the following form,
âš« data_type array_name[1st dimension size][2nd
dimension size];
ď‚§ For examples,
âš« int
âš« float
xInteger[3][4];
matrixNum[20][25];
ď‚§ The first line declares xInteger as an integer array with 3
rows and 4 columns.
ď‚§ Second line declares a matrixNum as a floating-point array
with 20 rows and 25 columns.
DOUBLE SCRIPTED ARRAY WITH 3 ROWS
AND 4
COLUMNS
Array ppt you can learn in very few slides.
Array ppt you can learn in very few slides.
 #include <stdio.h>
 int main() {
âš« int abc[5][4] ={ {0,1,2,3}, {4,5,6,7}, {8,9,10,11},
{12,13,14,15}, {16,17,18,19} };
âš« for (int i=0; i<=4; i++) {
 printf("%d ",abc[i]);
âš« }
âš« return 0;
 }
 Output: 1600101376 1600101392 1600101408
1600101424 1600101440
LIST OF STUDENTS AND THEIR
SUBJECT
MARKS
10 23 31 11
20 43 21 21
12 22 30 13
30 31 26 41
13 03 41 15
Student
s
Mark
s
Find the average mark scored by
each student?
#include<stdio.h>
#define ROW 5
#define COL 4
int main(void)
{
int i, j;
double total;
int
marks[ROW]
[COL]= { 10,
23, 31, 11, 20,
43, 21, 21,12,
22, 30, 13, 30, 31,
26, 41,13, 03, 41,
15 };
for(i = 0; i <
ROW ; i++)
{
total =
0.0;
INITIALIZATION OF 2D ARRAY
 int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };
 OR
 int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
 1st one is recommended.
THINGS THAT YOU MUST CONSIDER WHILE
INITIALIZING A 2D ARRAY
 We already know, when we initialize a normal array (or you
can say one dimensional array) during declaration, we need
not to specify the size of it. However that’s not the case with
2D array, you must always specify the second dimension even
if you are specifying elements during the declaration.
 /* Valid declaration*/
 int abc[2][2] = {1, 2, 3 ,4 }
 /* Valid declaration*/
 int abc[][2] = {1, 2, 3 ,4 }
 /* Invalid declaration – you must specify second
dimension*/
 int abc[][] = {1, 2, 3 ,4 }
 /* Invalid because of the same reason
mentioned above*/
 int abc[2][] = {1, 2, 3 ,4 }
ď‚§ For array storing string
âš« char Name[6][10] = {"Mr. Bean", "Mr. Bush",
"Nicole", "Kidman", "Arnold", "Jodie"};
ď‚§ Here, we can initialize the array with 6 strings, each with
maximum 9 characters long.
ď‚§ If depicted in rows and columns it will look something
like the following and can be considered as contiguous
arrangement in the memory.
ASSIGNMENTS
 Print Transpose of a Matrix
 Add Two Matrix Using Multi-dimensional Arrays
 Multiply to Matrix Using Multi-dimensional
Arrays
#include <stdio.h>
void main()
{
static int array[10][10];
int i, j, m, n;
printf("Enter the order
of the matrix n");
scanf("%d %d", &m,
&n);
printf("Enter the coefiicients of the matrixn");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j)
{ scanf("%d", &array[i][j]);
}
}
printf("The given matrix is n");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j)
{ printf(" %d", array[i]
[j]);
}
printf("n");
}
printf("Transpose of matrix
is n");
for (j = 0; j < n; ++j){
for (i = 0; i < m; ++i)
{ printf(" %d", array[i][j]);
}
printf("n");
}
#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("nEnter elements of 1st matrix:n");
for(i=0; i<r; ++i) {
for(j=0; j<c; ++j) {
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of 2nd matrix:n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j) {
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}
/
/ Adding Two matrices
for(i=0;i<r;++i)
for(j=0;j<c;++j) { sum[i]
[j]=a[i][j]+b[i][j];
}
/
/ Displaying the result
printf("nSum of two matrix is: nn");
for(i=0;i<r;++i)
for(j=0;j<c;++j) {
printf("%d ",sum[i][j]);
if(j==c-1) { printf("nn"); }
}
return 0;
}
 #include <stdio.h>
 int main() {
âš« int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
âš« printf("Enter rows and column for first matrix: ");
âš« scanf("%d %d", &r1, &c1);
âš« printf("Enter rows and column for second matrix: ");
âš« scanf("%d %d",&r2, &c2);
âš« while (c1 != r2) {
 printf("Error! Not compatible for multiplicationn");
âš« }
âš« printf("nEnter elements of matrix 1:n");
âš« for(i=0; i<r1; ++i)
 for(j=0; j<c1; ++j) {
 printf("Enter elements a%d%d: ",i+1, j+1);
 scanf("%d", &a[i][j]);
 }
âš« printf("nEnter elements of matrix 2:n");
âš« for(i=0; i<r2; ++i)
 for(j=0; j<c2; ++j) {
 printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
 }
âš« /
/ Initializing all elements of result matrix to 0
âš« for(i=0; i<r1; ++i)
 for(j=0; j<c2; ++j) {
 result[i][j] = 0;
 }
âš« /
/ Multiplying matrices a and b and /
/ storing result in result matrix
for(i=0; i<r1; ++i)
 for(j=0; j<c2; ++j)
 for(k=0; k<c1; ++k) {
âš« result[i][j]+=a[i][k]*b[k][j];
 }
âš« /
/ Displaying the result
âš« printf("nOutput Matrix:n");
âš« for(i=0; i<r1; ++i)
 for(j=0; j<c2; ++j) {
 printf("%d ", result[i][j]);
 if(j == c2-1) printf("nn");
 }
âš« return 0;
 }
3D: A 3D ARRAY IS AN ARRAY OF
2D
ARRAYS.
 #include<stdio.h>
 int main(){
 int
i,j,k,x[][2][3]={{{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}};
 for(i=0;i<2;i++)
 for(j=0;j<2;j++)
 for(k=0;k<3;k++){

printf("x[%d,%d,%d]=%dn",i,j,k,x[i][j][k]);
 }
 return 0;
 }

More Related Content

PPTX
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
yatakumar84
 
PPTX
Array.pptx
Govindraj Chittapur
 
PDF
Array in C.pdf
georgejustymirobi1
 
PDF
Array.pdf
mounikanarra3
 
PPTX
Array in C
adityas29
 
PPT
Array THE DATA STRUCTURE. ITS THE STRUCT
duttasoumyajit5
 
PPTX
3.ArraysandPointers.pptx
FolkAdonis
 
PDF
Arrays In C
yndaravind
 
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
yatakumar84
 
Array.pptx
Govindraj Chittapur
 
Array in C.pdf
georgejustymirobi1
 
Array.pdf
mounikanarra3
 
Array in C
adityas29
 
Array THE DATA STRUCTURE. ITS THE STRUCT
duttasoumyajit5
 
3.ArraysandPointers.pptx
FolkAdonis
 
Arrays In C
yndaravind
 

Similar to Array ppt you can learn in very few slides. (20)

PDF
Array&amp;string
chanchal ghosh
 
PPTX
Arrays in C language
Shubham Sharma
 
PDF
Arrays-Computer programming
nmahi96
 
PPTX
Arrays in c
CHANDAN KUMAR
 
PPTX
C-Arrays & Strings (computer programming).pptx
yusuph2410
 
PPTX
Programming in c Arrays
janani thirupathi
 
PDF
02 arrays
Rajan Gautam
 
PPTX
Arrays
AnaraAlam
 
PPTX
Array and its operation in C programming
Rhishav Poudyal
 
PPTX
Array,MULTI ARRAY, IN C
naveed jamali
 
PDF
Unit 2
TPLatchoumi
 
PDF
Introduction to Arrays in C
Thesis Scientist Private Limited
 
PDF
VIT351 Software Development VI Unit2
YOGESH SINGH
 
PDF
Arrays
Steven Wallach
 
PPTX
Chapter 13.pptx
AnisZahirahAzman
 
PPTX
CHAPTER 5
mohd_mizan
 
PPTX
COM1407: Arrays
Hemantha Kulathilake
 
PDF
ARRAYS
muniryaseen
 
PDF
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
Array&amp;string
chanchal ghosh
 
Arrays in C language
Shubham Sharma
 
Arrays-Computer programming
nmahi96
 
Arrays in c
CHANDAN KUMAR
 
C-Arrays & Strings (computer programming).pptx
yusuph2410
 
Programming in c Arrays
janani thirupathi
 
02 arrays
Rajan Gautam
 
Arrays
AnaraAlam
 
Array and its operation in C programming
Rhishav Poudyal
 
Array,MULTI ARRAY, IN C
naveed jamali
 
Unit 2
TPLatchoumi
 
Introduction to Arrays in C
Thesis Scientist Private Limited
 
VIT351 Software Development VI Unit2
YOGESH SINGH
 
Arrays
Steven Wallach
 
Chapter 13.pptx
AnisZahirahAzman
 
CHAPTER 5
mohd_mizan
 
COM1407: Arrays
Hemantha Kulathilake
 
ARRAYS
muniryaseen
 
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
Ad

Recently uploaded (20)

PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPTX
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
Inventory management chapter in automation and robotics.
atisht0104
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
Software Testing Tools - names and explanation
shruti533256
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
Information Retrieval and Extraction - Module 7
premSankar19
 
Ad

Array ppt you can learn in very few slides.

  • 2. ARRAYS ď‚§ An array is a collection of elements of the same type that are referenced by a common name. ď‚§ Compared to the basic data type (int, float) it is an aggregate or derived data type. ď‚§ All the elements of an array occupy a set of contiguous memory locations. ď‚§ Why need to use array type? ď‚§ Consider the following issue:  "We have a list of 1000 students' marks of an integer type. If using the basic data type (int), we will declare something like the following…"  int studMark0, studMark1, ...studMark999
  • 3. ď‚§ Can you imagine how long we have to write the declaration part by using normal variable declaration? int main(void) { int studMark1, studMark2, studMark3, studMark4, …, …, studMark998, stuMark999, studMark1000; … … return 0; }
  • 4. ď‚§ By using an array, we just declare like this,  int studMark[1000]; ď‚§ This will reserve 1000 contiguous memory locations for storing the students’ marks. ď‚§ Graphically, this can be depicted as in the following figure.
  • 5. ď‚§ This absolutely has simplified our declaration of the variables. ď‚§ We can use index or subscript to identify each element or location in the memory. ď‚§ Hence, if we have an index of j, studMark[j] would refer to the jth element in the array of studMark. ď‚§ For example, studMark[0] will refer to the first element of the array. ď‚§ Thus by changing the value of j, we could refer to any element in the array. ď‚§ So, array has simplified our declaration and of course, manipulation of the data.
  • 6. ď‚§ A single or one dimensional array declaration has the following form, âš« array_element_data_type array_name[array_ size]; ď‚§ Here, array_element_data_type defines the base type of the array, which is the type of each element in the array. ď‚§ array_name is any valid C identifier name that obeys the same rule for the identifier naming. ď‚§ array_size defines how many elements the array will hold.
  • 7. ď‚§ For example, to declare an array of 30 characters, that construct a people name, we could declare, âš« char cName[30]; ď‚§ Which can be depicted as follows, ď‚§ In this statement, the array character can store up to 30 characters with the first character occupying location cName[0] and the last character occupying cName[29]. ď‚§ Note that the index runs from 0 to 29. In C, an index always starts from 0 and ends with array's (size-1). ď‚§ So, take note the difference between the array size and subscript/index terms.
  • 8. ď‚§ Examples of the one-dimensional array declarations,  int  float  char xNum[20], yNum[50]; fPrice[10], fYield; chLetter[70]; ď‚§ The first example declares two arrays named xNum and yNum of type int. Array xNum can store up to 20 integer numbers while yNum can store up to 50 numbers. ď‚§ The second line declares the array fPrice of type float. It can store up to 10 floating-point values. ď‚§ fYield is basic variable which shows array type can be declared together with basic type provided the type is similar. ď‚§ The third line declares the array chLetter of type char. It can store a string up to 69 characters. ď‚§ Why 69 instead of 70? Remember, a string has a null terminating character (0) at the end, so we must reserve for it.
  • 9. ARRAY INITIALIZATION ď‚§ An array may be initialized at the time of declaration. ď‚§ Initialization of an array may take the following form,  type array_name[size] = {a_list_of_value}; ď‚§ For example:   idNum[7] = {1, 2, 3, 4, 5, 6, 7}; fFloatNum[5] = {5.6, 5.7, 5.8, 5.9, 6.1}; chVowel[6] = {'a', 'e', 'i', 'o', 'u',  int float char '0'}; ď‚§ The first line declares an integer array idNum and it immediately assigns the values 1, 2, 3, ..., 7 to idNum[0], idNum[1], idNum[2],..., idNum[6] respectively. ď‚§ The second line assigns the values 5.6 to fFloatNum[0], 5.7 to fFloatNum[1], and so on. ď‚§ Similarly the third line assigns the characters 'a' to chVowel[0], 'e' to chVowel[1], and so on. Note again, for characters we must use the single apostrophe/quote (') to enclose them. ď‚§ Also, the last character in chVowel is NULL character ('0').
  • 10. ď‚§ Initialization of an array of type char for holding strings may take the following form, âš« char array_name[size] = "string_lateral_constant"; ď‚§ For example, the array chVowel in the previous example could have been written more compactly as follows, âš« char chVowel[6] = "aeiou"; ď‚§ When the value assigned to a character array is a string (which must be enclosed in double quotes), the compiler automatically supplies the NULL character but we still have to reserve one extra place for the NULL. ď‚§ For unsized array (variable sized), we can declare as follow, âš« char chName[ ] = "Mr. Dracula"; ď‚§ C compiler automatically creates an array which is big enough to hold all the initializer.
  • 11. RETRIEVING ARRAY ELEMENTS  If you want to retrieve specific element then then you have to specify not only the array or variable name but also the index number of interest.  For example:  int Arr[]={1,3,5,6,8};  printf(“%dt%dn”,Arr[1],Arr[2]);  Output: 3 5
  • 12. ARRAY EXAMPLE  Take 10 integer input from user and store then in an array and find the sum of all numbers stored in array. #include<stdio.h> int main(){ int i,sum=0,arr[10]; for(i=0;i<10;i++) scanf(“%d”,&arr[i]); for(i=0;i<10;i++) sum+=arr[i]; printf(“Sum of input integers is %dn”,sum); return 0;
  • 13. Summarize the response of a survey. Input: Response of the survey, can be in the range between 0 and 10. Assume the population size to be 40. Output: Frequency of each response.
  • 14.  #include<stdio.h>  #define SIZE 40  #define ANS 11  int main(void) {  int response[SIZE];  int freq[ANS] = {0};  int i;  for(i=0; i< SIZE; i++){  scanf(“%d”,&response[i]);  ++freq[response[i]];  }    } for(i=0;i<ANS;i++) printf("Frequency of %d is %dn",i,freq[i]);
  • 15. ASSIGNMENT  Read from user ages of all students in class and save them in an array which can store floating point and find average, minimum and maximum age.  A six faced die is rolled 600 times. Find the frequency of the occurrence of each face?
  • 16.  int rand(void): returns a pseudo-random number in the range of 0 to RAND_MAX.  RAND_MAX: is a constant whose default value may vary between implementations but it is granted to be at least 32767.  Issue: If we generate a sequence of random number with rand() function, it will create the same sequence again and again every time program runs.
  • 17.  The srand() function sets the starting point for producing a series of pseudo-random integers. If srand() is not called, the rand() seed is set as if srand(1) were called at program start.  The pseudo-random number generator should only be seeded once, before any calls to rand(), and the start of the program.  Standard practice is to use the result of a call to srand(time(0)) as the seed. However, time() returns a time_t value which vary everytime and hence the pseudo-random number vary for every program call.
  • 18. #include<stdio.h> int main(){ int i,j,arr[7]={0}; srand(time(0)); for (i=0;i<600;i++) { j=rand()%6; j=j+1; arr[j]++; } for(i=1;i<=6;i++) printf("%d came out for %d times n",i,arr[i]);
  • 19.  [sourav@gaya]$ ./a.out 1 came out for 113 times 2 came out for 114 times 3 came out for 102 times 4 came out for 86 times 5 came out for 99 times 6 came out for 86 times
  • 20. ASSIGNMENT  Store marks obtained by students in an array. Find if there is more than one student who scored same marks. Assume minimum marks obtained is 30 and maximum marks obtained is 85.
  • 21. #include<stdio.h> int main(){ int i,j,x,arr[10]; printf("Enter 10 integer numbern"); for(i=0;i<10;i++){ scanf("%d",&arr[i]); } for(i=0;i<9;i++){ x=arr[i]; for(j=i+1;j<10;j++){ if(x==arr[j]) printf("% d number appeared more than once
  • 22. [sourav@gaya]$ ./a.out Enter 10 integer number 1 2 3 4 5 6 2 4 8 9 2 number appeared more than once 4 number appeared more than once
  • 23. TWO DIMENSIONAL/2D ARRAYS ď‚§ A two dimensional array has two subscripts/indexes. ď‚§ The first subscript refers to the row, and the second, to the column. ď‚§ Its declaration has the following form, âš« data_type array_name[1st dimension size][2nd dimension size]; ď‚§ For examples, âš« int âš« float xInteger[3][4]; matrixNum[20][25]; ď‚§ The first line declares xInteger as an integer array with 3 rows and 4 columns. ď‚§ Second line declares a matrixNum as a floating-point array with 20 rows and 25 columns.
  • 24. DOUBLE SCRIPTED ARRAY WITH 3 ROWS AND 4 COLUMNS
  • 27.  #include <stdio.h>  int main() { âš« int abc[5][4] ={ {0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}, {16,17,18,19} }; âš« for (int i=0; i<=4; i++) {  printf("%d ",abc[i]); âš« } âš« return 0;  }  Output: 1600101376 1600101392 1600101408 1600101424 1600101440
  • 28. LIST OF STUDENTS AND THEIR SUBJECT MARKS 10 23 31 11 20 43 21 21 12 22 30 13 30 31 26 41 13 03 41 15 Student s Mark s Find the average mark scored by each student?
  • 29. #include<stdio.h> #define ROW 5 #define COL 4 int main(void) { int i, j; double total; int marks[ROW] [COL]= { 10, 23, 31, 11, 20, 43, 21, 21,12, 22, 30, 13, 30, 31, 26, 41,13, 03, 41, 15 }; for(i = 0; i < ROW ; i++) { total = 0.0;
  • 30. INITIALIZATION OF 2D ARRAY  int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };  OR  int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};  1st one is recommended.
  • 31. THINGS THAT YOU MUST CONSIDER WHILE INITIALIZING A 2D ARRAY  We already know, when we initialize a normal array (or you can say one dimensional array) during declaration, we need not to specify the size of it. However that’s not the case with 2D array, you must always specify the second dimension even if you are specifying elements during the declaration.  /* Valid declaration*/  int abc[2][2] = {1, 2, 3 ,4 }  /* Valid declaration*/  int abc[][2] = {1, 2, 3 ,4 }  /* Invalid declaration – you must specify second dimension*/  int abc[][] = {1, 2, 3 ,4 }  /* Invalid because of the same reason mentioned above*/  int abc[2][] = {1, 2, 3 ,4 }
  • 32. ď‚§ For array storing string âš« char Name[6][10] = {"Mr. Bean", "Mr. Bush", "Nicole", "Kidman", "Arnold", "Jodie"}; ď‚§ Here, we can initialize the array with 6 strings, each with maximum 9 characters long. ď‚§ If depicted in rows and columns it will look something like the following and can be considered as contiguous arrangement in the memory.
  • 33. ASSIGNMENTS  Print Transpose of a Matrix  Add Two Matrix Using Multi-dimensional Arrays  Multiply to Matrix Using Multi-dimensional Arrays
  • 34. #include <stdio.h> void main() { static int array[10][10]; int i, j, m, n; printf("Enter the order of the matrix n"); scanf("%d %d", &m, &n); printf("Enter the coefiicients of the matrixn"); for (i = 0; i < m; ++i){ for (j = 0; j < n; ++j) { scanf("%d", &array[i][j]); } }
  • 35. printf("The given matrix is n"); for (i = 0; i < m; ++i){ for (j = 0; j < n; ++j) { printf(" %d", array[i] [j]); } printf("n"); } printf("Transpose of matrix is n"); for (j = 0; j < n; ++j){ for (i = 0; i < m; ++i) { printf(" %d", array[i][j]); } printf("n"); }
  • 36. #include <stdio.h> int main() { int r, c, a[100][100], b[100][100], sum[100][100], i, j; printf("Enter number of rows (between 1 and 100): "); scanf("%d", &r); printf("Enter number of columns (between 1 and 100): "); scanf("%d", &c); printf("nEnter elements of 1st matrix:n"); for(i=0; i<r; ++i) { for(j=0; j<c; ++j) { printf("Enter element a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); } }
  • 37. printf("Enter elements of 2nd matrix:n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { printf("Enter element a%d%d: ",i+1, j+1); scanf("%d", &b[i][j]); } / / Adding Two matrices for(i=0;i<r;++i) for(j=0;j<c;++j) { sum[i] [j]=a[i][j]+b[i][j]; } / / Displaying the result printf("nSum of two matrix is: nn"); for(i=0;i<r;++i) for(j=0;j<c;++j) { printf("%d ",sum[i][j]); if(j==c-1) { printf("nn"); } } return 0; }
  • 38.  #include <stdio.h>  int main() { âš« int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k; âš« printf("Enter rows and column for first matrix: "); âš« scanf("%d %d", &r1, &c1); âš« printf("Enter rows and column for second matrix: "); âš« scanf("%d %d",&r2, &c2); âš« while (c1 != r2) {  printf("Error! Not compatible for multiplicationn"); âš« }
  • 39. âš« printf("nEnter elements of matrix 1:n"); âš« for(i=0; i<r1; ++i)  for(j=0; j<c1; ++j) {  printf("Enter elements a%d%d: ",i+1, j+1);  scanf("%d", &a[i][j]);  } âš« printf("nEnter elements of matrix 2:n"); âš« for(i=0; i<r2; ++i)  for(j=0; j<c2; ++j) {  printf("Enter elements b%d%d: ",i+1, j+1); scanf("%d",&b[i][j]);  }
  • 40. âš« / / Initializing all elements of result matrix to 0 âš« for(i=0; i<r1; ++i)  for(j=0; j<c2; ++j) {  result[i][j] = 0;  } âš« / / Multiplying matrices a and b and / / storing result in result matrix for(i=0; i<r1; ++i)  for(j=0; j<c2; ++j)  for(k=0; k<c1; ++k) { âš« result[i][j]+=a[i][k]*b[k][j];  }
  • 41. âš« / / Displaying the result âš« printf("nOutput Matrix:n"); âš« for(i=0; i<r1; ++i)  for(j=0; j<c2; ++j) {  printf("%d ", result[i][j]);  if(j == c2-1) printf("nn");  } âš« return 0;  }
  • 42. 3D: A 3D ARRAY IS AN ARRAY OF 2D ARRAYS.  #include<stdio.h>  int main(){  int i,j,k,x[][2][3]={{{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}};  for(i=0;i<2;i++)  for(j=0;j<2;j++)  for(k=0;k<3;k++){  printf("x[%d,%d,%d]=%dn",i,j,k,x[i][j][k]);  }  return 0;  }