Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Feld filtern (1) (Felder)

Füllen Sie ein Feld mit den Zahlen:

2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14.

Schreiben Sie nun ein Programm, das aus dem obigen Feld ein neues Feld erzeugt, das nur noch die Zahlen enthält, die größer als 10 sind. Der Prototyp der Funktion soll wie folgt aussehen:

filter(original: integer[]) : integer[]

1 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

Kommentare (1)

BKluszynski 9. März 2012 22:41   reply report
Eine der Lösungen die hier unter "generic" angegeben sind ist eigentlich in ABAP geschrieben, sobald es das zur Auswahl gibt ändere ich es.

LG

23 Lösung(en)

# Zahlen grösser 10 werden gefiltert
def filter(a):   
   b=[]
   for zahl in a:
      if zahl > 10:
         b.append(zahl)
   return b
   
print filter([2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14])

                

Lösung von: Martin Guggisberg (Universität Basel / PH FHNW)

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="de">

  <head>
    <title>Filter</title>  
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <script type="text/javascript" src="filter.js"></script> 
  </head>

  <body> 
    <h1>Filter</h1>
    <input id= "f1" type="text" size="5" /> <br />
    <input id= "f2" type="text" size="5" /> <br />
    <input id= "f3" type="text" size="5" /> <br />
    <input id= "f4" type="text" size="5" /> <br />
    <input id= "f5" type="text" size="5" /> <br />
    <input id= "f6" type="text" size="5" /> <br />
    <input id= "f7" type="text" size="5" /> <br />
    <input id= "f8" type="text" size="5" /> <br />
    <input id= "f9" type="text" size="5" /> <br />
    <input id="f10" type="text" size="5" /> <br />
    <input id="f11" type="text" size="5" /> <br />
    <input id="f12" type="text" size="5" /> <br />
    
    <input onclick="filtern();" type="button" value="Go" />

    <p id="ausgabe" />

  </body>
</html>

/////////////////////////////////////////////////////////////
// filter.js:
/////////////

function filtern() {
    alleZahlenArray  = fuenfWerteEinlesen();
    gefilterterArray = kleiner11Rausfilter(alleZahlenArray);
    ausgabe(gefilterterArray); }

function fuenfWerteEinlesen() {
    alleZahlen = new Array(12);
    for(index = 0; index < alleZahlen.length; index = index + 1) {
	alleZahlen[index] = zahlVonFeldLesen(index + 1);  }
    return alleZahlen; }

// Feldnummer 1..5
function zahlVonFeldLesen(feldNummer) {
   ele = document.getElementById("f" + feldNummer);
   val = ele.value;
   return val*1;  }

function ausgabe(resultatArray) {
    ausgabeText = "Ausgabe :" + arrayCSV(resultatArray);
    ausgabePara = document.getElementById("ausgabe");
    ausgabePara.innerHTML = ausgabeText;
}

// Array in Comma-Separated-Values (CSV) String umwandeln
function arrayCSV(array) {
    len = array.length;
    resultat = "";
    for(index = 0; index < len; index = index + 1) {
	resultat = resultat + array[index];
	if(index < len -1) {
	    resultat = resultat + ", ";	}
    }
    return resultat;
}

// resultat enthält alle elemente > 10
function kleiner11Rausfilter(alleZahlenArray) {
    lenNeu = elementeGroesserZehnZaehlen(alleZahlenArray);
    resultat = new Array(lenNeu);
    elementeGroesserZehnKopieren(alleZahlenArray, resultat);
    return resultat;
} 

// > 10
function elementeGroesserZehnZaehlen(zahlen) {
    anzahl = 0;
    for(zahl in zahlen) {
	if(zahl > 10) {
	    anzahl = anzahl + 1;
	}
    }
    /* identisch:
       for(i = 0; i < zahlen.length; i=i+1) {
         zahl = zahlen[i];
	 if(zahl > 10) {
	   anzahl = anzahl + 1;
	 }
       }
     */
    return anzahl;
}

