Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CSE 1202: Computer Programming Sessional

EXPERIMENT NO: 05

EXPERIMENT NAME: Study of Arrays in C Programming.

OBJECTIVE(S):

• To understand programming using different dimensions of Array


• To be familiar with programming using string

THEORY:
Arrays:
An array is a collection of variables of the same type that are referenced by a common name. In C,
all arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element. Arrays may have from one to several
dimensions. A specific element in an array is accessed by an index.

One Dimensional Array:

The general form of single-dimension array declaration is:

type variable-name[size];

Here, type declares the base type of the array, size defines how many elements the array will
hold. For example, the following declares as integer array named sample that is ten elements
long:

int sample[10];

In C, all arrays have zero as the index of their first element. This declares an integer array that
has ten elements, sample[0] through sample[9].

Operations
1. Insertion
2. Deletion
3. Searching
4. Sorting
5. Merging
CSE 1202: Computer Programming Sessional

String Standard Functions


Character arrays are called strings. Group of characters, digits, symbols enclosed within quotation
marks are called as strings.
Functions Description
strlen() Determines the length of a string
strcpy() Copies astring from source to destination
strncpy() Copies charcters of a string to another string upto the specified length
stricmp() Compares characters of two strings
strcmp() Compares characters of two strings upto the specified length
strncmp() Compares characters of two strings upto the specified length
strnicmp() Compares characters of two strings upto the specified length
strlwr() Converts uppercase characters of a string to lower case
strupr() Converts lowercase characters of a string to upper case
strdup() Duplicates a string
strchr() Determines the first occurrence of a given character in a string
strrchr() Determines the last occurrence of a given character in a string
strstr() Determines the first occurrence of a given string in another string
strcat() Appends source string to destination string
strrev() Reverses all characters of a string
strset() Sets all characters of a string with a given argument or symbol
strspn() Finds up to what length two strings are identical
Searches the first occurrence of the character in a given string and then
strpbrk()
displays the string starting from that charcter

/* To find the average of 10 numbers */


# include <stdio.h>
main()
{
int i, sum, sample[10];
for (i=0; i<10; i++)
{
CSE 1202: Computer Programming Sessional

printf ("\nEnter number %d: ", i);


scanf ("%d", &sample[i]);

sum = 0;
for (i=0; i<10; i++)
sum = sum + sample[i];

printf ("\nThe average is: %d\n", sum/10);

}
Two-dimensional arrays:

To declare two-dimensional integer array num of size (3,4), we write: int num[3][4]; Left Index
determines row and right index determines column. Two dimensional arrays are stored in a row-
column matrix where the first index indicates the row and the second indicates the column. This
means that the right most index changes faster than the leftmost when accessing the elements in
the array in the order in which they are actually stored in memory.

For example: char Ch[4][3]

Ch [0][0] Ch [0][1] Ch [0][2]


Ch [1][0] Ch [1][1] Ch [1][2]
Ch [2][0] Ch [2][1] Ch [2][2]
Ch [3][0] Ch [3][1] Ch [3][2]

We notice that to move along any row, we have to change right index keeping the left index
unchanged and to move along any column, we have to change left index keeping the right index
unchanged.

Example:

main ()
{
int t, i, k=1, num [3][4];
for (t=0; t <3; t ++) // controlling left index
for (i=0; i<4; ++i) // controlling right index
CSE 1202: Computer Programming Sessional

num [t][i] = k++;


for (t=0; t <3; t ++)
{
for (i=0; i<4; ++i)
printf ("%3d", num[t][i]);
printf ("\n");
}}
Output:
1234
5678
9 10 11 12
main ()
{
int t, i, k=1, num [3][4];
for (i=0; i<4; ++i) // controlling right index
for (t=0; t <3; t ++) // controlling left index
num [t][i] = k++;
for (t=0; t <3; t ++)
{
for (i=0; i<4; ++i)
printf ("%3d", num[t][i]);
printf ("\n");
}}

Output:

1 4 7 10
2 5 8 11
3 6 9 12

Carefully notice the difference between the two codes and their corresponding outputs.
CSE 1202: Computer Programming Sessional

Array Initialization:
Example 1:
10 element integer array is initialized with the numbers 1 through 10 as:

int I[10] ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Example 2:

char str[9] = “I like C”;


this is same as writing:
char str[9] = {‘I’, ‘ ’, ‘l’,‘i’, ‘k’,‘e’,‘ ’,‘C’, ‘\0’};
String:
A string constant is a list of characters enclosed by double quotation marks. For example: “hello,
this is a test”. In C, In order to allow variable length strings the \0 character is used to indicate the
end of a string. A null is specified using ‘\0’. Because of the null terminator, it is necessary to
declare character array to be one character longer than the largest string that they are to hold. For
example, to declare an array str that can hold a 10 character string, we write: char str[11]; this
makes room for the null at the end of the string. The easiest way to input a string from the keyboard
is with the gets() library function. The general form gets() is: gets(array_name); To read a string,
call gets() with the name of the array, without any index, as its arguments. The gets() function will
continue to read characters until you enter a carriage return. The carriage return does not
become part of the string instead a null terminator is placed at the end. Another way to input a
string: scanf(“%s”,array_name); The puts() functions writes its string argument to the screen
followed by a newline. Its prototype is: puts(array_name); Another way to write string into
display: printf(“%s”,array_name); Array of strings: To create an array of strings, a two
dimensional character array is used with the size of the left-Index determining the number of
strings and the size of the right index specifying the maximum length of each string. For example,
to declare an array of 30 strings each having a maximum length of 80 characters. char
str_array[30][80]; To access an individual string is quite easy: you simply specify only the left
index.
CSE 1202: Computer Programming Sessional

Example using string:


main ()
{
char name[3][30]; // name can store 3 names each having 30 characters maximum.
int age[3];
int i;
for(i=0;i<3;i++)
{
printf("enter name:");
scanf("%s",&name[i]);
printf("enter age:");
scanf("%d",&age[i]);
}
for(i=0;i<3;i++)
{
printf("%s's age is %d years.\n",name[i],age[i]);
}
}

PRACTICE PROBLEMS

• A palindrome is a word, number, phrase, or other


sequence of characters which reads the same
backward as forward, such as madam or racecar or
Program to find whether a string the number 10201.
1
is palindrome or not.
• Take user input
• Use strcpy, strrev, strcmp
• Print whether it is palindrome or not
• Take input: first name, Last name
Program to concatenation of multiple
2
strings. • Print : Full Name
CSE 1202: Computer Programming Sessional

• Take input: First Name, Last Name


Create Custom welcome message • Print welcome message
3
taking user input.
“Welcome to Dept. of EEE, JUST”
• Use nested for loop
Write a program for sorting the • If array[i]< array[j]
4 elements of an array in descending >>temp=array[i];
order >> array[i]=array[j]
>> array[j]=temp
• Take inputs in an array variable
Write a program for finding the
5 • Check each element if it is greater than the next
largest number in an array
element using for loop

You might also like