Question

In: Computer Science

6 C++ Questions: 19. Assuming an int is size 4 bytes, what is the size in...

6 C++ Questions:

19. Assuming an int is size 4 bytes, what is the size in memory of the below array:

int cards[10] = {7, 4, 7, 5, 7};

20. In the array from question 19, what is the value of the below elements:

cards[1];
cards[8];

Assume you have an array with 128 integers which is sorted from lowest to highest.
21a. You are searching for a value which is contained in the array using the binary search algorithm. What is the minimum number of comparisons it will take?
21b. You are searching for a value which is contained in the array using the linear search algorithm. What is the maximum number of comparisons it will take?

22. True/False: in order to use the linear search algorithm on an array, the array must be sorted

23. True/False: a bubble sort on an integer array is guaranteed to do at least one swap.

24. Write a function which takes an array of ints and size of array as parameters, and returns the maximum value in the array. You do not need to demonstrate calling this function from main(). It must have the following signature:

int findMax(const int array[], int size);

Solutions

Expert Solution

19. Solution

According to the question, It is given the size of int is 4 bytes.

Then the size of the memory for the given array : int cards[10] = {7, 4, 7, 5, 7}; is

When we use fewer elements than the size specified remaining elements will set to 0 by default .

int cards[10] = {7, 4, 7, 5, 7}; // actual array is {7, 4, 7, 5, 7, 0, 0, 0, 0, 0}

Then size of the given array int cards[10] = {7, 4, 7, 5, 7};   will be = 4 bytes* 10 elements

= 40 bytes

So, 40 bytes is the size in memory for the given array.

20. Solution

The value of the below elements:

cards[1];
cards[8];

When we use fewer elements than the size specified remaining elements will set to 0 by default .

int cards[10] = {7, 4, 7, 5, 7}; // actual array is {7, 4, 7, 5, 7, 0, 0, 0, 0, 0}

Array index will start from 0.

cards[0] will be 7.

So,

cards[1] will contain value 4.

cards[8] will contain value 0.

21 a. Solution

It is given that the array contains 128 integers and elements in the array are sorted from lowest to highest.

It is also given that element to be searched is always present in the array.

When we are searching for a value which is contained in the array using the binary search algorithm.

The minimum number of comparisons it will take is 1.

When the element to be searched is the middle element or the first element compared by the algorithm.

[When Size of array is even(N is even) It is compared with either n/2 or (n+1)/2 th element]

The array the binary search algorithm will only do 1 comparison and stop executing.

So, minimum number of comparisons will be 1.

21b. Solution

It is given that the array contains 128 integers and elements in the array are sorted from lowest to highest.

It is also given that element to be searched is always present in the array.

You are searching for a value which is contained in the array using the linear search algorithm.

[ Linear search algorithm: The array is traversed sequentially and every element is checked. ]

The maximum number of comparisons it will take is 128.

When the element to be searched is the last element of the array It will compare with all 128 elements present in the array.

So, maximum number of comparisons will be 128.

22. Solution

True/False: In order to use the linear search algorithm on an array, the array must be sorted

Solution: False

No, It is not compulsory to have an array to be sorted in order to use the linear search algorithm on an array.

Linear search is a very basic and simple search algorithm. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found.

But, there is a limitation for Binary Search Algorithm.

That is, the array or list of elements must be sorted for the binary search algorithm to work on it.

23. Solution

True/False: A bubble sort on an integer array is guaranteed to do at least one swap.

Solution: False

No, it is not true that a bubble sort on an integer array is guaranteed to do at least one swap.

Because, when the given array is already sorted, when we apply bubble sort on that array there will be no swap of the elements in the array.

So, It is not mandatory to have at least one swap.

24.

Write a function which takes an array of ints and size of array as parameters, and returns the maximum value in the array. You do not need to demonstrate calling this function from main(). It must have the following signature:

int findMax(const int array[], int size);

Solution:

int findMax(const int array[], int size){

   int i;

   int max = array[0];     // Initialize first element as maximum element

   // Traverse array elements from second and compare every element with current max  

   for (i = 1; i < size; i++){

       if (array[i] > max)

           max = array[i];

     }
     return max;
}


Related Solutions

C++ Questions: 1. True/False: an int uses 4 bytes in memory on all systems. 2. True/False:...
C++ Questions: 1. True/False: an int uses 4 bytes in memory on all systems. 2. True/False: a double typically offers better precision than a float. 3. True/False: The following two statements are equivalent assuming isPair is a variable of type bool: if (isPair) if (isPair == true) 4. Provide a value we could put in the blank so that the value of x will be 20 after the code snippet. int x = _____; x *= 4; 5. Provide a...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int register i, j; for (j = 0; j< Size; j ++) { { for (i = 0; i< Size; i++) C[i; j] = A[i; j] + B[i; j]; } } Assume that the program is running on a system using demand paging and the page size is 1 Kilobyte. Each integer is 4 bytes long. It is clear that each array requires a 16-page...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
Consider a 4-way set-associative cache, 4 rows, a line size of 128 bytes and a write-back...
Consider a 4-way set-associative cache, 4 rows, a line size of 128 bytes and a write-back policy. The following requests are made to memory: Load: 0b0011111001000101 Load: 0b1000111110110100 Load: 0b0110101111110100 Store: 0b0010110000000110 Store: 0b1111001001110101 Store: 0b1110000111000001 Load: 0b0000000010100110 Load: 0b0101001001001101 Assuming the machine is in cold-start, profile the contents to the cache after all of the requests have been made. State any assumption you make (if needed).
Design a C program to print out the unsigned char (integer) values of the 4 bytes...
Design a C program to print out the unsigned char (integer) values of the 4 bytes in sequential order (i.e., the first byte is printed first). Using the code framework provided below, the printing is done by the function void byte_value(int *), in which only pointer variables can be declared and used. #include <stdio.h> ​ void byte_value(int *); ​ int main() { int n = 1; byte_value(&n); printf("Enter an integer: "); if (scanf("%d", &n) == 1) byte_value(&n); return 0; }...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT