<< Chapter < Page Chapter >> Page >

2.4) Write a C++ function that checks if a square matrix with order of n is symmetric or not. And then write a C++ program that inputs a square matrix with order of n and then checks if it is symmetric or not.

2.5) A palindrome is a string that reads the same both forward and backward. Some examples are

Given the following function that returns true if the parameter string is a palindrome or false , otherwise.

bool palindrome(char strg[])

{

int len, k, j;

len = strlen(strg);

k = len/2;

j = 0;

bool palin = true;

while ( j<k&&palin)

if (strg[j] != strg[len -1-j])

palin = false;

else

++ j;

return (palin)

}

Write a C++ program that reads several strings, one at a time and checks if each string is a palindrome or not. Use a while loop in your program. The loop terminates when the user enters a string starting with a ‘*’.

2.6) Write a boolean function that can checks if all the elements in integer array are unique (i.e. we can not find any pair of elements that are equal to each other).

Write a C++ program that inputs an integer array and invokes the function to check if all the elements in integer array are unique.

2.7) Given the following formula for computing the number of combinations of m things out of n, denote C(n, m).

C(n, m) = 1 if m = 0 or m=n

C(n, m) = C(n-1, m) + C(n-1, m-1) if 0<m<n

  1. Write a recursive function to compute C(n,m).
  2. Write a complete C++ program that reads two integers N and M and invokes the function to compute C(N,M) and prints the result out.

2.8) The greatest common divisor of two positive integers is the largest integer that is a divisor of both of them. For example, 3 is the greatest common divisor of 6 and 15, and 1 is the greatest common divisor of 15 and 22. Here is a recursive function that computes the greatest common divisor of two positive integers:

int gcd(int p, int q)

{

int r ;

if (( r = p%q == 0)

return q ;

else

return gcd(q, r) ;

}

a. First write a C++ program to test the function.

b. Write and test an equivalent iterative function.

Lab session 7: pointers

1. objective

The objectives of Lab session 7 are to get familiar with how to use pointers in C++.

2. experiment

2.1) Run the following program to determine the size of two data types long and byte.

# include<iostream.h>

void main()

{

byte* a;

long* b;

cout<<sizeof(a)<<endl;

cout<<sizeof(b)<<endl;

}

2.2) Given the following code segment:

float pay;

float *ptr_pay;

pay=2313.54;

ptr_pay =&pay;

determine the values of the following expressions:

a. pay

b. *ptr_pay

c. *pay

d.&pay

2.3) Read for understanding the following program:

#include<iostream.h>

void main()

{

int a;

int *aPtr; // aPtr is a pointer to an integer

a = 7;

aPtr =&a; //aPtr set to address of a

cout<<“The address of a is “<<&a

<<“\nThe value of aPtr is “<<aPtr;

cout<<“\n\nThe value of a is “<<a

<<“\nThe value of *aPtr is “<<*aPtr

<<endl;

}

Run the program and explain the output.

2.4) Given the following array and pointer declarations:

int ara[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int *ip1, *ip2;

Determine which of the following assignments are valid.

a. ip1 = ara;

b. ip2 = ip1 =&ara[3];

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask