0% found this document useful (0 votes)
65 views23 pages

Robert Lafore, Chapter-7, Arrays and String

This solved problems of exercise are from Robert Lafore's C++ book. The file contains the source file in word document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views23 pages

Robert Lafore, Chapter-7, Arrays and String

This solved problems of exercise are from Robert Lafore's C++ book. The file contains the source file in word document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Chapter -7

Arrays and String


Robert Lafore C++

Ans to the question no -1


The code segment:
#include<bits/stdc++.h>
using namespace std;
void reverseit(char str[],int l)
{
int i=0,j=l-1;
char temp;
for(i=0;i<l/2;i++,j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
int main()
{
char str[10000];
cout<<"Enter the string: ";
cin.get(str,10000);
int d=strlen(str);
reverseit(str,d);
cout<<"The reversed string: "<<str<<endl;
}
Ans to the question -2
The code segment:
#include<bits/stdc++.h>
using namespace std;
class employee
{
private:
string name;
long long int employee_no;
public:
employee():name("No Name Yet"),employee_no(0){};
void getdata()
{
cout<<"Enter the Name: ";
cin>>name;
cout<<"Enter the Employee number: ";
cin>>employee_no;
cout<<endl;
}
void putdata()
{
cout<<"\tName: "<<name<<setw(20)<<"\tEmployee no: "<<employee_no<<endl;
}
};
int main()
{
employee arr[100];
cout<<"Inut array length : ";
int n,i;
cin>>n;
for(i=0;i<n;i++)
{
arr[i].getdata();
}
for(i=0;i<n;i++)
{
cout<<"Employee "<<i+1<<" : ";
arr[i].putdata();
}

Ans to the question no – 3


The code segment:

#include <bits/stdc++.h>
using namespace std;

class Distance{
int feet;
float inches;
public:
Distance(): feet(0), inches(0) { };
Distance(int f, int i): feet(f), inches(i) { };
void getdist();
void showdist() const;
void add_dist(Distance d);
void div_dist(int k);
};
void Distance::getdist(){
cout << "Enter feet: ";
cin >> feet;
cout << "\nEnter inches: ";
cin >> inches;
}

void Distance::showdist() const{


cout << feet << "\'-" << inches << '\"' << endl;
}

void Distance::add_dist(Distance d){


feet += d.feet;
inches += d.inches;
if(inches > 11){
inches -= 12;
feet++;
}
}

void Distance::div_dist(int k){


int total = feet * 12 + inches;
total /= k;
feet = total / 12;
inches = total % 12;
}

int main(){
const int MAX = 100;
Distance d[MAX], sum;
int n = 0;
char ch;
do{
d[n].getdist();
sum.add_dist(d[n++]);
cout << "Want to continue (y/n): ";
cin >> ch;
cout << endl;
} while(ch != 'n');
sum.div_dist(n);
sum.showdist();
return 0;
}

Ans to the question no- 4


The code segment:

#include<bits/stdc++.h>
using namespace std;

int maxint(int *a, int n){


int maxi = 0, index;
for(int i = 0; i < n; i++){
if(a[i] > maxi)
{
maxi = a[i];
index = i;
}
}
return index;
}

int main(){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
int index = maxint(a, n);
cout << a[index] << " at " << index;
return 0;
}

Ans to the question no – 5


The code segment:

#include<bits/stdc++.h>
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();
}
};

int main()
{
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):";
cin >> ch;
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();
return 0;
}

Ans to the question no – 6


The code segment:
#include<bits/stdc++.h>
using namespace std;
enum Suit {clubs, diamonds, hearts, spades};
const int jack = 11;
const int queen = 12;
const int king = 13;
const int ace = 14;
class card{
private:
int number;
Suit suit;
public:
card() {};
void set(int n, Suit s);
void display(void);
};
void card::set(int n, Suit s){
suit = s;
number = n;
}
void card::display(void){
cout << setw(4);
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 << "♣"; break;

case diamonds: cout << "♦"; break;

case hearts: cout << "♥"; break;

case spades: cout << "♠"; break;


}
}

