<< Chapter < Page Chapter >> Page >

2.6) Write a C++ program to compute the sum and average of N single-precision floating point numbers entered by the user. The value of N is also entered by the user.

2.7) The value of Euler constant, e, is approximated by the following series:

e = 1 + 1/1! +1/2! + 1/3! + 1/4! + 1/5! + …

Using this series, write a C++ program to compute e approximately. Use while structure that terminates when the difference between two successive approximations becomes less than 1.0E-6.

2.8) Write a C++ program to calculate and display the amount of money accumulated in a saving bank account at the end of each year for 10 years when depositing 1000$ in the bank. The program has to display the amount of money when interest rates change from 6% to 12% with the increment 1%. Thus, you should use two nested loops: the outer loop iterates according to interest rates and the inner loop iterates according to the years.

2.9) Write a C++ program that inputs a positive integer n and lists out n first prime numbers.

2.10) Write a C++ program to display an equilateral triangle with the height h (h is entered from the user). Example: h = 4

Lab session 4: arrays

1. objective

The objective of Lab session 4 is to get familiar with arrays, one-dimensional and two-dimensional arrays.

2. experiment

2.1) Test the following program.

#include<iostream.h>

const int NUM = 8;

void main()

{

int nums[NUM];

int total = 0; // Holds total of user’s eight numbers.

int ctr;

for (ctr=0; ctr<NUM; ctr++)

{

cout<<“Please enter the next number...”;

cin>>nums[ctr];

total += nums[ctr];

}

cout<<“The total of the numbers is “<<total<<“\n”;

return;

}

2.2) If the array weights is declared as in the following statement, then what the value of weights[5] is ?

int weights[10] = {5, 2, 4};

2.3) Given the statement:

char teams[] = {‘E’,’a’,’g’,’l’,’e’,’s’,’\0', ’R’, ‘a’,’m’,’s’,’\0'};

which of the following statement is valid?

a. cout<<teams;

b. cout<<teams+7;

c. cout<<(teams+3);

d. cout<<teams[0];

e. cout<<(teams+0)[0];

f. cout<<(teams+5);

2.4) Given the array declaration:

int grades[3][5]= {80,90,96,73,65,67,90,68,92,84,70, 55,95,78,100};

Determine the value of the following subscripted variables:

a. grades[2][3]

b. grades[2][4]

c. grades[0][1]

2.5) Write a C++ program that inputs an array consisting of n single-precision floating point numbers and finds the smallest element in the array. (n is an integer that is entered by the user).

2.6) Write a C++ program that inputs an integer array and finds the last element in the array.

2.7) Write a C++ program that inputs an integer array and then inserts the integer value X in the first position in the array.

2.8) Write a C++ program that inputs an integer array and checks if all the elements in the array are unique (i.e. we can not find any pair of elements that are equal to each other).

2.9) Write a C++ program that inputs a matrix and then transposes it and displays the transposed matrix. Transposing a square matrix is to swap:

a(i,j)<==>a(j,i) for all i, j

For example, given the matrix

After transposing it, it becomes as follows:

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