Wednesday 12 September 2012

Stop watch

#include<iostream>
#include<conio.h>
//#include<stdlib.h>
//#include<stdio.h>
//#include<math.h>
//#include<dos.h>
//#include<string.h>
using namespace std;
void main()
{
//clrscr();


for(int i=0;i<23;i++)

{

for(int j=0;j<59;j++)

{
 
for(int k=0;k<59;k++)
 
{

  
for(int m=0;m<60;m++)
  
{
   cout<<"\n\n\n\n\n\t\t\t\t"<<i<<" :"<<j<<" :"<<k<<" :"<<m;

   
//delay(15);
 
//clrscr();
  
}
 
 
}

}

}



//getch();
}

table print

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a,b=1,limit;
cout<<"Enter the number= ";
cin>>a;
cout<<"\nEnter the limit= ";
cin>>limit;
while(b<=limit)
{
  cout<<"\n"<<a<<" * "<<b<<" = "<<a*b<<"\n";
  b=b++;
  }
  getch();
  }

calculate height and widht of rectangular

#include<iostream.h>
using namespace std;

class rect{
int left;
int right;
int top;
int bottom;

public:

rect(){
left=0;
right=0;
top=0;
bottom=0;
}

void widht(int w,int x){
int wid;
left=w;
right=x;
wid=right-left;
cout<<wid;
}

void height(int y,int z){
int hei;
top=y;
bottom=z;
hei=bottom-top;
cout<<hei;
}
};
void main(){
clrscr();
rect s1;
int w,x,y,z;
cout<<"\nenter your left side :";
cin>>w;
cout<<"\nenter your right side :";
cin>>x;
cout<<"\nenter your top side :";
cin>>y;
cout<<"\nenter you bootom side :";
cin>>z;

cout<<"\n width :";
s1.widht(w,x);

cout<<"\n height :";
s1.height(y,z);


}

guess a number game

#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
int number,i=1,true;
srand(time(0));
true=1+rand()%40;
while(i==1)
{
cout<<"\nEnter the number between (1_40)= ";

cin>>number;
if(number==true)
{
cout<<"\nYou won the game";
break;
}
if(number!=true)
{
cout<<"\nYou don't won the game";
if(number>true)
{
cout<<"\nThe number you entered is greater than the true number";
}
else
cout<<"\nThe number you entered is lesser than the true number";
continue;
}
}
getch();

}

Palindrom

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,a,b,c,d,e,f,g,h;
cout<<"Enter the 5 digit integer=";
cin>>x;
a=x/10000;
b=x%10000;
c=b/1000;
d=b%1000;
e=d/100;
f=d%100;
g=f/10;
h=f%10;
if(a==h&&c==g)
cout<<"the number is palindrome";
else
cout<<"not a palindrome";
getch();
}

Multiple or not

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a,b;
 cout<<"Enter the first integar:";
 cin>>a;
 cout<<"\nEnter the second integar:";
 cin>>b;
 if(a%b==0)
 cout<<"\n"<<a<<" is a multiple of "<<b;
 else
 cout<<"\n"<<a<<" is not the miltiple of "<<b;
 getch();
 }

Histogram

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d,e,f,i=1;
cout<<"Enter the 5 digit Integer=";
cin>>a>>b>>c>>d>>e;
while(i<=a)
{
 cout<<"*";
 i++;
 }
 i=1;
 cout<<endl;
 while(i<=b)
 {
  cout<<"*";
  i++;
  }
  cout<<endl;
  i=1;
  while(i<=c)
  {
  cout<<"*";
  i++;
  }
  cout<<endl;
  i=1;
  while(i<=d)
  {
  cout<<"*";
  i++;
  }
  cout<<endl;
  i=1;
  while(i<=e)
  {
  cout<<"*";
  i++;
  }

  getch();
  }

sample question of c++ 4



                                                                                    Total Marks: 10
Course: Programming Fundamentals
Objectives: Functions, random number generation, algorithm building
                                                                          
Problem:
We want to make a program which implements a simple variation of LUDO game.


Following are the rules of the game:

1.     There are only two players playing the game.

2.     Each player is playing with a single piece and two pieces start in the same color start area.

3.     The players are using a single die. In order to simulate rolling a die, use rand() function to get a randomly generated number in the range 1 - 6.

