assignment of array

9
Assignment of Array, Matrices and String 1-D array 1. Write a function in C++, which accepts an integer array and its size as parameters and rearranges the array in reverse. Example:If an array of nine elements initially contains the elements as 4, 2, 5, 1, 6, 7, 8, 12, 10 Then the function should rearrange the array as 10,12, 8, 7, 6,1, 5, 2, 4 2. Write a function in C++, which accepts an integer array and its size as parameters and rearranges the array in reverse. Example:If an array of nine elements initially contains the elements as 4, 2, 5, 1, 7, 8, 12, 10 Then the function should rearrange the array as 7,8,12,10,4,2,5,1 3. Write a function which accepts an integer array and its size as parameter and return 1 if an array is a palindrome or return 0. 4. Write a function which takes an integer array and its size as parameter and display sum of even and odd elements in an array and no of elements divisible by 3. 5. Write a function which takes a integer array and its size as parameter and Insert element from 2 nd location in an array 6. Write that accept an integer array, its size and location as parameter and Insert an element in an array at that location. 7. Write a function which takes a integer array and its size as parameter and Delete 1 st element from an array 8. Write that accept an integer array, its size and location as parameter and delete element from that location 9 Write function in C++ which accepts an integer array and size as arguments and assign values into a 2D array of integers in the following format : If the array is 1, 2, 3, 4, 5, 6 The resultant 2D array is given below 1 2 3 4 5 6 1 2 3 4 5 0 1 2 3 4 0 0 1 2 3 0 0 0

Upload: niti-arora

Post on 18-Nov-2014

112 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Assignment of Array

Assignment of Array, Matrices and String

1-D array

1. Write a function in C++, which accepts an integer array and its size as parameters and rearranges the array in reverse.

Example:If an array of nine elements initially contains the elements as 4, 2, 5, 1, 6, 7, 8, 12, 10Then the function should rearrange the array as 10,12, 8, 7, 6,1, 5, 2, 42. Write a function in C++, which accepts an integer array and its size as parameters and

rearranges the array in reverse.Example:If an array of nine elements initially contains the elements as 4, 2, 5, 1, 7, 8, 12, 10

Then the function should rearrange the array as 7,8,12,10,4,2,5,13. Write a function which accepts an integer array and its size as parameter and return 1 if an

array is a palindrome or return 0.4. Write a function which takes an integer array and its size as parameter and display sum of even

and odd elements in an array and no of elements divisible by 3.5. Write a function which takes a integer array and its size as parameter and Insert element from

2nd location in an array6. Write that accept an integer array, its size and location as parameter and Insert an element in an

array at that location.7. Write a function which takes a integer array and its size as parameter and Delete 1st element

from an array8. Write that accept an integer array, its size and location as parameter and delete element from

that location9 Write function in C++ which accepts an integer array and size as arguments and

assign values into a 2D array of integers in the following format :If the array is 1, 2, 3, 4, 5, 6The resultant 2D array is given below1 2 3 4 5 61 2 3 4 5 01 2 3 4 0 01 2 3 0 0 01 2 0 0 0 01 0 0 0 0 0If the array is 1, 2, 3The resultant 2D array is given :1 2 31 2 01 0 0

10 Write a function in C++ which accepts an integer array and its size as argumentsand replaces elements having even values with its half and elements having oddvalues with twice its value .11. Write function in C++ which accepts an integer array and size as arguments andreplaces elements having odd values with thrice its value and elements having evenvalues with twice its value.

Page 2: Assignment of Array

