You are on page 1of 19

NAME:- ANJALI THAKUR SECTION:- B6002 ROLL NO. :-A24 SUBMITTED TO:- Er.

MOHIT ARORA CSE ASSIGENMENT :- 2

Que. 1:- Write a program to generate m n size matrix with the help of two dimensional arrays with the following features:a) Sum of all the elements of m n size matrix.

ANS:- #include<stdio.h> #include<conio.h> int main() { int matrix[99][99]; int sum = 0, i, j; printf("enter the value of m"); scanf("%d",&m); printf("enter the value of n"); scanf("%d",&n); printf("enter the elements of matrix"); for(i = 0; i<m; i++)

{ for(j = 0; j<n; j++) { scanf("%d",matrix[i][j]); } }

for(i = 0; i<m; i++) { for(j = 0; j<n; j++) { sum=sum+matrix[i][j]; } } printf("%d\n",sum); getch(); } b) Product of all the elements of m x n matrix ANS:- #include<stdio.h> #include<conio.h> int main() { int matrix[99][99]; int multiply = 1, i, j; print("enter the value of m"); scanf("%d",&m);

print("enter the value of n"); scanf("%d",&n); print("enter the elements of matrix"); for(i = 0; i<m; i++) { for(j = 0; j<n; j++) { scanf("%d",matrix[i][j]) } }

for(i = 0; i<m; i++) { for(j = 0; j<n; j++) { multiply=multiply *matrix[i][j]; } } printf("%d\n",sum); getch(); } Que.2:- Write a function which performs the following tasks:a. Receive the address of two arguments of type int. b. Receive the third argument of type char as operation to be performed on above two numbers. c. Operations will include; Swapping and printing prime numbers in between the two numbers. d). After the calculation of the result, it will be passed to some other function; again with two arguments, one is result and second is operation performed. e.) For every operation a unique color will be defined and result will be displayed in that only.

a) ANS:#include<stdio.h> #include<conio.h> void swap(int*, int*); void main() { int a,b; scanf(%d%d, &a,&b); swap(&a,&b) printf(%d%d,a,b); getch(); } void swap(int *x, int*y) { int z; z=*x; *x=*y; *y=z; Printf(%d%d,*x,*y); }

b)ANS:#include<stdio.h> #include<conio.h>

void operation(int*,int*, char); void main() { int a, b,choice; operation(&a,&b, choice); } void operation(int*a,int*b,choice) { return; } c).ANS: #include<stdio.h> #include<conio.h> void result (int,char); void operation(int*,int*, char); void main() { int a,b .c, char choice; int i,j,temp; operation (&a,&b, choice); } void operation(int*a, int*b, char choice) { int t; if(strcmp(choice,swapping)==0) {

t=*a; *a=*b; *b=t; result(1,choice); } else if(strcmp(choice,prime)==0) { for(i=a;i<=b;i++) { temp=0; for(j=1;j<=b;j++) { c=i/j; if(c==0) { temp++; } } if(temp==2) { result(i,choice); } } else printf(invalid choice); } }

return; }

e) ANS:#include<stdio.h> #include<conio.h> void result(int ,char); void operation(int*,int*, char); void main() { int a,b,i,j,temp; int c; char choice; clrscr(); operation (&a,&b, choice); getch(); } void operation(int*a, int*b, char choice) { { result(0,choice); } { result(i,choice); } } void result(int I,char choice)

{ if(i==0) { text color (15); text background(6); printf(the swapped values of a and b are%d%d, a,b); printf( this color corresponding to swapping); } else { textcolor(29); textbackground(30); printf(the prime no. is%d,i); printf(this color correspond to prime no.); } return; }

Que3:- Write a program that asks user to input sequence of numbers, afterwards it calculates arithmetic middle of the given sequence. Program also prints numbers smaller than arithmetic middle, and afterwards prints numbers bigger than arithmetic middle. ANS:-# include<stdlib.h> #include<stdio.h> #include<conio.h> #include<process.h> void main() { int sq[50]; int mid,i,j,n,c; clrscr(); printf(enter the no. of sequences of no.); scanf(%d,&n);

printf(enter the sequence); c=0; for(i=0;i<n;i++) { scanf(%d,&sq[i]); c++; } mid=c/n; for(i=0;i<n;i++) { if(sq[i]>sq[mid]) { printf(number bigger than arithmetic middle\n); printf(%d,sq[i]); } else if(sq[i]<sq[mid]) { printf(number smaller than arithmetic middle\n); print(%d,sq[i]); } else { exit(0); } } getch(); } Q. 4 Write a program to create a digital watch with the help of the one dimensional array. Watch should have the following functionality: a. Start and Stop at any time. b. Alarm Facility c. Conversion from 24 hour to 12 hour format and vice versa.

