Arrays
Arrays
Why arrays?
The fundamental data types namely char, int, float are constrained by the fact
that a variable of these types can store only one value at any given time. In order
to handle a large volume of data in terms of reading, processing and printing we
need a derived data type known as array.
Def:
For instance, we can use an array name salary to represent a set of salaries of a
group of employees in an organization. We can refer to the individual salaries by
writing a number called index or subscript in brackets after the array name.
e.g. salary[10][2]
Advantages
• It is used to represent multiple data items of same type by using only single
name.
• It can be used to implement other data structures like linked list, stacks,
queues, trees, graphs, etc.
Disadvantages
• We must know in advance that how many elements are to be stored in array.
• Array is static structure. It means that array is of fixed size. The memory which
is allocated to array can not be increased or decreased.
• Since array is of fixed size, if we allocate more memory than requirement then
the memory space will be wasted. And if we allocate less memory than
requirement, then it can create problem.
The ability to use a single name to represent a collection of items and to refer to
an item by specifying the item number enables us to develop concise and
efficient programs.
We can use arrays to represent not only simple lists of values but also tables of
data in two, three or more dimensions. We have the following types of arrays:
• One-dimensional array
#include <stdio.h>
int main() {
int values[5];
Answer: B.
Two - Dimensional Array
int x[3][3];
2D Array
Two-Dimensional Array
#include<stdio.h>
void main(){
int disp[2][3];
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
}
MCQ - 2
Answer: D.
Character Arrays / Strings
Characters Arrays/Strings
General form:
char string_name[size];
The ‘size’ determines the number of characters in the string_name.
e.g. char city[10];
char name[30];
Characters Arrays/Strings…..
• We can also declare the size much larger than the string size in the initialize.
e.g. char str[10] = “GOOD”;
G O O D \0 \0 \0 \0 \0 \0
Characters Arrays/Strings…..
NOTE: that unlike previous scanf calls, in the case of character arrays, the
ampersand (&) is not required before the variable name.
• The problem with the scanf( ) function is that it terminates its input on the first
white space it finds.
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Characters Arrays/Strings…..
The above program segment reads a line of input from the keyboard and display
the same on the screen.
Characters Arrays/Strings…..
a. getchar()
Code Segment:
char line[80],ch;
int c = 0;
printf(“Enter text:”);
do
{
ch=getchar();
line[c] = ch;
c++;
} while(ch !=’\n’);
Characters Arrays/Strings…..
a. gets()
This function is available in the <stdio.h> header file.
General format:
gets(str);
where str is a string name.
This function reads characters into the character array from the
keyboard until a new-line character is encountered and then adds a null
character to the string.
Code Segment:
char line[80];
gets(line);
Characters Arrays/Strings…..
Code Segment:
char name[6] = “ARNAV’;
for(i=0; i<6; i++)
putchar(name[i]);
b. puts()
The function puts() is defined in the header file <stdio.h>.
General format:
puts(str);
where str is a string name.
Characters Arrays/Strings…..
Code Segment:
char line[80];
gets(line);
puts(line);
• Strcpy
This library function is used to copy a string and can be used like this:
strcpy(destination, source)
(It is not possible in C to do this: string1 = string2).
Note: strcpy() will not perform any boundary checking, and thus there is a risk of
overrunning the strings.
Characters Arrays/Strings…..
Strcmp
This library function is used to compare two strings and can be used like
this:
strcmp(str1, str2)
• If the first string is greater than the second string a number greater than null
is returned.
• If the first string is less than the second string a number less than null is
returned.
• If the first and the second string are equal a null is returned.
Characters Arrays/Strings…..
Note: strcmp() will not perform any boundary checking, and thus there is a
risk of overrunning the strings.
Strcat
This library function concatenates a string onto the end of the other string.
The result is returned.
Characters Arrays/Strings…..
Note: strcat() will not perform any boundary checking, and thus there is a risk of
overrunning the strings.
strlen
This library function returns the length of a string. (All characters before the null
termination.)
Take a look at the example:
name = "jane";
result = strlen(name); //Will return size of four.