I am practicing c++. I need to convert these 2 codes to c++. I am having trouble with the for loop conversion like in the next example:
# Cuenta todos los números enteros que estén en una lista
import random
def cuenta_pares(numeros):
cuenta = 0
for numero in numeros:
if numero % 2 == 0:
cuenta += 1
return cuenta
def cuenta_impares(numeros):
cuenta = 0
for idx in range(len(numeros)):
if numeros[idx] % 2 != 0:
cuenta += 1
return cuenta
def main():
terminos = int(input("Cuántos numeros quieres que tenga la lista: "))
numeros = []
for id in range(terminos):
numeros.append(random.randint(1, 20))
pares = cuenta_pares(numeros)
impares = cuenta_impares(numeros)
print("Tu lista es: ", numeros)
print(f"Tu lista tiene {pares} numeros pares")
print(f"Tu lista tiene {impares} numeros impares")
main()
Also the next example has the for loops. I already managed to declare the variables but haven't been able to manage the for loops. and
def cuadrado(width, height):
if width % 2 == 0:
width += 1
if height % 2 == 0:
height +=1
for row in range(height):
for col in range(width):
if 0 < row < height - 1 and 0 < col < width - 1:
print(" ", end = "")
elif col % 2 == 0:
print("+", end = "")
else:
print("-", end = "")
print()
def main():
alto = int(input("Dame el alto del cuadrado: "))
ancho = int(input("Dame el ancho del cuadrado: "))
cuadrado(ancho, alto)
main()