Professional Documents
Culture Documents
Arrays and Strings in C
Arrays and Strings in C
Debasis Samanta
Computer Science & Engineering
Indian Institute of Technology Kharagpur
Spring-2017
Lecture #4
Arrays and Strings
1-D array
3-D array
2-D array
x is a 10-element one
dimensional array
• General syntax:
type array-name [size];
• type specifies the type of element that will be contained in the array (int,
float, char, etc.)
• size is an integer constant which indicates the maximum number of
elements that can be stored inside the array.
Example
int marks[5];
Here, marks is an array containing a maximum of 5 integers.
• Be careful!
• If we are not sure of the exact size of the array, we can define an array of a large
size.
int marks[50];
there is no element like x[50], x[-1], x[100], etc.
x x+k x+2k
Here,
• x is the starting address of the array in memory
• The array index starts at zero. That is, the location of a[0] is at x
• Let k is the number of bytes allocated per array element
• Element a[i] is allocated at the memory location having address x + i*k
Example:
int marks[5];
:
:
marks[8] = 75;
• The above assignment would not report any error; however, execution will fail.
Examples
int marks[5] = {72, 83, 65, 80, 76};
char name[4] = {‘A’, ‘m’, ‘i’, ‘t’};
#include <stdio.h>
Array
main()
{
declaration
int a[10], i, min;
main()
{
int a[size], i, min;
min = 99999;
for (i=0; i<size; i++)
{
if (a[i] < min)
min = a[i];
}
printf (“\n Minimum is %d”, min);
}
• Define an array of large size and use only the required number of elements.
#include <stdio.h>
main()
{
int a[100], i, min, n;
min = 99999;
for (i=0; i<n; i++)
{
if (a[i] < min)
min = a[i];
}
printf (“\n Minimum is %d”, min);
}
main()
{
int grade_pt[nsub], cred[nsub], i,
gp_sum=0, cred_sum=0, gpa;
•The elements are printed all in one line (starting with a new line).
printf (“\n”);
for (j=0; j<25; j++)
printf (“%f”, a[j]);
• The table can be regarded as a matrix consisting of four rows and five columns.
• Examples
int marks[4][5];
float sales[12][25];
double matrix[100][100];
• a[i][j]
Row
:: is allocated memory location at 1
address x + (i * c + j) * k 2
a[0]0] a[0][1] a[0]2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
Row 0 Row 1 Row 2
Note:
• The elements can be entered all in one line or in different lines.
#include <stdio.h>
#define M 10;
#define N 15;
#include <stdio.h>
main ()
main
{ ()
{ int n;
int a[M][N];
scanf("%d",&n);
for (i=0; i<M; i++)
printf("%d\n",n);
} for (j=0; j<N; j++)
scanf("%d",&a[i][j]);
}
• Examples.
int rainFall [day][month][city];
will store the rainfall data for each day in a year and for a number of cities.
“Hello” H e l l o ‘\0’
• C[0] gets the value ‘d', C[1] the value ‘e', and so on. The last (7th) location
receives the null character ‘\0’.
• The trailing null character is missing here. C automatically puts it at the end.
• Note also that for individual characters, C uses single quotes, whereas for
strings, it uses double quotes.
CS 11001 : Programming and Data Structures Lecture #04: © DSamanta
Declaring String Variables
• A string is declared like any other array:
char string-name [size];
• Reading words
• The ampersand (&) is not required before the variable name with “%s”.
• The problem here is that the string is taken to be upto the first white space
(blank, tab, carriage return, etc.)
• If we type “Debasis Samanta”
• name will be assigned the string “Debasis”!
c = c – 1; Make it a valid
line[c] = ‘\0’; string
char line[81];
:
:
scanf (“%[ABCDEFGHIJKLMNOPQRSTUVWXYZ]”, line);
char line[81];
:
:
scanf (“%[^\n]”, line);
• Alternatively, we can use printf with the “%c” format and each
character one after another.
i = 0;
while (name[i++] !=‘\0’)
printf(“%c”, name[i]);
• Examples:
strcpy (city, “Calcutta”);
strcpy (city, mycity);
• Warning:
• Assignment operator do not work for strings.
city = “Calcutta”; -> INVALID
char city[15];
int n; n is assigned 8
:
:
strcpy (city, “Calcutta”);
n = strlen (city);
#include <stdio.h>
#include <string.h>
main()
{
char line[81];
int i, n, count=0;
scanf (“%[^\n]”, line);
n = strlen (line);
for (i=0; i<n; i++)
{
if (isupper (line[i])
count++;
}
printf (“\n The no. of uppercase ltr in the str %s is %d”,line, count);
}
#include <stdio.h>
#include <string.h>
#define MAX 1000;
main()
{
char myText[MAX];
int i, n, wCount=0; lCount = 0;
while ((c=getchar()) != 0) myText[i++] = c;
myText[i] = ‘\0’;
n = strlen (myText);
for (i=0; i<n; i++)
{
switch(myText[i]){
case ‘ ‘: wCount++; break;
case ‘\.’ :lcount++; break;
}
printf (“\n The number of words is %d and sentences is %d”,wCount, lCount);
}
CS 11001 : Programming and Data Structures Lecture #04: © DSamanta
Any question?
b) If it is stored in maim memory, then in which part of the memory (data area, heap area or instruction area) it is stored?
d) Are they stored in contagious locations or distributed at random thorough the entire memory available?
3. Suppose, you have declared an array long x[100]; Assume that the word size of the memory is 2
bytes. If the starting location of the array (i.e., the location of x[0]) in the memory is 1124, then what will be
the memory location of x[8]?
5. What will be the memory location of a[i][j] in memory, if the starting location of the array is x and the
2-D array is stored in column major order? Assume, each element of the array needs w words of the memory.
Column
0 1 2 3
0
Row
6. What should be the memory locations in (a) row-major and (b) column major order of any non-zero
elements of the following diagonal matrices.
What is the advantages and limitations in the above ways of initializing a string variable. Explain your
answers with appropriate C programs.
9. It is required to store the name of 20 cities. Assume that name of a city should not exceed 20 characters. Give
an idea how the data can be stored in a string matrix say myCities, tht is, myCity[i] will stored the
name of the i-th city.