Menu

Wednesday, 13 May 2015

Chapter 5,Problem b: Let Us C by Yashawant Kanetkar Solution Manual

Problem Statement:

A positive integer is entered through the keyboard, write a
program to obtain the prime factors of the number. Modify the
function suitably to obtain the prime factors recursively.

Solution:

//Ahmad Furqan
//P5.b
#include <iostream>
#include <conio.h>
using namespace std;
bool is_prime(int num)
{
if (num == 2 || num == 3)
return true;
if (num < 2 || num%2==0)  //If number is less than 2 or even its not prime
return false;
//starting from 3 we try dividing by odd numbers,
//since its not even its not divisible by any even number
//we need to go upto num/2
for (int i = 3; i < num / 2; i += 2)
{
if (num%i == 0)
return false;
}
return true;
}
void show_prime_fact_rec(int num)
{
if (is_prime(num)) //if num is prime show it and return since it don't have any factors
{
cout << num << endl;
return;
}
//find smallest prime factor of the number and show it
int i;
for (i = 2; i < num / 2; i++)
{
if (num%i == 0)
{
if (is_prime(i))
break;
}
}
cout << i << endl;
//call function with num divided with lowest prime factor to find other factors
return show_prime_fact_rec(num/i);
}
void main(void)
{
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Prime factors of the number are:" << endl;
show_prime_fact_rec(num);
_getch();
}

Chapter 5,Problem a: Let Us C by Yashawant Kanetkar solution manual

Problem Statement:

A 5-digit positive integer is entered through the keyboard,
write a function to calculate sum of digits of the 5-digit
number:
(1) Without using recursion
(2) Using recursion

Solution:


//Ahmad Furqan
//P5.a
#include <iostream>
#include <conio.h>
using namespace std;
int dig_sum(int num)
{
int sum = 0;
for (int i = 0; i < 5; i++) //5 iterations for each digit
{
sum += num % 10; //remainder of number divided with 10 is the rightmost digit
num = num / 10; //dividing number with 10 eliminates reightmost digit.
}
return sum;
}
int dig_sum_rec(int num)
{
if (num <10)
return num;
//return sum of rightmost digit and sum of digits of number without rightmost digit
return dig_sum_rec(num / 10) + (num % 10);
}
void main(void)
{
int num;
cout << "Enter 5 digit number:";
cin >> num;
cout << "Sum of digits without recursion: " << dig_sum(num);
cout << "\nSum of digits with recursion: " << dig_sum_rec(num);
_getch();
}

Monday, 11 May 2015

Chapter 1, Problem c: Let Us C by Yashawant Kanetkar Solution Manual

Problem Statement:

If the marks obtained by a student in five different subjects
are input through the keyboard, find out the aggregate marks
and percentage marks obtained by the student. Assume that
the maximum marks that can be obtained by a student in each
subject is 100.

Solution:


#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
float s1,s2,s3,s4,s5,tmark,per;
char a;
cout<< "Enter Students marks in five different sunjects separated with semicolon(:)\n";
cin >>s1>>a>>s2>>a>>s3>>a>>s4>>a>>s5;
tmark=s1+s2+s3+s4+s5;
per=tmark/500*100;
cout<<"\nAggregate marks="<<tmark
<<"\npersentage marks="<<per<<'\n';
getch();

}

Chapter 1, Problem b: Lect Us C By Yashawant Kanetkar Solution Manual

Problem Statement:

The distance between two cities (in km.) is input through the
keyboard. Write a program to convert and print this distance
in meters, feet, inches and centimeters.

Solution:

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int km,m,cm;
float inch,foot;
cout<<"Enter Distance between two cities in Kilo Meters:\n";
cin>>km;
m=km*1000;
cm=m*100;
foot=3.28084*m;
inch=12*foot;
cout<<"The Distance is:\n"
<<m<<" meters\n"
<<cm<<" centi meters\n"
<<foot<<" Feet\n"
<<inch<<" inches\n";
getch();
}

Chapter 1, Problem a, Let Us C by Yashawant Kanetkar Solution Manual

Problem Statement:

Ramesh’s basic salary is input through the keyboard. His
dearness allowance is 40% of basic salary, and house rent
allowance is 20% of basic salary. Write a program to calculate
his gross salary.


Solution:


#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int basic_sal,gross_sal;
cout<<"Enter Rimshah's Basic salary:\n";
cin>>basic_sal;
int dear_all=40*basic_sal/100;
int rent_all=20*basic_sal/100;
gross_sal=basic_sal+dear_all+rent_all;
cout<<"Rimshah's Gross Salary is "<<gross_sal<<'\n';
getch();
}



Friday, 5 December 2014