// >10  aus "alt" in "neu" kopieren
function elementeGroesserZehnKopieren(alt, neu) {
    f = 0; t = 0;
    while(f < alt.length) {
	if(alt[f] > 10) {
	    neu[t] = alt[f];
	    t = t + 1;
	}
	f = f + 1;
    }
}
                

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

  /**
  * filter
  *
  * filtert aus einem Interger-Feld die Elemente heraus, die größer als 10 sind.
  * Liefert ein Interger-Feld erg zurück, welches nur noch die nicht gefilterten
  * Elemente enthält. Das Interger-Feld erg hat eine um die gefilterten Elemente
  * verringerte Länge gegenüber dem Interger-Feld original.
  *
  * @param original int[]
  * @return erg int[]
  */
  public int[] filter(int[] original){
  
    // Temporäres Integer-Feld zur Aufnahme der neu gereihten Elemente
    int[] f = new int[original.length];
    // Elementzaehler für die Elemente mit Wert größer 10
    int countElem = 0;

    for (int i = 0; i < original.length; i++){
    
      // Aktuelles Element bestimmen
      int elem = original[i];
      
      // Elemente filtern und neu reihen
      if (elem > 10){
        countElem = countElem + 1;
        f[countElem-1] = elem;
      }
    }

    int[]erg = new int[countElem]; // Rückgabefeld anlegen
    // Kopieren der nicht gefilterten Elemente in das Rückgabefeld.
    for (int i = 0; i < countElem; i++){
      erg[i] = f[i];
    }
    return erg;
  }
                

Lösung von: Jürgen Mang (Heinrich-Emanuel-Merck-Schule Darmstadt)

*process langlvl(saa2);
*process aggregate, attributes;
*process flag(W), source, insource, options, xref,nest, number, offset;
*process gonumber, snap;

 /*********************************************************************/
 /* Autor : philipp gressly freimann (@ http://www.sanits-training.ch)*/
 /* Datum : 16. Nov. 2011                                             */
 /* Aufgaben Kap 6: Felder (http://www.programmieraufgaben.ch)        */
 /*********************************************************************/

 Felder : proc options(main noexecops);

  dcl Zahlen dim (12) bin fixed(31)
      init(2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14);

  dcl Anz                 bin fixed(15);
  dcl Gefiltert  dim(Anz) bin fixed(31) controlled;
  call filter(Zahlen, Gefiltert);
  put skip list("gefilterte Liste:");
  call ausgabe(Gefiltert);
  free Gefiltert;

  filter: proc(fld, result);
    dcl fld dim(*) bin fixed(31);
    dcl result dim(Anz) bin fixed(31) controlled;
    Anz = countBig(10, fld);
    allocate result;
    call fillBig(10, fld, result);
  end filter;

  countBig: proc (infimum, feld) returns (bin fixed(15));
    dcl infimum        bin fixed(31);
    dcl feld    dim(*) bin fixed(31);

    dcl anzahl         bin fixed(15) init(0);
    dcl idx            bin fixed(15);
    do idx = 1 to dimension(feld);
      if(infimum < feld(idx)) then do;
        anzahl += 1;
      end; /* if */
    end; /* do */
    return (anzahl);
  end countBig;

  fillBig: proc (infimum, in, out);
    dcl infimum         bin fixed(31);
    dcl in       dim(*) bin fixed(31);
    dcl out      dim(*) bin fixed(31);

    dcl idxIn           bin fixed(15);
    dcl idxOut          bin fixed(15) init(1);
    do idxIn = 1 to dimension(in);
      if(infimum < in(idxIn)) then do;
        out(idxOut) = in(idxIn);
        idxOut += 1;
      end; /* if */
    end; /* do */
  end fillBig;

  ausgabe: proc(fld);
    dcl fld dim(*) bin fixed(31);
    dcl i          bin fixed(15);
    do  i = 1 to dimension(fld);
      put edit(i, fld(i)) (p'-----9.', p'--9');
    end; /* do */
  end ausgabe;

 end Felder;
                

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

{- (c) Reto Hablützel / rethab.ch, 2011 -}

filter' :: (Integral a) => [a] -> [a]

filter' xs = filter (>10) xs

main = putStrLn . show $ filter' [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14]
                

Lösung von: Reto Hablützel (www.rethab.ch)

"ABAP CODE

REPORT  z_filter_field.

DATA: feld TYPE STANDARD TABLE OF i,
      wa LIKE LINE OF feld,
      feld2 like feld.


APPEND 2 TO feld.
APPEND 17 TO feld.
APPEND 10 TO feld.
APPEND 9 TO feld.
APPEND 16 TO feld.
APPEND 3 TO feld.
APPEND 9 TO feld.
APPEND 16 TO feld.
APPEND 5 TO feld.
APPEND 1 TO feld.
APPEND 17 TO feld.
APPEND 14 TO feld.


LOOP AT feld INTO wa.
IF wa > 10.
  APPEND wa to feld2.
  WRITE wa.
ENDIF.
ENDLOOP.
                

Lösung von: Benjamin Kluszynski (( Coder :-) ))

class Program
    {
        static void Main(string[] args)
        {
            int[] feld = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
            (from f in feld where f > 10 select f).ToList<int>().ForEach(i => Console.Write("{0} ", i));
            Console.ReadLine();
        }
    }
                

Lösung von: Luca Welker (SBS Software GmbH)

"""Feld filtern"""

feld = [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14]
feld_neu = []
for i in feld:
    if i> 10:
        feld_neu.append(i)
print(feld_neu)
    
    
                

Lösung von: Py Thon ()

// Autor:				Andy Großhennig
// Solution for task:	Feld filtern (1) (Felder)

#include <iostream>

using namespace std;

// Function: Take the array and get all numbers bigger than ten
void sort(short sh_arrNumbers[12])
{
	// Loop: Check all numbers of the value and show it if the number is bigger than ten
	for(short sh = 0;sh < 12;sh++)
	{
		if(sh_arrNumbers[sh] > 10)
		{
			cout << sh_arrNumbers[sh] << "  ";
		}
	}
}

void arr()
{
	short sh_arrNumbers[12] = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};

	for(short sh = 0;sh < 12;sh++)
	{
		cout << sh_arrNumbers[sh] << "  ";
	}
	cout << "\n\n";

	sort(sh_arrNumbers);
}

int main()
{
	arr();
	
	cout << "\n\n";
	system("Pause");
	return 0;
}
                

Lösung von: Andy Großhennig (Bundeswehr)

def filter(liste):
      return [i for i in liste if i>10]

print(filter([2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14]))
                

Lösung von: Name nicht veröffentlicht

unfiltered_array = [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14]
def filter(array)
  filtered_array = []
  array.each {|x| filtered_array << x if x > 10}
  return filtered_array
end
puts filter(unfiltered_array).inspect
                

Lösung von: Name nicht veröffentlicht

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>


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

	for(int i = 0; i < 12; i += 1) {
		if(iFeld[i] >= 10) {
			iFeld2[i] = iFeld[i];
			printf("%i\n", iFeld2[i]);
		}
	}

	getchar();
	return 0;
}
                

Lösung von: Elias Zech (Optics Balzers)

arr = [2,17,10,9,16,3,9,16,5,1,17,14]
arr.select! {|elem| elem >10}
p arr
                

Lösung von: Thomas Lüthi (Xovis AG)

/**
 * Füllen Sie ein Feld mit den Zahlen: 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14.
 * Schreiben Sie nun ein Programm, das aus dem obigen Feld ein neues Feld
 * erzeugt, das nur noch die Zahlen enthält, die größer als 10 sind. Der
 * Prototyp der Funktion soll wie folgt aussehen:
 * 
 * filter(original: integer[]) : integer[]
 * 
 * @author rober_000
 *
 */
public class Filter {
	final static int[] original = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };

	public static void main(String[] args) {
		int[] transform = original;
		transform = filter(transform);
		printArray(transform);

	}
	private static void printArray(int[] x) {
		for (int i = 0; i < x.length; i++) {
			System.out.print(x[i]);
			System.out.print(" ");
			
		}
	}

	private static int[] filter(int[] original) {
		int counter = 0;
		for (int i = 0; i < original.length; i++) {
			if (original[i] >= 10) {
				counter++;
			}
		}
		
		int[] neuesFeld = new int[counter];
		
		counter = 0;
		for (int i = 0; i < original.length; i++) {
			if (original[i] >= 10) {
				neuesFeld[counter] = original[i];
				counter++;
			}
		}
		return neuesFeld;

	}
}

                

Lösung von: Robert König (Universität Passau)

