Laboratory Exercise #6 Arrays and Strings Objectives
Laboratory Exercise #6 Arrays and Strings Objectives
Laboratory Exercise #6 Arrays and Strings Objectives
OBJECTIVES
To introduce the concept of arrays.
To learn more about strings and <string.h>.
THEORY
ARRAYS
An array is a collection of variables of the same type occupying an adjacent region in memory.
e.g.
elements 5 23 -1 400
Array elements are numbered or indexed from 0 to (N-1) where N is called the array size. N
must be a positive non-zero number. In the case above, N is 4. The index numbers 0 to 3 are
also known as the subscripts of the array name no.
Array Initialization
An array must be initialized before its use. In the example above we initialize the array with:
int no[4]; //initialization
no[0] = 5; //assignment of elements
no[1] = 23;
no[2] = -1;
no[3] = 400;
or:
int no[4] = {5, 23, -1, 400}; //initialization and assignment of elements
If the number of values in the list is less than the size of the array, the remaining elements of
the array are initialised to zero. Consequently, if the number of values in the list is more than
N-1, then an error will occur. In
int no[] = {5, 23, -1, 400};
where the size of the array is not specified, the compiler automatically assigns the array size N
(in this case, 4) by counting the number of elements in the list.
#include <stdio.h>
Output:
#define SIZE 4
Enter no[0] = 2
int main() Enter no[1] = 4
{ Enter no[2] = 8
int no[SIZE], i; Enter no[3] = 16
for (i=0; i<SIZE; i++)
{ no[0] = 2
printf("Enter no[%i] = ", i); no[1] = 4
scanf("%i", &no[i]); no[2] = 8
}
no[3] = 16
for (i=0; i<SIZE; i++)
{
printf("\nno[%i] = %i", i, no[i]);
}
return 0;
}
A string is simply an array of characters. Its initialization method is slightly different from other
data types because a NULL, or the end-of-string character ‘ \0’ is appended at the end of the
array. e.g.
char letters[6] = { ’h’, ’e’, ’l’, ’l’, ’o’,’\0’ }; //has 6 elements but only 5 may be used
char letters[] = “abcde”; //in this form, ‘\0’ is automatically appended
Example: Output:
What is your name? MARIA
int main() Hello, MARIA!
{
char name[20]; //name may have 19 characters only What is your name? MARIA SHARA
printf("What is your name? "); Hello, MARIA!
scanf("%s", name); //the & beside name may be omitted Note: Only the MARIA is assigned to
printf("Hello, %s!", name); name because of the space. Try
return 0; changing %s to %[ ABC...Z].
} Characters inside the [] define the
valid input chars. Include a space.
The number of subscripts determines the array dimension. For example, no [N] refers to a one-
dimensional array size N. Similarly, no[ N][ M ] refers to a two-dimensional array which is like
a table having N columns and M rows. Higher-dimensional arrays may be formed by adding
more subscripts e.g. no[N][M][L]
The program below allows us to enter elements into a 2-dimensional integer array. This may be
used with matrices or multiplication tables, and several other applications.
For strings, two-dimensional arrays usually refer to the first subscript as the array index while
the second subscript is the length of each string. For example:
means there are 10 names with each name 19 (one less 20) characters long. Do not select a
length that is too long since this consumes memory space.
The program below asks for a date in month/day/year format then outputs the month name in
words. It selects the name from an array. monthname[0] refers to January while month[11]
refers to December.
STRING.H
This header file is needed to manipulate character arrays. Don’t forget to type #include
<string.h> at the upper portion of your code to use its functions. They are the ff:
EXERCISES
1. Make an ATM program which will display the following menu:
ATM Menu
C - Check Balance
D - Deposit
W - Withdraw
Select an option: ___
Use a function for each C, D and W selection. Once an option is chosen, the required
output is displayed, then the program ends. The ff. are the output requirements:
C option: (Make sure to initialize the balance amount.) The balance amount will
display.
D option: The amount to deposit is asked. Add this to balance then display the new
balance.
W option: The amount to withdraw is asked. Subtract this from balance then display
the new balance. (Assume that the withdrawn amount is less than the balance.)
2. Revise your ATM program in #1. Add a last option, Q-Quit. The ATM menu should
reappear (hint: use loop) after each C,D or W option procedure displays the balance.
Except of course when option Q is selected. Add also an error-trapping message and
procedure which will ask for a new amount whenever an amount larger than balance is
entered to be withdrawn.
3. Ask for a string. Display its first letter.
e.g. Enter a string: hello
hello start with the letter h
4. Ask for a string. Display but reverse the order of the characters.
e.g. Enter a string: hello
olleh
5. Ask for a sentence from a user. Redisplay the sentence.
6. Ask for a string. Determine if the entered string is a palindrome.
Note: A palindrome is a word or group of words that can be read from left to right, and
vice versa. Examples are: dad, madam, level, radar, eye, a nut for a jar of tuna
7. Ask for 10 input numbers to a 1D integer array. Then call a function that will determine
the smallest and the largest from the entered values.
8. Ask for input values to a 1D integer array of size 10. Then call a function that will display
them in the reverse order.
9. Ask for input values to a 1D integer array of size 10. Sort the entered values in an
ascending order. Use bubble sorting.
10. Ask for the elements of two 3x2 matrices, A and B. Then add and display A + B.
e.g.
A= 3 4 0 B= 6 3 9
2 -1 5 -2 4 7
A + B = 9 7 9
0 3 12