Arr (5) : (1, 1, 2, 2, 2) Output: Sum (5) : (2, 3, 0, 0, 0) Example: Arr (5) : (3, 3, 1, 2, 5) Output: Sum (5) : (1, 1, 2, 0, 1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Assignment_05

CS1002-Programming Fundamentals
Fall-2021

Opening Date: 8-Dec-2021 Date of Submission: 14-Dec-2021, 5:00 PM

Q1. Write a program to find the count of elements repeating in an array, store the count of those
elements in another array respective of the element index number. Assume that elements in the
first array have values >=1 and <=10 and it has 200 elements in total. Also print the histogram of
the elements showing the frequency of each number.

Example with an array of five elements:


Arr[5]: [1, 1, 2, 2, 2]
Output:
Sum[5]: [2, 3, 0, 0, 0]
Example:
Arr[5]: [3, 3, 1, 2, 5]
Output:
Sum[5]: [1, 1, 2, 0, 1]

Q2. Write a program to update every element in an array with the multiplication of the second next
and second previous values of a given array of integers. If you come across an index where the
second next value doesn’t exist, then start from the beginning of the array. Similarly, if you come
across an index where the second previous value doesn’t exit, then take the value from the last
index of array.

Example:
Arr1[6]: [1,2,3,4,5,6]
Output:
Arr2[6]: [3x5, 4x6, 5x1, 2x6, 1x3, 2x4]

Q3. An array has N integers that it reads from a file. It then encrypts the read numbers by replacing
the number with the sum of the remaining numbers in the array. These encrypted numbers are
output to another file.
First line of the file contains the number of integers that this file contains. This number will always
be less than 100. Then the actual integers start. Output file will also have this number and then
the encrypted numbers.
Example:
Input.txt Encrypted.txt
6 12 3 4 8 10 7 6 32 41 40 36 34 37

Array read from the file: Arr[100] = {12,3,4,8,10,7,0,0,…..,0}


Encrypted array is obtained as
Arr1[100] = ( 3+4+8+10+7, 12+4+8+10+7, 12+3+8+10+7, 12+3+4+8+7, 12+3+4+8+10}
Encrypted array: Arr1[100] = {32 41 40 36 34 37,0,0,0,…,0}
Q4. Now write another program that will take the encrypted file in Q3 and will decrypt it to
decrypted.txt.
The decryption can be done by a mathematical observation given below.
Lets assume n=4.
The original array can be assumed as original[4] = {a,b,c,d}
The encrypted array is obtained as Encrypted[4] = {b+c+d, a+c+d, a+b+d, a+b+c}
Add all the elements we will get
Assignment_05
CS1002-Programming Fundamentals
Fall-2021

Sum = Encrypted[0] + Encrypted[1] + Encrypted[2] + Encrypted[3]


= 3(a+b+c)
Sum of elements of Original[] = sum / n-1
Thus for a given encrypted array Encrypted[] of size n, the sum of elements of the original array
Original[] can be calculated as sum = (Encrypted[0] + Encrypted[1] + Encrypted[2] … Encrypted[n-
1])/ (n-1)
Original[0] = sum – Encrypted[0]
Original[1] = sum – Encrypted[1]
.
.
Original[n-1] = sum – Encrypted[n-1]

Q5. Given an array of N elements. Get an integer K form user such that K<N. Now shift the elements
of the array K positions to the right.
Input: Array = {1, 3, 5, 4, 9, 8, 2, 6, 7, 10} and K = 4
Output: {2, 6, 7, 10, 1, 3, 5, 4, 9, 8}

Q6. Given two arrays of N elements each. Check if the two arrays are reverse of each other or not.

Input:
Array1 = {1, 3, 5, 4, 9, 8, 2, 6, 7, 10}
Array2 = {10, 7, 6, 2, 8, 9, 4, 5, 3, 1}
Output:
Array1 is reverse of Array2

Q7. Given a 2-Dimensinoal Array of m*n dimensions. Get two integers R and C from user such that
R<m and C<n. Now, Find the largest element in the Rth row and the smallest element in the Cth
column.
For Example: if a 4*4 array is given with R = 3 and C = 3. Then find the largest element in the last
row and smallest element in the last column. Note that Rows and Columns start from 0

Q8. Given a 2-Dimensional array of m*n dimensions. Treat this array as a matrix and find its transpose.
Display the original as well as the altered array

Q9. Given two 2-Dimensional Arrays of m*n and n*k Dimensions. Treat these arrays as matrices and
find their product. Display the original as well as the altered array.

Q10. Take a character 2d array. Input values in them, if character ‘*’ occurs between any two
integers then you print out the product of those two. And if character ‘+’ occurs between any two
integers then print the sum of those two integers. If the user tries to enter two ‘*’ or ‘+’ right after
another, then ask the user to enter the value again, until he/she enters an integer value
Example:
Arr[3][3]
1,2,3,*,* WRONG INPUT TRY AGAIN. 4, 5, *, 6, 7
Arr[3][3]: 1 2 3
Assignment_05
CS1002-Programming Fundamentals
Fall-2021

*45
*67
Output: Product: 12 30
Sum: 0
Example 2:
Arr[3][3]: 1, 2, *, 3, +,4,1,*,4
Output: Product: 6 4
Sum: 7
Q11. Write a program that can be used to assign seats for a commercial airplane. The airplane has
13 rows, with six seats in each row. Rows 1 and 2 are first class, rows 3 through 7 are business
class, and rows 8 through 13 are economy class. Your program must prompt the user to enter the
following information:
a. Ticket type (first class, business class, or economy class)
b. Desired seat

Output the seating plan in the following form:


A B C D E F
Row 1 * * X * X X
Row 2 * X * X * X
Row 3 * * X X * X
Row 4 X * X * X X
Row 5 * X * X * *
Row 6 * X * * * X
Row 7 X * * * X X
Row 8 * X * X X *
Row 9 X * X X * X
Row 10 * X * X X X
Row 11 * * X * X *
Row 12 * * X X * X
Row 13 * * * * X *

Here, * indicates that the seat is available; X indicates that the seat is occupied. Make this a
menu-driven program; show the user’s choices and allow the user to make the appropriate
choices.

Q12. Write a program that randomly generates a 20 x 20 two-dimensional array, board, of type int.
An element board[i][j] is a peak (either a maximum or a minimum) if all its neighbours (there
should be either 3, 5, or 8 neighbours for any cell) are less than board[i][j], or greater than
board[i][j]. The program should output all elements in board, with their indices, which are peak.
It should also output if a peak is a maximum or a minimum.
e.g. a board of 5 x 5 is shown below and neighbors of a cell are shown.
5 10 2 3 2
7 21 3 7 5
9 8 1 2 3
4 6 8 9 1
5 2 6 3 4
Here the first element at 0,0 is a peak as all its neighbors have value greater than it. All the cells
highlighted in yellow are minimum peak and the cells highlighted in green are maximum peaks.
Assignment_05
CS1002-Programming Fundamentals
Fall-2021

Submission Guidelines:
• Submit your own original work. Plagiarism will not be tolerated, either done from the internet
or from any fellow classmate.
• You have to submit source files (.cpp files), input and output files (if any) for this assignment.
Compress or zip all the source files in a folder.
• Save your source file in the following format: section_21FrollNo_A5_P# e.g.
A_21F1234_A5_P1.

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