0% found this document useful (0 votes)
24 views4 pages

Assignment - 4 PF (Fall - 24)

Uploaded by

Shahmir Yousaf
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)
24 views4 pages

Assignment - 4 PF (Fall - 24)

Uploaded by

Shahmir Yousaf
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/ 4

National University of Computer and Emerging Sciences, Lahore

Campus
Course: Programming Fundamentals Course Code: CS 1002
Program: BS (Computer Science) Semester: Fall 2024
Due Date 31-OCT-2024 at 11:59 pm Total Marks: 80
Section: 1D Page(s): 4
Type: Assignment 4
Important Instructions:
1. You have to upload only .cpp file. Assignment in any other format (extension) will not be
accepted and will be awarded with zero marks. You have to make a zip file and upload it onto
the google classroom submission folder. For question 1, name your solution file with your roll
number, i.e., Q1_24L_1111.cpp. Similarly, you can name other questions.
2. You are not allowed to copy solutions from other students. We will check your code for
plagiarism using plagiarism checkers. If any sort of cheating is found, negative marks will be
given to all students involved.
3. Late submission of your solution is not allowed

Question # 1: [Marks: 10]


Write a C++ program that has an integer array of size N. The array contains N integers. You can
hardcode the elements of the array. Now the program will ask the user to enter an integer number.
Your program will tell whether the number given by the user is the summation of any three numbers in
the array. Also print the three numbers whose sum is equal to the given number.
For example, if the array is: 9, 4, 54, 23, 54, -23 0, 54, 5, 8 and the number entered by the user is 86,
then your program will print 9, 54 and 23.
It is possible to have more than one triplet whose summation equals the given number. In this case,
print all triplets. Such as, if the user enters 85, then the program will print:
a. 54, 23, and 8
b. 54, 54, and -23

Question 2: [Marks: 5+5+5=15]


Write a program that takes upto 20 integers (Capacity) ……. with -99.
1. Further your program should be able to identify the distinct elements and store it in an array named
as DistinctArray and then it should display the DistinctArray. Distinct elements of an array are such that
if an element appears more than once, then it should be printed once only.
2. Further your program should be able to identify the unique element and store it in an array named as
UniqueArray and then it should display the UniqueArray. Unique elements of an array are the ones
which occur only once in an array.
3. Now your program should make sure that the DistinctArray should be sorted in increasing order.

Sample Input:
20 11 12 20 16 15 12 16 8 12 -99
Sample Output:
Distinct Element in Sorted (Increasing order) are: 8 11 12 15 16 20
Unique Element in Sorted(Decreasing order ) are: 15 11 8

FAST School of Computing FAST-NU,Lahore Page 1


Question 3: [Marks: 10]
You have an array of zeros and ones, move all zeros to the left and ones to the right.

Sample Input:
Enter the Array. Enter -1 to exit: 1 0 0 1 0 1 0 0 0 -1
Sample Output:
Segregated Array: 0 0 0 0 0 0 1 1 1

Question 4: [Marks: 20]


You are given an array of Prime, Fibonacci and Non-PrimeFibonacci (the numbers neither prime nor
fibonacci) numbers in random order. Segregate all Fibonacci on left side, Prime on right side and Non-
PrimeFibonacci in the middle of the array. If a number is Prime and Fibonacci both, it's your choice to
treat it as a Prime or Fibonacci number. Also find the MinimumSwapsCount - the number of swaps
required to get the final array.

Sample Input: Size: 13


INDEX 0 1 2 3 4 5 6 7 8 9 10 11 12
VALUE 2 5 4 17 34 11 10 8 18 24 23 89 1
Sample Output
INDEX 0 1 2 3 4 5 6 7 8 9 10 11 12
VALUE 1 5 89 8 34 4 10 24 18 17 23 11 2
Minimum Swaps Count: 5
Fibonacci elements are {1, 5, 89, 8, 34}, NonPrimeFibonacci elements are {4, 10, 24, 18} and
Prime elements are {17, 23, 11, 2}

Question 5: [Marks: 5]
You are given an array and an index. You have to add an element at the given index of the given array
and print the new array .
Sample Input : -6 -4 0 1 4 56 589 index = 2 , element = -2
Sample Output: -6 -4 -2 0 1 4 56 589

FAST School of Computing FAST-NU,Lahore Page 2


Question 6: [Marks: 10]
Write a program which sorts the given data with respect to their frequency.
Hint: Do exactly the same process as you did for finding the unique first Us. Then make another array
Freq holding frequencies, in which you should populate the frequency of each element in a unique array
and save it as a Freq array. Now sort Us not on the basis of values but their corresponding frequency in
Freq, the element with highest frequency should appear first in the Us array. Now replace all the values
inside the data array D, by replacing each Us value one by one, as many times as their frequency is there
in the Freq array.

Sample input:
Input Array: 2 5 76 53 2 89 4 76 2 2 43 53 53 2 89 76 76 -99
Sample Output:
Us: 2 5 76 53 89 4 43
Freq: 5 1 4 3 2 1 1
Sorted Us: 2 76 53 89 5 4 43
New D Array: 2 2 2 2 2 76 76 76 76 53 53 53 89 89 5 4 43

Question 7: [Marks 5+5=10]


Part-1:

Write a program that keeps on taking input from the user until the user enters -1 (at maximum 100
values) and then sort the even index values, in increasing order and odd index values, in decreasing
order
Sample Input
INDEX 0 1 2 3 4 5 6 7
VALUE 100 10 2 3 27 9 19 13

Sample Output
INDEX 0 1 2 3 4 5 6 7
VALUE 2 13 19 10 27 9 100 3

Part-2:

Given an array of integers, print and sort the array in such a way that the first element is first maximum
and second element is first minimum and so on.

Sample Input: Size: 8


INDEX 0 1 2 3 4 5 6 7
VALUE 17 11 12 13 14 15 16 18

Sample Output
INDEX 0 1 2 3 4 5 6 7
VALUE 18 11 17 12 16 13 15 14

FAST School of Computing FAST-NU,Lahore Page 3


FAST School of Computing FAST-NU,Lahore Page 4

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