0% found this document useful (0 votes)
54 views13 pages

C++ Array and String Assignment 2

Uploaded by

Raj Shukla
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)
54 views13 pages

C++ Array and String Assignment 2

Uploaded by

Raj Shukla
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/ 13

C++ Array and String Assignment 2

Q1. Q1. Write a program in C++ to read n number of values in an array and display it in reverse order.

Test Data :

Input the number of elements to store in the array :3

Input 3 number of elements in the array :

element - 0 : 2

element - 1 : 5

element - 2 : 7

Expected Output :

The values store into the array are :

257

The values store into the array in reverse are :

752

Q2. Write a program in C++ to print all unique elements in an array.

Test Data :

Print all unique elements of an array:

------------------------------------------

Input the number of elements to be stored in the array: 4

Input 4 elements in the array :

element - 0 : 3

element - 1 : 2

element - 2 : 2

element - 3 : 5

Expected Output :

The unique elements found in the array are:

3 5

By Ram Lovewanshi Mobile : 7648904739Page 1


C++ Array and String Assignment 2
Q3. Write a program in C++ to count a total number of duplicate elements in an array.

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 5

element - 1 : 1

element - 2 : 1

Expected Output :

Total number of duplicate elements found in the array is : 1

Q4.Write a program in C++ to merge two arrays of same size sorted in descending order.

Test Data :

Input the number of elements to be stored in the first array :3

Input 3 elements in the array :

element - 0 : 1

element - 1 : 2

element - 2 : 3

Input the number of elements to be stored in the second array :3

Input 3 elements in the array :

element - 0 : 1

element - 1 : 2

element - 2 : 3

Expected Output :

The merged array in descending order is :

332211

By Ram Lovewanshi Mobile : 7648904739Page 2


C++ Array and String Assignment 2

Q5. Write a program in C++ to find the maximum and minimum element in an array.

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 45

element - 1 : 25

element - 2 : 21

Expected Output :

Maximum element is : 45

Minimum element is : 21

Q6. Write a program in C++ to separate odd and even integers in separate arrays.

Test Data :

Input the number of elements to be stored in the array :5

Input 5 elements in the array :

element - 0 : 25

element - 1 : 47

element - 2 : 42

element - 3 : 56

element - 4 : 32

Expected Output :

The Even elements are :

42 56 32

The Odd elements are :

25 47

By Ram Lovewanshi Mobile : 7648904739Page 3


C++ Array and String Assignment 2

Q7. Write a program in C++ to sort elements of array in ascending order.

Test Data :

Input the size of array : 5

Input 5 elements in the array :

element - 0 : 2

element - 1 : 7

element - 2 : 4

element - 3 : 5

element - 4 : 9

Expected Output :

Elements of array in sorted ascending order:

24579

Q8. Write a program in C++ to find the second largest element in an array.

Test Data :

Input the size of array : 5

Input 5 elements in the array :

element - 0 : 2

element - 1 : 9

element - 2 : 1

element - 3 : 4

element - 4 : 6

Expected Output :

The Second largest element in the array is : 6

By Ram Lovewanshi Mobile : 7648904739Page 4


C++ Array and String Assignment 2

Q9. Write a program in C++ to find the second smallest element in an array.

Test Data :

Input the size of array : 5

Input 5 elements in the array (value must be <9999) :

element - 0 : 0

element - 1 : 9

element - 2 : 4

element - 3 : 6

element - 4 : 5

Expected Output :

The Second smallest element in the array is : 4

Q10. Write a program in C++ for a 2D array of size 3x3 and print the matrix.

Test Data :

Input elements in the matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [0],[2] : 3

element - [1],[0] : 4

element - [1],[1] : 5

element - [1],[2] : 6

element - [2],[0] : 7

element - [2],[1] : 8

element - [2],[2] : 9

By Ram Lovewanshi Mobile : 7648904739Page 5


C++ Array and String Assignment 2
Expected Output :

The matrix is :

1 2 3

4 5 6

7 8 9

Q11. Write a program in C++ for multiplication of two square Matrices.

Test Data :

Input the rows and columns of first matrix : 2 2

Input the rows and columns of second matrix : 2 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Input elements in the second matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

Expected Output :

The First matrix is :

12

