Posts

Finding the most repeated element, and the greatest and smallest values in an array

Image
Hi, this is Mazen. In this post, we're going to solve a programming example, which is three searching operations on an array. First, we introduce two simple approaches to find the most repeated element, that is - the element which has the most count. Next, we elaborate on the logic of finding either of the greatest and smallest elements. The question says: Write a program that searches an array for the most repeated number, and the smallest and greatest values It's a good practice to separate the requirements into subproblems and then putting all of them together to make up the main program. Therefore, we're going to start solving the first part of the question which is finding the most repeated number. To do that, there are two simple approaches. I'm going to show you both of them and it's for you which one to choose. Before doing the three operations, we assume that you previously declared an integer array, named it " array " and got ten inputs for it. ...

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;    ...

Finding (positive - negative - odd - even - zero) numbers in an array

Image
Welcome again to a new lesson and a new example. We're continuing practicing what we learned in the conceptual lesson about arrays ( Taking inputs from the user, storing them in a (1D-2D-3D) array and printing them on the screen in c++ ). In today's example, we make a simple program in C++ that searches a one-dimensional array for some specific numbers and returns the count of numbers that matches the specified type. The question says: Take 20 integer inputs from the user and output the count of the following:  1. Positive numbers. 2. Negative numbers. 3. Odd numbers. 4. Even numbers. 5. Zeros. Step 1: Declare a 20-element one-dimensional array.      int array[20]; Step 2: Prompt the user to enter 20 integer values and store them in the previously declared array - use a ( for ) loop to pass through the array indexes.      std::cout << "Enter twenty integers:\n";      for (int i = 0; i < 20; i++)      { ...

Searching an array for an entered value

Image
Hi everyone, welcome to a new lesson and new example. Today we have an example on arrays, specifically, single dimensional arrays. We deep dived previously in executing input/output on arrays but conceptually. In this article and the coming ones, we're going to practice the concepts we learned. If you want an explanatory article about arrays from the scratch, I would rather reading the following blog before reading this. Otherwise, if you have enough knowledge about arrays and doing I/O on them, and you want something more hands on, just complete reading this article. Taking inputs from the user, storing them in a (1D-2D-3D) array and printing them on the screen in c++ Now let's start solving our example. The question says: Take 10 integer inputs from the user and store them in an array. Again ask the user to enter an integer, then the program should tell the user whether the entered integer exists or not. Step 1: Create a single dimensional array with a size of 10. int array[1...