Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Zähle vorkommen in Array (Felder)

Gegeben ist ein Array zum Beispiel mit Zahlen als Werten:

{2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14}

Schreiben Sie ein Programm, das von einem Array bestehend aus Zahlen prüft, wie viele dieser Zahlen durch 3 teilbar sind.

Die Teilbarkeit durch drei wird einfach damit gefunden, indem der Divisionsrest (modulo oder % in einigen Sprachen) berechnet wird und dann geprüft wird, ob dieser Divisionsrest gleich Null ist.

1 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

Kommentare (1)

gressly 11. November 2014 06:59   reply report
Diese Aufgabe und die Aufgabe des "Vorkommen In Array" (wo also geprüft wird, ob eine Zahl im Array vorkommt. Fehlen im Buch. In Auflage 2 unbedingt aufnehmen. Eventuell auch die Werte als real (float oder double) abspeichern, damit es für Anfänger keine Verwechslungen mit dem Index geben kann.

26 Lösung(en)

public class ZaehleVorkommenInArray {

	public static void main(String[] args) {
		new ZaehleVorkommenInArray().top();
	}


	void top() {
		int[] zahlen = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};
		int anzahlDurchDreiTeilbare = 0;
		for(int i = 0; i < zahlen.length; i++) {
			if(durchDreiTeilbar(zahlen[i])) {
				anzahlDurchDreiTeilbare = anzahlDurchDreiTeilbare + 1;
			} // end if
		} // end for
		System.out.println(anzahlDurchDreiTeilbare + " Zahlen sind durch drei teilbar.");
	} // end method top();


	boolean durchDreiTeilbar(int zahl) {
		return 0 == zahl % 3; // % = Restbildung in Java
	}

} // end of class
                

Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

object Vorkommen extends App {

  println(Array(2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14).count(_ % 3 == 0))

}
                

Lösung von: Name nicht veröffentlicht

werte = (2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14)
zaehler = 0

for i in werte:
    if i % 3 == 0:
        zaehler += 1

print(zaehler, ' Zahlen in der Liste sind glatt durch 3 teilbar.')
                

Lösung von: Hilli Hilli ()

import java.util.List;
import java.util.Arrays;
import java.util.stream.Stream;

public class Occurence {
    public static void main(String[] args) {
        final List<Integer> numbers = Arrays.asList(
            2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14
        );

        System.out.println(
            numbers.stream()
                   .filter(n -> n % 3 == 0)
                   .count()
        );
    }
}
                

Lösung von: Name nicht veröffentlicht

function checkModulo3($arr) {
	$anzIntegers = 0;
	
	foreach($arr as $int) {
		if($int % 3 == 0)
			$anzIntegers++;
	}
	
	return $anzIntegers;
}

$intArray = array(2,6,45,8,16,3,17,27);

echo 'Das Array enth&auml;lt folgende Zahlen<br />';
foreach ($intArray as $int){
	echo $int.', ';
}
echo '<br />Davon sind '. checkModulo3($intArray) . ' Zahlen durch 3 teilbar';
                

Lösung von: Name nicht veröffentlicht

"""author= Cromewell"""


def Array():
    numbers= [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14]
    counter= 0
    for i in numbers:
        if i % 3==0:
            counter += 1
    print(counter)
Array()

                

Lösung von: Name nicht veröffentlicht

public class MyClass{
 public static void main(final String[] args) {
  int[] numbers = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};
  int count = 0;
  for (int number : numbers)
   if (number % 3 == 0)
    count++;
  System.out.println(count + " Zahlen aus dem Array sind durch 3 Teilbar");
 }
}
                

Lösung von: Sebastian Littel ()

public class Teilbarkeiten {
	static int[] numbers = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
	static int divider = 3;

	public static void main(String[] args) {
		int i = checkNumbers(divider);
		System.out.println("Es können " + i + " Zahlen im Array durch " + divider + " geteilt werden");
	}

	public static int checkNumbers(int divideBy) {
		int i = 0;

		for (int number : numbers)
			if (number % divideBy == 0) i++;

		return i;
	}
}
                

Lösung von: Aaron Hodel (OSOS)

#mit Ausgabe der Zahlen, die durch 3 teilbar sind
array = [1,2,3,4,5,6,7,8,9,11,12]
zaehler = 0
liste_durch_drei = []
for i in array:
    if i%3 ==0:
        liste_durch_drei.append(i)
        zaehler+=1
