Computer Programming Exercises and Solutions
Computer Programming Exercises and Solutions
To determine if a number is automorphic, compute its square and compare the square's last digits to the original number's digits. In a program, convert both the number and its square to strings, and check if the string of the square ends with the string representation of the number. This logic utilizes the properties of string manipulation for effective comparison .
Selection Sort is effective for ordering words alphabetically because it repeatedly selects the smallest (or largest, depending on order) element from the unsorted portion and moves it to the sorted portion. In implementation, the array of words is iterated through, selecting the lexicographically smallest word in each pass and swapping it with the current position, resulting in an alphabetically sorted list .
To implement a hotel management system that identifies a visitor's room, use a two-dimensional array where each row represents a floor, and each column represents a room number. When a visitor's name is entered, a loop through the array allows you to find the entry, retrieve the index of the row (floor), and the column (room number) to determine and display the visitor's location within the hotel .
A program for Fahrenheit to Celsius conversion using a switch statement can be structured by first prompting the user for their temperature conversion choice. The switch statement corresponds to each case (e.g., convert to Celsius or convert to Fahrenheit), applying the appropriate conversion formula: C = 5/9 * (F - 32) for Fahrenheit to Celsius, or F = 1.8 * C + 32 for Celsius to Fahrenheit. Include a default case for erroneous input .
To efficiently sort student marks and roll numbers in descending order, you can utilize a sorting algorithm such as Bubble Sort or Selection Sort. First, store the marks and corresponding roll numbers in parallel arrays. Apply the sorting algorithm to the marks while simultaneously swapping the roll numbers to maintain their link with the marks. Finally, iterate through the sorted arrays and print each roll number with its corresponding mark .
To segregate even and odd numbers from a given array, traverse the array while checking the modulus of each number. If the number is divisible by 2, append it to the 'even' array; otherwise, append it to the 'odd' array. This separation allows for an ordered collection of even and odd numbers .
To rearrange a series of numbers by inverting the first and second halves, first determine the midpoint of the array. Then swap elements from the first half with the corresponding elements from the second half, iterating up to the midpoint. This technique effectively reverses the order of the two halves without changing each half's internal sequence .
To calculate sums for negative numbers, positive even numbers, and positive odd numbers in a list, iterate through each number in the list. Use conditional statements to identify the type of each number: add to a negative sum if the number is negative, to the positive even sum if it is positive and even, and to the positive odd sum if it is positive and odd. Summing these individually provides the desired totals .
A two-dimensional array efficiently organizes examination scores by tabulating each student's marks across multiple subjects, where each row corresponds to a student's scores. Calculating averages per student involves iterating through their row entries, and specific scores can be quickly accessed through indexed traversal, facilitating both data retrieval and calculation tasks .
To calculate the position of a visitor in a hotel represented by a two-dimensional array, each floor and room is indexed. When a visitor's name is entered, search the array to match the name, which will yield the array indices corresponding to the floor and room. This setup effectively maps each array index to a real-world location in the hotel .