Sunteți pe pagina 1din 15

Homework Title / No.

: _____2_____________________________Course Code : CAP301

Course Instructor : Mandeep kaur Course Tutor (if applicable) : ____________

Date of Allotment : _____________________ Date of submission :


_26-09-2010_________________

Student’s Roll No.___ RD3002B78____________________ Section No. :


___D3002______________________

Declaration:
I declare that this assignment is my individual work. I have not copied from any other
student’s work or from any other source except where due acknowledgment is made
explicitly in the text, nor has any part been written for me by another person.
Student’s Signature :
Paramjeet Kaur

Evaluator’s comments:
_____________________________________________________________________
Marks obtained : ___________ out of ______________________
Content of Homework should start from this page only:

PART A
1. Write a function itob(n, s,b) that converts integer ‘n’ into
base ‘b’ character representation into string ‘s’.
E.g. itob (n.s, 16) format ‘n’ as a hexadecimal integer in
‘s’.
ANSWER

#include<stdio.h>
#include<conio.h>
#include<math.h>
void itob(int n; char str[], int base)
{
int change;
if(base==2)
{
change=0;
for(i=0; n!=0; i++)
{
change=change + (n%2)*(pow(10,i));
n=n%10;
}
s=change;
printf(“\n the equivalant binary number is :”);
puts(s);
}
if(base==8)
{
change=0;
for(i=0; n!=0; i++)
{
change=change + (n%8)*(pow(10,i));
n=n%10;
}
s=change;
printf(“\n the equivalant octal number is :”);
puts(s);

}
if(base==16)
{
change=0;
for(i=0; n!=0; i++)
{
change=change + (n%16)*(pow(10,i));
n=n%10;
}
s=change;
printf(“\n the equivalant hexadecimal number is :”);
puts(s);

}
}
void main()
{
int n, b; char str[100];clrscr();
printf(“enter the value for number:”);
scanf(“%d”,&n);
printf(“enter the value for base in which do you want to
convert the no.”);
scanf(“%d”,b);
itob(n,s,b);
getch();
}

2. Write a program to find the sum of reciprocals of first n


natural nos. using recursion

ANSWER

#include<stdio.h>
#include<conio.h>
float fact(int);
float sum=0.0;
void main()
{
int n;
float sum1;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
sum1=fact(n);
printf("\nAnswer=%f",s1);
getch();
}

float fact(int a)
{
if(a>=1)
{
float b;
b=(float)a;
sum=sum+b+(1/b);
fact(a-1);
}
return(sum);
}
OUTPUT: -
Enter any number : 7
Answer=0.837456

3. Explain with example how a recursive function works?

ANSWER
Recursion:

 A function is called ‘recursive’ if a statement within the body


of a function calls the same function. It is also called ‘circular
definition’. Recursion is thus a process of defining something in
terms of itself. An example of a recursive program is given below

Program:  To calculate the factorial value using recursion

01 #include <stdio.h>
02 int fact(int n);
03  
04 int main() {
05  int x, i;
06  printf("Enter a value for x: \n");
07  scanf("%d", &x);
08  i = fact(x);
09  printf("\nFactorial of %d is %d", x, i);
10  return 0;
11 }
12  
13 int fact(int n) {
14  /* n=0 indicates a terminating condition */
15  if (n <= 0) {
16   return (1);
17  } else {
18   /* function calling itself */
19   return (n * fact(n - 1));
20   /*n*fact(n-1) is a recursive expression */
21  }
22 }
Output:

Enter a value for x:

Factorial of 4 is 24

Explanation:

fact(n) = n * fact(n-1)
If n=4
fact(4) = 4 * fact(3) there is a call to fact(3)
fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1 * fact(0)
fact(0) = 1

fact(1) = 1 * 1 = 1
fact(2) = 2 * 1 = 2
fact(3) = 3 * 2 = 6
Thus fact(4) = 4 * 6 = 24

Terminating condition(n <= 0 here;) is a must for a


recursive program. Otherwise the program enters into an
infinite loop.

PART B

4 Write a program to insert an element in an unsorted list


(on the specified location).

ANSWER
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,item,loc,n;
int a[10];
printf("\n enter no.of elements");
scanf("%d",&n);
printf("\n enter elements");
for(x=0;x<n;x++)
scanf("%d",&a[x]);
printf("\n enter location");
scanf("%d",&loc);
printf("\n enter element to insert");
scanf("%d",&item);
for(y=n-1;y>=loc;y--)
{
a[y+1]=a[y];
}
a[loc]=item;
for(x=0;x<=n;x++)
printf("\n %d",a[x]);
getch();
}

