SLC21 Week2 - Programming arrays

in #slc21w2sergeyk2 months ago

Assalam-o-Alaikum!!!

image.png

Greetings to my all STEEMIT members. Hopefully you all are great and enjoying your best life on STEEMIT. I am also fine ALHAMDULILLAH

Declare an array of any type: Explain how to use an array, how to access array elements. Assign values ​​to some of its elements, and use those values ​​(display). What are the advantages of an array over ordinary variables

We can say that Array is the addition if elements of same data type. We can access array through data types. And through this effieicient method, we can access and stored many items of the same data type

image.png

Advantages:

Arrays is a single variable that not only allows elements to be stored, but is also very organized, and in individual cases it also reduces need of indivisual variables. It handles searching or debugging tasks very well. It allows quick access and organizes a fixed set of elements. It groups related data and keeps code clean and tidy. Makes it much easier to understand

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?
  • Array Name as Pointer:

The array name a is actually a constant pointer to the first element of the array (a[0]).
If you use cout << a;, this will output the memory address where the array starts, not the array elements themselves. The address is displayed as a hexadecimal value (like 0x7ffc...).

Pointer Arithmetic (e.g., a + 2):

  • Since a is a pointer to the first element, expressions like a + 2 or a - 2 perform pointer arithmetic.
  • a + 2 means the address of the element that is two positions ahead of a[0], which is a[2].
  • a - 2 would attempt to move two positions backward from the start of the array, which is invalid for array boundaries and will result in undefined behavior if accessed.

Using cout << a + 2; and cout << a - 2;:

cout << a + 2; displays the memory address of a[2].
cout << a - 2; displays the memory address two integer sizes before the start of the array, which is generally invalid.
If cout << a; Displays 4000:

Assuming a is an int array and cout << a; displays 4000 (as a hypothetical memory address), a + 1 would be the address of a[1], which would be 4004.
In C++, each int is typically 4 bytes, so moving to the next integer address increases the address by 4. Therefore:
a points to 4000
a + 1 points to 4004
a + 2 points to 4008, and so on.

Let’s break down what happens with cout << a + 2;, cout << a - 2;, and the effects of pointer arithmetic in general.

Understanding cout << a + 2; and cout << a - 2;

  • Pointer Arithmetic:

a + 1 points to the address of the next element (a[1]).
Since a is an int array, each element typically occupies 4 bytes (assuming int is 4 bytes on this system).

Thus:
a + 1 points to 4000 + 4 = 4004
a + 2 points to 4000 + 2 * 4 = 4008
a - 2 would point to 4000 - 2 * 4 = 3992
Using cout << a + 2; and cout << a - 2;:

cout << a + 2; will display the memory address 4008, which is the address of a[2].
cout << a - 2; will display the memory address 3992. This is technically two int positions before the start of the array (a[0]). Accessing a - 2 is invalid and leads to undefined behavior if dereferenced, as it’s outside the array bounds.

Example Explanation
If cout << a; displays 4000, then:

cout << a + 1; will display 4004
cout << a + 2; will display 4008
cout << a - 2; (while invalid for accessing values) would display 3992

image.png

Can an array have two dimensions?

Yes, an array has 2D we display and organize the data in columns and rows which are arranged in our table. It is also supposed to handle any data for which it is capable of 2D indexing.

image.png

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!!!

While solving this problem, we will let a number k. And we will also store it in an array. After that we will generate this as specified. and then found a divisor by using efficient method by without using the function. then our result will be stored in an an array

image.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 itno

In this problem we will fine the required number in the array. And if we find the required number in the array then print yes. But if we did not fine the required number then, it will print No

image.png

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

I found this is very interesting. In which we founds an array from number 12 to 60. And we have to show it in this way that it will show thw even number replaces to 7 and odd number replaces to 77

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

Here is the program with Output

image.png

So here I am trying to do my all tasks. And I do my level best. I hope you will appreciate me.

Special Thanks to @sergeyk

Invite friends @maryamnadeem , @uzma4884 @drhira

Regards:@arinaz08

Sort:  
Loading...

Coin Marketplace

STEEM 0.20
TRX 0.24
JST 0.037
BTC 96071.21
ETH 3327.74
USDT 1.00
SBD 3.21