0% found this document useful (0 votes)
14 views

Worksheet 2

Haramaya University cpp Worksheets

Uploaded by

neguyared837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Worksheet 2

Haramaya University cpp Worksheets

Uploaded by

neguyared837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Haramaya University

College of Computing and Informatics

Department of Computer Science


Computer Programming

Worksheet II

Part1: Looping Statements


Instruction: Choose the most appropriate looping statement to solve the following problems and
write a program based on the selected looping statement
1. Write a program that displays n numbers
a. Starting from zero.
b. Starting from one.
c. Starting from a number entered from the keyboard.
Where n is a positive number to be entered from the keyboard.
2. Write a program that displays the first n positive integer numbers in reversed (descending)
order where n is a positive number to be entered from the keyboard.
3. Write a program that lets a user to enter numbers as long as s/he has entered a positive
number. When the user enters a negative number, the program must display the prompt that
says ‘Now you have entered a negative number’ then after it should be terminated.
4. Write a program that displays the upper and the lower cases of each of the Latin alphabets as
shown in the following example.
Example
Aa Bb Cc Dd . . . Zz.
5. Write a program that computes and displays the sum, the difference and the product of a
range of consecutive numbers where the starting and ending numbers of the range are to be
entered from the keyboard.
6. Write a program that computes and displays the sum of the reciprocals of n positive numbers,
where n is positive integer number to be entered from the keyboard.
Example
1+1/2+1/3+ . . . +1/n.
7. Write a program that computes and displays the square root each of the numbers entered
from the keyboard. Once a number is entered and its square root is displayed, the program
should only continue repeating the same task if the user is willing to do.
8. Write a program that calculates and displays the factorial of n where n is a positive integer
number to be entered from the keyboard.
Example
n! = n * (n-1) * (n-2) * . . . * 2 * 1
1! = 0! = 1
9. Write a program that accepts list of marks of students and counts the number of students who
have scored marks greater than 20 in an exam out of 30. The total number of students who
took the test is to be entered from the keyboard.

1 | Computer Programming: Worksheet II


10. Write a program that displays the maximum and the minimum of a list of numbers entered
from the keyboard. Where the size of the list is to be entered from the keyboard.
11. Write a program that reverses the digits of a given positive integer number entered from the
keyboard.
12. Write a program that displays the multiplication table in a proper output format.
13. Write a C++ program that prints all numbers less than 2000 that are evenly divisible by 10
using:
a) While loop
b) For loop
c) Do –while loop
14. Write a program in C++ to generate a Fibonacci of n numbers, where n is to be defined by a
programmer. ( the series should be: 1 1 2 3 5 8 13 21 32 and son on. A given term in this
series is the summation of the two terms immediately before it.)

2 | Computer Programming: Worksheet II


15. Write separate programs that generate each of the following patterns.

a.
1 g.
12 5
123 454
1234 34543
12345 2345432
b. 123454321
123 2345432
456 34543
789 454
c. 5
12345 h.
2345 abcdefg
345 abcdef
45 abcde
5 abcd
d. abc
12345 ab
1234 a
123 i.
12 a
1 bc
e. de f
543212345 gh i j
5432345 kl mno
54345 pq r stu
545 k.
5 abcdef
545 bcdef
54345 cdef
5432345 def
543212345 ef
f
f. *********
*********
*********
*********

3 | Computer Programming: Worksheet II


Part2: Array, String and Pointer
1. Write an array declaration for the following
A) A list of 100 floating point voltage
B) A list of 100 integer years
C) A list of 30 characters
D) A list of 32 floating-point velocities
2. Write a program to input the following values into an array named volts: 10.95,
16.32, 8.22, 15.98, 26.22, 13.54, 8.22, 6.45, and 173.86. After the data has been entered,
have your program output the values.
3. Write a program to input eight integer numbers into an array named temp. As each
number is input, add the number into a total. After all numbers are input, display
the number and their average.
4. Determine the output produced by the following program
#include <iostream.h>
int main()
{
int i, j,val[3][4]={8,16,9,52,3,15,27,6,14,25,2,10};
for (i=0;i<3;i++)
for(j=0;j<4;j++)
cout<<” “<<val[i][j];
return 0;}
5. Write a c++ program to select the values in a four by five array of integers in
increasing order and store the selected values in a single dimensional array named
sort. Use the following values to initialize the array 16, 22, 99, 4, 18, -258, 4, 101, 5, 98,
105, 6, 15, 2, 45, 33, 88, 72, 16.
6. If test is a variable, what does &test mean?
7. If a variable is declared as a pointer, what must be stored in the variable?
8. Write declaration statement of the following
A) The variable pointed to by a_addr is an integer
B) b_addr is a pointer to a double-precision variable
C) date is a pointer to float
D) pt_char is a pointer to a character
9. Replace each of the following reference to a subscripted variable with a pointer
reference
A) prices[5] C) temp[20]
B) mile[0] D) test[a+b] where a=5, b=4
10. Replace each of the following references using a pointer with an array representation
A) *(message+6) C) *amount
B) *(stock+2)
11. Introduce int variables x and y and int pointer variables p and q. Set x to 2, y to 8, p
to the address of x, and q to the address of y. Then print the following information:
A) The address of x and the value of x.
B) The value of p and the value of *p.