a) ANS#include<stdlib.h> #include<stdio.h> #include<conio.h> #include<dos.h>

#include<process.h> void main() { char j; char a=a; int ar[2]; int time[2]; int alarm[2]; int s,f,k,I,z,j,h,m; clrscr(); printf(\n emter 1 to start the clock); printf(\n enter 0 to stop the clock); scanf(%d,&k); if(k==1) } printf(1.\t 12 hour clock.); print (2.\t 24 hour clock); printf(\n enter the choice); scanf(%d,&f); switch(f); { case 1: { clrscr(); printf( enter the timr for alarm); for(z=0; z<2;z++)

{ scanf(%d,&ar[z]); } label a; for(i=0;i<12;i++) { for (j=0;j<60;j++) { for(s=0;s<60;j++) { delay(1000); clrscr(); if(1==0) { time[0]=0; time[1]=j; printf(%d :%d,time[0],time[1]); printf(%cm,a); } else time[0]=I; time[1]=j; printf(%d:%d, time[0],time[1]); } if((time[0]==ar[0])&&(time[1]==ar[1])) { printf(press esc to stop alarm); while(getch()!=27)

{ print(\a); }}} if(a==a) { a=p; goto label a; } case2: { clrscr(); printf(enter the time for alarm); for(z=0;z<2;z++) { scanf(%d,&ar[z]); } for(i=0;i<24;i++) { for(j=0:j<60:j++) { for(s=0;s<60;s++) { delay(1000); time[0]=1; time[1]=j; clrscr(); printf(%d:%d,time[0],time[1]); }

if((time[0]==ar[0]&&(time[1]==ar[1]) { printf(press esc to stop the alarm\a); while (getch()!=27); } break; } else if (k==0) { i=0; j=0; exit(0); } clrscr(); printf(enter your choice :12 or 24); scanf(%d,m); if(m==12) { printf(enter the time); for(h=0;h<2;h++) { sc anf(%d,&alarm[h]); } if((alarm[0]>12)&&(alarm[0]-12; printf(%d:%d pm,alarm[0],alarm[1]);

} else if((ar[0]==24) { ar[0]=ar[0]-24; printf(00:%d am,alarm[1]); } } else if(m==24) { printf(enter the time); for(h=0;h<2;h++); { scanf(%d,&alarm[n]); } printf(enter a=am or p=pm); scanf(%c,&j); if(j==a) { printf(%d:%d, alarm[0],alarm[1]); } else if(j==p) { alarm[0]=alarm[0]+12; printf(%d:%d,alarm[0],alarm[1]); } } getch();

Que5:- Elaborate the concept of local, global variable and recursion in functions by giving appropriate example of a C program making use of the all mentioned concepts.

ANS:- LOCAL VARIABLE:- local variables are those variables which has limitation of boundary where it can be used . local variable have a scope in which its usage is possible. eg:int sum(int a); void main() { j=sum(a); clrscr(); printf(sum,j); getch(); } int sum(int a); { a=0; int m; for(m=0;m<5;m++) { a=a+m; } return a; } GLOBAL VARIABLE:- global variable are the variable which can be used throughout the program without using any constraints.

eg:int m=3; viod main() { clrscr(); printf(sum,m); getch(); } RECURSION:- when a function calls itself it is called recursion. eg:void main() { printf(I m main); main(); }

Que6:- Q. 6 Write a program to create an array with the following features: a. Initialized with the data of at least 10 keywords of C Language. b. Stores brief information of 10 entered keywords as above. User should be provided with the interactive system which posses the functionality of searching the stored information of the particular keyword in the memory at run time. If user enters any data which does not match with the stored keywords, he/she should be prompted to search again or to quit the application

ANS:#include<stdio.h> #include<conio.h>

#include<time.h> void main() { int ar[50]; int choice 1; int n; long int ar[50]; int choice 2; int search; clrscr(); printf(enter your choice from(1-10) to input); printf(\n enter the size upto which you want to store); scanf(%d,n);

switch(choice) { case 1: clrscr(); printf(enter); for(i=0;i<n;i++) { scanf(%d,ar1[n]); } break; case 2: ar[10]

for(i=0;i<10;i++) { scanf(%d,ar[1]); } printf(enter your choice in which keyword you want to search your data); scanf(%d,choice 2); switch (choice 2) { case1 : printf(enter the data you want to search); scanf(%d, search 1); search=search 1; for(i=0;i<n;i++) { ar[i]=ar1[i]; } } label 1: for(i=0;i<n;i++) { if(ar[i]==search) { printf(%d,i); } else printf(sorry!!! search again);

printf(\n press esc to quit application); while (getch ()!=27); }

You might also like