Assignment 2
Assignment 2
Inst
Instructions:
a. You are required to submit this assignment in both soft and hard form. Your
hard copy will be hand-written.
b. Soft copy of your assignment will consist of a single PDF document
containing images of the hand-written assignment (Use CamScanner app
from PlayStore for this purpose)
c. Your assignment will be marked on the basis of your code-understanding,
get prepared to present any program in the coming lab after due date.
d. Your assignments are lengthy, try to divide your work on all days, otherwise
it would be hard to complete in a single day.
Assignment Tasks
Task #1
Write a C++ program which takes user input into an integer array of size 10, it then prints the
sum of all elements in an array.
Example:
Input:
Arr[10] = {3, 4, 1, 7, 6, 2, 8, 7, 1, 9 }
Output: 48
Task #2
Write a C++ program which takes user input into an integer array of size 9, it then checks
whether that array’s values are a palindrome or not. And prints a message accordingly.
Example 1:
Input: arr = { 1, 2, 3, 4, 5, 4, 3, 2, 1}
Input: arr = { 4, 2, 8, 4, 5, 4, 3, 2, 1}
Task #3
Write a C++ Program which takes input from user into an integer array of size 10, it then takes
user input into two integers ‘var1’ and ‘var2’.
1. Check if these values are present in array, if either of them aren’t then prompt user to
input var1 and var2 again.
2. After this, swap the indices where ‘var1’ and ‘var2’ values are present in the array.
3. print the whole array to show.
Example 1:
Input:
arr= {1,2,3,4,5,6,7,8,9,10}
var1=1, var2=4
Output:
arr= {4,2,3,1,5,6,7,8,9,10}
Task #4
Write a C++ program which takes input from user into an integer array of size 8, it then finds the
top 3 largest numbers in that array, and it prints the sum of them.
Example:
Input:
Arr = {4, 8, 5, 2, 9, 1, 3, 4}
Output:
Sum = 22
Task #5
Write a C++ program which takes input from user into an integer array of size 9, it then finds the
first 3 minimum integers in that array, and it prints the sum of it.
Example:
Output:
Sum = 7
Task #6
Write a C++ program which takes input from user into a float array of size 5, it then finds the
average of float array values and prints the closely related value in the array and its index.
Example:
Input:
Output:
Value = 3.5
Index = 2
Explanation:
Average of the given array is 3.4, and most closely related value to the average is
3.5 on index 2.
Task #7
Write a C++ program which takes input from user into an integer array of size 8, it then finds the
unique values in that array and prints them, you must not print the duplicates.
Example:
Input:
Arr3 = {1, 1, 2, 3, 4, 5, 5, 6}
Output:
2, 3, 4, 6
Task #8
Write a C++ program which takes input from user into an integer array of size 7.
• Check those value divisible by 7, store sums of quotients in ‘sum7’.
• Check those value divisible by 3 store sums of quotients in ‘sum3’.
• After this, sum of all other values in ‘sumRest’.
• Add all the sum values and print the final Total Value.
Example:
Input:
Array= {7,14,21,2,5,4,6}
Output:
Sum of rest = 11
6 + 9 + 11 = 26
Task #9
Write a C++ program which takes input from user into an integer array of size 8, takes user input
of integer varible ‘index’. The program then:-
• prints the sum of indices before that ‘index’
• prints the product of indices after that ‘index’
Example:
Input:
index = 5
Output:
Sum = 20
Product = 630
Task #10
Write a C++ program which takes input from user into an integer array of size 12, You need to
print the product of those elements with values ranging from 50-60, and sum of all other
elements of array. You are restricted to use only one for-loop after input.
Example:
Input:
Arr= {1,2,3,45,56,67,76,55,69,59,81,60}
Output:
Task #11
Write a C++ program which takes input from user into an integer array of size 12. Now take
another integer input from the user in variable ‘n’. Now find all the elements in the array which
are complete multiplies of ‘n’ and print their sum.
Example:
Input:
Arr= {1,2,3,45,56,67,76,55,69,59,81,60}
n = 3
Output:
Task #12
Write a C++ program in which you input marks as value for 15 students and depending on the
marks value, print stars accordingly in front of the roll number.
[Note: Marks cannot exceed 5 as array values.]
[Note: This example uses only 3 students for demo, but you must use 15 for your solution.]
Example:
Input:
Students[0] = 3
Students[1] = 2
Students[2] = 5
Output:
#0 marks = ***
#1 marks = **
#2 marks = *****
Task #13
Write a C++ program which takes input from user into an integer array of size >= 5. It then
prints the product of all elements in an array.
Example:
Input:
Arr = {2, 5, 2, 7, 1}
Output:
140
Task #14
Write a C++ program that takes 10 integer inputs from the user and stores it in an array. After
that your program takes two more inputs from the user,
1. any index of the array in variable ‘ind’
2. a new integer number in variable ‘n’
Now your program replaces the old value in array on index ‘ind’ with new taken value in ‘n’
Example:
Input:
ind = 3
n = 11
Output:
Task #15
In a C++ program take two arrays Arr1 & Arr2, both of size 10. Take input for both the arrays in
a single for loop. After that take first value from Arr2 and subtract it from first value of Arr1, and
repeat the process till last index.
• Value of Arr2[0] from value of Arr1[0]
• Value of Arr2[1] from value of Arr1[1]
• .
• .
• .
• Value of Arr2[9] from value of Arr1[9]
Example:
Input:
Arr2 = {2, 3, 4, 5, 6, 7, 8, 8, 9, 7}
Output:
1, 2, 3, 4, 5, 6, 7, 0, 0, 3