Posts

Showing posts with the label chapter_8

Let Us C (Chapter 8 : Arrays) : Program-13_3

Image
(3) Write a program to sort a 4 x 4 matrix . // Let Us C (Chapter 8 : Arrays) : Program-13_3 /* (3) Write a program to sort a 4 x 4 matrix. */ #include<stdio.h> #include<conio.h> void main() { int M1[4][4],i,j,k,smallest,temp,*arr; clrscr(); printf("\nProgram to sort elements of a 4x4 Matrix"); printf("\nEnter 4 X 4 matrix"); printf("\nEnter 16 elements of 4x4 Matrix separated by space\n"); for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%d",&M1[i][j]); } } printf("***** Entered Matrix is *****\n"); for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf("%4d",M1[i][j]); // print matrix } printf("\n"); } arr = M1; // Copy matrix for(i=0;i<15;i++) { for(j=i+1;j<16;j++) { if(*(arr+i) > *(arr+j)) { temp = *(arr + i); *(arr + i) = *(arr + j); *(arr + j) = temp; } } } printf("***** Sorted Matrix is *****\n"); for(i=0;...

Let Us C (Chapter 8 : Arrays) : Program-13_2

Image
(2)Write a program to multiply any two 3 x 3 matrices. // Let Us C (Chapter 8 : Arrays) : Program-13_2 /* (2)Write a program to multiply any two 3 x 3 matrices. */ #include<stdio.h> #include<conio.h> void main() { int M1[3][3], M2[3][3], M3[3][3], i,j,k,sum; clrscr(); printf("\nProgram for Multiplication of Matrices"); printf("\nEnter two 3 X 3 matrices"); printf("\nEnter 9 elements of 1st Matrix separated by space\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { //printf(" Enter Element %d : ",M1[i][j]); scanf("%d",&M1[i][j]); } } printf("\nEnter 9 elements of 2nd Matrix separated by space\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { //printf(" Enter Element %d : ",M2[i][j]); scanf("%d",&M2[i][j]); } } printf("***** 1st Matrix *****\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%4d",M1[i][j]); } ...

Let Us C (Chapter 8 : Arrays) : Program-13_1

Image
(1) Write a program to add two 6 x 6 matrices. // Let Us C (Chapter 8 : Arrays) : Program-13_1 /* (1) Write a program to add two 6 x 6 matrices. */ #include<stdio.h> #include<conio.h> void main() { int M1[6][6], M2[6][6], M3[6][6],i,j,k=1; clrscr(); printf("\nProgram for Addition of Matrices"); printf("\nEnter two 6 X 6 matrices"); printf("\nEnter 36 elements of 1st Matrix separated by space\n"); for(i=0;i<6;i++) { for(j=0;j<6;j++) { scanf("%d",&M1[i][j]); } } printf("\nEnter 36 elements of 2nd Matrix seprated by space\n"); k=1; for(i=0;i<6;i++) { for(j=0;j<6;j++) { //printf(" Enter Element %d : ",k++); scanf("%d",&M2[i][j]); } } printf("***** 1st Matrix *****\n"); for(i=0;i<6;i++) { for(j=0;j<6;j++) { printf("%4d",M1[i][j]); } printf("\n"); } printf("***** 2nd Matrix *****\n")...

Let Us C (Chapter 8 : Arrays) : Program-12

Image
Very often in fairs we come across a puzzle that contains 15 numbered square pieces mounted on a frame. These pieces can be moved horizontally or vertically. A possible arrangement of these pieces is shown below:  01      04      15      07  08      10      02      11  14      03      06      13  12      09      05      Blank  As you can see there is a blank at bottom right corner. Implement the following procedure through a program: Draw the boxes as shown above. Display the numbers in the above order. Allow the user to hit any of the arrow keys (up, down, left, or right). If user hits say, right arrow key then the piece with a number 5 should move to the right and blank should replace the original position of 5. Similarly, if down arrow key is hit, then 13 should move do...

Let Us C (Chapter 8 : Arrays) : Program-11

Image
(k) Write a program to obtain transpose of a 4 x 4 matrix. The transpose of a matrix is obtained by exchanging the elements of each row with the elements of the corresponding column // Let Us C (Chapter 8 : Arrays) : Program-11 /* Write a program to obtain transpose of a 4 x 4 matrix. The transpose of a matrix is obtained by exchanging the elements of each row with the elements of the corresponding column. */ #include<stdio.h> long int Arr[4][4]; unsigned int i,j; void main() { clrscr(); for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf("Enter Arr[%d][%d] element: ",i,j); scanf("%ld",&Arr[i][j]); } } printf("\nMatrix is :\n"); for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf("%0.2ld ",Arr[i][j]); } printf("\n"); } printf("\nTranspose Matrix is: \n"); for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf("%0.2ld ",Arr[j][i]); } printf("\n"); } } ...

Let Us C (Chapter 8 : Arrays) : Program-10

Image
(j) Write a program to pick up the largest number from any 5 row by 5 column matrix. // Let Us C (Chapter 8 : Arrays) : Program-10 /* Write a program to pick up the largest number from any 5 row by 5 column matrix */ #include<stdio.h> long int Arr[5][5],Largest; unsigned Write a program to pick up the largest number from any 5 row by 5 column matrix. i,j; void main() { clrscr(); printf("Enter 25 elements of 5x5 matrix\n"); for(i=0;i<5;i++) { for(j=0;j<5;j++) { printf("Enter [%d][%d]th element: ",i,j); scanf("%ld",&Arr[i][j]); } } Largest = Arr[0][0]; printf("\nEntered Matrix is : \n"); for(i=0;i<5;i++) { for(j=0;j<5;j++) { printf("%8ld ",Arr[i][j]); if(Largest < Arr[i][j]) Largest = Arr[i][j]; } printf("\n"); } printf("\nLargest element is : %ld",Largest); } OUTPUT

Let Us C (Chapter 8 : Arrays) : Program-9

Image
(i) The screen is divided into 25 rows and 80 columns. The characters that are displayed on the screen are stored in a special memory called VDU memory (not to be confused with ordinary memory). Each character displayed on the screen occupies two bytes in VDU memory. The first of these bytes contains the ASCII value of the character being displayed, whereas, the second byte contains the colour in which the character is displayed. For example, the ASCII value of the character present on zeroth row and zeroth column on the screen is stored at location number 0xB8000000. Therefore the colour of this character would be present at location number 0xB8000000 + 1. Similarly ASCII value of character in row 0, col 1 will be at location 0xB8000000 + 2, and its colour at 0xB8000000 + 3. With this knowledge write a program which when executed would keep converting every capital letter on the screen to small case letter and every small case letter to capital letter. The procedure should stop the mo...

Let Us C (Chapter 8 : Arrays) : Program-8

Image
(h) Write a program which performs the following tasks: 1) initialize an integer array of 10 elements in main( ) 2) pass the entire array to a function modify( ) 3) in modify( ) multiply each element of array by 3 4) return the control to main( ) and print the new array elements in main() // Let Us C (Chapter 8 : Arrays) : Program-8 /* (h) Write a program which performs the following tasks: 1) initialize an integer array of 10 elements in main( ) 2) pass the entire array to a function modify( ) 3) in modify( ) multiply each element of array by 3 4) return the control to main( ) and print the new array elements in main() */ #include<stdio.h> void modify(unsigned int *); // Function declaration void main() { int i,j=1,Arr[10]; clrscr(); for(i=0;i<10;i++) { Arr[i]=j++; printf("%2d ",Arr[i]); } printf("\n"); modify(Arr); // Function call, passing array to function for(i=0;i<10;i++) printf(...

Let Us C (Chapter 8 : Arrays) : Program-7

Image
(g) Find the smallest number in an array using pointers. // Let Us C (Chapter 8 : Arrays) : Program-7 /* Find the smallest number in an array using pointers */ #include<stdio.h> #define ARRAY_SIZE 10 long int arr[ARRAY_SIZE],smallest,*ptr; unsigned int i,N; void main() { clrscr(); printf("Program to find smallest number in an array using pointers"); printf("Enter no. of elements: "); scanf("%d",&N); for(i=0;i<N;i++) { printf("Element %d: ",i); scanf("%ld",&arr[i]); } ptr = arr; // passing array address to pointer smallest = arr[0]; for(i=0;i<N;i++) { if(smallest > *(ptr+i)) // comparing using pointer { smallest = *(ptr+i); } } printf("Smallest number in array is %ld",smallest); } OUTPUT

Let Us C (Chapter 8 : Arrays) : Program-6

Image
(f) If an array arr contains n elements, then write a program to check if arr[0] = arr[n-1], arr[1] = arr[n-2] and so on. // Let Us C (Chapter 8 : Arrays) : Program-6 /* If an array arr contains n elements, then write a program to check if arr[0] = arr[n-1], arr[1] = arr[n-2] and so on */ #include<stdio.h> int Arr[100],N,M,i,j=0; void main() { // We basically need to find array is symmetric or not clrscr(); printf("Program to check if array is symmetric\n"); printf("How many elements?: "); scanf("%d",&N); for(i=0;i<N;i++) { printf("Element %d: ",i+1); scanf("%d",&Arr[i]); } M = N/2; for(i=0;i<M;i++) { if(Arr[i] == Arr[N-1-i]) j++; else break; } if(j==M) printf("Array is symmetric"); else printf("Array is asymmetric"); } OUTPUT

