Sie sind auf Seite 1von 9

Assignment

On

Advanced Problem Solving Technique

Date: January 16th, 2012

Problem 1 : Given a set of n students examination mark in the range of 0 to 100, make a count of number of student passed the examination. A pass is awarded for all marks of 50 and above. Example: Let the mark obtain by student are 30,43,44,50,52,54,70,88,90. The students who has scored more than equal to 50 is 6. Algorithm 1. Start 2. Read the variables for number of students, the marks of the students, and set count to 0 3. Apply if else loop 4. If the mark in step 2 is greater than equal to 50 then increase the counter by 1 and print pass statement. 5. If mark is less than 50, print fail statement. 6. Stop Program: # include <studio.h> void main { int n, m, i, count=0; printf( Enter the no. of students:); scanf(%d,&n); for(i=0; i<=100;i++) { printf( Enter the no. of marks:); scanf(%d,&m); if (m>=50) { printf( student is pass:); count++; } else printf( student is fail:); getch;

Problem 2: Given a number n, compute n factorial ( written as n! ) when n >=0.

Example: Let the nth term of factorial is 6 then its value should be 6 x5x4x3x2x1 = 720 Algorithm 1. Start 2. Define the variable to find the factorial 3. Define the call function i. If no. equals 1, then return the value 1 otherwise return n*factorial(n-1) 4. Define the main body 5. Read the number to compute factorial e.g. 6 6. Pass the arg to step 3 to compute factorial till input number 7. Print the required solution 8. Stop Program: # include <studio.h> long int factorial (int n) { if (n==1) return (1); else return (n*factorial(n-1)); } void main( ) { int num; printf(Enter a no to compute factorial); scanf(%d, &num); printf(The factorial is %d,factorial(num)); getch(); }

Problem 3: Generate and print nth term of Fibonacci sequence when n>=1 Example: Let the nth term of Fibonacci is 5 then its value should be 4 from its sequence as 0,1,1,2,3,5 Algorithm 1. Start 2. Define the return type 3. Define the calling function i. If no enter is 1, return value as 0 ii. If no enter is 2. Return value as 1 iii. If no is more than three ,return third value as the sum of nearest predecessors as sum= fib(n-1) + fib(n-2) 4. Define the main body 5. Enter the no. to compute Fibonacci series 6. Run the loop by calling the step 3 to compute the Fibonacci series and stop if the no is greater than loop value. 7. Stop Program: # include <studio.h> int fib(int n) { if( n==1) return 0; else if (n==1) return1; else return( fib(n-1)+fib(n-2)); } void main( ) { int terms; printf(how amnt terms do you need?); scanf(%d, &terms); for(i=1;i<=terms;i++) printf(The Fibonacci is %d,factorial(i)); getch();

Problem 4: Design algorithm that accepts a positive integer and reverse it order of its digit Example: Let the number be 4562 then its reverse is 2654

Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; (b) Divide num by 10 (3) Return rev_num Program: #include <stdio.h> int reversDigits(int num) { int rev_num = 0; while(num > 0) { rev_num = rev_num*10 + num%10; num = num/10; } return rev_num; } int main() { int num = 4562; printf("Reverse of no. is %d", reversDigits(num)); getchar(); return 0; }

Problem 5: Convert a decimal integer to its binary representation. Example: Let the decimal number be 13 then its binary number is 1101 Algorithm 1. Declare variable in array 2. Give the number to find the binary equivalent 3. Calculate the modulus value divided by base K and integer value divided by kof entered value 4. Print the modulus 5. Print the result 6. Repeat step 3 and 4 till integer value is greater than zero

Let index=0. Divide 13 by 2. This is 6 with a remainder of 1. So, we store the remainder in the array, i.e., d[index]=1 where index=0. Basically, we're means that d0=1. Increment index to 1, and go back to to of loop. Since 6 is not 0, we again divide by 2. This is 3, with a remainder of 0. We store the remainder in the array, i.e., d[index]=0 where index=1. Equivalently, d1=0. Increment index to 2, and go back to to of loop. Since 3 is not 0, we again divide by 2. This is 1, with a remainder of 1. We store the remainder in the array, i.e., d[index]=1 where index=2. Equivalently, d2=1. Increment index to 3, and go back to to of loop. Since 1 is not 0, we again divide by 2. This is 0, with a remainder of 1. We store the remainder in the array, i.e., d[index]=1 where index=3. Equivalently, d3=1. Increment index to 4, and go back to to of loop. At this point, our value is 0, so we're done.

Program: the pseudoProgram for converting from base 10 to base K


// num stores a value in base 10 // solution will have digits in an array

#include <stdio.h>
index = 0 ;float digit[50],int num; while ( num != 0 ) { remainder = num % K ; // assume K > 1 num = num / K ; // integer division digit[ index ] = remainder ; index ++ ; }

Problem:6 Design an algorithm to evaluate the function sin(x) as defined by the infinite series expansion Sin(x) = x/1! x3/ 3! + x5/5! x7/7! + .. Algorithm 1. Start 2. Read the number of terms n and the value x in main 3. Call the recursive function compute() from main with argument x and n 4. Recursive function compute() call other recursive function numerator() with arguments x and index of each term that returns numerator of the term 5. Recursive function compute() call other recursive function fact() with argument the index of each term that return denominator(factorial) of the term 6. Compute(x,2*n-1,s) finds the sum of all the terms in the series. 7. Print the result Program: #include<stdio.h> long int fact(int j) \*recursive function to calculate factorial*/ { if(j==0) return 1; else return (j*fact(j-1)); } float numerator (float x, int n) \* recursive function to calculate xn/ { if (n==1) return x; else return(x*numerator(x,n-1)); } float numerator (float x, int n,int s) \* recursive function to sum all the terms*/ { if (n==1) return x; else return(s*numerator(x,n)/fact(n)+compute(x,n-2,-1*s));

} void main(void) { int n,s; float x; printf("Enter the no. of terms:); scanf(%d,&n); printf("Enter the value of angle in degree:); scanf(%f,&x); x= x*3.14/180 /* to convert degree in radian*/ if(n%2==0) /* if the no of terms even,sign begins from negative*/ s=-1; else s=1; printf(sum of terms:%f,compute(x,2*n-1,s)); }

Problem:7 Given the character representation of a integer convert it to its conventional decimal format

Algorithm: 1.Define and Initialize variables 2. Enter the string number to convert in conventional decimal format 3. Calculate the ascii to integer function 4. Print the value Program: { int i; char Input [123]; printf ("Enter a number: "); gets ( Input ); i = atoi (Input); printf ("Value entered is %d); return 0; }

Das könnte Ihnen auch gefallen