Top Banner
UNIT 5 Arrays and Strings
21

05_ArraysAndStrings

Dec 07, 2015

Download

Documents

Rhea Verallo

helps you understand better about array and string functions.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 05_ArraysAndStrings

UNIT 5

Arrays and Strings

Page 2: 05_ArraysAndStrings

CpE311N: C Programming

Importance of arrays in computer programming.

Array declaration and definition

One-dimensional Array

Two-dimensional Array

Multi-dimensional Array

Strings

Topics

Page 3: 05_ArraysAndStrings

CpE311N: C Programming

To learn the importance of arrays in computer programming.

To apply concepts of arrays using the C language.

To create simple programs in one, two or multidimensional arrays.

To make simple string programs using the standard string functions.

Objectives

Page 4: 05_ArraysAndStrings

CpE311N: C Programming

An array is a collection of variables of the same type and placed in memory contiguously .

The set of data of data in an array is only given one name and the indices are used to differentiate each of the data.

The array is important to avoid redundancy of variable declaration and for easy access of data that are placed contiguously in memory.

It is the job of the programmer to ensure that the array is large enough to hold what the program will put in them.

Arrays

Page 5: 05_ArraysAndStrings

CpE311N: C Programming

Syntax Declaration:<data_type> array_name[size_of_array] = {optional initialization

data};

Example: int num[5]={ 2, 4, 3, 10, 20};

One-Dimensional Array

202 4 3 10

Index 0 1 2 3 4

Data num[0] num[1] num[2] …

Address &num[0] &num[1] &num[2] …

or num

Note: Min Index =0

Max Index = size -1

Important:

The name of the array is the starting address of the array.

Page 6: 05_ArraysAndStrings

CpE311N: C Programming

One-Dimensional Array

Sample Program : Compute Sum of Array Inputs

1 /* Filename: SumArray.c

Program Description: Ask 5 integers and compute the sum.

Programmer: Pedro de la Cruz

Date Written: May 29,2005

*/ Sample Output:

Enter a number: 3

Enter a number: 1

Enter a number: 2

Enter a number: 5

Enter a number: 2

Sum = 13

2

3

4

5

6

7

8

9

10

#include<stdio.h>

int main(void)

{ int num[5];

int x, sum=0;

for(x=0; x,5; x++)

{ printf(“Enter a number: “);

scanf(“%d”, &num[0]);

}

for(x=0; x<5; x++)

sum += num[x];

printf(“\nSum=%d”, sum);

}

11

12

13

14

15

16

Page 7: 05_ArraysAndStrings

CpE311N: C Programming

Seatwork

Make a program that will ask 10 integers and determine how many numbers are positive, negative or zero.

Make a program that will ask 10 integers and determine how many of the inputs are odd or even numbers.

One-Dimensional Array

Page 8: 05_ArraysAndStrings

CpE311N: C Programming

In C, a string is a group of characters terminated by a null character (‘\0’).

It is necessary to declare character arrays to be one character longer than the size of the string.

Example: char str[6]= “hello”;

Strings

h ‘\0’e l l o

Page 9: 05_ArraysAndStrings

CpE311N: C Programming

Sample Program : Ask Characters and Make it a String

1 /* Filename: Function1.c Program Description: Ask 4 characters, then make it a string. Programmer: Pedro de la Cruz Date Written: May 29,2005*/

Sample Output:

Enter a character: aEnter a character: bEnter a character: cEnter a character: dString: abcd

23456789

10

#include<stdio.h>int main(void){ char str[5], ch; int x;

for(x=0; x<5; x++) { printf(“\nEnter a character:”); ch=getche( ); str[x]=ch; }

str[x]= ‘\0’;

printf(“\nString: %s”, str);}

111213141516171819

Strings

Page 10: 05_ArraysAndStrings

CpE311N: C Programming

Sample Program : Ask A String Using A Standard Function

1 /* Filename: Function1.c Program Description: Ask a string using scanf or gets. Programmer: Pedro de la Cruz Date Written: May 29,2005*/

Sample Output:

Enter your name: MayAnnEnter your surname: CruzCruz

23456789

10

#include<stdio.h>int main(void){ char str[80];

printf(“\nEnter your name: “); scanf(“%s”, &str[0]);

printf(“Enter your surname:”); gets(str);

puts(str);

}

111213141516171819

Strings

Page 11: 05_ArraysAndStrings

CpE311N: C Programming

strcpy( dest, source) –copy the content of source string to dest string

strncpy(dest, source, n) – copy at most n character from source to dest

Common String Library Functions

Sample Program : String Copy

1 /* Filename: StringCopy.c Program Description: Example of strcpy and strncpy. Programmer: Pedro de la Cruz Date Written: May 29,2005*/

Output:

I will passI willhello

2345678

#include<stdio.h>#include<string.h>int main(void){ char s1[80], s2[80]; char s3[6]=“hello”;

strcpy(s1, “I will pass”);strcpy(s2,s3);strncpy(s2,s1,6);

puts(s1);puts(s2);puts(s3);}

910111213141516

Page 12: 05_ArraysAndStrings

CpE311N: C Programming

strcat( dest, source) –appends source string to destination string

strncat(dest, source, n) – appends at most n characters of source string to dest

Common String Library Functions

Sample Program : String Concatenate

1 /* Filename: StringConcat.c Program Description: Example of strcat and strncat Programmer: Pedro de la Cruz Date Written: May 29,2005*/

Output:

hello friendhello

2345678

#include<stdio.h>#include<string.h>