Let Us C (Chapter 8 : Arrays) : Program-5

Image
(e) Write a program to copy the contents of one array into another in the reverse order. // Let Us C (Chapter 8 : Arrays) : Program-5 /* Write a program to copy the contents of one array into another in the reverse order */ #include<stdio.h> int Source[10],Dest[10],i,j,N; void main() { clrscr(); printf("Program to copy the contents of array in the reverse order\n"); printf("How many elements in array?: "); scanf("%d",&N); for(i=0;i<N;i++) { printf("Enter element %d: ",i); scanf("%d",&Source[i]); } printf("\nSource Array is\n"); for(i=0;i<N;i++) printf("%d ",Source[i]); j=N; for(i=0;i<N;i++) Dest[--j] = Source[i]; // copying array in reverse order printf("\nDestination array is: \n"); for(i=0;i<N;i++) printf("%d ",Dest[i]); } OUTPUT

Let Us C (Chapter 8 : Arrays) : Program-4

Image
(d) Implement the following procedure to generate prime numbers from 1 to 100 into a program. This procedure is called sieve of Eratosthenes. Step1: Fill an array num[100] with numbers from 1 to 100 Step2: Starting with the second entry in the array, set all its multiples to zero. Step3: Proceed to the next non-zero element and set all its multiples to zero. Step4: Repeat step 3 till you have set up the multiples of all the non-zero elements to zero Step5: At the conclusion of step 4, all the non-zero entries left in the array would be prime numbers, so print out these numbers.  // Let Us C (Chapter 8 : Arrays) : Program-1 /* Implement the following procedure to generate prime numbers from 1 to 100 into a program. This procedure is called sieve of Eratosthenes. Step1: Fill an array num[100] with numbers from 1 to 100 Step2: Starting with the second entry in the array, set all its multiples to zero. Step3: Proceed to the next non-zero element and set all its multiples to zer...

Let Us C (Chapter 8 : Arrays) : Program-3

Image
(c) Implement the Selection Sort, Bubble Sort and Insertion sort algorithms on a set of 25 numbers. // Let Us C (Chapter 8 : Arrays) : Program-3 /* Implement the Selection Sort, Bubble Sort and Insertion sort algorithms on a set of 25 numbers */ #include<stdio.h> #define N 5 int arr[10],arr_SS[10],arr_BS[10],arr_IS[10],i,j,k,m,temp,lowest; void main() { clrscr(); printf("Program to implement Selection/Bubble/Insertion sort\n"); printf("Enter %d No.s separated by space\n",N); for(i=0;i<N;i++) scanf("%d",&arr[i]); //*************** Selection sort ****************** for(i=0;i<N;i++) arr_SS[i] = arr[i]; for(i=0;i<N-1;i++) { for(j=i+1;j<N;j++) { if(arr_SS[j] < arr_SS[i]) { temp=arr_SS[i]; arr_SS[i]=arr_SS[j]; arr_SS[j]=temp; } } } printf("\nAfter Selection Sort, array ...

Let Us C (Chapter 8 : Arrays) : Program-2

Image
(b) Twenty-five numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive, how many are negative, how many are even and how many odd. // Let Us C (Chapter 8 : Arrays) : Program-2 /* Twenty-five numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive, how many are negative, how many are even and how many odd */ #include<stdio.h> int arr[25],i,num,j=0,odd=0,even=0,pos=0,neg=0; void main() { clrscr(); printf("Program to find positive/negative/even/odd numbers out of an array\n"); printf("Enter 25 No.s separated by space\n"); for(i=0;i<25;i++) scanf("%d",&arr[i]); while(j<25) { if(arr[j]%2==0) even++; else odd++; if(arr[j]<0) neg++; else pos++; j++; } printf("List has %d EVEN, %d ODD, %d POSITIVE ...

Let Us C (Chapter 8 : Arrays) : Program-1

Image
(a) Twenty-five numbers are entered from the keyboard into an array. The number to be searched is entered through the keyboard by the user. Write a program to find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array. // Let Us C (Chapter 8 : Arrays) : Program-1 /* Twenty-five numbers are entered from the keyboard into an array. The number to be searched is entered through the keyboard by the user. Write a program to find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array */ #include<stdio.h> int arr[25],i,num,j=0,count=0; void main() { clrscr(); printf("Program to find a number out of an array\n"); printf("Enter 25 No.s separated by space\n"); for(i=0;i<25;i++) scanf("%d",&arr[i]); printf("Enter a No. : "); scanf("%d",&num); while...