Upload
chetan-hiremath
View
248
Download
0
Embed Size (px)
Citation preview
8/12/2019 Ccp Lab Manual Vtu 1sem
1/20
mahesh m
Follow
WEDNESDAY, 30 OCTOBER 2013
ccp lab manual
ccp lab manual preferred by vtu
10CPL16/26 COMPUTER PROGRAMMING LABORATORY
PART A
1. Design, develop and execute a program in C to find and output all the roots of a given
quadratic equation, for non-zero coefficients.2. Design, develop and execute a program in C to implement Euclids algorithm to find the
GCD and LCM of two integers and to output the results along with the given integers.
3. Design, develop and execute a program in C to reverse a given four digit integernumberand check whether it is a palindrome or not. Output the given number with suitable
message.4. Design, develop and execute a program in C to evaluate the given polynomial
f(x) = a4x4 + a3x3 + a2x2 + a1x1 + a0 for given value of x and the coefficients usingHornersmethod.
5. Design, develop and execute a program in C to copy its input to its output, replacingeach
string of one or more blanks by a single blank.6. Design, develop and execute a program in C to input N integer numbers in ascending
order
into a single dimension array, and then to perform a binary search for a given key integernumber and report success or failure in the form of a suitable message.7. Design, develop and execute a program in C to input N integer numbers into a single
dimension array, sort them in to ascending order using bubble sort technique, and then toprint both the given array and the sorted array with suitable headings.
8. Design, develop and execute a program in C to compute and print the word length onthehost machine.
PART B
9. Design, develop and execute a program in C to calculate the approximate value of exp
(0.5)using the Taylor Series expansion for the exponential function. Use the terms in the
powered by
Share this on Facebook
Tweet this
View stats
(NEW) Appointmen t gadget >>
SHARE IT
TOTAL PAGEVIEWS
6 9 8
2013(1)
October(1)
ccp lab manual
BLOG ARCHIVE
mahesh m
Follow 20
View my complete profile
ABOUT ME
Share 0 More Next Blog Create Blog Sign
http://www.blogger.com/http://www.blogger.com/http://www.blogger.com/home#createhttps://www.blogger.com/next-blog?navBar=true&blogID=8777956395285966872https://plus.google.com/113873419898317646856https://plus.google.com/113873419898317646856http://ccp-lab-manual.blogspot.in/2013/10/ccp-lab-manual.htmlhttp://ccp-lab-manual.blogspot.in/2013_10_01_archive.htmlhttp://void%280%29/http://ccp-lab-manual.blogspot.in/search?updated-min=2013-01-01T00:00:00-08:00&updated-max=2014-01-01T00:00:00-08:00&max-results=1http://void%280%29/http://www.appointron.com/appointron-for-blogger?utm_source=shareit&utm_medium=text_ad&utm_content=textad&utm_campaign=shareithttp://void%280%29/http://twitter.com/home?status=Check%20out%20this%20site:%20http%3A//ccp-lab-manual.blogspot.in/http://twitter.com/home?status=Check%20out%20this%20site:%20http%3A//ccp-lab-manual.blogspot.in/http://www.facebook.com/share.php?u=http%3A//ccp-lab-manual.blogspot.in/&t=Check%20out%20this%20sitehttp://www.facebook.com/share.php?u=http%3A//ccp-lab-manual.blogspot.in/&t=Check%20out%20this%20sitehttp://www.youtube.com/http://ccp-lab-manual.blogspot.in/2013/10/ccp-lab-manual.htmlhttps://plus.google.com/113873419898317646856?prsrc=58/12/2019 Ccp Lab Manual Vtu 1sem
2/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 2
expansion until the last term is less than the machine epsilon defines as FLT_EPSILON
in theheader file . Print the value returned by the Mathematical function exp ( ) also.10. Design, develop and execute a program in C to read two matrices A (M x N) and B (P
x Q)and to compute the product of A and B if the matrices are compatible for multiplication.
The program is to print the input matrices and the resultant matrix with suitable headingsand format if the matrices are compatible for multiplication, otherwise the program must
print a suitable message.(For the purpose of demonstration, the array sizes M, N, P, andQ
can all be less than or equal to 3)Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 211. Design, develop and execute a parallel program in C to add, element-wise, two
onedimensionalarrays A and B of N integer elements and to store the result in another onedimensionalarray C of N integer element.
12. Design and develop a function rightrot(x, n) in C that returns the value of the integer xrotated to the right by n bit positions as an unsigned integer. Invoke the function from the
main with different values for x and n and print the results with suitable headings.13. Design and develop a function isprime(x) that accepts an integer argument and
returns 1 if
the argument is prime and 0 otherwise. The function is to use plain division checkingapproach to determine if a given number is prime. Invoke this function from the main withdifferent values obtained from the user and print appropriate messages.
14. Design, develop and execute a parallel program in C to determine and print the primenumbers which are less than 100 making use of algorithm of the Sieve of Eratosthenes.
15. Design and develop a function reverses(s) in C to reverse the string s in place. Invokethisfunction from the main for different strings and print the original and reversed strings.
16. Design and develop a function matchany (s1,s2) which returns the first location in thestring
s1 where any character from the string s2 occurs, or 1 if s1 contains no character froms2.
Do not use the standard library function which does a similar job! Invoke the function
matchany (s1. s2) from the main for different strings and print both the strings and thereturn value from the function matchany (s1, s2).1. Note: In the practical examination the student has to answer two questions. One
question from Part A and one question from Part B, will be selected by the student bylots. All the questions listed in the syllabus have to be included in the lots. The change of
question (Part A only / Part B only / Both Part A & Part B) has to be considered, providedthe request is made for the same, within half an hour from the start of the examination.
The allotment of marks is as detailed below:Sl. # Activity Max. Marks1 Procedure
Writing program & procedure for the assigned problems along withalgorithms / flowchart
Part A 5*Part B 5*
2 ConductionExecution of the program and showing the results in proper format
Part A 10Part B 20
3 Viva-voce** 10Total Max. Marks 50
Minimum Passing Marks (40% of Max. Marks) 20
PART A
1) Design, develop and execute a program in C to find and output all the roots of
8/12/2019 Ccp Lab Manual Vtu 1sem
3/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 3
a given quadratic equation,for non-zero co-efficient
Algorithm
1. Start.
2. Input co-effic ient of equation a, b, c .3. IF any or all the coefficients are zero
Print Invalid inputELSE
d b2- 4acr |d|
IF d > 0r1 (-b +r)/ (2a)
r2 (-b -r)/ (2a)Print Roots are REAL and DISTINCT
Print r1, r2ELSE IF d < 0
r1 -b/ (2a)r2 r/ (2a)Print Roots are COMPLEX
Print r1 +i r2, r1 - i r2ELSE
r1-b/(2a)Print Roots are EQUAL
Print r1, r1END IF
END IFEND IF.
4. Stop
Program
#include #include
void main(){
int a,b,c;
float d,x1,x2,r;printf("Enter the three co-efficient :\n");scanf("%d%d%d",&a,&b,&c);if (a* b* c == 0)
{printf("\n Invalid Input ");
}else
{d = b * b - 4 * a * c;
r=sqrt(fabs(d));if (d > 0)
{x1 = (-b +r) / (2.0*a);
x2 = (-b -r) / (2.0*a);printf("\n The roots are real and distinct\n");printf("\n The roots are \n 1) x1=%f\t\t \n 2) x2=%f",x1,x2);
}else if (d == 0)
{x1 = x2 = -b/(2.0*a);
printf("\n The roots are real and equal\n");printf("\n The roots are: \n 1) x1=x2=%f",x1);
}else
8/12/2019 Ccp Lab Manual Vtu 1sem
4/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 4
{
x1 = -b / (2.0 * a);x2 = r / (2.0*a);printf("\n The roots are real and imaginary\n");
printf("\n The roots are:\n 1) %f +i %f \t\t\n 2) %f i %f ",x1,x2,x1,x2);}
}}
Sample Output
1. Enter the three co-efficient :1 4 4
The roots are real and equalThe roots are:
X1=X2=2.00002. Enter the three co-efficient :
1 - 5 6The roots are real and distinct
The roots are:X1=3.0000
X2=2.00003. Enter the three co-efficient :
2 3 4The roots are real and imaginaryThe roots are:
1) -0.750000 +i 1.1989582) -0.750000 - i 1.198958
4. Enter the three co-efficient :1 0 5
Invalid Input
2) Design, develop and execute a program in C to implement Euclids algorithm to
find the GCD and LCM of two integers and to output the results along with the
given integers.
Algorithm
1. Start.
2. Input m , n.3. Initialize p m , q n.
4. Until n 0rem m mod n
m nn rem
END until5. GCD m6. LCM p * q / GCD.
7. Print p, q, GCD, LCM.
8. Stop
Program
#includevoid main()
{int m,n,p,q,gcd,lcm,rem;
printf("Enter two numbers : ");scanf("%d%d",&m,&n);
p = m;q = n;
8/12/2019 Ccp Lab Manual Vtu 1sem
5/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 5
while(n!=0)
{rem=m%n;m=n;
n=rem;}
gcd = m;lcm = (p * q) / gcd;
printf("\n The LCM of %d and %d = %d",p,q,lcm);printf("\n The GCD of %d and %d = %d",p,q,gcd);
}Sample Output
Enter two numbers:34
66The LCM = 1122The GCD = 2
3) Design, develop and execute a program in C to reverse a given four digit
integer number and check whether it is a palindrome or not. Output the given
number with suitable message.
Algorithm
1. Start.2. Input n.3. Initialize a n, rev 0, rem 0.
4. IF n 9999Print Not a 4 digit number
Goto step 7.5. Until n 0
rem n % 10rev rev *10 + rem.n n / 10.
END until6. IF a EQUAL TO rev
Print Palindrome.
ELSEPrint Not a Palindrome.7. Stop.
Program
#include void main()
{int n,rev=0,rem,a;
printf("Enter a number : ");scanf("%d",&n);
a = n;
if(n9999){printf( Not a 4 digit number\n);
exit(0);}while(n != 0)
{rem=n%10;
rev= rev*10+rem;n = n / 10;
}if(a==rev)
printf("\n The given Number %d is Palindrome",a);
8/12/2019 Ccp Lab Manual Vtu 1sem
6/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 6
else
printf("\n The given Number %d is not Palindrome",a);}Sample Output
1. Enter a number:201
Not a four digit number2. Enter a number:
5642The Number is not Palindrome
3. Enter a number:8118
The Number is Palindrome
4) Design, develop and execute a program in C to evaluate the given polynomial
f(x)= a4x4 + a3x3 + a2x2 + a1x1 + a0 for given value of x and the coefficients using
Horners method.
Algorithm
1. Start.
2. Read n.3. FOR i 0 to n in steps of 1
Read a[i]
END FOR4. Read x.5. poly a[0]
6. FOR i 1 to n in steps of 1poly poly * x + a[i]
END FOR.7. Print poly.
8. Stop.
Program
#include
void main(){
int n,i,x,a[10],poly=0;printf("\n Enter the degree of the polynomial : ");
scanf("%d",&n);printf("\n Enter the %d coefficients\n",n+1);for(i = 0 ; i
8/12/2019 Ccp Lab Manual Vtu 1sem
7/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 7
5) Design, develop and execute a program in C to copy its input to its output,
replacing each string of one or more blanks by a single blank.
Algorithm
1. Start
2. Read the text in Array c3. FOR i 0 to c [i] '\0' in steps of 1
IF c [i] = ' 'Print ' '
End IfUntil c [i] = \0'Increment i
End UntilPrint the character
End For4. Stop
Program
#include #include
void main(){
char c[50];
int I;printf("Enter the text");scanf(%[^\t\n], c);
for(i=0;c[i]!='\0';i++){
if (c[i]==' ')printf(%c, c[i]);
while (c[i]==' ' )i++;printf(%c, c[i]);
}}
Sample output
Enter the textwelcomewelcome
6) Design, develop and execute a program in C to input N integer numbers in
ascending order into a single dimension array, and then to perform a binary
search for a given key integer number and report success or failure in the form of
a suitable message.
Algorithm
1. Start.2. Initialize Flag 0
3. Read n.
4. FOR i 0 to n-1 in steps of 1Read numbers in ascending order in Array a[ ]END FOR.
5. Read key.6. Initialize low0, highn -1.
7. Until low a[mid]
lowmid + 1
8/12/2019 Ccp Lab Manual Vtu 1sem
8/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 8
ELSE
highmid 1END IFEND Until
10. IF Flag = 1Print Successful Search.
ELSEPrint Unsuccessful Search.
END IF11. Stop.
program
#include void main()
{int n,i,a[10],key,low,high,mid,Flag=0;
printf("\n Enter the no. of elements : ");scanf("%d",&n);
printf("\n Enter %d elements in ascending order ",n);for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);printf("\n Enter the key element to search : ");
scanf("%d",&key);low = 0;high = n-1;
while(low a[mid])low=mid+1;
elsehigh = mid-1;}
if(Flag==1)printf("\n Successful Search ");
elseprintf("\n Unsuccessful Search");
}
Sample Output
Enter the no. of elements: 5
Enter 5 elements in ascending order
102030
4050
Enter the key element to search: 30Search Successful
Enter the no. of elements: 5Enter 5 elements in ascending order
23456Enter the key element to search : 7
unsuccessful Search
8/12/2019 Ccp Lab Manual Vtu 1sem
9/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 9
7) Design, develop and execute a program in C to input N integer numbers into a
single dimension array, sort them in to ascending order using bubble sort
technique, and then to print both the given array and the sorted array with
suitable headings.
Algorithm
1. Start.
2. Read n.3. FOR i0 to n-1 in steps of 1
Read integer numbers in array a [ ]b[i]a[i]END FOR.
4. FOR i0 to n-1 in steps of 1FOR j0 to n-i in steps of 1
IF a[j] > a[j + 1]temp= a[j]
a[j] = a[j + 1]a[j+1] = temp
END IFEND FOR
END FOR5. Print original array b[i]
FOR i0 to n-1 in steps of 1Print b[i]END FOR.
6. Print sorted array a[i]FOR i0 to n-1 in steps of 1
Print a[i]END FOR.
7. Stop.
Program
#include
void main(){
int n,i,j,a[10],b[10],temp;printf("\n Enter the no. of elements : ");
scanf("%d",&n);printf("\n Enter %d elements ",n);
for(i = 0 ; i < n ; i++){
scanf("%d",&a[i]);b[i]=a[i];
}for(i = 0 ; i < n-1 ; i++){
for(j = 0 ; j < n-i; j++)
{if(a[j] > a[j+1]){
temp = a[j];a[j] = a[j+1];
a[j+1] = temp;}
}}
printf("\n The original elements are\n ");for(i = 0 ; i < n ; i++)
printf("%d \n",b[i]);printf("\n The Sorted elements are ");
8/12/2019 Ccp Lab Manual Vtu 1sem
10/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 10
for(i = 0 ; i < n ; i++)
printf("%d \n",a[i]);}
Sample Output
Enter the no. of elements : 5Enter 5 elements
91152
314
The original elements are91
152
314
The Sorted elements are131
452
91
8) Design, develop and execute a program in C to compute and print the word
length on the host machine.
Algorithm
1. Start2. Initialize
Var-1Wordlen 03. Until (var)
Wordlen++Var
8/12/2019 Ccp Lab Manual Vtu 1sem
11/20
8/12/2019 Ccp Lab Manual Vtu 1sem
12/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 12
Read b[i][j]
END FOREND FOR6. Matrix multiplication
FOR i 0 to m in steps of 1FOR j 0 to q in steps of 1
c[i][j] 0FOR k 0 to n in steps of 1
c[i][j] c[i][j] + a[i][k] * b[k][j]END FOR
END FOREND FOR
End if7. Print matrix A
FOR i 0 to m in steps of 1FOR j 0 to n in steps of 1Print a[i][j]
END FOREND FOR
8. Print matrix BFOR i 0 to p in steps of 1
FOR j 0 to q in steps of 1
Print b[i][j]END FOREND FOR
9. Print matrix Csample out put
FOR i 0 to m in steps of 1FOR j 0 to q in steps of 1
Read c[i][j]END FOR
END FORELSE
Print Multiplication not possibleEND IF
10. Stop.
Program
#include
void main(){
int a[10][10],b[10][10],c[10][10];int m,n,p,q,i,j,k;
printf("\n Enter the order of the matrix A :");scanf("%d%d",&m,&n);printf("\n Enter the order of the matrix B :");
scanf("%d%d",&p,&q);
if(n==p){printf("\n Enter the elements of matrix A \n");
for(i = 0 ; i < m ; i++){
for(j = 0 ; j < n ; j++)scanf("%d",&a[i][j]);
}printf("\n Enter the elements of matrix B \n");
for(i = 0 ; i < p ; i++){for(j = 0 ; j < q ; j++)
scanf("%d",&b[i][j]);
8/12/2019 Ccp Lab Manual Vtu 1sem
13/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 13
}f
or(i = 0 ; i < m ; i++){for(j = 0 ; j < q ; j++)
{c[i][j]=0;
for(k = 0 ; k < n ; k++)c[i][j] += a[i][k] * b[k][j];
}}
printf("\n MATRIX A \n");for(i = 0 ; i < m ; i++)
{for(j = 0 ; j < n ; j++)
{printf(" %d \t", a[i][j]);}
printf("\n");}
printf("\n MATRIX B \n");for(i = 0 ; i < p ; i++)
{
for(j = 0 ; j < q ; j++){printf(" %d \t", b[i][j]);
}printf("\n");
}printf("\n MATRIX C \n");for(i = 0 ; i < m ; i++)
{for(j = 0 ; j < q ; j++)
{printf(" %d \t", c[i][j]);
}
printf("\n");}}else
printf("Matrix A & B is not multiplicable");}
Sample OutputEnter the order of the matrix A :2 2
Enter the order of the matrix B :2 2Enter the elements of matrix A1 2
3 4Enter the elements of matrix B
2 34 5
MATRIX A1 2
3 4MATRIX B
2 34 5
MATRIX C10 1322 29
Enter the order of the matrix A :2 3Enter the order of the matrix B :2 3
8/12/2019 Ccp Lab Manual Vtu 1sem
14/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 14
Matrix A & B is not multiplicable
11) Design, develop and execute a parallel program in C to add, element-wise, two
one-dimensional arrays A and B of N integer elements and to store the result in
another one-dimensional array C of N integer elements.
Algorithm
1. Start2. Read n
3. For i 0 to n in steps of 1Read elements of Array A
END For4. For j 0 to n in steps of 1
Read elements of Array B5. For i0, to n in steps of 1
c[i]=a[i]+b[i]Print C
6. Stop.
program
#include
#includevoid main()
{int i,j,k,n,a[10],b[10],c[10];printf("enter the size of an arrays\n");
scanf("%d",&n);printf("enter array elements of A\n");
for(i=0;i
8/12/2019 Ccp Lab Manual Vtu 1sem
15/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 15
2. Read x and n
3. Call function RR(x, n)6. Print the result7. Stop
Algorithm to rotate value by n bits1. IF n==0
Return xELSE
Return ((x >> n) | (x > n) | (x
8/12/2019 Ccp Lab Manual Vtu 1sem
16/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 16
#include
int prime(int n){int n,i;
for(i=2;i
8/12/2019 Ccp Lab Manual Vtu 1sem
17/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 17
#pragma omp parallel for
for(i=0;i
8/12/2019 Ccp Lab Manual Vtu 1sem
18/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 18
4. Call function reverse(s)
5. StopAlgorithm to reverse a string1. Start
2. Calculate the length3. For i 0, i
8/12/2019 Ccp Lab Manual Vtu 1sem
19/20
4/10/2014 ccp lab manual vtu 1sem
http://ccp-lab-manual.blogspot.in/ 19
4. Call function x = matchany(str1,str2)
5. IF x is equal -1Print no character matchingELSE
Print position and matched characterEnd if
6. Stop.Algorithm of matchany function
1.For i 0 to s1[i] != null in steps of 1For j 0 to s2[j] != null in steps of 1
IF s1[i] = s2[j]Return i
End IFEnd For
End For2. Return -13. Return
Program
#include
#include int matchany(char s1[ ], char s2[ ]);
void main(){char str1[20],str2[20];
int x;printf("\nEnter first String:");
scanf("%s",str1);printf("\nEnter second String:");
scanf("%s",str2);x=matchany(str1,str2);
if(x==-1)printf("No charecter matching\n");else
printf("The position is %d and charecter is %c\n",x+1,str1[x]);}
int matchany(char s1[10], char s2[10]){int i,j;
for(i=0;s1[i]!='\0';i++){
for(j=0;s2[j]!='\0';j++){
if(s1[i] == s2[j])return i;
}}
return -1;}
Sample OutputEnter first String: indiaEnter second String: mandia
The position is 1 and character is iEnter first String: hkbk
Enter second String: classNo character matching
END
8/12/2019 Ccp Lab Manual Vtu 1sem
20/20
4/10/2014 ccp lab manual vtu 1sem
Home
Subscribe to: Posts (Atom)
Posted by mahesh m at 03:36 No comments:
ccp program by mahesh
+2 Recommend this on Google
0
ONEFACT07
Posts
All Comments
ccp-lab-manual. Picture Window template. Powered by Blogger.
http://www.blogger.com/http://www.blogger.com/share-post.g?blogID=8777956395285966872&postID=7263122211591184516&target=pinteresthttp://www.blogger.com/share-post.g?blogID=8777956395285966872&postID=7263122211591184516&target=facebookhttp://www.blogger.com/share-post.g?blogID=8777956395285966872&postID=7263122211591184516&target=twitterhttp://www.blogger.com/share-post.g?blogID=8777956395285966872&postID=7263122211591184516&target=bloghttp://www.blogger.com/share-post.g?blogID=8777956395285966872&postID=7263122211591184516&target=emailhttp://ccp-lab-manual.blogspot.in/2013/10/ccp-lab-manual.html#comment-formhttp://ccp-lab-manual.blogspot.in/2013/10/ccp-lab-manual.htmlhttps://plus.google.com/113873419898317646856http://ccp-lab-manual.blogspot.com/feeds/posts/defaulthttp://ccp-lab-manual.blogspot.in/