Você está na página 1de 11

GE6151 / COMPUTER PROGRAMMING

VELAMMAL ENGINEERING COLLEGE GE6151 COMPUTER PROGRAMMING


UNIT III 2 MARKS 1. Define an Array An array is a collection of elements of the same data type. The data type of an element is called element type. It is a data structure that is used for the storage of homogenous data. 2. What is one dimensional array? One dimensional array consists of a fixed number of elements of the same data type organized as a simple linear sequence. The elements of a one dimensional array can be accessed by using a single subscript, they are known as single sub scripted variables. (e.g.) int a [10]; 3. What are two dimensional arrays? A two dimensional array has its elements arranged in a rectangular grid of rows and columns. The elements of a two dimensional array can be accessed by using a row subscript and a column subscript. A two dimensional array is known as a matrix. (e.g.) int a [5][10]; 4. Give the syntax of the declaration of two dimensional arrays. <storage_class_specifier><type_qualifier><type_modifier>typeidentifier [<row_specifier>] [column_specifier]<=initialization_list<...>>; 5. What is array of pointers? An array of pointers is a collection of addresses. The addresses in an array of pointers could be the addresses of isolated variables or the addresses of array elements or any other addresses. 6. What is pointer to an array? To create a pointer that points to a complete array instead of pointing to the individual elements of an array or isolated variables. Such a pointer is known as a pointer to an array. Syntax: int (*p1)[5]; 7. What are the merits and demerits of Arrays? Merits: Direct Indexing The time required to access any element in an array of any dimension is almost the same irrespective of its location in the array. Demerits: 1. The memory to an array is allocated at the compile time. 2. Arrays are static in nature. The size of an array cannot be expanded or cannot be squeezed at the run time. 3. The size of an array has to be kept big enough to accommodate the worst cases. Therefore, memory usage in case of arrays is inefficient. 8. What is meant by linear search? A list can be searched sequentially wherein the search for the data item starts from the beginning and continues till the end of the list. 9. What is meant by sorting? It is an operation in which all elements of a list are arranged in a predetermined order. The elements can be arranged in a sequence from smallest to largest such that every element is less than or equal to its next neighbour in the list. Such an arrangement is called ascending order. 10. Define Strings. A string literal is a sequence of zero or more characters enclosed within double quotes. (e.g.) Viji 11. What is meant by length of the string? The length of the string is defined as the number of characters present in it. The terminating null character is not counted while determining the length of the string.

1 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING


(e.g.) strlen(abc); The length of the string is 3 12. Give the syntax for string variable or a character array. <s_class_specifier><type_qualifier><type_modifier>charidentifier [<size_specifier>] <=initialization_list OR string literal>; 13. Give the syntax of array of strings. <sclass_specifier><type_qualifier><type_modifier>charidentifier [row_specifier] [column_specifier]<=initialization_list>; 14. What is array of character pointers? An array of strings can be stored by using an array of character pointers. What is a pointer variable and what are the uses of pointer variable?

15. Write a C program to find sum of n integers that are stored in an array.
main() { int array[10]; int i; int sum = 0; for ( i = 0; i < 11; i++){ scanf("%d", &array[i]); } for (i = 0; i < 11; i++) { sum += array[i]; } printf("%d", sum); return 0;

2 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING


16 MARKS 1. Develop a C program to sort N numbers in ascending order. (16)

#include <stdio.h> #define MAXSIZE 10 void main() { int array[MAXSIZE]; int i, j, num, temp; printf("Enter the value of num \n"); scanf("%d", &num); printf("Enter the elements one by one \n"); for (i = 0; i < num; i++) { scanf("%d", &array[i]); } printf("Input array is \n"); for (i = 0; i < num; i++) { printf("%d\n", array[i]); } /* Bubble sorting begins */ for (i = 0; i < num; i++) { for (j = 0; j < (num - i - 1); j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } printf("Sorted array is...\n"); for (i = 0; i < num; i++) { printf("%d\n", array[i]); } } OUTPUT: Enter the value of num 6 Enter the elements one by one 23 45 67 89 12 34 Input array is 23 45 67 89

3 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING


12 34 Sorted array is... 12 23 34 45 67 89

2.

Develop a C program to sort N names alphabetically.

#include <stdio.h> #include <string.h> void main() { char name[10][8], tname[10][8], temp[8]; int i, j, n; printf("Enter the value of n \n"); scanf("%d", &n); printf("Enter %d names n", \n); for (i = 0; i < n; i++) { scanf("%s", name[i]); strcpy(tname[i], name[i]); } for (i = 0; i < n - 1 ; i++) { for (j = i + 1; j < n; j++) { if (strcmp(name[i], name[j]) > 0) { strcpy(temp, name[i]); strcpy(name[i], name[j]); strcpy(name[j], temp); } } } printf("\n----------------------------------------\n"); printf("Input NamestSorted names\n"); printf("------------------------------------------\n"); for (i = 0; i < n; i++) { printf("%s\t\t%s\n", tname[i], name[i]); } printf("------------------------------------------\n"); } OUTPUT Enter the value of n 7 Enter 7 names heap stack queue

4 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING


object class program project ---------------------------------------Input Names Sorted names -----------------------------------------heap class stack heap queue object object program class project program queue project stack

3.

What is a two-dimensional array and how to declare and initialize a Two-dimensional array in C?

Two Dimensional Array It is a collection of data elements of same data type arranged in rows and columns (that is, in two dimensions). Declaration of Two-Dimensional Array Type arrayName[numberOfRows][numberOfColumn]; For example, int Sales[3][5]; Initialization of Two-Dimensional Array An two-dimensional array can be initialized along with declaration. For two-dimensional array initialization, elements of each row are enclosed within curly braces and separated by commas. All rows are enclosed within curly braces. int A[4][3] = {{22, 23, 10}, {15, 25, 13}, {20, 74, 67}, {11, 18, 14}}; Referring to Array Elements To access the elements of a two-dimensional array, we need a pair of indices: one for the row position and one for the column position. The format is as simple as: name[rowIndex][columnIndex] EXAMPLE #include<stdio.h> int main(){ int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p; printf("\nEnter the row and column of first matrix"); scanf("%d %d",&m,&n);

5 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING


printf("\nEnter the row and column of second matrix"); scanf("%d %d",&o,&p); if(n!=o){ printf("Matrix mutiplication is not possible"); printf("\nColumn of first matrix must be same as row of second matrix"); } else{ printf("\nEnter the First matrix->"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nEnter the Second matrix->"); for(i=0;i<o;i++) for(j=0;j<p;j++) scanf("%d",&b[i][j]); printf("\nThe First matrix is\n"); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<n;j++){ printf("%d\t",a[i][j]); } } printf("\nThe Second matrix is\n"); for(i=0;i<o;i++){ printf("\n"); for(j=0;j<p;j++){ printf("%d\t",b[i][j]); } } for(i=0;i<m;i++) for(j=0;j<p;j++) c[i][j]=0; for(i=0;i<m;i++){ //row of first matrix for(j=0;j<p;j++){ //column of second matrix sum=0; for(k=0;k<n;k++) sum=sum+a[i][k]*b[k][j]; c[i][j]=sum; } } } printf("\nThe multiplication of two matrix is\n"); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<p;j++){ printf("%d\t",c[i][j]); } } return 0; } OUTPUT:

6 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING

4.

Write a C program to reverse a given string. (10)

Strings reverse using strrev in c programming language #include<stdio.h> #include<string.h> int main(){ char str[50]; char *rev; printf("Enter any string : "); scanf("%s",str); rev = strrev(str); printf("Reverse string is : %s",rev); return 0; } C PROGRAM TO REVERSE A STRING WITHOUT USING STRING FUNCTIONS. #include<stdio.h> int main(){ char str[50]; char rev[50]; int i=-1,j=0; printf("Enter any string : "); scanf("%s",str); while(str[++i]!='\0'); while(i>=0) rev[j++] = str[--i]; rev[j]='\0';

7 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING

printf("Reverse of string is : %s",rev); return 0; } Sample output: Enter any string : cquestionbank.blogspot.com Reverse of string is : moc.topsgolb.knabnoitseuqc

C program to reverse a string using pointers #include<stdio.h> int main(){ char str[50]; char rev[50]; char *sptr = str; char *rptr = rev; int i=-1; printf("Enter any string : "); scanf("%s",str); while(*sptr){ sptr++; i++; } while(i>=0){ sptr--; *rptr = *sptr; rptr++; --i; } *rptr='\0'; printf("Reverse of string is : %s",rev); return 0; }

Sample output: Enter any string : Pointer Reverse of string is : retnioP

5.

Write a C program to find the largest and smallest number in the array.

C code to find largest and smallest number in an array

#include<stdio.h> int main(){ int a[50],size,i,big,small;

8 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING

printf("\nEnter the size of the array: "); scanf("%d",&size); printf("\nEnter %d elements in to the array: ", size); for(i=0;i<size;i++) scanf("%d",&a[i]); big=a[0]; for(i=1;i<size;i++){ if(big<a[i]) big=a[i]; } printf("Largest element: %d",big); small=a[0]; for(i=1;i<size;i++){ if(small>a[i]) small=a[i]; } printf("Smallest element: %d",small); return 0; } Sample Output: Enter the size of the array: 4 Enter 4 elements in to the array: 2 7 8 1 Largest element: 8 Smallest element: 1 6. Briefly explain the various string handling functions in C.

A string is a sequence of characters. Any sequence or set of characters defined within double quotation symbols is a constant string Following are some of the useful string handling functions supported by C. 1) strlen() 2) strcpy() 3) strncpy() 4) strcat() 5) strncat() 6) strcmp() 7) strncmp() 8) strcmpi() 9) strncmpi() Function & Purpose S.N. strcpy(s1, s2); 1 Copies string s2 into string s1. strcat(s1, s2); 2 Concatenates string s2 onto the end of string s1. strlen(s1); 3 Returns the length of string s1. strcmp(s1, s2); 4 Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. strchr(s1, ch); 5 Returns a pointer to the first occurrence of character ch in string s1.

9 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING


strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1 strlwr() Converts string to lowercase strupr() Converts string to uppercase

#include <stdio.h> #include <string.h> int main () { char str1[12] = "Hello"; char str2[12] = "World"; char str3[12]; int len ; /* copy str1 into str3 */ strcpy(str3, str1); printf("strcpy( str3, str1) : %s\n", str3 ); /* concatenates str1 and str2 */ strcat( str1, str2); printf("strcat( str1, str2): %s\n", str1 ); /* total lenghth of str1 after concatenation */ len = strlen(str1); printf("strlen(str1) : %d\n", len ); return 0; } OUTPUT: strcpy( str3, str1) : Hello strcat( str1, str2): HelloWorld strlen(str1) : 10

#include <stdio.h> #include <string.h> int main(){ char str1[]="LOWer Case"; puts(strlwr(str1)); //converts to lowercase and displays it. return 0; } Output lower case

#include <stdio.h>

10 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

GE6151 / COMPUTER PROGRAMMING


#include <string.h> int main(){ char str1[]="UppEr Case"; puts(strupr(str1)); //Converts to uppercase and displays it. return 0; } Output UPPER CASE

11 Prepared by Department of CSE & IT, Velammal Engineering College, Chennai.

Você também pode gostar