Chapter 7,Problem 6:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//Ahmad Furqan Attari
//P7.6
#include 
#include  //for srand(), rand()
#include  //for time for srand()
#include  //for _getch()
using namespace std;
enum Suit { clubs, diamonds, hearts, spades };
//from 2 to 10 are integers without names
const int jack = 11;
const int queen = 12;
const int king = 13;
const int ace = 14;
////////////////////////////////////////////////////////////////
class card
{
private:
 int number; //2 to 10, jack, queen, king, ace
 Suit suit; //clubs, diamonds, hearts, spades
public:
 card() //constructor
 { }
 void set(int n, Suit s) //set card
 {
  suit = s; number = n;
 }
 void display(); //display card
};
//--------------------------------------------------------------
void card::display() //display the card
{
 if (number >= 2 && number <= 10)
  cout << number;
 else
  switch (number)
 {
  case jack: cout << "J"; break;
  case queen: cout << "Q"; break;
  case king: cout << "K"; break;
  case ace: cout << "A"; break;
 }
 switch (suit)
 {
 case clubs: cout << static_cast(5); break;
 case diamonds: cout << static_cast(4); break;
 case hearts: cout << static_cast(3); break;
 case spades: cout << static_cast(6); break;
 }
}
////////////////////////////////////////////////////////////////
void main(void)
{
 card deck[52];
 int j;
 cout << endl;
 for (j = 0; j<52; j++) //make an ordered deck
 {
  int num = (j % 13) + 2; //cycles through 2 to 14, 4 times
  Suit su = Suit(j / 13); //cycles through 0 to 3, 13 times
  deck[j].set(num, su); //set card
 }
 cout << "\nOrdered deck : \n";
 for (j = 0; j<52; j++) //display ordered deck
 {
  deck[j].display();
  cout << " ";
  if (!((j + 1) % 13)) //newline every 13 cards
   cout << endl;
 }
 srand(time(NULL)); //seed random numbers with time
 for (j = 0; j<52; j++) //for each card in the deck,
 {
  int k = rand() % 52; //pick another card at random
  card temp = deck[j]; //and swap them
  deck[j] = deck[k];
  deck[k] = temp;
 }
 cout << "\nPlayers' Hands : \n";
 cout << "Player 1:";
 for (j = 0; j<52; j++) //display shuffled deck
 {
  deck[j].display();
  cout << ", ";
  if (!((j + 1) % 13)) //newline every 13 cards
  {
   cout << endl;
   if (j<51)
    cout << "Player " << (j + 1) / 13 + 1 << ":";
  }
 }
 _getch();
} //end main

Chapter 7,Problem 5:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//Ahmad Furqan Attari
//p7.5
#include 
#include 
#include 
using namespace std;
const int SIZE = 10;
class fraction
{
private:
 int num, den;
public:
 fraction()
 {}
 fraction(int n, int d) :num(n), den(d)
 {
  lowterms();
 }
 void take_frac(void)
 {
  char ch;
  cin >> num >> ch >> den;
 }
 void show_frac(void)
 {
  cout << setw(2) << num << "/" << setw(2) << den;
 }
 void lowterms(void) // change ourself to lowest terms
 {
  long tnum, tden, temp, gcd;
  tnum = labs(num); // use non-negative copies
  tden = labs(den); // (needs cmath)
  if (tden == 0) // check for n/0
  {
   cout << "Illegal fraction : division by 0"; exit(1);
  }
  else if (tnum == 0) // check for 0/n
  {
   num = 0; den = 1; return;
  }
  // this ‘while’ loop finds the gcd of tnum and tden
  while (tnum != 0)
  {
   if (tnum < tden) // ensure numerator larger
   {
    temp = tnum; tnum = tden; tden = temp;
   } // swap them
   tnum = tnum - tden; // subtract them
  }
  gcd = tden; // this is greatest common divisor
  num = num / gcd; // divide both num and den by gcd
  den = den / gcd; // to reduce frac to lowest terms
 }
 void add_frac(fraction a, fraction b)
 {
  num = a.num*b.den + a.den*b.num;
  den = a.den*b.den;
  lowterms();
 }
 void sub_frac(fraction a, fraction b)
 {
  num = a.num*b.den - a.den*b.num;
  den = a.den*b.den;
  lowterms();
 }
 void mul_frac(fraction a, fraction b)
 {
  num = a.num*b.num;
  den = a.den*b.den;
  lowterms();
 }
 void div_frac(fraction a, fraction b)
 {
  num = a.num*b.den;
  den = a.den*b.num;
  lowterms();
 }
};
void main(void)
{
 fraction f[SIZE],sum,divisor,result;
 char ch;
 int i=0;
 do
 {
  cout << "Enter a fraction: ";
  f[i].take_frac();
  i++;
  cout << "Enter again?(y/n):";
  ch = _getche();
  cout << endl;
 } while (ch == 'y'&& i < SIZE);
 sum = { 0, 1 };
 for (int j = 0; j < i; j++)
 {
  sum.add_frac(sum, f[j]);
 }
 divisor = { 1, i + 1 };
 result.div_frac(sum, divisor);
 cout << "Average of entered fractions:";
 result.show_frac();
 _getch();
}