Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

werte

Empfohlene Antworten

Veröffentlicht

hallo,

warum verhält sich die variable i im folgenden code so komisch ?

sie sollte jeweils eigentlich um 1 erhöht werden, erhöht sich bei mir aber um 1millionirgendwas. wenn ich double sum[1]; zu double sum[2]; ändere klappt alles. bin anfänger und deswegen für eine klärung sehr dankbar.

#include <cstdlib>

#include <iostream>

#include <math.h>

#include <stdio.h>

using namespace std;

// Schwerpunkt einer Punktschar

// pn=Anzahl der Punkte

void fc_splist(double feld[][2], int pn)

{

int i=0;

double sum[1];

sum[0]=0, sum[1]=0;

for (i=0; i<pn; i++)

{ cout << "\ni: " << i;

sum[0]=sum[0]+feld[0];

sum[1]=sum[1]+feld[1];

}//for

sum[0]=sum[0]/pn;

sum[1]=sum[1]/pn;

cout << "\nxs:" << sum[0] << " ys:" << sum[1] << "\n";

cout << "\ni: " << i;

}

int main()

{

double b [3][2]={

{1,2},

{3,4},

{10.9,12.3}

};

fc_splist(b,3);

system("PAUSE");

return 1;

}

warum nicht ?

und warum hat es einfluss auf i ?

warum nicht ?
Weil es ein Array mit nur einem Element ist. sum[2] wäre das zweite Element. Wenn dein Array kein zweites Element hat, darfst du nicht darauf zugreifen.

Wenn du ein Array als "typ name[x]" deklarierst, dann ist der größte erlaubte Index für den Zugriff x-1, nicht x.

und warum hat es einfluss auf i ?

Dein Code erzeugt undefiniertes Verhalten, insofern ist es ziemlich sinnlos, sich darüber Gedanken zu machen, was passiert, weil alles passieren könnte. Der Code könnte sogar funktionieren.

Tatsächlich wird es wohl so sein, dass i im Speicher hinter sum liegt. Indem du auf sum hinter dessen Ende zugreifst, pfuschst du im Speicherbereich von i herum.

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.