4.     At the start of the game, each player's piece is placed in the start area. i.e., worksum of each piece is 0.

5.     Players take turns to throw a die. A player must first throw a 6 to be able to move a piece from the starting area onto the starting square. In each subsequent turn the player moves a piece forward 1 to 6 squares as indicated by the die.

When a player throws a 6, he/she is granted another turn as a bonus. If the player gets a 6 on the bonus turn as well, he gets another bonus turn and this could continue until the player gets a number other than 6. e.g., the player rolls the die and gets 6, then he gets an extra bonus turn. If he again gets a 6, he gets another bonus turn. Let’s say he gets a 3 this time. Now, his turn will be over and he moves his piece by 6+6+3 = 15 squares.

We will simulate moving the pieces by maintaining the worksums for each piece. Only after a player gets a 6, we start incrementing the worksum of his piece by the number on the die when he rolls the die on his turn. e,g, a player’s piece is in the start area, therefore its worksum is 0. On his turn, he rolls the die and gets a 5, the worksum of his piece remains 0. Next time, when his turn comes, he gets a 6 and 3. The worksum of his piece becomes 3. Now each time, his turn comes, his worksum will be incremented by the number he gets when the die is rolled.

6.     Once a piece has completed the circuit of the board it reaches HOME. The player whose piece gets HOME first is the one who wins the game.

We will simulate reaching HOME for a piece by checking if the worksum  of the piece is 56 or not. The player whose worksum becomes 56 first is the winner.
7.     The player must throw the exact number to advance to the HOME area.

This could be simulated by not contributing the number on the die to the worksum of the piece if the worksum exceeds 56.  e.g., if the current worksum of a piece is 53 and the player rolls the die and gets 4, then 4 will not contribute to worksum since it requires exactly 3 to move to HOME area. The worksum in this case will remain 53.
Similarly, if the current worksum is 49 and the player rolls the die and gets a 6 and then a 3 due to extra turn, then 6+3 = 9 will not contribute to worksum since it exceeds 7 needed to move to HOME area. The worksum in this case will remain 53.

8.     If a player's piece lands on a square containing the opponent's piece, the opponent's piece is captured and returns to the starting area. It will again require a 6 to move to the starting square.

The could be simulated by resetting the worksum of the opponent’s piece to 0 and setting the start flag to false, if the number on the die makes the worksum equal to that of the opponent e.g., if the worksums of player 1 and player 2 are 10 and 15 and it is player 1’s turn now. Player 1 rolls the die and gets 5, then the worksum of player 1 will become 15 and that of player 2 will be reset to 0.



Please see the sample output on next page.



Submission Guidelines:
-          Follow the submission instructions from TA.
-          No Late Submissions will be entertained.
-          Copied Assignments will be marked Zero.

Sample Output


Player1:
Please roll the die: 3
worksum = 0

Player2:
Please roll the die: 5
worksum = 0
______________________________________________
Player1:
Please roll the die: 5
worksum = 0

Player2:
Please roll the die: 6
Please roll the die again: 3
worksum = 3
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 5
worksum = 5

Player2:
Please roll the die: 4
worksum = 7
___________________________________________________
Player1:
Please roll the die: 5
worksum = 10

Player2:
Please roll the die: 4
worksum = 11
___________________________________________________
Player1:
Please roll the die: 1
worksum = 11

Player2:
Please roll the die: 2
worksum = 0
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 3
worksum = 20

Player2:
Please roll the die: 4
worksum = 0
___________________________________________________
Player1:
Please roll the die: 1
worksum = 21

Player2:
Please roll the die: 6
Please roll the die again: 4
worksum = 4
___________________________________________________
Player1:
Please roll the die: 3
worksum = 24

Player2:
Please roll the die: 3
worksum = 7
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 2
worksum = 32

Player2:
Please roll the die: 1
worksum = 13
___________________________________________________
Player1:
Please roll the die: 5
worksum = 37

Player2:
Please roll the die: 6
Please roll the die again: 6
Please roll the die again: 2
worksum = 27
___________________________________________________
Player1:
Please roll the die: 3
worksum = 40

Player2:
Please roll the die: 1
worksum = 28
___________________________________________________
Player1:
Please roll the die: 4
worksum = 44