print(zaehler, " Zahlen sind durch 3 teilbar. \nDie Zahlen lauten: ",
      liste_durch_drei)   
                

Lösung von: Py Thon ()

document.write([2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14].filter(function(v){ return !(v%3) }).length)
                

Lösung von: Tobias Petri (energie graz)

use strict;

my @array = (2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14);


print Durch_Drei_Teilbar(@array)."\n";


sub Durch_Drei_Teilbar {
    my @array = @_;
    my $teilbar = 0;
    for(@array){
        ++$teilbar if($_%3==0);
    }
    return($teilbar);
}
                

Lösung von: Paul Scheinast (http://scheinast.eu)

package berechnung;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;

	public class berechnung {
	//Liste für die Integer
	public static List<Integer> zahlen=new ArrayList<Integer>();
	public static void main (String [] args)
		{
			//Zufällige Integer Generieren
			for(int i=0;i<20;i++)
			{
				Random random=new Random();
				int zahl = random.nextInt(100) ;
				zahlen.add(zahl);
			}
		//Variable für die Anzahl der teilbaren durch 3
		int anzahl_zahlen_teilbar=0;
		//Teilbare suchen
		for(int i=0;i<zahlen.size();i++)
			{
				if(zahlen.get(i)%3==0)
					{
						anzahl_zahlen_teilbar++;
					}
				
			}
		//Ausgabe der Anzahl der durch drei teilbaren Zahlen
        System.out.println("Zahlen innerhalb des Arrays: "+zahlen);
		System.out.println("Es sind "+anzahl_zahlen_teilbar+" zahlen durch 3 teilbar.");
		
		
}
}
                

Lösung von: Name nicht veröffentlicht

using System;
using System.Linq;

namespace Zähle_Vorkommen_in_Array {
	class Program {
		static void Main(string[] args) {
			int[] Zahlen = new int[] {
				2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14
			};

			Console.WriteLine(Zahlen.Count(x => x % 3 == 0));

			Console.ReadKey(true);
		}
	}
}
                

Lösung von: Marcel Kapma ()

var Array = new Array[Int](101)
var Array2 = new Array[Int](101)

var inti = scala.util.Random
var rand = inti.nextInt(100)
var counter = 0
var counter2 = 0
/*Schleife zum befüllen*/
while(counter != 100){
  Array(counter) = rand
  rand = inti.nextInt(100)
  counter = counter + 1
}

/*schleife zum abfrage*/
while(counter2 != 100){
  if(Array(counter2) == 0){
    Array(counter2) = inti.nextInt(100)
  }
  Array2(counter2) = Array(counter2) % 3
  if(Array2(counter2) == 0){
    
    println(Array(counter2) + " ist durch 3 teilbar")
  }
counter2 = counter2 + 1  
}
                

Lösung von: Name nicht veröffentlicht

public static void main(String[] args) {
		int [] arr = {2, 17, 10, 9, 16, 3,9,16,5,1,17,14};
		int anzahl = 0;
		for(int i = 0; i < arr.length;i++){
			if(arr[i] % 3 == 0){
				anzahl++;
			}
		}
		System.out.print("In dem Array sind ");
		System.out.print(anzahl);
		System.out.print(" Elemente durch 3 teilbar.");
	}
                

Lösung von: Loris Leuenberger (BiCT AG)

Module Module1
    Sub Main()
        Dim arr() As Integer = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14}
        Dim count As Integer = 0
        Console.WriteLine("Folgenede Zahlen im Array sind durch 3 teilbar: ")
        For Each el In arr
            If el Mod 3 = 0 Then
                Console.WriteLine(el)
                count += 1
            End If
        Next
        Console.WriteLine(vbNewLine & "Anzahl: " & count)
        Console.ReadLine()
    End Sub
End Module
                

Lösung von: Elias Zech (Optics Balzers)

<?php

$arr = array(2, 17, 10, 9, 16, 3, 9, 16, 5, 15, 1, 17, 14);

for ($i =0; $i < count($arr); $i++)
{
    if ($arr[$i] % 3 == 0)
    {
        echo $arr[$i] . " kann man durch 3 teilen";
        echo "<br>";
    }
}



?>
                

Lösung von: Maik Scheiermann (Powercloud GmbH)

#include <stdio.h>

int main(void) {
	int i, summeZahl[12] = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};
	int zahlenDurchDrei = 0;
	
	for(i = 0; i < 12; i++) {
		
		if(summeZahl[i] % 3 == 0) {
			zahlenDurchDrei++;
		}
	}
	printf("Es sind: %d Zahlen durch 3 teilbar", zahlenDurchDrei);
	
	return 0;
}

                

Lösung von: Max Maro (Baumschule )

print(len([a for a in array if a%3==0]))
                

Lösung von: rob ert (tub)

def array(zahl):
    counter = 0
    for i in zahl:
        if i % 3 == 0:
            counter += 1
            print(i, end = "")
    print(" das heißt", counter, "zahlen in der Liste sind teilbar durch
3")
    

zahl = [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14]

array(zahl)

                

Lösung von: Hermann Lallah (THM)

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	int man[] = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 }, j = 0;

	for(int i = 0; i <= 12; i++)
	{
		if (man[i] % 3 == 0)
		{
			j++;
		}
	}

	cout << "Es sind " << j << " Zahlen des Arrays durch 3 teilbar." << endl;

	system("PAUSE");
}
                

Lösung von: Name nicht veröffentlicht

#include <stdio.h>



int main()

{
   int i;
   int Modulo;
   int lang;
   int z;
   z=0;

   int array[] = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};

   lang=sizeof(array)/sizeof(int);


   for(i=0;i<=lang;i++)
   {

       Modulo=array[i]%3;

       if(Modulo==0)
       {
           printf("%i ist eine Primzahl\n",array[i]);

           z++;
       }

       else if(array[i]!=0)

       {
           printf("%i ist keine Primzahl\n",array[i]);
       }
   }



   printf("ingesammt kommen %i Primzahlen vor",z);


return 0;

}


                

Lösung von: Name nicht veröffentlicht

public class ArrayDurch3 {

	public static void main(String[] args) {
		int [] number = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};
		System.out.println(display(division3(number)));
	}
	
	public static int[] division3 ( int[] n) {
		for (int i = 0; i< n.length; i++) {
				
			int x = n[i];
			if(x%3 == 0) {
				 n[i] = x;
			}
			else {
				n[i] = 0;
			}
		}
	return  n;
	}

	public static String display(int[] x) {
		
		String string = "";
		for(int i=0; i<x.length; i++) {
			int y = x[i];
			if(y != 0) {
				string = string + y + ", ";
			}
		}
		return string;
	}
}

                

Lösung von: Name nicht veröffentlicht

using System;
namespace Array
{
    class Program
    {
        static void Main()
        {
            int[] array = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
            int a = 0;
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] % 3 == 0)
                {
                    a++;
                }
            }
            Console.WriteLine("Anzahl, welche durch 3 Teilbar sind: {0}", a);
            Console.ReadKey();  // endl
        }
    }
}
                

Lösung von: Name nicht veröffentlicht

// C++ 17

#include <iostream>
#include <vector>

template <typename T, typename Func>
void vector_iter(const std::vector<T>& v, const Func& func) {
    for (const auto& e : v) func(e);
}

int main() {
    std::vector<int> v{ 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
    const auto d{ 3 };
    auto c{ 0 };
    vector_iter(v, [&](auto e) { if (e % d == 0) c++; });
    std::cout << "Anzahl der durch " << d << " teilbaren Zahlen: " << c << "\n";
}
                

Lösung von: Jens Kelm (@JKooP)

// NET 6.x | C# 10.x | VS-2022

(int count, IList<int> lst) GetNumbersModX(IList<int> l, int d) => (l.Count(x => x % d == 0), l.Where(x => x % d == 0).ToList());

var (count, lst) = GetNumbersModX(new List<int> { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 }, 3);
Console.WriteLine($"Anzahl: {count} | Zahlen: {string.Join(", ", lst)}");

// Anzahl: 3 | Zahlen: 9, 3, 9
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

Im obigen Array sind genau drei Zahlen durch drei teilbar, nämlich zwei mal die Neun und einmal die Drei selbst.

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 0.25
Schwierigkeit: Mittel
Webcode: 07oi-q7s2
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen