Lecture 7 - Introduction To Data Structures
Lecture 7 - Introduction To Data Structures
Basic Concepts
G. Mariga: [email protected]
1
Data types
• The programmer has to tell the system before-hand, the type
of data such as numbers or characters the computer program
is to work on.
2
Data types
• For example in C,
– Integer requires 2 or 4 bytes (For 16 or 32-bit machine
4
C Language Data types
5
Primary data types
6
Primary data types
General examples are:
• Integer( int, integer, short, long, byte)-Integers are whole numbers with a
• Floating point- The float data type is used to store fractional numbers
(real numbers)
• Boolean-True or false
7
Structured data type
8
Data structures
9
Data structures
• A Queue is an abstract data type in which items are entered
at one end and removed from the other end for First In First
Out (FIFO). Like a waiting line in a bank or supermarket.
50 65 78 90 80
Scores [5]
0 1 2 3 4
13
Output
14
User input into an array
• You can use the for loop and scanf() function to capture input
from the user and the display the output
• Write a program requires the user to declare an array of as
much as 100 real numbers but choose how many to input.
• The program should calculates and display total and average
of the numbers entered.
Array of 100 elements
#include <stdio.h>
void main()
{
int i, count, number[100], sum;
double average;
sum =0;
printf ("How many numbers to enter?");
scanf("%d", &count);
for (i=0;i<count;i++)
{
printf ("Enter a number");
scanf("%d", &number[i]);
sum+=number[i];
}
Array of 100 integers Cont..
for (i=0;i<count;i++)
{
printf("Number %d is-> %d\n",i+1, number[i]);
}
average=(double)sum/count;
printf("Sum of %d numbers is %d\n ", count,sum);
printf("The Average is %2.3lf\n", average);
}
Output
Array of characters
• Unlike most programming languages, C does not provide a string as an
inbuilt data type. To manipulate text, we create an array of characters:
– char arrayname [elements] e.g. char Student_Name [20];
• Character arrays can be initialized using string literals. For example
– char City[20] = “Nairobi";
• City actually has 7 elements but the extra 8th space is used to store the
Null character '\0' that terminates the string.
• Since Array name is address of an array, & is not needed for scanf
(): scanf( "%s", name );. See sample program below
19
#include<stdio.h>
int main ()
{
char name[20];
char course [40]= "Data Structures & Algorithms";
printf("Enter name");
scanf("%s",name);
printf("Your name is %s \n",name);
printf("Taking a %s Bridging course\n",course);
return 0;
}
Array of Char output
Using string I/O functions
• When scanf () function is used, it reads characters until white space
encountered. This is why for the input James Kamureng’, only James is printed
on the console.
• Therefore we use string input function gets() and output function puts().
• To use the two complementary functions be sure to include the string.h
header file
#include <string.h>
…………………………………………
printf("Enter name");
gets(name);
• ………..
printf("Your name is");
puts(name);
#include<stdio.h>
#include <string.h>
int main ()
{
char name[20];int lettercount;
printf("Enter name");
gets(name);
printf("\n");
printf("Your name is");
puts(name);
lettercount=strlen(name);
printf("The name has %d ", lettercount);
return 0;
}
23
What Next ?
Pointers
24
What are Pointers?
• The statement:
char *courseptr ="Data Structures & Algorithms";
• Creates an array named courseptr using a pointer. Char * points to the
base address of the memory block allocated Data Structures &
Algorithms
#include<stdio.h>
#include<string.h>
int main ()
{
char name[20];
char *nameptr = name; //pointer to name array
//creates an array of charactres using a pointer
char *courseptr = "Data Structures and Algorithms";
printf("Enter name");
gets(name);
printf("Your name is ");
puts(nameptr);
printf("Taking a %s Bridging course\n",courseptr);
return 0;
}
Call-by-Reference
• In the Lecture two, we used pass-by-value to pass a copy of the actual
parameter to a function [Modular Programming]
• Instead of passing a value, we can pass an address to a value using the
address of operator.
• Once the address is passed, we dereference the pointer to get the actual
value. For example:
num_cubed= cube(&number);
• Passes the address of the of number to the cube () function
include<stdio.h>
void swapnumbers (int *num1, int *num2);
int main(){
int numb1, numb2;
printf("Enter the first number");
scanf("%d",&numb1);
printf("Enter the second number");
scanf("%d",&numb2);
Example 2
printf(“Before swapping Number1 is %d and
Number2 is %d \n ", numb1,numb2);
swapnumbers(&numb1,&numb2);
printf(“After swapping Number1 is %d Number %d
\n ", numb1,numb2);
return 0;}
void swapnumbers (int *num1, int *num2)
{
int temp;
temp= *num1;
*num1= *num2;
*num2=temp;
}
Passing an Arrays to a Function
• To pass an array to a function, you only need to
specify its name in the call statement.
#include <stdio.h>
#define SIZE 4
struct product {
int product_id;
int quantity;
};//end of record type definition
Array of structures
int main()
{
struct product item[SIZE];
int count;
for (count=0;count<SIZE;count++)
{
printf ("Enter the item number");
scanf("%d", &item[count].product_id);
printf ("Enter the quantity");
scanf("%d", &item[count].quantity);
}
Array of Structures
for (count=0;count<SIZE;count++)
{
printf("Product_id %d -->",
item[count].product_id);
printf("Quantity in stock %d \n",
item[count].quantity);
}
return 0;
}
Passing a structure as a parameter