12. Write a function in C++, which accepts an integer array and its size as argumentsand swap the elements of every even location with its following odd location.Example : If an array of nine elements initially contains the elements as2,4,1,6,5,7,9,23,10then the function should rearrange the array as4,2,6,1,7,5,23,9,1013 Write function in C++ which accepts an integer array and size as arguments andassign values into a 2D array of integers in the following format :If the array is 1, 2, 3, 4, 5, 6The resultant 2D array is given below :1 0 0 0 0 01 2 0 0 0 01 2 3 0 0 01 2 3 4 0 01 2 3 4 5 01 2 3 4 5 6If the array is 1, 2, 3The resultant 2D array is given :1 0 01 2 01 2 314 Define the function SwapArray(int[ ], int),that would expect a 1D integer arrayNUMBERS and its size N. the function should rearrange the array in such a way thatthe values of that locations of the array are exchanged. (Assume the size of the arrayto be even).Example :If the array initially contains {2, 5, 9, 14, 17, 8, 19, 16}Then after rearrangement the array should contain {5, 2, 14, 9, 8, 17, 16, 19}15 Given two arrays of integers A and B of sizes M and N respectively. Write a function named MIX() which will produce a third array named C, such that the following sequence is followed :

All even numbers of A from left to right are copied into C from left to right.All odd numbers of A from left to right are copied into C from right to leftAll even numbers of B from left to right are copied into C from left to right.All odd numbers of B from left to right are copied into C from right to leftA, B and C are passed as arguments to MIX().

e.g. : A is {3,2,1,7,6,3} and B is {9,3,5,6,2,8,10}, the resultant array C is {2,6,6,2,8,10,5,3,9,3,7,1,3}16. Write a function Merge() which takes 2 integer array and their sizes as parameter and merge to form a sorted array. The 1st array is in ascending order and 2nd in descending order and resultant array should be is ascending ordrExample 1st array {2,5,8,95,111} 2nd array {90,56,34,4,1} resultant array {1,2,4,5,56,90,95,111}17. Considering the following key set: 42,29,74,11,65,58, use insertion sort to sortthe data in ascending order and indicate the sequences of steps required.Note:- no need to write the logic or code for insertion sort.

Page 3: Assignment of Array

2-D Array

1. Write a function MAX in C++ which will return the Largest number stored in a two dimensional array of Integers.2. Write a function in c++ which accepts a 2D array of integers and its size as arguments and displays the elements which lies on diagonals.[ Assuming the2D array to be a square matrix with odd dimensions , i.e 3x3, 5x5,7x7, etc ]Example if the array content is5 4 36 7 8 1 2 9Output through the function should be Diagonal one : 5 7 9Diagonal two : 3 7 1 3. Write a function in C++ which accepts a 2D array of integers and its size as arguments and displays the elements of the middle row and the elements of middle column.

Example if the array content is 3 5 47 6 92 1 8

Output through the function should be:Middle row: 769 Middle column: 5 6 1

4. Write a function in C++ to print the product of each column of a two dimensionalarray passed as the arguments of the function.Example : If the two dimensional array contains

1 2 43 5 64 3 22 1 5

Then the output should appear as:Product of Column 1 = 24Product of Column 2 = 30Product of Column 3 =240

5. Write a function in C++ to print the sun of each row of a two dimensionalarray passed as the arguments of the function.Example : If the two dimensional array contains

1 2 43 5 64 3 22 1 5

Then the output should appear as:Sun of Row 1 = 7Sum of Row 2 = 14Sum of Row 3 =9Sum of Row 4=8

Page 4: Assignment of Array

6. Write a function in C++ to print sum of all values which either are divisible by 2or divisible by 3 present in a 2D array passed as the argument of the function.7. Write a function in C++ to find the sum of diagonal elements from a 2D array oftype float. Use the array and its size as parameters with float as its return type.8. Write a user-defined function in C++ to display those elements of 2D arrayT[4][4] which are divisible by 100. Assume the content of the array is already presentand the function prototype is as follows: void showhundred( int T[4][4]);9. Write a user-defined function named Lower_half1() which takes 2D array A, withsize N rows and N columns as argument and prints the lower half of the array.

2 3 1 5 0 27 1 5 3 1 7 1

Input 2 5 7 8 1 output 2 5 70 1 5 0 1 0 1 5 03 4 9 1 5 3 4 9 1 5

