2. 1.0 Introduction
Defn: A data structure is an organized grouping of data items treated
as a unit.
Data structures helps to minimize the problems associated with sorting
data and with trying to find data elements quickly and easily.
The principal data structures includes
arrays
strings
record
lists
trees
queues
stacks
Access tables.
3. 2.0 Arrays
Def: An Array is a list of elements that are all of the same data
type and are accessed through a common name.
Arrays form a convenient way to handle groups of related data.
For e.g. you might want to create a collection of 5 integers.
One way to do it would be to declare 5 integers directly;
int a, b, c, d, e ;
This is Ok, but what if you needed a thousand integers? An
easier way is to use arrays.
4. 2.1 One-Dimensional Arrays
Defn: A list of items can be given one variable name using only
one subscript; such a variable is known as a one dimensional array.
The general form of a 1-D array:
type var-name[size];
Where;
type: is a valid C data type
var-name: is a name of an array
size: specifies the maximum number of elements in the array.
E.g. int mark[4];
(i.e. This is an integer array ‘Mark’ with 4 elements).
5. Syntax:
data-type array-name[size] = { list of values };
Where;
list of values is a comma-separated list of constants.
E.g (i). int mark[4] = {54, 61, 62, 70};
Where;
mark [0] = 54;
mark [1] = 61;
mark [2] = 62;
mark [3] = 70;
2.1.1 Initializing a 1-D Array
6. Eg (ii) char ch[5] = {‘s’, ‘t’, ‘p’, ‘q’, ‘r’};
Where,
ch[0]= ‘s’;
ch[1]= ‘t’;
ch[2]= ‘p’;
ch[3]= ‘q’;
ch[4]= ‘r’;
NB: One of the nice things about array indexing is that you
can use a for loop to handle, manage or manipulate the array.
7. E.g.1 This program uses a ‘for’ loop to initialize the values in the
array and then display them on a monitor.
#include<stdio.h>
#include<conio.h>
main ( ){
int i, arr [5];
for (i =0; i < 5; i++)
arr[i] = i;
for (i =0; i < 5; i++)
printf("arr[%d] = %d n", i, arr[i]);
getch();
}
8. E.g. 2. This program declares an array mark as integer and assign values to
it. Next it uses a ‘for’ loop to display them on the monitor.
#include<stdio.h>
#include<conio.h>
main ( )
{
int mark[4] ={ 54, 61, 62, 70};
int i;
for (i = 0; i < 4; i++)
printf ("t mark[%d] = %d n ", i , mark[i] );
getch();
}
9. Eg 3. This program input 10 numbers entered by the user to an array
and compute their sum.
#include<stdio.h>
#include<conio.h>
main ( ){
int val[10], i, total=0;
printf("Enter any ten numbers: ");
for(i=0; i<10; i++)
scanf("%d", &val[i]); // input numbers
for(i=0; i<10; i++)
total = total + val[i]; // find total
printf("n Total is: %d", total);
getch();
}
10. 2.1.2 Searching an Array
If we wish to know whether or not a particular value is present within
an array we may find out by conducting a search.
E.g. 1: Write a program to search the element 62 in the array mark given
earlier. Display it when found.
#include<stdio.h>
#include<conio.h>
main ( ){
int i, mark [4] = {54,61,62,70};
for (i = 0; i < 4; i++)
if (62 == mark[i])
printf ("n Element %d found", mark [i]);
getch();
}
11. Eg 2. Modify Eg 1 above such that 62 is entered from the keyboard.
#include<stdio.h>
#include<conio.h>
main ( )
{
int i, num, mark [4] = {54,61,62,70};
printf("n Given an Array mark[4]= {54,61,62,70}");
printf("nn Enter a number to search a number from an array
mark");
printf("nn You have only 5 chances");
scanf("n%d", &num);
12. for (i = 0; i < 4; i++)
if (num == mark[i])
printf ("n Element %d found", mark [i]);
else
printf ("n number %d not in the array ", num);
getch();
}
Class Ex:
Q1. Write a program that allows the user to input 10 elements
into an array and then search for any element and display it
when found. If not found display this message, “The number
…….is not in the array.”
13. 2.2 Two-Dimensional Arrays
Defn: It is simply defined as array of arrays.
The general form of a 2-D array:
type array-name[rows][columns];
E.g. int count [4] [5];
This is a 4x5 2-D integer array
A 2-D array is accessed a row at a time, from left to right.
15. Eg1: This program loads a 4x5 array with the product of the
indices, then displays the array in row, column format.
#include<stdio.h>
#include<conio.h>
main ( )
{
int count [4][5];
int i, j;
for (i = 0; i<4; i++)
for (j = 0; j<5; j++)
count[i][j] = i * j;
16. 3.0 Strings
Def: A String in C is simply an array of characters.
Any group of characters defined between double quotation marks
is a string constant. Such as –
“This is a C Programming Language.”
General format of a string array is;
char string_name[size];
Where;
string_name is the valid variable name given to the string and
size determines the number of characters in the string.
17. For Eg.
char city[10];
‘city’ is the character array or a string that can be store10 characters.
char name[30];
‘name’ is an array of characters that can store 30 characters
18. 3.1. Initializing a String
Like numerical arrays the strings can also be initialized. It can be
done in following three forms:
Eg. char city[8] = “Computer”; or
char city[ ] = “Computer”; or
char city[8] = {‘C’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’ };
Where;
city[0]=‘C’;
city[1]=‘o’;
:
city[7]=‘r’;
19. There are several ways to read a string from the keyboard. One
of them is by using a gets( ) function. (This function also
available in stdio.h)
To use gets ( ), call it using the name of a character array without
any index.
20. Eg. This program reads a string entered at the keyboard. It then displays the
contents of that string one character at a time.
#include<stdio.h>
#include<string.h>
#include<conio.h>
main ( ){
char myarr[80];
int i;
printf ("enter a string (less than 80 characters): ");
gets(myarr);
for (i = 0; myarr[i]; i++)
printf ("%c", myarr[i]); /* prints one character at a time */
getch();
}
21. 3.2. String Handling Functions
C library is supports a large collection of string handling
functions. They are all defined in string.h file.
The four most important string related functions are:
strcpy ( )
strcat ( )
strcmp ( )
strlen ( )
22. (1) The strcpy ( ) : (string copy)
Its general form is
strcpy (to, from);
It copies the contents of from to to. The contents of from are
unchanged.
e.g. This fragment copies the string “Hello” into str and
displays it on the screen.
char str [20];
strcpy (str, “Hello”)
23. N.B: The strcpy ( ) performs no bound checking, so make sure that
the receiving array is large enough to hold what is being copied plus
the null terminator.
(2) The strcat ( ): (string concatenation)
Its general form is;
strcat (to, from);
It adds the contents of from to to.
24. E.g. This fragment copies a string “Hello” to an array ‘str’. Next it
adds the string “there” to str, and hence display “Hello there”.
char str [40];
strcpy (str, “Hello”);
strcat (str,“there”);
printf (str);
N.B: The strcat ( ) performs no bound checking.
25. (3) The strcmp ( ) (string compare)
Its general form is,
strcmp (S1, S2);
It compares two strings. It returns
- zero if the strings are the same.
- Less than zero if S1<S2
- Greater than zero if S1>S2
26. N.B: The strings are compared in dictionary order. That is;
• a string is less than another when it would appear before the
other in a dictionary
• a string is greater than the other when it would appear after
the other..
• The comparision is not based upon the length of the string.
• The comparision is case-sensitive, lower case character
being greater than uppercase.
E.g. printf(‘%d”, strcmp (“one”, “one”);
This fragment prints 0, because the strings are the same.
27. (4) The strlen ( ) (string length)
Its general from is
strlen (str);
• It returns the length, in characters of a string.
• It does not count the null terminator.
e.g. This fragment will display 4 on the screen.
printf(“%d”, strlen (“test”));
28. Eg. This program request input of two strings, then demonstrates
the four string functions covered earlier.
#include<stdio.h>
#include<string.h>
#include<conio.h>
main ( )
{
char str1[80], str2[80];
int i;
printf ("enter the 1st string: ");
gets (str1);
printf (" enter the 2nd string: ");
gets (str2);
29. printf("nn * see how long the strings are *nn");
printf ("%s is %d characters long n", str1, strlen(str1));
printf("%s is %d characters long n", str2, strlen (str2));
printf("nn * Compare the strings *n");
i = strcmp(str1, str2);
if (i == 0)
printf ("The strings are equal n");
else if(i<0)
printf ("%s is less than %s n", str1, str2);
else
printf("%s is greater than %sn", str1, str2);
30. printf("nn * concatenate str2 to end of str1 *nn");
if (strlen(str1) + strlen(str2)<80) /*checking if there is enough
space*/
strcat(str1, str2);
printf ("n%s", str1);
printf("nn * copy str2 to str1*nn");
strcpy (str1,str2);
printf ("n%s %sn", str1,str2);
getch();
}
#23:Criterion validity is also divided into two: Concurrent validity refers to a comparison between the measure in the question and an outcome assessed at the same time. Predictive validity compares the scale in question with an outcome assessed at a later time (Wikipedia)].