Download presentation
Presentation is loading. Please wait.
Published byPhilip Atkins Modified over 8 years ago
1
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must define the array that is going to hold a string to be one byte larger than the largest string it will be required to hold, to make room for the null. A string constant is also null-terminated by the compiler automatically.
2
Strings in Memory char str[20] = “Rahul arora”; Rahularora\0???????? The empty string "" occupies 1 char
3
String Initializers char pet[5] = { ‘l’, ‘a’, ‘m’, ‘b’, ‘\0’ } ; char pet[5] ; pet[0] = ‘l’ ; pet[1] = ‘a’ ; pet[2] = ‘m’ ; pet[3] = ‘b’ ; pet[4] = ‘\0’ ; char pet[5] = "lamb" ; But not: char pet[5]; pet = “lamb” ; /* No array assignment in C */ Remember that initializers are not assignment statements! all equivalent
4
Gets() To read a string from the keyboard we must use C’s standard library functions, gets(). To use gets(), call it using the name of a character array without any index. The gets() function reads characters until you press “Enter” The carriage return is not stored, but is replaced by a null, which terminates the string. The gets() function performs no bounds checking Be sure to call it with an array large enough to hold the expected input.
5
A string entered at the keyboard. #include “stdio.h” main() { char str[80]; int i; printf(“Enter a string (less than 80 chars) :\n”); gets(str); for(i=0; str[i]; i++) printf(“%c”, str[i]); } Notice how the program uses the fact that a null is false to control the loop that outputs the string.
6
Display a string, using printf(). #include “stdio.h” main(){ char str[80]; printf(“Enter a string (less than 80 chars) : \n”); gets(str); printf(str); /* output the string */ } Since the first argument to printf() is a string, you simply use str without any index as the first argument to printf(). If you wanted to output a newline, you could output str like this: printf(“%s\n”, str);
7
Puts() To output a string we can use C’s standard library functions, puts(). Note:the scanf will take the space as the termination of string so we cannot enter a string “rahul arora” using the scanf statement but we can get the same string using gets( ).
8
Two dimensional Arrays Collection of rows & columns. A two-dimensional array is essentially an array of one-dimensional arrays and is most easily thought of in a row, column format. Eg. Checkerboard Matrices
9
Declaration & initialization type array-name[nrow][ncol]; char name[3][10]; Initialization: type array-name[nrow][ncol]= {value-list}; int sqr [3] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; Char name[3][10]={“utkarsh”,”anurag”,”ankita”};
10
Initialization of arrays within arrays Eg. int list[3][4]; int daymon[2][12] = { {31,28,31,30,31,30,31,31,30,31,30,31}, {31,29,31,30,31,30,31,31,30,31,30,31} };
11
Two-Dimensional Arrays # define MAXROW 3 # define MAXCOL 4 int list[MAXROW][MAXCOL] for (int row = 0; row < MAXROW; row++) for (int col = 0; col < MAXCOL; col++) scanf (“%d”,&list[row][col]) list 0 1 2 3 0 1 2 2 3 4 1 2 3 3 4 5 list[0] list[1] list[2] 012012
12
EG.Listing the marks of students in different subjects int marks[3][4]; [0] [1] [2] [3] marks[0] marks[1] marks[2] ( subject no.) ( student no.)
13
Memory size Bytes = size of 1 st index *size of 2 nd index *sizeof(base type) Representation in Memory: Row-Major order Column-Major order
14
Storing the marks of all students in all subjects for (students = 0; students < MAXSTU; students++) for (subjects = 0; subjects < MAXSUB; subjects++) scanf (“%d”, &marks[students][subjects]); highest = 0; for (students = 0; students < MAXSTU; students++) { for (subjects = 0; subjects < MAXSUB; subjects++ ) { if (marks[students][subjects]> highest) highest = marks[students][subjects]; } printf (“the highest of all the subject is %d”,highest); /* to find the maximum of all the marks scored */
15
/* to find the sum of all the marks scored by each individual in all the subjects */ for (students = 0; students < MAXSTU; students++) { total[students] = 0; for (subjects = 0; subjects < MAXSUB; subjects++) total[students]=total[students]+marks[students][subjects]; }
16
for (int subjects = 0; subjects < MAXSUB; subjects++ ) { highest[subjects] = 0; for (int students = 0; students < MAXSTU; students++) { if (marks[students][subjects]> highest[subjects]) highest[subjects] = marks[students][subjects]; } printf (“the highest of the subject number %d is %d”, (subjects+1),highest[subjects]); } /* to find the highest marks scored in each subject*/
17
Exercise 1.Program to obtain transpose of of 6*6 Matrix 2.Program to sort the elements of 5*5 Matrix 3.Program to multiply matrices where the size can be upto 50 rows & 50 columns. 4.Program to generate 100 random numbers between 1 and 100.Count the number of occurrences of each number and print them out in order of frequency of occurrence.(Hint: use rand( )%100 to generate random numbers.)
18
Three dimensional arrays [0] [1] [2] [3] 45 65 55 80 63 5975 66 72 64 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 65 70 Section 1 [0] [1] [2] [3] 50 55 75 70 63 5585 86 72 65 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 In the previous examples we had stored marks of all the students in all their subjects. This is only for one section. If you want to store marks for another section, one more table has to be created. Similarly for a third section, another table has to be created.
19
Three - Dimensional array ( Subject no.) Section No. ( student no.) Table for Section 1 Table for Section 2 Table for Section 3 Three dimensional arrays can be visualized as a group of tables. Here it can be noticed that the third component is the section no. Therefore the dimensions would be Section, Student, Subject
20
The three dimensional array would be declared as follows marks[MAXSEC][MAXSTU][MAXSUB] or marks[3][120][6], for a maximum of 3 sections. 120 students. 6 subjects. Three - Dimensional array
21
Storing the marks of all students in all the sections in all subjects in a three- dimensional array /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ # define MAXSEC 3 # define MAXSTU 120 # define MAXSUB 6 void main() { int marks[MAXSEC][MAXSTU][MAXSUB]; int sections,students,subjects; /* taking the input of all the marks of all students */ for (sections=0; sections < MAXSEC; sections++) for (students = 0; students < MAXSTU; students++) for (subjects = 0; subjects < MAXSUB; subjects++) scanf (“%d”, &marks[sections][students][subjects]); }
22
Highest marks in any subject in any section [0] [1] [2] [3] 45 65 55 80 63 5975 66 72 64 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 65 70 Section 1 [0] [1] [2] [3] 50 55 75 70 63 5585 86 72 65 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 Highest 86
23
Highest marks in any subject in any section /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ int highest=0; for (sections=0; sections < MAXSEC; sections++) for (students = 0; students < MAXSTU; students++) for (subjects = 0; subjects < MAXSUB; subjects++) { if (highest < marks[sections][students][subjects]) highest = marks[sections][students][subjects]; }
24
Highest marks in each subject in any section [0] [1] [2] [3] 45 65 55 80 63 5975 66 72 64 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 65 70 Section 1 [0] [1] [2] [3] 50 55 75 70 63 5585 86 72 65 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 Highest[subjects] 75858086
25
Highest marks in each subject in any section /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ int highest[MAXSUB]; for (subjects = 0; subjects < MAXSUB; subjects++) { highest[subjects]=0; for (sections=0; sections < MAXSEC; sections++) for (students = 0; students < MAXSTU; students++) { if (highest[subjects] < marks[sections][students][subjects]) highest[subjects] = marks[sections][students][subjects]; }
26
Highest marks in each subject in each section [0] [1] [2] [3] 45 65 55 80 63 5975 66 72 64 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 65 70 Section 1 [0] [1] [2] [3] 50 55 75 70 63 5585 86 72 65 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 Highest[sections][subjects] 75 85 70 8072 86 65
27
Highest marks in each subject for each section /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ int highest[MAXSEC][MAXSUB]; for (subjects = 0; subjects < MAXSUB; subjects++) for (sections=0; sections < MAXSEC; sections++) { highest[sections][subjects]=0; for (students = 0; students < MAXSTU; students++) { if (highest[sections][subjects] < marks[sections][students][subjects]) highest[sections][subjects] = marks[sections][students][subjects]; }
28
Highest marks of each student in each section [0] [1] [2] [3] 45 65 55 80 63 5975 66 72 64 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 65 70 Section 1 [0] [1] [2] [3] 50 55 75 70 63 5585 86 72 65 marks[0] marks[1] marks[2] ( subject no.) ( student no.) 60 Section 2 Highest[students][sections] 86 72 75 85 80
29
Highest marks of each student in each section /* Program to get the inputs of marks of all the students in all their subjects for all sections*/ int highest[MAXSEC][MAXSTU]; for (sections=0; sections < MAXSEC; sections++) for (students = 0; students < MAXSTU; students++) highest[sections][students] = 0; for (subjects = 0; subjects < MAXSUB; subjects++) { if (highest[sections][students] < marks[sections][students][subjects]) highest[sections][students] = marks[sections][students][subjects]; }
30
DISADVANTAGE OF ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC Maximum size (maximum number of elements) requested by the programmer would be reserved in the memory irrespective of the usage of the number of elements by the user.The memory space that is unused is wasted. LESS RESOURCE UTILIZATION. DIFFERENT DATA TYPES COULD NOT BE STORED IN AN ARRAY If an array is declared int test[30], only integer values should be entered and not float or any other data type.
31
PARALLEL ARRAYS IF YOU WANT TO STORE DIFFERENT DATA TYPES For example employee id Gross salary of the employee. Then you require two arrays 1. For storing the integer values of the employee id. 2. For storing the float values of the salary. Therefore int empid [50] flaot salary[50]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.