Player2:
Please roll the die: 6
Please roll the die again: 3
worksum = 37
___________________________________________________
Player1:
Please roll the die: 5
worksum = 49

Player2:
Please roll the die: 3
worksum = 40
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 2
worksum = 49

Player2:
Please roll the die: 4
worksum = 44
___________________________________________________
Player1:
Please roll the die: 4
worksum = 53

Player2:
Please roll the die: 4
worksum = 48
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 3
worksum = 53

Player2:
Please roll the die: 6
Please roll the die again: 1
worksum = 55
___________________________________________________
Player1:
Please roll the die: 5
worksum = 53

Player2:
Please roll the die: 3
worksum = 55
___________________________________________________
Player1:
Please roll the die: 3
worksum = 56

*************
Player1 wins.
*************

sample question of c++ 3



                                                                                    Due Date: Nov 25th 2011      
Course: Programming Fundamentals                                                                     Total Marks: 20
                                                                          
Problem 1:                                                                                                                                        (5 points)
Implement a function that takes a number as input from console and returns whether the number is prime or not (it returns 1 if the number is prime and 0 if the number is not prime). Following is function prototype:

int isprime(int);

Write a program that prompts the user to input two numbers. The program calculates the average of prime numbers between these numbers and displays it in the format shown below. The program uses the function isprime included as a header file. Use only what we have learned so far in the class.


Hint: 2 is the first prime number. Please make sure that you take care of this in your program.

Sample Output
Enter first number: 1
Enter second number: 20

The average of prime numbers between 1 and 20 is 9.62

Problem 2:                                                                                                                                        (5 points)
Write a program that prompts the user to input a number. The program determines and prints twin primes till that number in the format shown below. The program uses the function isprime included as a header file. Use only what we have learned so far in the class.

Hint: A pair of prime numbers that differ by 2 (successive odd numbers that are both Prime numbers) is called twin primes. e.g., (3, 5), (5, 7), (11, 13),…

Sample Output
Enter the upper limit: 100

The twin primes from 1 till 100 are:

3, 5
5, 7
11, 13
17, 19
29, 31
41, 43
59, 61
71, 73

Problem 3:                                                                                                                                      (10 points)
Implement another function which takes a number as an argument and prints it as a product of prime numbers. The prototype of the function is:

            void productofprimes(int);

The function productofprimes calls the function isprime to determine the next prime numbers.

Write a program that reads a number from console and prints it as the product of prime numbers. The program uses isprime and productofprimes functions included as a header file. Use only what we have learned so far in the class.

e.g., 90 = 2 x3 x 3 x5
       100 = 2 x 2 x 2 x 5 x 5
Sample Output
Enter a number: 200

200  expressed as product of prime numbers is:
2 x 2 x 2 x 5 x 5


Sample Output
Enter a number: 19

19  expressed as product of prime numbers is:
19




Submission Guidelines:
-          No Late Submissions will be entertained
-          Copied Assignments will be marked Zero.

sample questions of c++ 2



                                                                                                Due Date: Nov 15th 2011      
Course: Programming Fundamentals                                                                     Total Marks: 20
                                                                          

These problems are reasonably complex and for most of the students it will take quite a while before they are able to figure out how to solve them. Therefore, I have given more than 10 days to submit this assignment. I am open to help anyone who puts in a serious effort but still unable to develop a program.

Problem 1:
Write a program that prompts the user to input two numbers. The program calculates the average of prime numbers between these numbers and displays it in the format shown below. Use only what we have learned so far in the class.

Hint: 2 is the first prime number. Please make sure that you take care of this in your program.

Sample Output
Enter first number: 1
Enter second number: 20

The average of prime numbers between 1 and 20 is 9.62


Problem 2:
Write a program that prompts the user to input a number. The program determines and prints twin primes till that number in the format shown below. Use only what we have learned so far in the class.

Hint: A pair of prime numbers that differ by 2 (successive odd numbers that are both Prime numbers) is called twin primes. e.g., (3, 5), (5, 7), (11, 13),…

Sample Output
Enter the upper limit: 100

The twin primes from 1 till 100 are:

3, 5
5, 7
11, 13
17, 19
29, 31
41, 43
59, 61
71, 73


Submission Guidelines:
-          No Late Submissions will be entertained
-          Copied Assignments will be marked Zero.