use strict;
#beispieldaten
my @array = (2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14);
#elemente finden
@array = bigger_than_10(@array);
#ausgeben
print "@array";
#function
sub bigger_than_10 {
return grep $_ >= 10, @_;
}
                

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

namespace feld_filtern
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] feld = { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
            foreach(int item in (from zahl in feld where zahl > 10 select zahl).ToList<int>())
            {
                Console.WriteLine("Größer 10: " + item);
            }             
        }
    }
}
                

Lösung von: Py Thon ()

<?php

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

for ($i = 0; $i<count($x); $i++)
{
    if ($x[$i] >= 10)
    {
        array_push($z, $x[$i]);
    }
}

for ($n=0; $n<count($z); $n++)
{
    echo $z[$n] . "<br>";
}



?>
                

Lösung von: Maik Scheiermann (Powercloud GmbH)

public class Main {

    public static void main(String[] args) {
        for(int i : filter(new int[] {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14})) {
            System.out.println(i);
        }
    }

    private static int[] filter(int[] original) {
        List<Integer> filtered = new ArrayList<>();

        for(int i : original) {
            if(i > 10) filtered.add(i);
        }

        return filtered.stream().mapToInt(Integer::intValue).toArray();
    }
}
                

Lösung von: Name nicht veröffentlicht

#include<stdio.h>


int feldFilter(int feld[]){
	int j, c;
	int feld2[] = {0};
	
	for (j=0; j<12; j++){
		if (feld[j] > 10){
			feld2[c] = feld[j];
			printf(" %d ", feld2[c]);
			c++;
		}	
	}	
}

int main() {
	int feld[] = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};
	int i;
	
	for (i=0; i<12; i++){
		printf(" %d ", feld[i]);
	}
	
	printf("\n");	
	
	feldFilter(feld);
	
	return 0;
}
                

Lösung von: Name nicht veröffentlicht

package ch.santis.programmierenlernen.kapitel6;

public class Aufgabe_6_3 {
	
	public static void main(String[] args) {
		new Aufgabe_6_3().top();
	}
	
	void top() {
		
		int [] array1 = {2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14};
		
		int [] array2;
		int array2length = 0;
		
		// Zählen wieviele Zahlen von Array1 grösser als 10 sind um die Länge vom Array2 zu bestimmen
		for(int index = 0; index < array1.length; index++) {
			if(array1[index] > 10) {
				array2length++;
			}
		}
		// Alle Zahlen von Array1 die grösser als 10 sind in die Felder von Array2 initialisieren
		array2 = new int [array2length];
		int index2 = 0;
		for(int index = 0; index < array1.length; index++) {
			if(array1[index] > 10) {
				array2[index2] = array1[index];
				index2++;
			}
		}
		// Printen
		for(int index : array1) {
			System.out.print(index + " ");
		}
		System.out.println("\n=============================");
		for(int index : array2) {
			System.out.print(index + " ");
		}
		
	}
	
}
                

Lösung von: Jan Roth (Santis Training AG)

let theArr = [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14];
let filtered = theArr.filter(num => num > 10);

console.log(filtered);
                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

// C++ 20 | VS-2022
#include <iostream>
#include <vector>
#include <ranges>

template<typename Func>
std::vector<int> get_filtered_list(std::vector<int> v_in, Func f) {
    std::vector<int> v_out{};
    for (const auto& i : v_in | std::views::filter(f)) // C++ 20!
        v_out.push_back(i); 
    return v_out;
}

void print(std::vector<int> v) {
    for(auto it{ v.begin() }; it != v.end() - 1; ++it)
        std::cout << *it << ", ";
    std::cout << v.back() << "\n";
}

int main() {
    const std::vector<int>v{ 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };

    const auto gt10{ [](int i) { return i > 10; } }; // greater than 10
    print(get_filtered_list(v, gt10));

    const auto lt10{ [](int i) { return i < 10; } }; // less than 10
    print(get_filtered_list(v, lt10));
}
                

Lösung von: Jens Kelm (@JKooP)

// NET 6.x | C# 10.x | VS-2022
var lst_in = new List<int> { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14 };
var lst_out = lst_in.Where(x => x > 10).ToList();
Console.WriteLine(string.Join(", ", lst_out));
                

Lösung von: Jens Kelm (@JKooP)

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 1
Schwierigkeit: k.A.
Webcode: zxku-89hw
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen