while( digits[digitNumber-1] == 0)
{
digitNumber--;
}
This segment is correct . Let say we have 11111110000000 as the array . Then suppose digitNumber is holding the 9th position (0-based index) . But originally it should be holding 6th position as after that all the values are zero. so going by this code whenever we have 0 value at digitNumber index we will decrease it's position(so to bring the digitNumber at its correct place) . After 3 loops we will have digitNumber at 6th position as required.
In the 5th segment we will not reach the correct position as it checking the value one less than it so digitNumber will stop at one place ahead of it .
In the 6th segment we are not checking the values uptill the msb instead we are getting towards the farther end of the array which is of no use and have leading zeroes.
In the 2nd segment , we are checking from the farthest end of the array which sometimes may have digitNumber at the place but not all times . so we should just start checking from the place currently hold by the digitNumber and not some other place.
You are given an array named digits of digitCapacity that holds the individual digits of a...
Consider the problem where you are given an array of n digits
[di] and a positive integer
b, and you need to compute the value of the number in that
base.
In general, you need to compute
For example:
(1011)2 = 1(1) + 1(2) + 0(4) + 1(8) = 11;
(1021)3 = 1(1) + 2(3) + 0(9) + 1(27) = 34, and
(1023)4 = 3(1) + 2(4) + 0(16) + 1(64) = 75.
In these examples, I give the digits...
What does the following code segment do to the array a[], given that the current_sizevariable holds the number of valid elements currently in the array? int element = 0; current_size++; for (int i = current_size - 1; i > pos; i--) { a[i] = a[i - 1]; } a[pos] = element; It shifts all the array elements one position to the left (lower index). It shifts all the array elements one position to the right (higher index). It removes the...
Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...
In Java Write a method named addDigits which accepts two inputs, num and index. The first input parameter num is a 4-digit positive integer and index is an int value which refers to a digit that the summation should start from. For example if the given num is 2345 and the index is 1, starting from the second digit, the result of 12 is returned which is the sum of 3, 4, and 5. If the num is not a...
1. Implement a tostring() member function. This function will return a string representation of the LargeInteger. You should probably used string streams to implement this function. Note that the digits of the large integer are stored in reverse of their display order, e.g. the 1's place (100 ) is in index 0 of digits, the 10's place (101 ) is in index 1, etc. 2. Implement a second constructor for the LargeInteger. This constructor will be used to construct a...
You are given an array of integers, where different integers may have different numbers of digits but the total number of digits over all integers in the array is n. Show how to sort the array in increasing order in O(n) time. Note: The number of integers in the array can be different for same value of n - for example the array with 2 integers 2468 and 12345 has 9 digits as well as the array with 5 integers...
In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...
Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...
write in java 1. Assume the availability of a method named makeLine that can be passed a non-negative integer n and a character c and return a String consisting of n identical characters that are all equal to c. Write a method named printTriangle that receives two integer parameters n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...
23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a second oversize array with size2 elements, write a method that returns the first array with the elements of the second appended to the end. If the capacity of the oversize array is not large enough to append all of the elements, append as many as will fit. Hint: Do not construct a new array. Instead, modify the contents of the oversize array inside the...