int main(){
card deck[52];
int j;
for(int j = 0; j < 52; j++){
int num = (j % 13) + 2;
Suit su = Suit(j / 13);
deck[j].set(num, su);
}
srand(time(NULL));
for(int j = 0; j < 52; j++){
int k = rand() % 52;
card temp = deck[j];
deck[j] = deck[k];
deck[k] = temp;
}
const int PLAYERS = 4;
const int CARDS_OF_PLAYER = 52 / 4;
card bridge[PLAYERS][CARDS_OF_PLAYER];
for(int i = 0; i < PLAYERS; i++)
for(int j = 0; j < CARDS_OF_PLAYER; j++)
bridge[i][j] = deck[i * CARDS_OF_PLAYER + j];
for(int i = 0; i < PLAYERS; i++){
cout << "Player No." << i + 1 << ": ";
for(int j = 0; j < CARDS_OF_PLAYER; j++)
bridge[i][j].display();
cout << endl;
}
return 0;
}

Ans to the question no – 7


The code segment:
#include<bits/stdc++.h>
using namespace std;
long double mstold(char s[]);
long double mstold(char s[])
{
long double sum = 0;
int p;
for(int i = 1; i < strlen(s); i++)
{
if(48 <= static_cast<int>(s[i]) && static_cast<int>(s[i]) <= 57)
sum = sum * 10 + static_cast<int>(s[i]) - 48;
else if(s[i] == '.')
p = i;
}
long double frac_part = pow(10, strlen(s) - 1 - p);
sum /= frac_part;
return sum;
}
int main()
{
const int MAX = 100;
char ch, s[MAX];
do
{
cout << "Enter the amount of money: ";
cin >> s;
long double lds = mstold(s);
cout << "Amount equal to: " << lds << endl;
cout << "Continue? (y/n): ";
cin >> ch;
cout << endl;
}
while(ch != 'n');
return 0;
}
Ans to the question no 8
The code segment:
#include<bits/stdc++.h>
using namespace std;
class safearay
{
private:
static const int LIMIT = 100;
int a[LIMIT];
public:
void putel(int index, int value);
int getel(int index);
};
void safearay::putel(int index, int value)
{
if(0 <= index && index <= LIMIT - 1)
a[index] = value;
else
cout << "The index is outOfBounds!\n";
}

int safearay::getel(int index)


{
int temp;
if(0 <= index && index <= LIMIT - 1)
{
temp = a[index];
cout << "Item No." << index << " is equal to " << temp << endl;
}
else
cout << "The index is outOfBounds!\n";
return temp;
}
int main()
{
safearay m;
char ch;
do
{
cout << "Enter \"p\" to enter an array element and \"g\" for conclusion: ";
char sym;
cin >> sym;
switch(sym)
{
case 'p':
int i, v;
cout << "Enter index and value: ";
cin >> i >> v;
m.putel(i, v);
break;
case 'g':
int j, temp;
cout << "Enter an array index: ";
cin >> j;
temp = m.getel(j);
break;
default:
cout << "Input Error!\n";
}
cout << "Continue? (y/n): ";
cin >> ch;
cout << endl;
}
while(ch != 'n');
return 0;
}
Ans to the question no – 9
The code segment:
#include<bits/stdc++.h>
using namespace std;
class Queue
{
private:
static const int MAX = 100;
int a[MAX];
int head, tail;
public:
Queue() {head = 0; tail = 0;}
void put(int value){
a[tail++ % 100] = value;
}
int get(void){
return a[(100 - head--) % 100];
}
};
int main()
{
Queue q;
q.put(11);
q.put(22);
cout << "1: " << q.get() << endl;
cout << "2: " << q.get() << endl;
q.put(33);
q.put(44);
q.put(55);
q.put(66);
cout << "3: " << q.get() << endl;
cout << "4: " << q.get() << endl;
cout << "5: " << q.get() << endl;
cout << "6: " << q.get() << endl;
return 0;
}

Ans the question no – 10


