SLC21 Week2 - Programming Arrays

Task # 1

Declare an array of any type:
explain how to use an array, how to access initializingts.
Assign values ​​to some of its elements, and use those values ​​(display)
What areinitializetages of an array over ordinary variables?


An array is a type of linear data structure; it can store multiple values of the same data type in adjacent memory locations. All values of an array have the same name.

To declare an array, we write the type, name, and use square brackets to mention the size.

To access elements of an array, we use the array name and index in square brackets, which is an ordinal number. Like in the above example, there are 5 elements; to access 4th element, we will use checklist[3], this is because the indexing starts from zero. The first element will be stored in checklist[0] and the last in checklist[4].

When we declare an array of a specific size, the memory is allocated for it accordingly. If we haven't initialized it, some garbage values which are already there in the memory will be automatically assigned to it.

There are two ways to initialise an array: at the time of declaration, like in the example below. The array contains the first 5 multiples of 2.

After declaration, one way was to write 5 lines of code and assign a multiple of 2 to each location, but I chose the most viable option—a for loop.

Now if you notice, after initialising the array, when I accessed elements, it displayed assigned values, not garbage.

Use: If I have a list of random numbers and I have to check if the numbers are positive multiples of 2, then instead of declaring and initialising unique variables for each multiple and then checking the requirements, we can simply initialise an array and loop through all its elements.

As seen in the above code, arrays can store multiple values of the same type under one name (variable), unlike typical variables. These can save repetitive lines of code, elements can easily be accessed and are scalable means size can be changed according to the requirement.



Task # 2

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 is a pointer or reference to its first element in the memory. If we try to display its value on the screen, we will get an address in the memory. Since I'm writing C, I won't be using cout<< commands.

%p prints the pointer value or memory address of the first element of array a and a+1and a+2prints the addresses of the second and third elements of the array in the memory whereas a-2 is a reference to location that is two blocks before the address of a.

These memory addresses look complex, right? We can represent these addresses as integers to make them look more comprehensible by using %d instead of %p.

If you notice each address is separated by four numbers because array is of integer type and an integer is 4 bytes. If the address of array a is 4000 then:

  • Address of a+1 will be 1*sizeof(int) + 4000 = 4004
  • Address of a+2 will be 2*sizeof(int) + 4000 = 4008
  • Address of a-1 will be -1*sizeof(int) + 4000 = 3996
  • Address of a-2 will be -2*sizeof(int) + 4000 = 3992

Here sizeof(int) is 4 (bytes).

However, 3996 (a-1) and 3992 (a-2) are invalid addresses for our array as the array starts from 4000.



Task # 3

Can an array have two dimensions?


Yes, an array can have two or even more dimensions. A two-dimensional array means an array whose each element is also an array. When computing data in tables, grids or matrices, we can used 2D arrays. The first dimension represents the rows as if in a table and the second dimension represents the columns.

A 2D array is declared and initialised like shown in the code below:

Size of a 2D array is declared with 2 pairs of square brackets. The number in the first pair represents the number of rows, and the number in the second pair represents the number of the columns. Two methods of initializing a 2D array and accesing and printing each element are shown in the code.

In memory, the storage order of a 2D array is first row, then second row, and so on.



Task # 4


Write a random number in the variable k. int k=rand()101rand()101up to()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!!!


To generate random numbers using rand(), I had to include stdliblibrary in the code. But this is not enough, as it will generate the same random number everytime. To get different random number each time the program is run, I have inlcuded time library to be able to use time() which gives a different seed based on the current time ensuring randomness every time the program is executed.

According to the given formula, variable k is assigned a random number in the range 500-1000500. To find the divisors of k in an efficient way, this time I haven't checked the divisibility of all numbers from 1 to k but I have only checked the numbers from 1 to sqrt of k; if any of those numbers are divisors of k then that means the quotient as a result of that division will also be the divisor.

For example, if 2 is divisor of 36 then 36÷2 (18) is also the divisor of 36. We don't have to iterate all the way to 18.

I have fixed the first divisor(element) to be 1 and started checking the divisors from 2 as can be seen in the loop (i=2). If the modulus of k with i is zero that means i is a divisor and is added to the array. If that divisor is not equal to k and the quotient (to rule out repetition and the number itself) then I have added quotient in the divisor list too, meaning array.

Since, the divisors are required to be stored in an array, I need to know the size of the array. I could roughly allocate 100 or even 1000 locations in the memory but I would be wasting a lot of memory. Knowing that I will be checking divisors only upto sqrt(k) and each check could give me either zero or two divisors. Keeping in view, the upper bound, two divisors for each check - this should mean divisors of k shouldn't exceed 2*sqrt(k) and since I'm not including k itself and the repeating numbers which I can certainly get, when k has a perfect root. Like for 36, 6 can repeat, for 49, 7 can repeat. I will not be using two more blocks from the memory and determine the size of array with 2*sqrt(k)-2.

This formula means bigger the value of k, larger the size of array. Like for if k = 1000500 then size of array would be 1998. I could fix the size to be 1998 but it's more optimal to determine the size of array after k is computed - it will save more memory and will make sure all divisors are stored in the array.

I could roughly fix the size to be 500 or even 1000 but this approach could lose some divisors if exceeded from the fixed size of array.

I could also dynamically allocate memory but for that I still need to know the exaxt number of divisors for it to be more optimal way than what I already did.



Task # 5

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


To generate random numbers from 10 to 50, I have used this formula rand()%41+10. rand()%41 will always generate a number from 0 to 40. Adding 10 to it will make sure the range is from 10 to 50.

I have then generated 55 such numbers and stored all those in an array of same size. Then I have simply looped through each element checking if the value is 37, if it is 37 then I have assigned 1 to a variable found and then broken the loop using break; because there's no need to check further.

If found =1 then I have printed a "Yes" statement and if zero then printed a "No" statement.



Task # 6

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


Like in the previous problem, I have generated random numbers from 12 to 60 using this formula - rand()%49+12. Modulus with 49 of the random number will generate number from 0 to 48 and adding 12 to it will make sure the number is always in the range 12 to 60.

I have generated 66 such numbers and stored in an array of same size. Then I have looped through each element and checked if it is even or odd. I replaced the value with 7 when even and 77 when odd.

Even is confirmed by checking if the modulus of number with 2 is zero. If not then it should be Odd.

After replacing, I have printed the whole array once more with replaced values which are either 7 or 77.



Task # 7

Fill the array of 77 numbers 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.

Using rand()%606+102, I have generated 77 numbers from 102 to 707 and stored in an array.

Now I have to find the two largest numbers. Technically assuming, the largest and second largest number is unique. For this, I have kept repetitive numbers in mind while writing the code.

In the start, I have assumed the largest number to be first element of the array and second largest number to be -1 (less than largest). In this case it could be even 101 because we are certain that all numbers will be greater than that.

I have then looped through the entire array, checking if the current number is greater than the largest number, if that's true then I have updated the largest number with current number and changed the second largest number to be the old largest.

If the current number is not greater or equal to the largest number but is greater than second largest number then I have updated the second largest number with the current number.

Although, it's very unlikely, but if all the numbers are same which means second largest number will remain -1 as initialized in the start, then the program will print - all numbers are same; otherwise, it will just print largest and second largest number.



Programming Language : C
Online Compiler : Programiz

Sort:  
Loading...

Coin Marketplace

STEEM 0.24
TRX 0.26
JST 0.041
BTC 98536.81
ETH 3483.92
USDT 1.00
SBD 3.40