SLC21 Week2: Programmin arrays

in #slc21w2sergeyk14 days ago

Programmin arrays.png

Edited by Canva

Hello steemians,

Declare an array of any type, explain how to use an array, how to access array elements.

We can define an array as a data structure that contains multiple values ​​of the same type to be stored in adjacent locations in memory i.e. all the values ​​are stored next to each other in the computer memory allowing quick access to them and efficient operation of each element in the array. This structure is very useful for organizing fixed-size data sets where each element can be manipulated individually while being grouped under the same variable name.

For example if we want to create an array of integers we simply declare int array[5]; which creates an array capable of holding five integers each element being accessed by an index that starts at zero so array[0] designates the first element of the array array[1] the second and so on until the last element at index four.

Once the array has been created it is possible to assign values to each element using its index.
For example by writing array[0] = 5 we assign the value 10 to the first element while array[1] = 10 sets the second element to 10. To use or display these values, simply go through the array with a loop, for example a for loop allows each element of the array to be processed iteratively and continuously, which facilitates repetitive operations and avoids handling each element manually. With the loop for (int i = 0; i < 5; i++) { printf("%d ", array[i]); } each element of the array will be displayed in turn which is extremely practical when working with large data sets.

Arrays have a considerable advantage over ordinary variables because they allow you to group a set of similar values under a single name which greatly simplifies data management and makes the code more compact and clearer instead of declaring several variables for each value the table allows them to be organized in a single structure, which also facilitates manipulations such as sorting or searching on sets of data. Having data contiguous in memory also improves access efficiency because the program can quickly access neighboring elements without having to search for each element individually. Thanks to this structure it becomes easy to carry out serial processing by applying identical operations to each element of the table, which is essential in many applications where data must be analyzed or manipulated repeatedly and systematically.

What is the name of the array? What will happen if you display this value on the screen? What does cout<<a+2;that mean cout<<a-2;? If cout<<a;4,000 is displayed when outputting, then how much will it be a+1?

The name of the array such as a in a declaration like int a[5]; represents the memory address of the first element of this array which is the precise location where the storage of its data in memory begins, therefore when we use the expression cout << a; directly the program does not display the value of a particular element of the array but rather the memory address of the element with index 0, in other words, the location where it is located the first integer of the table.

When we write an instruction like cout << a + 2; this means that the initial pointer a moves two positions forward, making it point to the address of the third element a[2] in C or C++ the + operator applied to the name of the array acts by moving the pointer of a certain number of elements depending on the type of data contained in the array which therefore takes into account the size of each element if a is an array of integers and each integer occupies 4 bytes in memory then 'a + 2' corresponds to the address of 'a[2]' this address being offset by 8 bytes from the address of leaving.

Similarly, when writing cout << a - 2;, this would attempt to move the pointer back two positions from the beginning of the array, which could lead to unpredictable behavior because "a - 2" points outside the memory area that was allocated for a; accessing an address outside of this reserved area could cause errors because the targeted memory is no longer managed by a and is therefore not safe for read or write operations.

For the last part of the question concerning the instruction cout << a; which displays an address such as 4000,that indicates the array a begins at memory address 4000, so if we write cout << a + 1; would show the address of the second element which is "a + 1", by moving the pointer to the next element of the array if "a" is an array of integers and each integer occupies 4 bytes then the address of "a + 1" would be 4004 because the movement is done by adding 4 bytes to reach the next integer in the sequence of data in the table.

Can an array have two dimensions?

Yes an array can indeed have two dimensions often called a 2D array in a 2D array the elements are arranged in rows and columns which gives it a structure similar to a matrix a two-dimensional array is in fact an array of arrays where each row itself represents an array containing several elements. To declare a 2D array in C or C++ you just need to specify both the number of rows and the number of columns for example:

image.png

In this declaration matrix has three rows and four columns which allows to store a total of 3 * 4 = 12 elements

To access an element in a two-dimensional array or assign a value to it, simply use two indices: the first indicates the row and the second the column, which makes it possible to identify each element precisely in the grid; therefore, the instruction matrix[0][0] = 1; assigns the value 1 to the element located in the first row and first column, while matrix[2][3] = 10; places the value 10 in the third row and the fourth column, and to display this value we use printf("%d", matrix[2][3]); which will print 10.

Two-dimensional arrays are particularly suited to situations where data needs to be organized in a grid format, whether to represent matrices in mathematics, game boards, data tables or any other structure where information is naturally arranged in rows and columns, making access and management of data simpler and more intuitive.

Write a random number in the variable k. int k=rand()101rand()101rand()101+500; Try to solve the task of finding divisors from the last lesson more efficiently

image.png

This program generates a random number, finds its divisors in an optimized manner by limiting the search up to the square root, then displays them. It first initializes the random number generator, computes a number by multiplying three random values and adding 500, then uses a loop to detect the divisors, storing them in an array. Finally, it loops through this table to display each divisor found, thus providing an efficient solution to identify and present the divisors of the generated number.

Fill the array with 55 numbers with random numbers from 10 to 50. If there is a number 37 among the elements of the array, print
it yes, and if there is no such number, print it no

Execution with printing "Yes":

image.pngimage.png

Execution with printing "No":

image.pngimage.png

In this code, each element of the array is filled with a random number between 10 and 50. Then, a loop goes through the array to check if any of the elements equals 37. If the program finds 37, it sets the found variable to 1 and stops the search. Finally, depending on the value of found, the program prints “Yes” if 37 is present, and “No” if it is not.

Fill an array of 66 numbers with random numbers from 12 to 60. Replace even elements with 7 and odd elements with 77

image.pngimage.png

This program starts by initializing a random number generator to ensure that each run produces different values. Next, the first loop fills a 66-element array with random numbers between 12 and 60, using (rand() % 49) + 12 to generate each number in that specific range. Once the array is filled, the second loop iterates through each element of the array to see if it is even or odd. If the element is even, it is replaced with 7, and if it is odd, it is replaced with 77, as instructed.

The program uses a final loop to display the modified array by printing each value after the replacement allowing you to view the array with the changes applied, where every even number has been replaced with 7 and every odd number with 77.

Fill the array of 77 numbers with random numbers from 102 to 707. Find the two largest numbers.

To interpret the phrase “two largest numbers,” consider that it refers to finding the two largest distinct values in the array. In other words, we look for the largest number and the second largest number, even if those numbers are close in value. If the largest number appears more than once, we always want to find the next highest number.

image.pngimage.png

The program first fills the array of values ​​with 77 random numbers between 102 and 707. Then it uses a loop to iterate through each element of the array and determine the two highest distinct values. If a value is greater than maxValue, it becomes the new maximum, and the old maxValue is assigned to secondMaxValue. If a value is less than maxValue but greater than secondMaxValue and different from maxValue, it is assigned to secondMaxValue.


Thank you very much for reading, it's time to invite my friends @analp, @crismenia, @elianyto participate in this contest.

Best Regards,
@kouba01

Sort:  
Loading...

Me honras con tu invitación joven @kouba01, pero de eso de programación no sé nada jaja. Ahora sí lo hiciste para enseñarme tu suéter para el frío. Eso sí me pareció interesante, sobre todo para hacer uno en crochet.

Dios bendiga tu inteligencia y ese don que tienes para enseñar a otros.

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.033
BTC 97050.26
ETH 3143.93
USDT 1.00
SBD 2.97