5 Write a program to calculate


a. Sum of diagonal elements of matrix.
b. Sum of second diagonal elements of matrix
ANSWER

Program to calculate Sum of Diagonal of a MATRIX

#include<stdio.h>
#include<conio.h>
void main()
{
int a[4][4],x,y,ch,sum1=0,sum2=0;
clrscr();
printf("Enter the elements of Matrix\n");
for(x=0;x<4;x++)
{
for(y=0;y<3;y++)
{
scanf("%d",&a[x][y]);
if(x==y || x+y==2)
sum1=sum1+a[x][y];
if(x+y==2)
sum2=sum2+a[x][y];
}
}
printf("\n4 X 4 Matrix is ....\n\n");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
printf("%d ",a[x][y]);
}
printf("\n");
}
printf("\n\n");
printf("Sum of Diagonal elements of Matrix = %d\n\n",sum1);
printf("Sum of Diagonal elements of Matrix = %d",sum2);
getch();
}

Q:-6. Write a menu driven program to perform addition,


subtraction and multiplication of two matrices.
Answer

#include<stdio.h>
#include<conio.h>
void enter_matrix(int m[10][10],int r,int c)
{
int x, y;
for(x=1; x<=r; x++)
{
for(y=1; y<=c; y++)
{
scanf("%d",&m[x][y]);
}
}
}
void print_matrix(int m[10][10],int r,int c)
{
int x , y;
for(x=1; x<=r; x++)
{
printf("\n");
for(y=1; y<=c; y++)
{
printf("\t%d", m[x][y]);
}
}
}
void main()
{
int x , y, k,s;
int no,mat1[10][10],mat2[10][10],r1,c1,r2,c2; clrscr();
printf("Enter the number of rows and colomns of 1st matrix:");
scanf("%d%d", &r1, &c1);
printf("Enter the number of rows and colomns of 2nd matrix:");
scanf("%d%d", &r2, &c2);
if(r1<0 || r1>10 || c1<0 || c1>10 || r2<0 || r2>10 || c2<0 || c2>10)
{
printf("\n invalid rows and colomns:");
goto a;
}
printf("\nEnter the elements of the matrix 1:");
enter_matrix(mat1,r1,c1);
printf("\nEnter the elements of matrix 2:");
enter_matrix(mat2,r2,c2);
printf("\n The matrices are :\n");
printf("\n the first matrix=");
print_matrix(mat1,r1,c1);
printf("\n the second matrix is=");
print_matrix(mat2,r2,c2);
printf("\n\n MENU :\n\n");
printf("\n 1. Addition.");
printf("\n 2. Subtraction.");
printf("\n3. Multiplication");
printf("\n0. Exit\n\n");
do
{
printf("\n enter the no. From menu:\n");
scanf("%d",&no);
switch(no)
{
case 1:
if( r1!=r2 || c1!=c2 )
{
printf("\nsum is not possible rows and
colmns are not equal so sum is not
possible. ");
break;
}
printf("\n The sum of matrices is :\n");
for(x=1; x<=r1; x++)
{
printf("\n");
for(y=1; y<=c2; y++)
{
s=mat1[x][y]+mat2[x][y];
printf("\t%d", s);
}
}
break;

case 2:
if( r1!=r2 || c1!=c2)
{
printf("\n subtraction is not possible.
rows and colomns are not equal so
sum is not possible. ");
break;
}
printf("\n The subtraction of matrices is
:\n");
for(x=1; x<=r1; x++)
{
printf("\n");
for(y=1; y<=c2; y++)
{
s=mat1[x][y] - mat2[x][y];
printf("\t%d", s);
}
}break;

case 3:
if(c2!=r1)
{
printf("\nmultiplication is not possible
r1 should be = c2.");
break;
}
printf("\n The product of matrices is :\n");
for(x=1; x<=r1; x++)
{
printf("\n");
for(y=1; y<=c2; y++)
{
s=0;
for(k=1; k<=r1; k++)
{
s=s+(mat1[x][k]*mat2[k]
[y]);
}
printf("\t%d",s);
}
}
break;

case 0:
goto a;

default:
printf("\nInvalid number. enter the valid
no.");
}
}while(no>0);
a:
getch();
}

S-ar putea să vă placă și