Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Felder umdrehen (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 die Reihenfolge im Feld umkehrt. Schreiben Sie die Methode so, dass sie auch für eine ungerade Anzahl von Werten funktioniert. Prototyp:

umkehren(feld: integer[])

Falls Ihre Programmiersprache keine Variablen-Parameter (Referenzen) erlaubt, sieht der Prototyp wie folgt aus:

umkehren(original: integer[]) : integer[]

1 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

Kommentare (1)

wwbaselmemes 10. April 2018 14:42   reply report
wer das gseht & mir e bild schickt kunnt in mini story

8 Lösung(en)

def umkehren(a):   
   a.reverse()
   print a

umkehren([2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14])
                
	int[] umtauschen(int[] array) {
		int[] tausch;
		tausch = new int[array.length];
		int tauschIndex = array.length-1;
		for (int arrayZahl: array){
			tausch[tauschIndex] = arrayZahl;
			tauschIndex= tauschIndex-1;
		}
		return tausch;
	}
                

Lösung von: Name nicht veröffentlicht

void umdrehen(int[] umzudrehen) {
  int tmp;
  for(int links = 0; links < umzudrehen.length / 2; links++) {
    int rechts = umzudrehen.length - links - 1;
    tmp                = umzudrehen[rechts];
    umzudrehen[rechts] = umzudrehen[links];
    umzudrehen[links]  = tmp;
  }
}
                

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

{$R+}
{$B+}
{FernUni}

program FeldUmdrehen (input,output);
{EingabeFeld aus 12 Elementen und Ausgabe umgedrehtes Feld}

const
GRENZE = 12;

type
tIndex = 1..GRENZE;
tFeld = array[tIndex] of integer;

var
Feld : tFeld;
LaufInd : integer;

procedure umkehren(var EingabeFeld : tFeld);
{umkehrung des Feldes}

var
NeuFeld : tFeld;
StartIndex,
LaufIndex : integer;

begin
StartIndex := 1;
LaufIndex := GRENZE;

while (StartIndex <=12) and (LaufIndex >=1) do
begin
  NeuFeld[StartIndex] := EingabeFeld[LaufIndex];
  StartIndex := StartIndex + 1;
  LaufIndex := LaufIndex -1;
end;
  EingabeFeld := NeuFeld;
end; {umkehren Ende}

begin
  write('Eingabe von 12 Zahlen!');
  writeln();
  for LaufInd := 1 to GRENZE do
  begin
    write('Geben Sie ein Wert fuer Zahl ',LaufInd, ': ');
    readln(Feld[LaufInd]);
  end;
  write('Ukehrung.....');
  umkehren(Feld);
  writeln();
  write('Ergebnis: ');
  for LaufInd := 1 to GRENZE  do
  begin
    write(Feld[LaufInd],' ');
  end;
  readln(); {Nur um die Ausgabe auf die Konsole zu sehen}
end.  
                

Lösung von: Name nicht veröffentlicht

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


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

	for(int z = 11; z >= 0; z -= 1) {
		printf("%i ", iZahl[z]);
	}

	getchar();
	return 0;
}
                

Lösung von: Elias Zech (Optics Balzers)

// macht eigentlich dasselbe wie Array.prototype.reverse,
// ändert aber nicht den ursprungsarray
function reverseArray(arr) {
  let out = [];
  for (let i = arr.length-1; i >= 0; i--) out.push(arr[i]);
  return out;
}

let theArray = [2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14];
console.log(reverseArray(theArray));                         // lissalanda@gmx.at
                

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

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

var arr = new int[] { 2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 5 };

for (var i = 0; i < arr.Length/2; i++)
    (arr[i], arr[^(i+1)]) = (arr[^(i+1)], arr[i]);

Console.WriteLine(string.Join(", ", arr));
                

Lösung von: Jens Kelm (@JKooP)

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

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

void reverse(std::vector<int>& v) {
	for (size_t i{ 0 }; i < v.size() / 2; i++)
		std::swap(v[i], v[v.size() - i - 1]);
}

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

Lösung von: Jens Kelm (@JKooP)

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 0.5
Schwierigkeit: k.A.
Webcode: jxsv-r4dw
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen