1 / 113

Dr. Jahangir UWP Data Structures Notes Unit-1

These notes are for the students of University Women's Polytechnic.

Download Presentation

Dr. Jahangir UWP Data Structures Notes Unit-1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIT 1: Introduction to Data Structures Computer Engineering Section University Women’s Polytechnic Aligarh Muslim University, Aligarh September 24, 2019 Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 1 / 113

  2. Outline Under Development Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 2 / 113

  3. Program Development Process Under Development Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 3 / 113

  4. Operations on Data Structures Contd... I Not all operations discussed above could be performed efficiently on all data structures. Operations which are efficient for one data structure may be inefficient (expensive) on other data structures. For example, with arrays search operation is efficient or at least affordable. Contrary to that insertions and deletions with arrays are expensive (inefficient). With linked lists search operation is as good as it is with the arrays. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 4 / 113

  5. Operations on Data Structures Contd... II However, insertions and deletions with linked lists are efficient. Variable performance (efficiency) of operations with re- gard to data structures is the main reason of the existence of so many data structures. Note: We shall soon learn about the efficiency or perfor- mance of operations. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 5 / 113

  6. Arrays: The Most Basic Data Structure I What are arrays? Arrays or one-dimensional arrays come into play when there is a need to store various elements of same types under a single name. We shall study arrays from the point of view of C-language. Like other variables in C-language, arrays also, need to be declared before use. Declaring Arrays General form of declaring an array variable say ’a’ is: data_type a[size]; data_type indicates the type of elements (int, float, double etc.) you want to store under the name ’a’. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 6 / 113

  7. Arrays: The Most Basic Data Structure II size indicates the maximum number of elements which could be stored with the name ’a’. Have a look at the following example: float x[50]; Above statement declares 50 variables of type float. Their names are x[0],x[1],x[2]...x[49]. Storage Structure for Arrays It means ’how elements of an array are stored in the memory?’. Again consider the following example: float x[50]; Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 7 / 113

  8. Arrays: The Most Basic Data Structure III On encountering the above declaration the compiler does the fol- lowing: 1 Reserves 50 locations of four bytes each in memory. 2 Name these locations as x[0],x[1],x[2]...x[49]. 3 First location is name as x[0] and the last as x[49]. 4 Following figure shows the memory map: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 8 / 113

  9. Arrays: The Most Basic Data Structure IV Clearly, array elements are stored in contiguous memory locations. By contiguous we mean adjacent (continuous) equal-sized blocks of memory. Accessing the Elements of an Array Let a be a properly declared array then, ithelement of a is accessed using the expression a[i]. Note that array index in C starts with 0. This must be kept in mind while using the expression. Consider the array x declared in above examples. Then, 5thand 20thelements of x could be accessed using expres- sions x[4] and x[19] respectively. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 9 / 113

  10. Arrays: The Most Basic Data Structure V Initializing Arrays Initialization of a variable means assigning a value to the variable at the time of declaration. Following is an example of an initialization of an ordinary variable p: int p = 5; Now the question is ’how to initialize an array when it consists of so many elements?’ The general form of an array initialization is: data_type array_name[size]={set of values separated by commas}; OR data_type array_name[ ]={set of values separated by commas}; Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 10 / 113

  11. Arrays: The Most Basic Data Structure VI Have a look at the initialization of array a: int a[5] = {12,31,23,5,91}; OR int a[ ] = {12,31,23,5,91}; Note that specifying size is optional when initializing the arrays. When size is not specified, the compiler counts the number of elements in the array using ’set of values’. It then allocates memory accordingly. If size is greater than the number of elements specified in ’set of values’ e.g.: int a[20] = {12,31,23,5,91}; then first five elements of array a, i.e. from a[0] to a[4] are initialized to the values given in ’set of values’. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 11 / 113

  12. Arrays: The Most Basic Data Structure VII Rest of the array elements (i.e. from a[5] to a[19]) are initialized to zero. Have a look at the following examples: Example 1: Array initialization: (size > no. of values given) #include <stdio.h> void main(void) { int a[10]={12,23,44,33},i; for(i=0;i<10;++i) printf("%4d",a[i]); } Example 2: Values 16,91,27,67,55,72,89,105 are stored in an array, a. Write a program to increase each element of the given array by 10. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 12 / 113

  13. Arrays: The Most Basic Data Structure VIII Have a look at following program: #include <stdio.h> void main(void) { int a[ ]={16,91,27,67,55,72,89,105},i; for(i=0;i<8;++i) { a[i]+=10;/*Same as a[i]=a[i]+10*/ printf("%4d",a[i]); } } Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 13 / 113

  14. Arrays: The Most Basic Data Structure IX Reading (Input) Array Elements Let a be a properly declared array of integers, then we know that ithelement of a is accessed using the expression a[i]. The ithelement of array a could be read using the following state- ment: scanf("%d",&a[i]); Using above expression, we can directly assign values to individual elements of array a, as shown below: a[0] = 12; a[1] = 23; ... a[9] = 7; Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 14 / 113

  15. Arrays: The Most Basic Data Structure X So, if we want to read (input) 10 elements in array a, we will need to execute following set of statements: scanf("%d",&a[0]); scanf("%d",&a[1]); scanf("%d",&a[2]); ... scanf("%d",&a[9]); Above set of statements can be replaced by the following single statement using a loop: for(i=0; i<10 ;++i) scanf("%d",&a[i]); Printing (Output) Array Elements Let a be a properly declared array of integers, then we know that ithelement of a is accessed using the expression a[i − 1]. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 15 / 113

  16. Arrays: The Most Basic Data Structure XI The ithelement of array a could be printed using the following statement: printf("%d",a[i]); So, if we want to print (output) first 10 elements of an array a, we will need to execute following set of statements: printf("%4d",a[0]); printf("%4d",a[1]); printf("%4d",a[2]); ... printf("%4d",a[9]); %4d specifier prints each value in a width of 4 and the values are right justified. In above example it is used to maintain proper gap between values to be printed. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 16 / 113

  17. Arrays: The Most Basic Data Structure XII Above set of statements can be replaced by the following single statement using a loop: for(i=0; i<10 ;++i) printf("%4d",a[i]); A Programming Example An array consists of N elements. Write a program to increase each element of the array by 10. The program is listed below: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 17 / 113

  18. Arrays: The Most Basic Data Structure XIII #include <stdio.h> int main(void) { int a[100],i,N; printf("\nEnter No. of Elements in the Array:"); scanf("%d",&N); printf("\nEnter %d Elements of the Array\n",N); /*Read elements and increase each element by 10*/ for(i=0;i<N;++i) { scanf("%d",&a[i]); a[i]+=10; } /*Print elements after increment*/ for(i=0;i<N;++i) printf("%4d",a[i]); return 100; } Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 18 / 113

  19. Arrays: Two Dimensional (2-d) Arrays or Matrices I What are 2-d Arrays? 2-d arrays or matrices are used when there is a need to store a table (rows & columns) of same type of numbers under a single name. Like one dimensional arrays we shall study 2-d arrays from the point of view of C-language. Like other variables in C-language, 2-d arrays also, need to be declared before use. Declaring 2-d Arrays General form of declaring a 2-d array variable say, x, is: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 19 / 113

  20. Arrays: Two Dimensional (2-d) Arrays or Matrices II data_type x[row-size] [column-size]; data_type indicates the type of table elements (int, float, double etc.) you want to store under the name x. row-size indicates the maximum number of rows which could be stored with the name x. column-size indicates the maximum number of columns which could be stored with the name x. Have a look at the following example: float x[3][2]; Above statement declares a 2-d that is capable of storing six ele- ments arranged in a table of three rows and two columns. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 20 / 113

  21. Arrays: Two Dimensional (2-d) Arrays or Matrices III Their names are x[0][0],x[0][1],x[1][0],x[1][1],x[2][1] and x[2][2]. Note that in C row & column indices start with 0. Storage Structure for 2-d Arrays It means ’how elements of a 2-d array are stored in the memory?’. Again consider the following example: float x[3] [2]; On encountering the above declaration the compiler does the fol- lowing: 1 Reserves 6 locations of four bytes each in memory. 2 Names locations as x[0][0],x[0][1],x[1][0],x[1][1],x[2][1] and x[2][2] 3 Following figure shows the memory map: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 21 / 113

  22. Arrays: Two Dimensional (2-d) Arrays or Matrices IV Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 22 / 113

  23. Arrays: Two Dimensional (2-d) Arrays or Matrices V Clearly, rows of a 2-d array are stored contiguously within the memory. By contiguous we mean adjacent (continuous) equal-sized blocks of memory. Accessing the Elements of a 2-d Array Let a be a properly declared 2-d array then, its jthelement in ith row is accessed using the expression a[i] [j]. Note that row and column indices in C start with 0. This must be kept in mind while using the expression. Initializing 2-d Arrays The general form of a 2-d array initialization is: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 23 / 113

  24. Arrays: Two Dimensional (2-d) Arrays or Matrices VI data_type array_name[row-size][column-size]={set of values separated by commas}; OR array_name[row-size][column-size]={{row 1 elements separated by commas}, {row 2 elements separated by commas}, {row 3 elements separated by commas}. . . }; Have a look at the initialization of array a: int a[2][3] = {1,1,1,2,2,2}; OR int a[2][3] = {{1,1,1},{2,2,2}}; When the array is completely initialized with all values, specifying the row-size is optional. The following initialization is perfectly acceptable: int a[ ][3] = {1,1,1,2,2,2}; OR int a[ ][3] = {{1,1,1},{2,2,2}}; Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 24 / 113

  25. Arrays: Two Dimensional (2-d) Arrays or Matrices VII If the values are missing in an initializer, they are automatically set to zero e.g.: int a[2][3] = {{1,1},{2}}; assigns 1 to a[0][0] & a[0][1], and 2 to a[1][0]. Rest of the array elements are initialized to zero. So, if we want to initialize all elements of a 2-d array to 0, it may be done as follows: int a[2][3] = {{0},{0}}; Have a look at following example: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 25 / 113

  26. Arrays: Two Dimensional (2-d) Arrays or Matrices VIII /*Program demonstrates 2-d array initialization*/ #include <stdio.h> void main(void) { int a[2][3]={1,1,1,2,2,2},b[2][3]={{1,1,1},{2,2,2}},c [][3]={{1,1,1},{2,2,2}},d[2][3]={{1,1},{2}},e [2][3]={{0},{0}},i,j; printf("\nArray a is:"); for(i=0;i<2;++i) { printf("\n"); for(j=0;j<3;++j) printf("%4d",a[i][j]); } printf("\nArray b is:"); for(i=0;i<2;++i) { printf("\n"); for(j=0;j<3;++j) printf("%4d",b[i][j]); Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 26 / 113

  27. Arrays: Two Dimensional (2-d) Arrays or Matrices IX } printf("\nArray c is:"); for(i=0;i<2;++i) { printf("\n"); for(j=0;j<3;++j) printf("%4d",c[i][j]); } printf("\nArray d is:"); for(i=0;i<2;++i) { printf("\n"); for(j=0;j<3;++j) printf("%4d",d[i][j]); } printf("\nArray e is:"); for(i=0;i<2;++i) { printf("\n"); for(j=0;j<3;++j) printf("%4d",e[i][j]); Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 27 / 113

  28. Arrays: Two Dimensional (2-d) Arrays or Matrices X } } Reading (input) Elements of a 2-d Array Let a be a properly declared 2-d array of integers, then we know that jthelement of a in ithrow is accessed using the expression a[i][j]. The jthelement of a in ithrow could be read using the following statement: scanf("%d",&a[i][j]); Using above expression, we can directly assign values to individual elements of a 2-d array, a, as shown below: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 28 / 113

  29. Arrays: Two Dimensional (2-d) Arrays or Matrices XI a[0][0] = 12; a[1][0] = 23; ... a[3][0] = 7; If we want to read (input) elements in a 2-d array, a, consisting of m rows and n columns (i.e. m × n array) , we need to execute following set of statements: for(i=0; i<m ;++i) for(j=0;j<n;++j) scanf("%d",&a[i][j]); Printing (output) Elements of a 2-d Array Let a be a properly declared 2-d array of integers, then we know that jthelement of a in ithrow is accessed using the expression a[i][j]. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 29 / 113

  30. Arrays: Two Dimensional (2-d) Arrays or Matrices XII The jthelement of a in ithrow could be printed using the following statement: printf("%d",a[i][j]); Using above expression, we can directly print values of individual elements of a 2-d array, a, as shown below: printf("%4d",a[0][0]); printf("%4d",a[1][0]); ... printf("%4d",a[3][0]); If we want to print (output) elements of a 2-d array, a, consisting of m rows and n columns (i.e. m × n array) , in matrix form we need to execute following set of statements: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 30 / 113

  31. Arrays: Two Dimensional (2-d) Arrays or Matrices XIII for(i=0; i<m ;++i) { printf("\n"); for(j=0;j<n;++j) printf("%4d",a[i][j]); } A Programming Example Write a program that reads two matrices a and b of order m × n and prints their sum on standard output device. Have a look at the following program: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 31 / 113

  32. Arrays: Two Dimensional (2-d) Arrays or Matrices XIV #include <stdio.h> void main(void) { int a[10][10],b[10][10],m,n,i,j; /*Read rows and columns in arrays*/ printf("\nEnter No. of Rows:"); scanf("%d",&m); printf("\nEnter No. of Columns:"); scanf("%d",&n); /*Read arrays*/ printf("\nEnter Array a\n"); for(i=0;i<m;++i) for(j=0;j<n;++j) scanf("%d",&a[i][j]); printf("\nEnter Array b\n"); for(i=0;i<m;++i) for(j=0;j<n;++j) scanf("%d",&b[i][j]); /*Add corresponding elements of a and b and print result as a matrix*/ printf("\nRESULT AFTER MATRICES ADDITION\n"); for(i=0;i<m;++i) { printf("\n"); Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 32 / 113

  33. Arrays: Two Dimensional (2-d) Arrays or Matrices XV for(j=0;j<n;++j) printf("%4d",a[i][j]+b[i][j]); } } Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 33 / 113

  34. Multidimensional Arrays We have gone through one and two dimensional arrays. C also supports arrays with three or more dimensions. However, they are rarely used. So we shall briefly look into them. The general form of declaring a multidimensional array is: data_type array_name[d1][d2][d3]...[dm]; Have a look at the following declarations of three and four dimen- sional arrays y and z. float y[3][5][12]; double z[2][3][4][4]; Array y is capable of holding 180 float elements while array z is capable of holding 96 double elements. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 34 / 113

  35. Data Structure Operations & Arrays I Following table summarizes arrays from the point of view of data structure operations: S.No. Operation Performance (Efficiency) 1. Traversing Affordable (Efficient) & Simple 2. Searching Affordable (Moderately Efficient) 3. Sorting Affordable (Moderately Efficient) 4. Merging Affordable 5. Insertions Not Affordable (Inefficient), Complex 6. Deletions Not Affordable (Inefficient), Complex Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 35 / 113

  36. Character Arrays & Strings I What are character arrays & strings? A Character Array is a sequence of characters that is treated as a single data item. A String is a sequence of characters that is treated as a single data item. Plus, a string is terminated by a null character ’\0’. So, the difference between a character array and a string is that the string is terminated with a special character ’\0’. Further note that any group of characters enclosed in double quotes represents a string constant (not a character array). C-language has no mechanism to directly read and print character arrays. However, this feature is well supported for strings. Indirectly character arrays can be read or printed. Following program illustrates above concepts: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 36 / 113

  37. Character Arrays & Strings II /*array6:Program differentiates character array and string*/ #include <stdio.h> int main(void) { int i; /*Define and initialize a character array*/ char p[]={’a’,’b’,’c’,’d’,’e’,’f’}; /*Define and initialize a String*/ char s[]="abcdef"; /*Print Size of the character array and the string and notice the difference*/ printf("\nSize of p=%d bytes",sizeof(p)); printf("\nSize of s=%d bytes",sizeof(s)); /*Print the character array and the string and notice printing of character array*/ printf("\np=%s",p); printf("\ns=%s",s); Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 37 / 113

  38. Character Arrays & Strings III /*Now print the character array as follows (indirect way )*/ printf("\np="); for(i=0;i<6;++i) printf("%c",p[i]); } Clearly, strings are deal with %s specification directly. However, character arrays are deal with %c along with some form of looping statement. Here, our main focus is strings as all character arrays could be converted into strings by appending a null character to them. Declaring Strings The general form of declaring a string is: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 38 / 113

  39. Character Arrays & Strings IV char string_name[size]; When a character string is declared, the compiler automatically adds a null character (’\0’) to the end of string. Therefore the size must be equal to the maximum number of char- acters in the string plus one. Have a look at following string declaration: char name[30]; The string variable name is capable of holding strings of length up to 29 characters. Initializing Strings The general form of initializing the string is: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 39 / 113

  40. Character Arrays & Strings V char string_name[size]={string characters enclosed in single quotes and separated by commas,’\0’}; OR char string_name[size]="string characters"; When initializing strings, specifying size is optional. So above declarations are equivalent to: char string_name[ ]={string characters enclosed in single quotes and separated by commas,’\0’}; OR char string_name[ ]="string characters"; So, all the following declarations are identical: char institute[12]={’P’,’o’,’l’,’y’,’t’,’e’,’c’,’h’,’n’,’i’,’c’,’\0’}; char institute[12]="Polytechnic"; char institute[ ]={’P’,’o’,’l’,’y’,’t’,’e’,’c’,’h’,’n’,’i’,’c’,’\0’}; char institute[ ]="Polytechnic"; Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 40 / 113

  41. Character Arrays & Strings VI When initializing, if size specified is larger than the number of characters supplied, the additional characters of the string are filled with ’\0’ character. Consider the following declaration: char Name[10]="JOHN"; Following figure shows the memory layout for string variable Name. Reading Strings 1. Using scanf function: scanf function with %s format specifier can be used to read a properly declare string as shown below: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 41 / 113

  42. Character Arrays & Strings VII char name[20]; scanf("%s",name); The problem with scanf is that it terminates its input as soon as it encounters a white space. A white space includes blanks, tabs, carriage returns, new lines and form feeds. So, in above example if we input "John Deer" to be assigned to name, only "John" is stored in it. However, the scanned portion of string is terminated with a ’\0’, the additional places in string are filled with garbage(?). So, memory layout for string name looks as follows: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 42 / 113

  43. Character Arrays & Strings VIII Clearly, if we want to read the name "John Deer", with scanf we need to declare two strings. Following program illustrates: /*array7: Reading strings using scanf*/ #include <stdio.h> void main(void) { char name[20],s1[10],s2[10]; int i; printf("\nEnter Name as John Deer:"); scanf("%s",name); printf("\nScanned in name=%s",name); printf("\n"); /*Verify that garbage values are stored in assigned locations*/ Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 43 / 113

  44. Character Arrays & Strings IX for(i=5;i<20;++i) printf("%c",name[i]); /*Scan the name John Deer again in two strings s1 and s2 */ printf("\nEnter name John Deer again:"); fflush(stdin); scanf("%s%s",s1,s2); printf("\nScanned name this time=%s %s",s1,s2); } There is still a way in C to read a string with white spaces using scanf. C supports a format specification known as the edit set conversion code (%[∧... ]). This specification can be used to read a line containing a variety of characters, including white spaces.. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 44 / 113

  45. Character Arrays & Strings X Have a look at following program which reads a string until a newline character is encountered. /*array8: Demonstrates reading of strings with white spaces using scanf*/ #include <stdio.h> void main() { char str[1000 ]; printf("Enter a string"); /*scan whole string , including the white spaces till \n is encountered*/ scanf("%[^\n]", str); printf("%s", str); } Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 45 / 113

  46. Character Arrays & Strings XI 2. Using gets function: Another conventional method of reading a string with white spaces is utilizing the gets function from stdio.h. Let str be a properly declared string then the statement: char str[40]; gets(str); reads characters into str from the keyboard until a new line char- acter is encountered. It also appends a null character(’\0’) to str. Have a look at following program: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 46 / 113

  47. Character Arrays & Strings XII /*array8: Demonstrates gets function*/ #include <stdio.h> void main() { char str[40]; printf("Enter a string:"); /*read whole string , including the white spaces till \n is encountered*/ gets(str); printf("%s", str); } 3. Using getchar function: In principle getchar function is used to read a single character from the keyboard. We can use this function repeatedly to read a string. Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 47 / 113

  48. Character Arrays & Strings XIII Have a look at following program which demonstrates same con- cept. /*array10: Demonstrates reding string using getchar()*/ #include <stdio.h> void main(void) { char c, str[100]; int i=0; while(1) { c=getchar(); if(c==’\n’) break; str[i++]=c; } str[i]=’\0’; printf("\nString Read Is:%s",str); } Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 48 / 113

  49. Character Arrays & Strings XIV The same program may be reproduced in following compact form: /*array11: Demonstrates reding string using getchar(): compact form*/ #include <stdio.h> void main(void) { char c, str[100]; int i=0; while((c=getchar())!=’\n’) str[i++]=c; str[i]=’\0’; printf("\nString Read Is:%s",str); } Another Example: Write a program to read and print a paragraph of text. Following program demonstrates: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 49 / 113

  50. Character Arrays & Strings XV /*array12: Demonstrates reading and printing a paragraph of text*/ #include <stdio.h> void main(void) { char c, str[1000]; int i=0; while((c=getchar())!=EOF) str[i++]=c; str[i]=’\0’; printf("\nString Read Is:%s",str); } The above program works well but we are still bound to follow the limit of the array declared to store the characters read. How to get rid of this? The following program demonstrates: Dr. J. Alam (CES) Unit 1: Introduction September 24, 2019 50 / 113

More Related