A Beginner's Guide to Arrays in Python and C++

A Beginner's Guide to Arrays in Python and C++

Introduction

Hey there, coding club members! I'm here to teach you all about arrays in Python and C++. Arrays are a fundamental data structure in programming, and understanding how they work is essential to becoming a proficient coder. So let's dive in!

Technically speaking

Arrays are like a bag of chips (or crisps, depending on where you're from). You have a bunch of little pieces (or elements) in one big container (the array). And just like how you can eat just one chip or the whole bag, you can access individual elements or the entire array in your code.

python

Let's start with Python. In Python, arrays are called list But don't let the name confuse you, they're still just a bunch of elements in a container. To create a list in Python, all you have to do is use square brackets [ ] and separate each element with a comma. For example:

my_list = [1, 2, 3, 4, 5]

Easy peasy, right? Now, if you want to access an individual element in the list, you can use its index. The first element is at index 0, the second at index 1, and so on. So, if you wanted to print the third element (which is 3) from the list above, you would do:

print(my_list[2])

Why 2? Because we start counting at 0 in programming. It's just one of those things you have to get used to.

C++

Okay, now onto C++. In C++, arrays are just called "array." Original, right? To create an array in C++, you have to specify the data type (int, float, etc.), the name of the array, and the number of elements in the array. For example:

int my_array[5] = {1, 2, 3, 4, 5};

This creates an array of 5 integers, just like the Python list we created earlier. And just like in Python, you can access individual elements in the array using their index. In C++, the first element is at index 0 (just like in Python), and the last element is at index 4 (because there are 5 elements in the array, starting at index 0).

So, if you wanted to print the third element from the array above, you would do:

cout << my_array[2] << endl;

Problem

problem 1

Given an array of integers, find the sum of all the even numbers in the array.

Python Solution:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum_of_evens = 0

for num in my_list:
  if num % 2 == 0:
    sum_of_evens += num

print(sum_of_evens)

C++ Solution:

int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum_of_evens = 0;

for (int i = 0; i < 10; i++) {
  if (my_array[i] % 2 == 0) {
    sum_of_evens += my_array[i];
  }
}

cout << sum_of_evens << endl;

problem 2

Find the maximum value in the array and print out the value and the index.

import math
my_list = [1,2,5,15,76,0,6,3,98,54,67,354,-44]
max = -math.inf
print(max)

for i in range(len(my_list)):
    if my_list[i]>max:
        max = my_list[i]

print(max)

here is C++ Solution:

#include <iostream>
#include <string>
using namespace std;

int main() {
  int my_array[13] = {1,2,5,15,76,0,6,3,98,54,67,354,-44};
  int max = -99999999;

  for(int i=0;i<size(my_array);i++){
    if(my_array[i]>max){
        max = my_array[i];
    }
  }

  cout<<"this is the max value in array: "<<max<<endl;
  return 0;
}

problem 3

Sort the arrays bellow:

here is the python solution:

arr = [1,2,5,15,76,0,6,3,98,54,67,354,-44]
n = len(arr)
for i in range(n):
    for j in range(n - i - 1):
        if arr[j] > arr[j + 1]:
            arr[j], arr[j + 1] = arr[j + 1], arr[j]

print(arr)

Here is the C++ solution:

#include <iostream>
using namespace std;

int main() {
    int arr[13] = {1,2,5,15,76,0,6,3,98,54,67,354,-44};
    int n = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}

Did you find this article valuable?

Support Neural Nonsense by becoming a sponsor. Any amount is appreciated!