1D Array Practice Questinos
1D Array Practice Questinos
Write a C++ program to find the "location/index of largest element" from an integer array. Declare an
integer array of size=10 and input its values from user. If there is more than one cell containing the
largest of the values in the array, then the program should print the index of first occurrence of largest
element i.e. index 2 and 5 contain the largest element so index 2 will be printed.
Write a C++ program to "rotate the integer array from right side". The program should shift the contents
of each cell one place to the right, except for the contents of the last cell, which should be moved into
the cell with index 0 . Thus, for example, if the sample array looks like this:
Before After
Write a C++ program to "shift the integer array from right side". Input the followings from user;
(1) an array of floating point values of size 10 with values input by user;
(2) an integer, call it "left", that tells the leftmost cell of the part of the array to be shifted;
(3) an integer, call it "right", that tells the rightmost cell of the part of the array to be shifted;
(4) a positive integer, call it "distance" that tells how many cells to shift by.
The program should make sure that left is less than or equal to right, and that distance is greater than
zero. If either of these conditions fails, the program should display the error message. Otherwise it
should shift by distance cells the contents of the array cells with index running from left to right . Thus,
for example, if the array input to the program looks like this:
For example i.e. if left has the value 3 , right has the value 7 , and distance has the value 2 , then the
program should shift the contents of cells 3 , 4 , 5 , 6 , and 7 to the right by 2 cells, so that when the
you’re done, the array will have been changed so that it looks like this:
The question marks in cells 3 and 4 indicate that we don't care what numbers are in those cells when the
program ends. Note that the contents of cells 8 and 9 have changed, but the contents of cell 10 is
Lab#12 – 1D Arrays 13-Nov,2023
unchanged. The program should take any precautions against the possibility that the cells will not be
shifted beyond the end of the array.
Write a C++ program to make an array of floating point values of size 10 and calculate the "subtotal"
such that it should replace the contents of each cell of array with the sum of the contents of all the
Before After
because 5.8 + 2.6 = 8.4 and 5.8 + 2.6 + 9.1 = 17.5 and so on. Note that the contents of cell 0 are not
changed.
Write a C++ program named "concatenate" that copies the cells of the two smaller arrays into one larger
array. Input the followings;
(1) Declare the first smaller array of int type with size 7 that will be copied;
(2) input the number of cells that will be copied from the first array;
(3) Declare the second smaller array of int type with size 4 that will be copied;
(4) input the number of cells that will be copied from the second array;
(5) Declare the larger array (enough size) into which values of both smaller arrays will be copied;
If the program discovers that the number of cells (size) in the larger array is not large enough to hold all
the numbers to be copied into it, then the program should indicate failure message Otherwise it should
start copying values. The program should not alter the contents of the smaller arrays. To take an
example, if the first two arrays input by user look like this:
then, provided the size of the larger array is at least 11, the large array should look like this when the
program returns:
Lab#12 – 1D Arrays 13-Nov,2023
Write a C++ program named "eliminate_duplicates" that takes an array of integers and eliminates all the
duplicate integers in the array.
If any duplicate integers are eliminated, then the program should also tell the new size of the array
(number of distinct integers in the array). Here is an example. Suppose the following array values are
input from user;
Then the program should alter the array so that it looks like this:
and it should display the new size of the array is 5 instead of 11 . The question marks in the cells after the
5th cell indicate that it does not matter what numbers are in those cells.
Write a C++ program that checks if a given array is a palindrome array or not. Palindrome number or
array is one which becomes the same when read from left side and right side i.e. 12321, abbcdcbba etc
Remember that you are not allowed to make a second array in the process.
Sample Input in array-1: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘e’, ‘d’, ‘c’, ‘b’, ‘a’]
Write a C++ program that creates an array of length 30 characters, then you need to get input
from user to get a series of characters in array. Program should tell how many unique values are present
in the array. Also, the program should tell the occurrence of each unique value in the character array.
Write a computer program that creates an integer array of size 10. Asks the user to populate the array
with numbers as taken input by the user(user must enter all numbers between 0-9 ). Form an integer
variable which has the list content numbers as elaborated by following:
Lab#12 – 1D Arrays 13-Nov,2023
Write a C++ program that creates two arrays of 20 integers, takes input from user, then program should
create 2 arrays(named ‘even’ and ‘odd’) each of size 20 integers. ‘Even’ array should have all even
integers from 1st array and ‘odd’ array should have all odd integers from 1st array.
Write a C++ program that creates two arrays of 10 integers, takes input from user, then program should
create a 3rd array of size 10 integers. 3rd array should have top 10 biggest integers from both arrays.