0% found this document useful (0 votes)
106 views6 pages

Assignment #4v PF

BSCS Assignment

Uploaded by

habiti747
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)
106 views6 pages

Assignment #4v PF

BSCS Assignment

Uploaded by

habiti747
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/ 6

Programming Fundamentals

Assignment no 4
Instructions:
Solve all questions and make a word document in which you have to add screenshots of program code
and output.
Note: Must add two programs in single page

Deadline: 23 April 2024 – time 9pm

Arrays
1. Write a program to print sum, average of all numbers, smallest and largest element of an array.
2. Take an array of 10 elements. Split it into middle and store the elements in two different arrays.
E.g.-
INITIAL array:
58 24 13 15 63 9 8 81 1 78
After splitting :
58 24 13 15 63
9 8 81 1 78
3. Write a program to shift every element of an array to circularly right. E.g.-
INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4
4. Find the largest number among 10 numbers entered by user from 1-1000,using Arrays ,write the
complete code in c++ and compile its output?
5. Sorting refers to arranging data in a particular format. Sort an array of integers in ascending order.
One of the algorithm is selection sort. Use below explanation of selection sort to do this.
INITIAL ARRAY :
2 3 1 45 15
First iteration : Compare every element after first element with first element and if it
is larger then swap. In first iteration, 2 is larger than 1. So, swap it.
1 3 2 45 15
Second iteration : Compare every element after second element with second element
and if it is larger then swap. In second iteration, 3 is larger than 2. So, swap it.
1 2 3 45 15
Third iteration : Nothing will swap as 3 is smaller than every element after it.
1 2 3 45 15
Fourth iteration : Compare every element after fourth element with fourth element
and if it is larger then swap. In fourth iteration, 45 is larger than 15. So, swap it.
1 2 3 15 45

Functions
6. Define two functions to print the maximum and the minimum number respectively among three
numbers entered by user.
7. Write a program which will ask the user to enter his/her marks (out of 100). Define a function that
will display grades according to the marks entered as below:
Marks Grade
91-100 AA
81-90 AB
71-80 BB
61-70 BC
51-60 CD
41-50 DD
<=40 Fail
8. Write a program to print the factorial of a number by defining a function named 'Factorial'.
Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 0
9. Using recursion, define a function to know nth term of a Fibonacci series.
Nth term of Fibonacci series is
F(n) = F(n-1)+F(n-2)
F(0) = 0
F(1) = 1
F(2) = F(1)+F(0) = 1+0 = 1
F(3) = F(2)+F(1) = 1+1 = 2
F(4) = F(3)+F(2) = 2+1 = 3
10. Write a program that takes as input your gross salary and your total saving and uses another
function named taxCalculator() to calculate your tax. The taxCalculator() function takes as
parameters the gross salary as well as the total savings amount. The tax is calculated as follows:
a) The savings is deducted from the gross income to calculate the taxable income. Maximum
deduction of savings can be Rs. 100,000, even though the amount can be more than this.
b) For up to 100,000 as taxable income the tax is 0 (Slab 0); beyond 100,000 to 200,000 tax is
10% of the difference above 100,000 (Slab 1); beyond 200,000 up to 500,000 the net tax is
the tax calculated from Slab 0 and Slab 1 and then 20% of the taxable income exceeding
200,000 (Slab 2); if its more than 500,000, then the tax is tax from Slab 0, Slab 1, Slab 2 and
30% of the amount exceeding 500,000.
11. Write a C++ program to find the second largest element in an array of integers using function
named “SecLar”.
12. Write a C++ program to find the most frequent element in an array of integers.
13. Write a C++ program to update every array element by multiplication of the next and previous
values of a given array of integers.
14. Write a C++ program to sort (in descending order) an array of distinct elements according to the
absolute difference of array elements and with a given value.
15. Write a C++ program to find the two repeating elements in a given array of integers.

Loops
16. Write a program in C++ to find the sum of the series 1 + 1/2^2 + 1/3^3 + ..+ 1/n^n.
Sample Output:
Input the value for nth term: 5
1/1^1 = 1
1/2^2 = 0.25
1/3^3 = 0.037037
1/4^4 = 0.00390625
1/5^5 = 0.00032
The sum of the above series is: 1.29126
17. Write a program in C++ to calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... +
(1+2+3+4+...+n).
Sample Output:
Input the value for nth term: 5
1=1
1+2 = 3
1+2+3 = 6
1+2+3+4 = 10
1+2+3+4+5 = 15
The sum of the above series is: 35
18. Write a C++ program that asks the user to enter positive integers in order to process count,
maximum, minimum, and average or terminate the process with -1.
Sample Output:
Your input is for termination. Here is the result below:
Number of positive integers is: 4
The maximum value is: 9
The minimum value is: 3
The average is 6.00
19. Write a program in C++ to print a square pattern with the # character. (nested loop)
Sample Output:
Print a pattern like square with # character:
--------------------------------------------------
Input the number of characters for a side: 4
####
####
####
####
20. Write a program in C++ to display the multiplication table vertically from 1 to n. (nested loop)
Sample Output:
Input the number upto: 5
Multiplication table from 1 to 5
1x1=1 2x1=2 3x1=3 4x1=4 5x1=5
1x2=2 2x2=4 3x2=6 4x2=8 5x2=10
1x3=3 2x3=6 3x3=9 4x3=12 5x3=15
1x4=4 2x4=8 3x4=12 4x4=16 5x4=20
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45
1x10=10 2x10=20 3x10=30 4x10=40 5x10=50
21. Write a C++ program to display a pattern like a diamond.
Sample Output:
Input number of rows (half of the diamond): 5

*
***
*****
*******
*********
*******
*****
***
*
22. Write a C++ program to create an array and get number from user, then display the smallest
number from the array using a function.
23. Write a program in C++ to display such a pattern for n number of rows using numbers. There will
be odd numbers in each row. The first and last number of each row will be 1 and the middle
column will be the row number. N numbers of columns will appear in the 1st row.
Sample Output:
Input number of rows: 7
1234567654321
12345654321
123454321
1234321
12321
121
1
24. Write a C++ program that prints all ASCII characters with their values.
Sample Output:
Input the starting value for ASCII characters: 65
Input the ending value for ASCII characters: 75
The ASCII characters:
65 --> A
66 --> B
67 --> C
68 --> D
69 --> E
70 --> F
71 --> G
72 --> H
73 --> I
74 --> J
75 --> K
25. Write a C++ program to convert a binary number to a decimal number.
Sample Output:
Input a binary number: 1011
The decimal number: 11

Note: Must practice the questions that are given in slides,


assignments and quizzes for proper exam preparation.

You might also like