SLC21 Week2 - Programming ArrayssteemCreated with Sketch.

in hive-145157 •  12 days ago 

SLC21 Week2 - Programmin arrays



Image 2024-11-07 at 10.33.jpg

Hello everyone,

I'm not in to programming that much, but I got some basic C language knowledge from my school days and I thought participating this course will be a good opportunity for me to refresh the knowledge and to learn something new. I went through the lesson by @sergeyk which was very informative and clear, of course I had to use a translator. Okay, let's see how well I have managed to do the homework.

Declare an array of any type and explain how to use it, how to access elements, and assign values to some elements. Display these values on the screen. Explain the advantages of arrays over regular variables.


Image 2024-11-09 at 09.15.jpg

For the question number 01, I declared a string type array. I basically took the characters of my name in to a string type array. So first I did the decelerating of the array, and then I gave the data to the array as a string. After that I demonstrated how to access elements of the array. I called the whole array for print and also I called them separately also. As it's an array we can call the elements as we need. It's one of the main advantages of arrays. And I also did a modification for one array element which is demonstrated in the above program.Let me who you what I got after compiling.

Image 2024-11-09 at 14.34.jpg


Explain the advantages of arrays over regular variables.


To answer this question, I like to point out as following advantages,

  • Ability to store/organise large amount of data with a single declaration ( single variable name). Ex : int marks[200];
  • Fast access to data despite of the quantity or complexity and enable efficient data handling
  • Ability to access any element in the array simply based on the position of that element in the array.
  • Repetitive tasks can be done easily. Ex: looping
  • Easy to handle data in different logical algorithms. Ex : sorting data

What is the name of an array? What will happen if you print it on the screen? What does cout << a + 2; and cout << a - 2; mean? If cout << a; displays 4000, what will a + 1 equal ?

This question made me to think deeply. I thought it's better to try it and see what happens. Okay, let's try this one.

Image 2024-11-09 at 15.42.jpg

A name of an array, in our example "a", it's simply a pointer to the first element of that particular array. In our example, it points to a[0] element. But when you print it, it will simply print the memory address of the first element of the array.

cout << a + 2 means the address of the third element of the array, as name "a", is a pointer to the start of this array, this command will forward the pointer for two positions forward. So simply this will output the address of a[2].

cout << a - 2 command guide the pointer two memory locations before the start of the array. This is display an address, but this memory location is outside the memory locations allocated for the array.

Image 2024-11-09 at 15.19.jpg

So simply a+1 will display the address of the a[1] element of the array. So it should display 4004. ( As it's a int array, it takes 4 bits of memory for an element).

Can an array have two dimensions?


Yes, We have focused only on one dimensional arrays up to now such as b[5]; , but we can also declare arrays of multiple dimensions. Let me show an example as follows, I'm declaring the following array marks[2][6];. In C++ we can very easily 2D array like that. If you are dealing with complex mathematical functions these 2D arrays are very useful.

Image 2024-11-09 at 16.02.jpg

Assign a random number to variable k:int k = (rand() % 101) * (rand() % 101) * (rand() % 101) + 500;Optimize last lesson’s task of finding divisors by storing results in an array.

Image 2024-11-09 at 16.37.jpg

To complete the task, I used todays lesson, I used arrays to store the diversions. So using an array make it easy to use these values for a further step. Let me explain the code. srand(time(0)) is used to generate a random number based on the time. So this will create different random numbers each time I run the program. rand() % 101 generates a random number between 0 and 100. The program runs with a for loop to find the diversions and to store the diversions I used the array, int divisors[100].

Fill an array of 55 elements with random numbers from 10 to 50. If it contains 37, print "yes"; otherwise, print "no."


Image 2024-11-09 at 17.02.jpg

I coded to print the all the generated numbers to make the outcome more clear when it's compiled. I stored the random generated numbers to a array and with that it was easy to run it through a for loop and find whether 37 is generated or not.

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

Image 2024-11-09 at 17.13.jpg

So as the question says, I generated 66 random numbers in-between 12 and 60. And after that I replaced even numbers with 7 and odd numbers with 77, printed the numbers after replacing as above.

Fill an array of 77 elements with random numbers from 102 to 707. Find the two largest numbers, first explaining how you interpret "two largest numbers."

To achieve this we have to go trough each number of the array using a loop. We can initially declare two int variable ( let's take them as largest and secondlargest ). Then the algorithm run through the loop as, if the first array element larger than the second largest. we set first array element to the int largest and second array element to int secondlargest. Like that we can go through each of the array element using a for loop. At the end of the cycles we will get the largest and the second largest values to the initially defined two variables form the generated random numbers.

Image 2024-11-09 at 17.28.jpg

It was a very informative lesson. And it look me lot of time to complete this homework as I touched coding after some time. This bought me back some of my old memories.

Thank you.

-Vimukthi Vithanage

I like to invite : @vishwara, @virajherath and @nishadi89 to join the contest.

-Vimukthi Vithanage

About The Author :

Vimukkthi Vithanage (@besticofinder) is an Electrical Engineer and a Lecturer by profession , crypto enthusiast and a person who always values his freedom. He loves traveling , teaching and to spend time with his friends. A good reader and a good listener. He currently works in Faculty of Technology , University of Sabaragamuwa , Sri Lanka and also as a freelance SEO service provider. He writes on different topics related to his daily activities. If you like his writings , don't forget to show your love with an upvote and leave a feedback to show more love. And a resteem will be a great encouragement for his work. Feel free to contact him on discord anytime . He always love to make new friends.

Check his latest research work : visit

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Loading...

Wow thank you so much for sharing this with us. I was actually able to learn a lot from this your post and I must confess that this was so inspiring beyond what I can ever imagine of

Thank you very much for the kind feedback