By Ram Lovewanshi Mobile : 7648904739Page 6


C++ Array and String Assignment 2
34

The Second matrix is :

56

78

The multiplication of two matrix is :

19 22

43 50

Q12.Write a program in C++ to find transpose of a given matrix.

Test Data :

Input the rows and columns of the matrix : 2 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Expected Output :

The matrix is :

12

34

The transpose of a matrix is :

1 3

2 4

By Ram Lovewanshi Mobile : 7648904739Page 7


C++ Array and String Assignment 2

Q13. Write a program in C++to find sum of rows an columns of a Matrix.

Test Data :

Input the size of the square matrix : 2

Input elements in the first matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

Expected Output :

The First matrix is :

The matrix is :

56

78

The sum or rows and columns of the matrix is :

5 6 11

7 8 15

12 14

Q14. Write a program in C++ to accept two matrices and check whether they are equal.

Test Data :

Input Rows and Columns of the 1st matrix :2 2

Input Rows and Columns of the 2nd matrix :2 2

Input elements in the first matrix :

By Ram Lovewanshi Mobile : 7648904739Page 8


C++ Array and String Assignment 2
element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Input elements in the second matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Expected Output :

The first matrix is :

12

34

The second matrix is :

12

34

The Matrices can be compared :

Two matrices are equal.

Q15. Write a program in C++ to search an element in a row wise and column wise sorted matrix.

Expected Output :

The given array in matrix form is :

15 23 31 39

18 26 36 43

25 28 37 48

30 34 39 50

By Ram Lovewanshi Mobile : 7648904739Page 9


C++ Array and String Assignment 2
The given value for searching is: 37

The element Found at the position in the matrix is: 2, 2

Q16. Write a program in C++ to print individual characters of string in reverse order without using library
function

Test Data :

Input the string : WELCMOME

Expected Output :

The characters of the string in reverse are :

EMOMCLEW

Q17. Write a program in C++ to count the total number of words in a string.

Test Data :

Input the string : Welcome To Bhopal

Expected Output :

Total number of words in the string is : 3

Q18. Write a program in C++ to compare two strings without using string library functions.

Test Data :

Check the length of two strings:

--------------------------------

Input the 1st string : aabbcc

Input the 2nd string : abcdef

String1: aabbcc

String2: abcdef

Expected Output : Strings are not equal.

By Ram Lovewanshi Mobile : 7648904739Page 10


C++ Array and String Assignment 2

Q19. Write a program in C++ to count total number of alphabets, digits and special characters in a string.

Test Data :

Input the string : Welcome to bhopal123

Expected Output :

Number of Alphabets in the string is : 14

Number of Digits in the string is : 3

Number of Special characters in the string is : 2

Q20. Write a program in C++ to copy one string to another string without using library function

Test Data :

Input the string : This is a string to be copied.

Expected Output :

The First string is : This is a string to be copied.

The Second string is : This is a string to be copied.

Number of characters copied : 31

Q21. Write a program in C++ to count total number of vowel or consonant in a string.

Test Data :

Input the string : Welcome to bhopal

Expected Output :

The total number of vowel in the string is : 6

The total number of consonant in the string is : 9

Q22. Write a C++ program to sort a string array in ascending order.

By Ram Lovewanshi Mobile : 7648904739Page 11


C++ Array and String Assignment 2
Test Data :

Input the string : ACB

Expected Output :

After sorting the string appears like :

ABC

Q23. Write a program in C++ to read a sentence and replace lowercase characters by uppercase and
vice-versa.

Test Data :

Input the string : This Is A Test String.

Expected Output :

The given sentence is : This Is A Test String.

After Case changed the string is: tHIS iS a tEST sTRING

Q24. Write a program in C++ to Concatenate Two Strings Manually.

Test Data :

Input the first string : this is string one

Input the second string : this is string two

Expected Output :

After concatenation the string is :

this is string one this is string two

Q25. Write a program in C++ to convert a string to uppercase.

Test Data :

Input a string in lowercase : the quick brown fox jumps over the lazy dog

By Ram Lovewanshi Mobile : 7648904739Page 12


C++ Array and String Assignment 2
Expected Output :

Here is the above string in UPPERCASE :

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

By Ram Lovewanshi Mobile : 7648904739Page 13

You might also like