ta zc142-l6
Post on 03-Apr-2018
228 Views
Preview:
TRANSCRIPT
-
7/28/2019 TA ZC142-L6
1/46
TA ZC142 Computer ProgrammingLecture 6
Date: 25/01/2013
-
7/28/2019 TA ZC142-L6
2/46
Last Lecture
Continue loops
Do-While Loop
Arrays Derived Data Types
1-D Array
2-D Array
Memory organization
Programming Exercises
-
7/28/2019 TA ZC142-L6
3/46
Todays Agenda
Character Arrays (Strings)
Array of strings
Programming Exercise
-
7/28/2019 TA ZC142-L6
4/46
Arrays
Derived Data Type
-
7/28/2019 TA ZC142-L6
5/46
Arrays
An array is fixed sizesequencedcollection ofelements of the samedata typeaddressableby index or subscript.
It can be used to represent a list ofhomogenous values
Examples: List of marks of students
List of employee id no.s in an organization
-
7/28/2019 TA ZC142-L6
6/46
One Dimensional Arrays
List of items given one variable name using
only one subscript Declaration:
type variable_name[size];
Here size indicates the maximum number ofelements
The subscript value start from 0 to size-1
Example:
int number[10];
float number[10];
char grade[10];
-
7/28/2019 TA ZC142-L6
7/46
Two Dimension Array
-
7/28/2019 TA ZC142-L6
8/46
Two dimensional arrays also called as matrix
Declaration:
type variable_name[size1][size2];
size1>Number of rows in matrix
size2> Number of columns in matrix
Examples:
int number[4][3]; /* 12 elements */ float number[3][2]; /* 6 elements */
char name[10][20]; /* 200 chars */
-
7/28/2019 TA ZC142-L6
9/46
Initialization of a 2-D Array int a[2][3]={1,2,3,4,5,6};
int a[2][3]={{1,2,3}, {4,5,6}}; int a[][3]={{1,2,3}, {4,5,6}} ;
How values will be assigned in each case??
Following initializations are not allowed
int a[3][]={2,4,6,8,10,12};
int a[][]={2,4,6,8,10,12};
Note:If the first bracket pair is empty, thencompiler take the size from the number of inner
brace pairs
-
7/28/2019 TA ZC142-L6
10/46
Memory Map for 2-D Arrays
Kept in memory as a linear sequence of variables.
Two methods for storing- Row major
Column major
Example: int a[3][3];
Row major storage:
a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2],a[2][0], a[2][1], a[2][2]
Column major storage: a[0][0], a[1][0], a[2][0], a[0][1], a[1][1], a[2][1],
a[0][2], a[1][2], a[2][2]
-
7/28/2019 TA ZC142-L6
11/46
Accessing Two Dimensional Array
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; for(i=0;i
-
7/28/2019 TA ZC142-L6
12/46
Working with two dimensional Arrays
Matrix Addition and Subtraction
Let A[m][n] and B[p][q] are two matrices.
Precondition: m equals to p and n equals to q
-
7/28/2019 TA ZC142-L6
13/46
Matrix Addition
Algorithm Steps:
1. Read two matrices A and B andinitialize C matrix to zero
2. Repeat (3) for i=0 to m-1
3. Repeat (3.a) for j=0 to n-1
3.a) C[i][j] = A[i][j] +B[i][j]
4. Display C matrix.
-
7/28/2019 TA ZC142-L6
14/46
Write a program to find total marks of 5
students in 3 subjects and store the totalmarks of each student in a onedimensional array. Also find the highest
marks in each subject and store in anotherarray.
-
7/28/2019 TA ZC142-L6
15/46
Matrix Multiplication:
M1xM2
-
7/28/2019 TA ZC142-L6
16/46
Pre condition: Number of columns in M1 should beequal to number of rows in M2
Algorithm Steps: 1. Read two matrices M1[m][n] and M2[n][r] and
initialize another matrix M3[m][r] for storing result.
2. Repeat fori=0 to m-13. Repeat forj=0 to r-1
4. M3[i][j] = 0
5.
Repeat fork=0 to n-16. M3[i][j] += M1[i][k] * M2[k][j]
7. 3. Print matrix M3.
-
7/28/2019 TA ZC142-L6
17/46
Write a program to find transpose of amatrix
-
7/28/2019 TA ZC142-L6
18/46
Strings
Strings in C are represented by array ofcharacters
End of the string is marked with a specialcharacter NULL.
The corresponding escape sequence characteris \0.
C does not have string data type. Declaration of strings:
char str[30];
char line[80];
-
7/28/2019 TA ZC142-L6
19/46
String Initialization
char str[9] = I like C;
same as char str[9]={I, ,l,i,k,e, ,C,\0};
Q. Is there any difference between followingInitialization?
char str[]=BITS;
char str[4]= BITS;
Ans: Yes, in second declaration there is no
null character
-
7/28/2019 TA ZC142-L6
20/46
Printing Strings
char text[]=C Programming;printf("%s\n",text);
printf("%10.5s\n",text);
printf("%-10.5s",text);
Output???
C Programming C Pro
C Pro
-
7/28/2019 TA ZC142-L6
21/46
Reading a
String
Using scanf()
char text[30];
printf(Enter a string: ); scanf(%s,text);
printf(The string is : %s,text);
Sample output: Enter a string: hello
The string is: hello
Enter a string: hello how are you
The string is: hello
Note: scanf() takes str ing w ithou t blank space
-
7/28/2019 TA ZC142-L6
22/46
Reading a
String
char text[30]; printf(Enter a string: );
scanf(%[a-z]s,text);
printf(The string is : %s,text);
Sample output:
Enter a string: hello The string is: hello
Enter a string: hello123
The string is: hello
-
7/28/2019 TA ZC142-L6
23/46
Single Line input char text[80];
printf(Enter a string: );
scanf(%[^\n]s,text);/*newlineterminated string */
printf(The string is : %s,text);
Sample output: Enter a string: hello how are you
The string is: hello how are you
-
7/28/2019 TA ZC142-L6
24/46
Multi line Input char text[180];
printf(Enter a string terminate with ~: ); scanf(%[^~]s,text);
printf(The string is : %s,text);
Note: After ^ any character can be used toterminate the input.
Sample output:
Enter a string terminate with ~: hello how areyou. ~
The string is: hello how are you.
-
7/28/2019 TA ZC142-L6
25/46
Input String using gets()
char str[200]; printf(Enter a string :);
gets(str);
printf(The string is :%s,str);
Output
Enter a string :C programming
The string is : C programming
-
7/28/2019 TA ZC142-L6
26/46
int main()
{ char s[80],ws[80];
int i,j;
printf(Enter the text:\n);
gets(s);/* reading text from user */
printf(The text without blank space is:\n);
puts(ws);/* printing text on monitor */
return;
}
for(i=0,j=0; s[i]!=\0; i++)
{ if(s[i]!= )ws[j++] = s[i];
}ws[j]=\0;
Impo tant Cha acte f nctions in ct pe h
-
7/28/2019 TA ZC142-L6
27/46
Important Character functions in ctype.h
isdigit(c) /*Returns a nonzero if c is a digit*/
islower(c) /* Returns a nonzero if c is a lower case
alphabetic character */
isalpha(c) /*Returns a nonzero if c is an alphabet*/
isspace(c) /*Returns a nonzero for blanks */
isupper(c) /*Returns a nonzero if c is capital letter*/
toupper(c) /* Returns upper case of c */
tolower(c)/* Returns lower case of c */
-
7/28/2019 TA ZC142-L6
28/46
String Manipulation functions in string.h
strcpy(s1,s2)/* copies s2 into s1 */
strcat(s1,s2)/* concatenates s2 to s1 */
strlen(s)/* returns the length of s */
strcmp(s1,s2)/*returns 0 if s1 and s2 are same
returns less then 0 if s1s2 */
-
7/28/2019 TA ZC142-L6
29/46
String length
char txt[100],c; int i=0,len=0;
gets(txt);
while(txt[i] != '\0') {
i++;
} printf("len = %d\n",i);
printf("len using strlen is %d\n",strlen(txt));
-
7/28/2019 TA ZC142-L6
30/46
Implementation of strcat() void main()/* s2 is concatenated after s1 */
{
char s1[100],s2[100];
int i = 0,j = 0;
printf("Enter first string\n");
scanf("%[^\n]",s1);
printf("Enter second string\n");
scanf("%[^\n]",s2);
while(s1[i++] != '\0');
i--;
while(s2[j] != '\0')
s1[i++] = s2[j++];
s1[i] = '\0';
printf("\n Final string is:%s",s1);
}
-
7/28/2019 TA ZC142-L6
31/46
Palindrome problem: Implementation
void main()
{ char str[80];int left,right,i,len,flag = 1;printf ("Enter a string");for(i = 0;(str[i] = getchar())!='\n';++i);len = i-1;for(left = 0,right = len; left < right; ++left,--right){ if(str[left]!= str[right])
{ flag = 0;break;
}}if(flag)printf("\n String is palindrome");else
printf("\n String is not a palindrome");}
-
7/28/2019 TA ZC142-L6
32/46
Write a program to read a matrix ofcharacters. Dimensions are taken from
the user.
(a) Convert a lower case alphabet intoupper case alphabet.
if there is a character other thanalphabet replace that character with
$. Display the final matrix with all the
upper case alphabets and $.
-
7/28/2019 TA ZC142-L6
33/46
(b) Find the vowels inthe resultant
matrix. Printnumber of vowelsin each row. Alsoprint the vowel
along with its index.
a f # T @
i s ! o m
V 5 u Q i
-
7/28/2019 TA ZC142-L6
34/46
Array of Strings
Exercise on Array of Strings
-
7/28/2019 TA ZC142-L6
35/46
Arrays of Strings
Declaration:
char name[5][30];
Five strings each contains maximum thirty characters.
Initialization: char[5][10]={One,Two,Three,Four,Five};
Other valid declarations:
char[][]={One,Two,Three,Four,Five};
char[5][]={One,Two,Three,Four,Five};
-
7/28/2019 TA ZC142-L6
36/46
String Arrays: Reading and Displaying
void main()
{ char name[5][30];
printf(\n Enter five strings);
/* Reading strings */
for(i=0;i
-
7/28/2019 TA ZC142-L6
37/46
Programming on Array of
String
-
7/28/2019 TA ZC142-L6
38/46
Write a C program to read the full name ofthe customers and their telephone numbers.
Full name includes Surname followed by firstname and then the middle name.
Sort the customer names in alphabetical
order with surname first followed by comma(,) and initials of the first and middle names.Display this sorted customer list along with
their telephone no. E.g Ghokle Suraj Ram should be written as
Ghokle,S.R
OUTPUT
-
7/28/2019 TA ZC142-L6
39/46
OUTPUT
asma@atlas:~/cp1$ ./a.out
Enter names and telephone numberssharma anil sudhir 1234
verma mona abhay 9876
garg sona abhi 7654
modak tinu pintu 9870shree shits sumit 8765
Customer list in alphabetical order
garg,s.a 7654modak,t.p 9870
sharma,a.s 1234
shree,s.s 8765
verma,m.a 9876
-
7/28/2019 TA ZC142-L6
40/46
Declarations
charfname[20][10],mname[20][10],sname[20][10];
charname[20][15],telephone[20][10],dummy[20];
-
7/28/2019 TA ZC142-L6
41/46
for(i=0;i
-
7/28/2019 TA ZC142-L6
42/46
Code for Sorting list in alphabetical order along
with telephone numbers
for(i=1;i
-
7/28/2019 TA ZC142-L6
43/46
Print the list
for(i=0;i
-
7/28/2019 TA ZC142-L6
44/46
Exercise : Array of Strings
Problem Statement: Write a C program thatwill read and store the details of a list ofstudents in the format
ID NAME MARKS
And produce the following output
1. Alphabetical list of Names, IDs and Marks.
2. List sorted on IDs3. List sorted on Marks
-
7/28/2019 TA ZC142-L6
45/46
Exercise
1. Write a C program which will read aline of text and rewrite it in the
alphabetical order.
2. Write a C program to replace a
particular word by another word in agiven string. Both the words areprovided by the user at run time.
-
7/28/2019 TA ZC142-L6
46/46
Any Doubts orQuestions?
top related