Posts

Showing posts from September, 2020

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(&quo

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 zero. S

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 &a

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

Chapter 1 : Introduction

Image
# Let us C Chapter 1 : Introduction Let us C (by Yashavant Kanetkar) There are four important aspects of any language. They are 1) The way it stores data 2) The way it operates upon this data 3) How it accomplishes input and output 4) How it lets you control the sequence of execution of instructions in a program. This chapter explains first three of them. C is different from C++, C# and JAVA in following aspects. 1) C++, C# or Java make use of Object Oriented Programming (OOP). 2) Major parts of popular operating systems like Windows, UNIX, Linux is still written in C. 3) Device driver programs are exclusively written in C. 4) Speed is where C language scores over other languages. C Learning Steps The C Character Set Types of C Constants C constants can be divided into two major categories: (a) Primary Constants (b) Secondary Constants Types of C Constants Rules for Constructing Integer Constants (a) An integer constant must have at least one digit. (b) It must not have a decimal point