SLC21 Week2 - Programmin arrays

in #slc21w2sergeyk15 days ago

We are at it again friends and I welcome you to this post. It is my pleasure to have you here.

1000207859.png
Canvas

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

Here I will be talking about arrays with examples in Python programming. Let's start with declaring an Array

Declaring an Array

Like I said I will be using Python to illustrate my examples. Talking about Python, we can create a list that will function (work) like an array in other programming languages. We all know that programming lists are what allows us to store multiple (many) items in a single variable which we can see below.

1000207777.png

Using an Array

With Array we can store and manage multiple (many) values of the same type under one variable name, making it easier for us to work with a large set of data.

Accessing an Array

We can access array elements by their index beginning from 0 for the first element, 1 for the second element, 2 for the third element, 3 for the fourth element, 5 for the fifth element, and so on.

Assigning Values to Elements

I can also change the value of an element by directly assigning a new value to the element as shown below.

1000207776.png

If we want to print values and use the values in the expression will have to print it as shared below.

Advantages of Arrays Over Ordinary Variables

  • Array as said earlier, allows us to store multiple values in a single structure, which makes it easier to manage and reduce the number of variables meaning it is efficient.

  • Array uses indexes allowing direct fast access to elements, meaning it is index-based access.

  • Arrays make it easier to loop through elements and perform operations on each element.

  • Arrays provide a better and more organized memory layout as compared to individual variables, improving performance.

  • Finally, it is useful for handling multiple (many) values efficiently as it makes code easier to maintain.


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 bea+1?

The name of an array such as a in int a[5] = {10, 20, 30, 40, 50};, acts as a pointer to the first element of the array. This means a holds the memory address where the array begins.

If I try to output the array name directly with cout <<a; in C++, it will show the memory address of the first element in the array and not the array values. The reason for this is that a is essentially a pointer to the first element.

Understanding court << a + 2;

  • a + 2' This means that I am shifting the pointera by two positions forward, pointing to the third element in the array since the indexing is zero-based.

  • cout << a + 2; will show the memory address of the third element.

  • If I want to show the value of the third element, I will need to make is of court << *(a + 2); which dereferences the pointer to get the actual value.

Understanding cout<<a-2

  • a - 2 would attempt to shift the pointer backward by two elements. This goes out of bounds if I should try to access memory before the start of the array. It is an undefined behavior in C++, which should be avoided.

If cout<<a;4,000 is displayed, it means 4000 is the memory address of the first element in the array.

Calculating a + 1

  • a + 1 would be the memory address of the second element in the array. If a the address of the first element is 4000, then it means +1 will then be 4004, but assuming each int takes up 4 bytes in memory. If so then cout << a + 1; will display 4004.

Can an array have two dimensions?

Yes, an array can have two dimensions and we can declare a 2D array to help us specify columns and several rows, which we can also initialize a 2D array at the point of declaring the 2D array. 2D arrays help us to manage and organize data in a well-structured format, which is most useful for tables, matrices, and other similar structures.

array2.PNG

The above program initializes a 3×4 matrix, which then iterates through the rows and columns as we have mentioned earlier to print out each of the elements in 2D array format.


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 (so that the divisors are found faster) and write the results (not on the screen) but in an array. Since the transfer of arrays to a function is not a simple topic - filling the array should not be done in the form of a function!!!

I will be using the efficient method of iterating up to the square root of the number to find divisors easier. The code generates a random number, finds its division more efficiently and faster, and stores safely in an array with the use of functions as we can see in the screenshot image. In solving the problem, the number K is stored in an array.

array3.PNG
array4.PNG


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 into

In this program, we will look at the required number in the array. If the required number (37) in the array is found it will print YES, otherwise, it will print NO if the required number is not found.

array5.PNG


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

This program will print the modified array with each element on a new line which I found interesting to do. The program shows the even number replaces it with 7 and then adds the number to 77.

1000207792.png


Fill the 77-number array with random numbers from 102 to 707. Find the two largest numbers. But the phrase "the two largest numbers" can have many interpretations. Therefore, first explain well how this phrase was understood. And then solve the problem.

In this program, if the second largest number is been updated from -1, the program will then print the two largest distinct numbers. Otherwise, it will indicate there weren't enough distinct numbers to find the two largest values.

array7.PNG

I am inviting; @kouba01, @simonmwigwe, and @ruthjoe

Cc:-
@sergeyk

Sort:  
Loading...

Common, I am so sure this must not have been easy for you because you must have really put in so much of effort and time in making sure that this work. I must give you a kudos for that

You have said it all. It wasn't easy. Thanks for your support

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 99049.34
ETH 3373.17
USDT 1.00
SBD 3.08