10. Write a user-defined function named Lower_half2() which takes 2D array A, withsize N rows and N columns as argument and prints the lower half of the array.

2 3 1 5 0 07 1 5 3 1 3 1

Input 2 5 7 8 1 output 7 8 10 1 5 0 1 1 5 0 13 4 9 1 5 3 4 9 1 5

11 Write a user-defined function named Upper_half1() which takes 2D array A, withsize N rows and N columns as argument and prints the lower half of the array.

2 3 1 5 0 2 3 1 5 07 1 5 3 1 7 1 5 3

Input 2 5 7 8 1 output 2 5 70 1 5 0 1 0 13 4 9 1 5 3

12 Write a user-defined function named Upper_half2() which takes 2D array A, withsize N rows and N columns as argument and prints the lower half of the array.

2 3 1 5 0 2 3 1 5 07 1 5 3 1 1 5 3 1

Input 2 5 7 8 1 output 7 8 10 1 5 0 1 0 13 4 9 1 5 5

13Write a user-defined function named reverse_row() which takes 2D array A, withsize N rows and N columns as argument and reverse rows of the matrix.

2 3 1 5 0 3 4 9 1 57 1 5 3 1 0 1 5 0 1

Input 2 5 7 8 1 output 2 5 7 8 10 1 5 0 1 7 1 5 3 13 4 9 1 5 2 3 1 5 0

Page 5: Assignment of Array

14 Write a user-defined function named Reverse_column() which takes 2D array A, withsize N rows and N columns as argument and prints the lower half of the array.

2 3 1 5 0 0 5 1 3 27 1 5 3 1 1 3 5 1 7

Input 2 5 7 8 1 output 1 8 7 5 20 1 5 0 1 1 0 5 1 03 4 9 1 5 5 1 9 4 3

15. An array A[10][20] is stored in the memory with each element requiring 4 bytesof storage. If the base address of the array in the memory is 400, determine thelocation of A[8][13] when the array VAL is stored (i) Row major (ii) Column major.16. An array VAL[1.15][1.10] is stored in the memory with each element requiring 4 bytes of storage. If the base address of the array VAL is 1500, determinethe location of VAL[12][9] when the array VAL is stored (i) Row wise (ii) Columnwise.17. An array X[10][20] is stored in the memory with each element requiring 4 bytesof storage. If the Base address of the array is 1000, calculate location of X[5][15]when the array X is stored using column major order.18. An array Arr[35][15] is stored in the memory along the row with each of itselement occupying 4 bytes . Find out the Base address and the address of elementArr[20][5] , if the location Arr[2][15] is stored at the address 3000.19. An array MAT[20][10] is stored in the memory along the row with each elementoccupying 4 bytes of the memory. Find out the Base address and the address ofelement MAT[10][5] , if the location MAT[3][7] is stored at the address 1000.20. An array Arr[2..15][3..20] is stored in the memory along the row with each elementoccupying 4 bytes. Find out the Base address of the location Arr[3][12], if the locationArr[5][2] is stored at the address 1500.

String

1 Write a member functions of the following classclass string{

char str[50];Public:void getcstring();void dispstring();void frequency_table(); //count no and display of uppercase, lowercase ,digits, alphabets and

//special character in the stringvoid no_of_words(); //count and display no of words, each word is separted by spacevoid string_length();void reverse_string(); //reverse and display stringvoid palindrome(); //display if an array is a palindrome;

};

Page 6: Assignment of Array

2. Write member function of following classclass 2strings{

char str1[50],str2[50];public:void string_compare(); //comaper both the stringvoid string_concat();//concatenate both the stringsvoid string_copy(); //copy first string to another

};Note:- do not use built in functions

3. write a function which take a string as parameter and display upper triangle and lower triangle of the string

. exampleString is COMPUTEROutput of upper triangle Output of lower triangleCOMPUTER CCOMPUTE COCOMPUT COMCOMPU COMPCOMP COMPUCOM COMPUTCO COMPUTEC COMPUTER