int main(void){ char s1[20], s2[20];

strcpy(s1,”hello”);strcpy(s2,” friend”);strcat(s1,s2);strncat(s2,s1,5)

puts(s1);puts(s2);}

910111213141516

Page 13: 05_ArraysAndStrings

CpE311N: C Programming

strlen( string) – determines the length of the string excluding the null terminator

Common String Library Functions

Sample Program : String Length

1 /* Filename: StringLength.c Program Description: Example of strlen Programmer: Pedro de la Cruz Date Written: May 29,2005*/

Sample Output:

Enter a string: pasarLength of String: 52

345678

#include<stdio.h>#include<string.h>

int main(void){ char str[20]; int len;

printf(“Enter a string: “);gets(str);len=strlen(str);

printf(“Length of String: %d”, len);

}

910111213141516

Page 14: 05_ArraysAndStrings

CpE311N: C Programming

strcmp( s1, s2) – compares two strings

Common String Library Functions

Return Values:0 s1==s2 <0 s1<s2 >0 s1>s2

Sample Program : String Compare

1 /* Filename: StringLength.c Program Description: Example of strcmp Programmer: Pedro de la Cruz Date Written: May 29,2005*/

Sample Output:

0

2345678

#include<stdio.h>#include<string.h>

int main(void){ char s1[5]=“hello”; char s2[5]=“hello”; int result;

result = strcmp(s1,s2);printf(“%d”, result);

}

910111213141516

Page 15: 05_ArraysAndStrings

CpE311N: C Programming

Seatwork

Ask a string and print it backwards.

Ask 5 strings and display the longest string.

Ask 3 strings and arrange them in alphabetical order.

Common String Library Functions

Page 16: 05_ArraysAndStrings

CpE311N: C Programming

Syntax Declaration:

<data_type> array_name[row][column] = {optional initialization data};

Example: int num[3][4] = { 1, 2, 3, 4,

5, 6, 7, 8,

9, 3, 1, 0};

Two-Dimensional Array

Note:

Min Col & Row =0

Max Row = Row – 1

Max Col = Column – 1

Size of array = col x row x size of data

Important:

When the name of the array is used with only 1 index specifying the row, it refers to the address of the first element of the row.

1 2 3 4

Column

0 1 2 3

5 6 7 8

9 3 1 0

0

1

2

Row

Data = num[0][0]

Address = &num[0][0]

or num[0]

Page 17: 05_ArraysAndStrings

CpE311N: C Programming

Two-Dimensional ArraySample Program : Fill a Two-dimensional Array

1 /* Filename: Function1.c Program Description: Ask 6 integers and place it in a 2x3 array. Programmer: Pedro de la Cruz Date Written: May 29,2005*/

Sample Output:

Enter a number: 1Enter a number: 10Enter a number: 2Enter a number: 20Enter a number: 3Enter a number: 30

23456789

10

#include<stdio.h>int main(void){ int x[2][3]; int row, col;

for(row=0; row<3; row++){ for(col=0; col<2; col++) { printf(“Enter a number: “); scanf(“%d”, &num[row][col]); } }

}

111213141516171819

Page 18: 05_ArraysAndStrings

CpE311N: C Programming

Sample Program : Array of Strings

1 /* Filename: StrArray.c Program Description: Array of Strings Programmer: Pedro de la Cruz Date Written: May 29,2005*/

Sample Output:

Enter a string: IEnter a string: loveEnter a string: programmingIloveprogramming

23456789

10

#include<stdio.h>int main(void){ char s[3][20]; int x;

for(x=0; x<3; x++){ printf(“Enter a string: “); gets(s[x]);}

for(x=0; x<3; x++) puts(s[x]);}

111213141516171819

Two-Dimensional Array

Page 19: 05_ArraysAndStrings

CpE311N: C Programming

Multidimensional arrays can have three, four or more dimensions.

Example: 3-Dimensional Array

Multidimensional Array

4 5 6 7

3 4 5 6

1 2 3 4

5 6 7 8

0 1 2 30

1

23

4

0 1 2 30

12

Row

Column

Plane

Syntax:

<data_type> array_name[plane][row][column]

int table[3][5][4] =

{ { /*Plane 0*/

{0,1,2,3}, /*Row 0*/

{1,2,3,4}, /*Row 1*/

{3,4,5,6}, /*Row 2*/

{4,5,6,7}, /*Row 3*/

{5,6,7,8}, /*Row 4*/

}

}

Page 20: 05_ArraysAndStrings

CpE311N: C Programming

Sample Program : Multidimensional Array

1 /* Filename: MultiDim.c Program Description: Multidimensional Array sample Programmer: Pedro de la Cruz Date Written: May 29,2005*/

Output:

0 1 2 31 2 3 42 3 4 5

23456789

10

#include<stdio.h>

main(){ int table[3][3][4]= { { /*Plane 0*/ {0,1,2,3}, {1,2,3,4}, {2,3,4,5}, } };

int plane, row, col;

for(plane=0;plane<1; plane++){ for(row=0; row<3; row++) { for(col=0; col<4; col++) printf(“\t%d”, table[plane][row][col}; printf(“\n”); }}}

111213141516171819

Multidimensional Array

Page 21: 05_ArraysAndStrings

CpE311N: C Programming

Computer Science: A Structure Programming Approach in C Behrouz A. Fourozan and Richard F. Gilberg

Using Turbo C

Borland Osborne

The C Programming Language

Kernighan and Ritchie

Applications Programming in C

Johnsonbaugh, et.al.

C Programming: The Accessible Guide to Professional Programming Steven Holzner

Problem Solving and Program Design in C Jeri Hanly, et.al

References