Vous êtes sur la page 1sur 6

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
2nd 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 1st 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
123456
123450
123400
123000
120000
100000
If the array is 1, 2, 3
The resultant 2D array is given :
123
120
100
10 Write a function in C++ which accepts an integer array and its size as arguments
and replaces elements having even values with its half and elements having odd
values with twice its value .
11. Write function in C++ which accepts an integer array and size as arguments and
replaces elements having odd values with thrice its value and elements having even
values with twice its value.
12. Write a function in C++, which accepts an integer array and its size as arguments
and swap the elements of every even location with its following odd location.
Example : If an array of nine elements initially contains the elements as
2,4,1,6,5,7,9,23,10
then the function should rearrange the array as
4,2,6,1,7,5,23,9,10
13 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 :
100000
120000
123000
123400
123450
123456
If the array is 1, 2, 3
The resultant 2D array is given :
100
120
123
14 Define the function SwapArray(int[ ], int),that would expect a 1D integer array
NUMBERS and its size N. the function should rearrange the array in such a way that
the values of that locations of the array are exchanged. (Assume the size of the array
to 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 left
All 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 left
A, 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 ordr
Example 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 sort
the data in ascending order and indicate the sequences of steps required.
Note:- no need to write the logic or code for insertion sort.
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 is
543
678
129
Output through the function should be
Diagonal one : 5 7 9
Diagonal 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
354
769
218
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 dimensional
array passed as the arguments of the function.
Example : If the two dimensional array contains
1 2 4
3 5 6
4 3 2
2 1 5
Then the output should appear as:
Product of Column 1 = 24
Product of Column 2 = 30
Product of Column 3 =240

5. Write a function in C++ to print the sun of each row of a two dimensional
array passed as the arguments of the function.
Example : If the two dimensional array contains
1 2 4
3 5 6
4 3 2
2 1 5
Then the output should appear as:
Sun of Row 1 = 7
Sum of Row 2 = 14
Sum of Row 3 =9
Sum of Row 4=8
6. Write a function in C++ to print sum of all values which either are divisible by 2
or 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 of
type 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 array
T[4][4] which are divisible by 100. Assume the content of the array is already present
and 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, with
size N rows and N columns as argument and prints the lower half of the array.
2 3 1 5 0 2
7 1 5 3 1 7 1
Input 2 5 7 8 1 output 2 5 7
0 1 5 0 1 0 1 5 0
3 4 9 1 5 3 4 9 1 5

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

2 3 1 5 0 0
7 1 5 3 1 3 1
Input 2 5 7 8 1 output 7 8 1
0 1 5 0 1 1 5 0 1
3 4 9 1 5 3 4 9 1 5
11 Write a user-defined function named Upper_half1() which takes 2D array A, with
size N rows and N columns as argument and prints the lower half of the array.
2 3 1 5 0 2 3 1 5 0
7 1 5 3 1 7 1 5 3
Input 2 5 7 8 1 output 2 5 7
0 1 5 0 1 0 1
3 4 9 1 5 3

12 Write a user-defined function named Upper_half2() which takes 2D array A, with


size N rows and N columns as argument and prints the lower half of the array.

2 3 1 5 0 2 3 1 5 0
7 1 5 3 1 1 5 3 1
Input 2 5 7 8 1 output 7 8 1
0 1 5 0 1 0 1
3 4 9 1 5 5
13Write a user-defined function named reverse_row() which takes 2D array A, with
size N rows and N columns as argument and reverse rows of the matrix.
2 3 1 5 0 3 4 9 1 5
7 1 5 3 1 0 1 5 0 1
Input 2 5 7 8 1 output 2 5 7 8 1
0 1 5 0 1 7 1 5 3 1
3 4 9 1 5 2 3 1 5 0
14 Write a user-defined function named Reverse_column() which takes 2D array A, with
size N rows and N columns as argument and prints the lower half of the array.

2 3 1 5 0 0 5 1 3 2
7 1 5 3 1 1 3 5 1 7
Input 2 5 7 8 1 output 1 8 7 5 2
0 1 5 0 1 1 0 5 1 0
3 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 bytes
of storage. If the base address of the array in the memory is 400, determine the
location 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, determine
the location of VAL[12][9] when the array VAL is stored (i) Row wise (ii) Column
wise.
17. An array X[10][20] is stored in the memory with each element requiring 4 bytes
of 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 its
element occupying 4 bytes . Find out the Base address and the address of element
Arr[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 element
occupying 4 bytes of the memory. Find out the Base address and the address of
element 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 element
occupying 4 bytes. Find out the Base address of the location Arr[3][12], if the location
Arr[5][2] is stored at the address 1500.

String

1 Write a member functions of the following class


class 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 string
void no_of_words(); //count and display no of words, each word is separted by space
void string_length();
void reverse_string(); //reverse and display string
void palindrome(); //display if an array is a palindrome;
};
2. Write member function of following class
class 2strings
{
char str1[50],str2[50];
public:
void string_compare(); //comaper both the string
void string_concat();//concatenate both the strings
void 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
. example
String is COMPUTER
Output of upper triangle Output of lower triangle
COMPUTER C
COMPUTE CO
COMPUT COM
COMPU COMP
COMP COMPU
COM COMPUT
CO COMPUTE
C COMPUTER

Vous aimerez peut-être aussi