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

Assignments 07

Uploaded by

5jr9n4xjvh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignments 07

Uploaded by

5jr9n4xjvh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming and Data Structures Laboratory | 2023-24 Spring semester, Section 03

Assignment 7 | March 14, 2024

Submission instructions
* Submit one .c file for each part of the assignment. Each of your .c files should be named as:
<your roll number>_A<assignment number>_<problem number>.c
For instance, if your roll number is 23CS10025, and if you are presently doing Assignment 7
which has 3 parts, then you should submit 3 separate .c files named as:
23CS10025_A7_1.c 23CS10025_A7_2.c 23CS10025_A7_3.c
* Submissions must be through the course Moodle, before the end of Lab session. Late
submissions will be penalized /not accepted.

1. [15 Marks] Take an array of 5 dis4nct integers in main(). The array elements can be
hardcoded inside the main func4on (that is, you don't need to take the array elements as
input from the user). Write the following func4ons.
- A func4on 'swap()' that takes two pointers to two integers and swaps the values of the
integers. The return type of the func4on will be void.
- A func4on 'reverse()' that takes an integer array and the number of elements in the
array. This func4on reverses the passed in array. For example, if the passed in array contains
the elements: [2 0 1 5 9], aMer the func4on reaches its end, the array gets reversed to: [9 5 1
0 2]. The return type of the func4on will be void. Use the 'swap()' func4on you wrote
inside 'reverse()'.
- A func4on 'print_arr()' that takes an integer array and the number of elements in
the array. This func4on prints the array elements nicely formaOed. The return type of the
func4on will be void.

Inside main(), take an array of 5 dis4nct integers. The array elements can be hardcoded
inside the main func4on (that is, you don't need to take the array elements as input from
the user). Call 'print_arr()' func4on on the array to print its elements. Next call the
reverse() func4on on this array to modify the array. AMer this again call the
'print_arr()' func4on to show the modified array. An example run can be:

./a.out
Before reverse: 2 0 1 5 9
After reverse: 9 5 1 0 2

2. [20 Marks] Declare in main() two integer arrays a[] and b[] that can contain a
maximum of 100 numbers. Take n as input and fill up a[] with n dis4nct integers using
scanf(). Write a func4on of the prototype: int find_wrong_order(int *x,
int *y, int n, int i) that fills up y[] with those elements in {x[0],...,x[i-
1]} that are larger than x[i] and those in {x[i+1],...,x[n-1]} that are smaller
than x[i]. It returns the number of elements filled up in y[].
Call find_wrong_order() from main() itera4vely for each element a[i],
i=0,2,...,n-1, and use b[] to store the array y[]. For each element in a[], print
from main() the elements of b[] and a[i] in the following format:
<the b[j] which are greater than a[i] and are to the leM of a[i] in a[]> <[a[i]]> <
the b[j] which are less than a[i] and are to the right of a[i] in a[]>. Note that in
some cases, the leM and right entries to <a[i]>can be both blank or one of them can be
blank. Example runs can be:
./a.out
Enter n: 5
Enter the elements of a[]: 4 1 3 5 2
[4] 1 3 2
4 [1]
4 [3] 2
[5] 2
4 3 5 [2]

./a.out
Enter n: 5
Enter the elements of a[]: 3 1 3 7 6
[3] 1
3 [1]
[3]
[7] 6
7 [6]

3. [15 Marks] In your program an user supplies as input the number of elements and
their values as posi4ve integers. Dynamically allocate a 1D array named "A" that stores these
elements. Dynamically allocate another array B of same size. Itera4vely take the largest
element of A one by one and store them in B. Use a func4on to get the largest element of
A. Note that you have to store the elements in B in non-decreasing order. Every 4me you
take the current largest element from A to put it in B, the corresponding element in A
should be assigned 0. Print both A and B. An example run can be:

./a.out
Enter n: 5
Enter the elements of A: 4 2 5 2 3
A = 0 0 0 0 0
B = 2 2 3 4 5

You might also like