The code segment:
#include<bits/stdc++.h>
using namespace std;
class matrix
{
private:
static const int LIMIT = 10;
int a[LIMIT][LIMIT];
public:
void putel(int index1, int index2, int value);
int getel(int index1, int index2);
};
void matrix::putel(int index1, int index2, int value)
{
if(0 <= index1 && index1 <= LIMIT - 1 && 0 <= index2 && index2 <= LIMIT - 1)
a[index1][index2] = value;
else
cout << "The index is outOfBounds!\n";
}
int matrix::getel(int index1, int index2)
{
int temp;
if(0 <= index1 && index1 <= LIMIT - 1 && 0 <= index2 && index2 <= LIMIT - 1)
{
temp = a[index1][index2];
cout << "[" << index1 << ", " << index2 << "] = " << temp << endl;
}
else
cout << "The index is outOfBounds!\n";
return temp;
}
int main()
{
matrix m;
char ch;
do
{
cout << "Enter \"p\" to enter an array element and \"g\" for conclusion: ";
char sym;
cin >> sym;
switch(sym)
{
case 'p':
int i1, i2, v;
cout << "Enter index and value: ";
cin >> i1 >> i2 >> v;
m.putel(i1, i2, v);
break;
case 'g':
int j1, j2, temp;
cout << "Enter an array index: ";
cin >> j1 >> j2;
temp = m.getel(j1, j2);
break;
default:
cout << "Input Error!\n";
}
cout << "Continue? (y/n): ";
cin >> ch;
cout << endl;
} while(ch != 'n');
return 0;
}

Ans to the question no – 11


The code segment:
#include<bits/stdc++.h>
using namespace std;
string ldtoms(long double sum);
string ldtoms(long double sum)
{
stringstream ss (stringstream::in | stringstream::out);
ss.setf(ios::fixed);
ss << setprecision(2) << sum;
string s = ss.str();
s.insert(0, "$");
int p = s.find('.') - 1, n = 0;
for(int i = p; i > 0; i--)
if(++n % 3 == 0)
s.insert(i, ",");
return s;
}
int main()
{
long double sum;
char ch;
do
{
cout << "Enter the amount of money: ";
cin >> sum;
string mss = ldtoms(sum);
cout << "Amount equal to " << mss << endl;
cout << "Continue? (y/n): ";
cin >> ch;
cout << endl;
} while(ch != 'n');
return 0;
}

Ans to the question no – 12


The code segment:
#include<bits/stdc++.h>
using namespace std;
long double mstold(string s);
string ldtoms(long double sum);
class bMoney{
private:
long double money;
public:
bMoney() {money = 0;}
bMoney(long double m) {money = m;};
void madd(bMoney b1, bMoney b2);
void getmoney(void);
void putmoney(void);
};
void bMoney::madd(bMoney b1, bMoney b2){
money = b1.money + b2.money;
}
void bMoney::getmoney(void){
string s;
cin >> s;
money = mstold(s);
}
void bMoney::putmoney(void){
string s = ldtoms(money);
cout << "Amount is equal " << s << endl;
}
long double mstold(string s){
long double sum = 0;
int p;
for(int i = 1; i < s.size(); i++){
if(48 <= static_cast<int>(s[i]) && static_cast<int>(s[i]) <= 57)
sum = sum * 10 + static_cast<int>(s[i]) - 48;
else if(s[i] == '.')
p = i;
}
long double frac_part = pow(10, s.size() - 1 - p);
sum /= frac_part;
return sum;
}
string ldtoms(long double sum){
stringstream ss (stringstream::in | stringstream::out);
ss.setf(ios::fixed);
ss << setprecision(2) << sum;
string s = ss.str();
s.insert(0, "$");
int p = s.find('.') - 1, n = 0;
for(int i = p; i > 0; i--)
if(++n % 3 == 0)
s.insert(i, ",");
return s;
}
int main(){
bMoney b, sum;
char ch;
do{
cout << "Enter the amount of money: ";
b.getmoney();
sum.madd(sum, b);
cout << "Continue? (y/n): ";
cin >> ch;
cout << endl;
} while(ch != 'n');
sum.putmoney();
return 0;
}

You might also like