C++ Programming
- Write a program to print Hello, World on the standard output.
- Write a program that uses the multiplication operator, *, to print the product.
- Program to use
a separate statement to print each operand. - Write a program that uses a while to sum the numbers from 50 to 100.
- write a while that prints the numbers from ten down to zero.
- Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.
- What are the differences between int, long, long long, and short?
- To calculate a mortgage payment, what types would you use for the rate, principal, and payment? Explain why you selected each type.
- Determine the type of each of the following literals. Explain the differences among the literals in each of the four examples:
- What, if any, are the differences between the following definitions: int month = 9, day = 7; int month = 09, day = 07;
- What values do these literals represent? What type does each have? (a) “Who goes with F\145rgus?\012” (b) 3.14e1L (c) 1024f (d) 3.14L
- Using escape sequences, write a program to print 2M followed by a newline. Modify the program to print 2, then a tab, then an M, followed by a newline.
- Explain the following definitions. For those that are illegal, explain what’s wrong and how to correct it.
(a) std::cin >> int input_value;
(b) int i = { 3.14 };
(c) double salary = wage = 9999.99;
(d) int i = 3.14; - Write a program to calculate the highest two numbers in an array with length n, and print these numbers with their indexes.
- Write a C++ program to change every letter in a given string with the letter following it in the alphabet (i.e. a becomes b, p becomes q, z becomes a).
- Write a C++ program to find the largest word in a given string.
- Write a C++ program to count all the characters, blank spaces and words in a given string.
- Write a C++ program to find a word in a given string that has the highest number repeated letters.
Write a program to calculate the highest two numbers in an array with length n, and print these numbers with their indexes.
Method 1
#include <iostream>
using namespace std;
int main ()
{
//Function to implement bubble sort
void bubbleSorting(int arr[], int num);
int x, num;
cout << "Enter the number of elements " << "\n";
cin >> num;
int *arr = new int (num);
cout << "Enter " << num << " numbers" << endl;
for(x=0;x< num; x++)
{
cin >> arr[x];
}
bubbleSorting(arr, num);
}
void bubbleSorting(int arr[], int num)
{
int i, j;
for (i = 0; i < num - 1; i++)
for (j = 0; j < num - i - 1; j++){
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
cout << "Largest number is : " << arr[num -1] << " at index: " << num -1 << endl;
cout << "Second Largest number is : " << arr[num -2] << " at index: " << num -2 << endl;
return;
}
Output
Enter the number of elements
6
Enter 6 numbers
23
32
45
12
8
37
Largest number is : 45 at index: 5
Second Largest number is : 37 at index: 4 Method 2
#include <iostream>
#include <climits> // For INT_MIN
using namespace std;
void findTwoHighest(int arr[], int n) {
int Highest = INT_MIN, secondHighest = INT_MIN;
int HighestIndex = -1, secondHighestIndex = -1;
for (int i = 0; i < n; i++) {
if (arr[i] > Highest) {
secondHighest = Highest;
secondHighestIndex = HighestIndex;
Highest = arr[i];
HighestIndex = i;
} else if (arr[i] > secondHighest && arr[i] != Highest) {
secondHighest = arr[i];
secondHighestIndex = i;
}
}
if (secondHighest == INT_MIN) {
cout << "There is no second highest number.\n";
} else {
cout << "Highest number: " << Highest << " at index " << HighestIndex << endl;
cout << "Second Highest number: " << secondHighest << " at index " << secondHighestIndex << endl;
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the numbers:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
findTwoHighest(arr, n);
return 0;
}
Output
Enter the number of elements: 6
Enter the numbers:
23
105
25
12
36
58
Highest number: 105 at index 1
Second Highest number: 58 at index 5