Posts

Showing posts from March, 2021

Searching an array for the second greatest value using C++

Image
Hi, this is Mazen. Today we're going to solve a new programming question using C++. We previously talked about searching an array for an entered number. Searching for the greatest or second greatest number is basically the same. Because finding the greatest value in an array isn't a huge issue, we're going to make our question a bit harder to find the second greatest number. In this example, we use arrays, loops, if statements and the ternary operator . The question says: Write a program that finds the second greatest number in an array Step 1: Declare an integer array with the size of 5, for instance.      int test[5]; Step 2: Get five numbers from the user and store them in the declared array. You can use a for loop to perform that. The number of repetitions is the same as the size of the array.      for (int i = 0; i < 5; i++)      { std::cout << "Integer #" << i + 1 << ": "; std::cin >> test[i]; ...

Checking an array if it's palindrome or not, using C++

Image
Hi, this is Mazen. Welcome to a new blog and new example. In this blog, we're going to solve a programming question using C++. Each step is explained before coded, so you can change it to a code using whatever programming language you would prefer. We're continuing our series of solving examples on arrays. Today's example is checking whether an entered array is palindrome or not. Simply, a palindrome number - or a text in general - is a set of digits that are the same either read from left to right or right to left. Lets take an example: 24742 is a palindrome number because when you read it either from the right or the left, it won't make difference. On contrast, 83525 is not a palindrome number, because when you read it from the right it would be 52538, which is definitely not the same when reading it from the left. Write a program to check whether the elements of an array are the same either read from left to right or right to left (palindrome), or not Step 1: Declar...

Copying an array in the reverse order in C++

Image
Hi, this is Mazen. Today I'm going to solve with you a new programming example. Today's example is like an enhanced model of the previous one. We declare two arrays, assign some values to one of them, and then copy the elements to the unassigned array. Before copying the elements, we reverse them. So they're being copied in the reversed order, the element in the last index goes into the first index; the last index's value of the first array goes into the first index in the second. In this example we assume that the array being copied has the size of 10. It can have any size but make sure that both of the two arrays - the source and destination - have the same size. The question says: Take 10 integer inputs from the user and store them in an array. Then copy all the elements into another array but in the reverse order. Step 1 : Declare an integer array with the size of 10 and name it " array ", for instance.      int array[10]; Step 2: Use a for loop ...

Two methods for reversing a text string in c++

Image
Hi, this is Mazen. Today, I'm going to solve with you a programming question using C++. You don't necessarily have to use C++ to solve this example, all I care about is the concept not the programming language being used. So just understand the step and apply it in whatever language you prefer. Our example for this blog is reversing a text and then outputting the reversed text. We use two simple approaches, the first one is using the normal class string that C++ and most of the modern programming languages provide. The other approach is using the array of characters, which typically is referred to as "c-string". The question says: Write a program that reverses a string and prints it on the screen Using C++ string class: Step 1: Prompt the user to enter a text and store it in a string variable using getline() function ( make sure you include the <string> header file ).      std::cout << "Enter a text: ";      std::string text;    ...