4 | Computer Programming: Worksheet II


C) The address of y and the value of y.
D) The value of q and the value of *q.
E) The address of p (not its contents!).
F) The address of q (not its contents!).
12. What is the output of the following program?

13. What is the output of the following program?

14. What is the output of the following program?

15. correct the following code so that it should print the elements of the array, using
pointer arithmetic:

5 | Computer Programming: Worksheet II


Part3: Function:
1. Write a function named mult() that accepts two floating-point numbers as a
parameters, multiplies these two numbers and display the result.
2. Write a function that accepts a year as a user input and returns a one if the year is a
leap year or a zero if it is not.
3. Write a program that can be used to play a number guessing game. The player is
first asked for the number of games he wishes to play. Each game gives the player a
maximum of three chances to guess a number chosen randomly. If the player makes
a correct guess, he is given a score depending on the number of guesses he has made
(10, 8, or 5). The final score is given as a percentage with some complementary
remarks.
4. Consider the following program:
int foo(int x)
{
static int k = x;
if (x == 0)
return 0;
if (x < 0)
return k + foo(-x);
else {
int y = foo(x-1);
return k + y + 1;
}
}
What will this program compute?
5. Consider the following code fragment
# include <iostream.h>
char key; int o,p;
long int number; float q;
int main() …
{ return p;
int a,b,c; }
double x,y; double func2( float fisrt, float last)
… {
return 0; int a,b,c,o,p;
} floar r;
double secnum; double s,t,x;
int func1(int num1, int num2) …
{ return s*t; }
Draw a box around the appropriate section of the above code to enclose the
scope of the variables key, secnum, y, and r.
6. Describe the difference between a local auto variable and a local static variable.
7. Given the following code:

6 | Computer Programming: Worksheet II


int sum(int 0)
{
if(n==0)
return 0;
else
return(n+sum(n-1));
}
a) show the terminating condition
b) show the recursive part
c) hand trace this peace of code for n=4 and find the output.
8. Write a recursive program that
a. Checks whether a given word is palindrome. A word is plaindrom if it
has same spelling when we read it from left to right and vice versa (e.g
noon)
b. Checks whether two one dimensional arrays of the same size are identical
(have the same elements)
9. Determine the values displayed by each cout statement in the following program:
#include <iostream.h>
int firstnum=20;
void display(void);
int main()
{
int firstnum =20;
cout<<”\nthe value of firstnum is<<firstnum<<endl;
display();
return 0;
}
void display(void)
{
cout<<”the value of firstnum is now<<endl;
return;
}
10. Write a modular program that reads a list of marks and display their range,
mean, median.
Suggested functions and their prototypes:
Void readList(float myList[], unsigned & maxNo);
Float findMax(const float myList [] , unsigned maxNo);
Float findMin(const float myList [] , unsigned maxNo);
Float findRange(const float myList [] , unsigned maxNo);
Float findmean(const float myList [] , unsigned num);
Void display(const float myList [] , unsigned maxNo);

7 | Computer Programming: Worksheet II


Part4: Structures
1. Write a functions InputData and DisplayData that reads and display the elements of
a record(structure). Use the student record defined below.
struct Student{
char name[20];
char sex;
int age;
};
2. Use the functions in problem number 1 to read and display a list of students record.
Modify the display function so that it displays the list in a tabular form. Add
another function to your program in problem number 2 which can display only the
list of female or male students depending on the user choice.
3. A student record consist of his id number, a list of marks, average mark, and his
rank.
struct Student{
unsigned id;
float marks[5];
float average;
unsigned rank;
};
use the above structure to write a program that can read the inputs (id number
and list of marks) for the students in a class and calculate the average mark for
each student. Your program should finally give the rank for each student. Write
a function that displays the list of students in the order of their rank
4. Write a program which defines a new structure type to contain the following
information about towns: the name (a sequence of characters); the population (an
integer); and a Boolean value (i.e. true or false) which indicates whether the town
has an airport or not. Use the typedef statement to define your own Boolean data
type -remember that true in C++ is equivalent to the integer value 1, and false is
equivalent to 0.
5. Declare a single structure data type suitable for an employee record of the type
illustrated below:
Number Name Rate Hours
3462 JONES 4.62 40
6793 Robbins 5.83 38
7834 Swain 6.89 40
9002 Williams 4.75 42
Using the data type declared above, write a C++ program that interactively
accepts the above data into an array of six structures. Once the data have been
entered, the program should create a payroll report listing each employee’s
name, number, and gross pay. Include the total gross pay of all employees at the
end of the report.

8 | Computer Programming: Worksheet II

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy