Sie sind auf Seite 1von 2

15CSE180 Computer Programming Laboratory

Lab 4
28 December 2018
 Arrays

1. Write a C program to find sum of all array elements.

2. Write a C program to find maximum and minimum element in an array.

3. Write a C program to count the number of odd and even elements in an array.

4. Write a C program to copy all elements from an array to another array.

5. Write a C program to insert an element in an array at a specified position.

6. Write a C program to delete an element in an array at a specified position.

7. Write a C program to delete all duplicates element in an array.

8. Write a C program to merge two arrays to a third array.

9. Write a C program to reverse an array.

10. Write a C program to divide a given array into two arrays.

11. Write a C program to replace every element of an array with its next element
(the last element can be repalced by ­1).

12. Given   an   array   of  N  integers.   The   task   is   to   find   the   smallest   index   of   an
element such that when multiplied by -1 the sum of whole array becomes 0. If
there is no such index return -1.

Given {1, 3, -5, 3, 4}, the index is 1 (i.e. when 3 is


multiplied by -1 then 1+(-3)+(-5)+3+4 = 0.

Given {5, 3, 6, -7, -4}, the index is -1

13.  Given an array of N elements, the task is to convert it into a permutation (each
number from  1 to N  occurs exactly once) by using the following operations a
minimum number of times:
a) increment any number
b) decrement any number

Given {1 1 4} the array can be converted to {1 2 3} with just


2 operations. {1 1+2 4-3}
The array {3 0} requires just 2 operations i.e {3-1 0+1}
gives {2 1}

14. Given   an   array   of   integers,   a  move  constitutes   choosing   any   element   and
incrementing   it   by   1.   Write   a   program   that   computes   the   number   of   moves
needed to make every value in an array unique.

Given {3 2 1 2 1 7} the output should be 6 moves.


{3 2 1 2 1 7} -> {3 3 1 2 1 7} -> {3 4 1 2 1 7} -> {3 4 1 3 1
7} -> {3 4 1 4 1 7} -> {3 4 1 5 1 7} -> {3 4 2 5 1 7} so six
moves

15. Given a binary array (consisting of only 1s and 0s) write a program to find the
minimum number of operations needed to make the given array beautiful.

An array is considered beautiful if it has no consecutive 0s and 1s. The following
constitutes one operation:
a) cut the array into two parts
b) reverse one of these parts
c) join them again to create a whole array again.

If an array cannot be made beautiful return ­1.

Given {1 1 0 0} output should be 2


operation 1: {1} {1 0 0} (cut)
{1} {0 0 1} (reverse)
{1 0 0 1} (join)
operation 2: {1 0} {0 1} (cut)
{1 0} {1 0} (reverse)
{1 0 1 0} (join)

Das könnte Ihnen auch gefallen