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.

Programmproblem

Empfohlene Antworten

Veröffentlicht

Hi,

Ich bin neu hier und habe ein Problem mit meinem Programm.

Das Programm soll aus einer in Arrays (sas1-8) eingelesenen Tabelle (8*22 Felder) die Differenzen von den Werten der benachbarten Spalten berechnen. Also Feld (0,0) - Feld (0,1) z.B. und das nur dann, wenn in keinem der beiden Felder 100 drinsteht. (if-BEdingung).

Das Programm läuft zwar (die Zahlen in der Tabelle werden auch richtig eingelesen), aber die FOR-Schleife, in der die Differenzen berechnet werden, wird NICHT durchlaufen (zumindest zeigt er mir das STOP (rote Zeile) nicht an). Ich weiß einfach nicht woran das liegt. Vllt könnt ihr mir das sagen.

Hier der Code:

#include <stdio.h>

#include <stdlib.h>


int main () {

int i;

int j;

int k;


printf("Start \n");

double *sas1;

double *sas2;

double *sas3;

double *sas4;

double *sas5;

double *sas6;

double *sas7;

double *sas8;

double *dg0;

double *dg1;

double *dg2;

double *dg3;

double *dg4;

double *dg5;

double *dg6;

// double *sasi;


sas1=(double*) malloc(22*sizeof(double));

sas2=(double*) malloc(22*sizeof(double));

sas3=(double*) malloc(22*sizeof(double));

sas4=(double*) malloc(22*sizeof(double));

sas5=(double*) malloc(22*sizeof(double));

sas6=(double*) malloc(22*sizeof(double));

sas7=(double*) malloc(22*sizeof(double));

sas8=(double*) malloc(22*sizeof(double));

dg0=(double*) malloc(22*sizeof(double));

dg1=(double*) malloc(22*sizeof(double));

dg2=(double*) malloc(22*sizeof(double));

dg3=(double*) malloc(22*sizeof(double));

dg4=(double*) malloc(22*sizeof(double));

dg5=(double*) malloc(22*sizeof(double));

dg6=(double*) malloc(22*sizeof(double));

// sasi=(double*) malloc(15*sizeof(double));


printf("Einlesen der Daten\n");


FILE *fdata;

fdata=fopen("tore","r");


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

fscanf(fdata,"%lf",&sas1[i]);

fscanf(fdata,"%lf",&sas2[i]);

fscanf(fdata,"%lf",&sas3[i]);

fscanf(fdata,"%lf",&sas4[i]);

fscanf(fdata,"%lf",&sas5[i]);

fscanf(fdata,"%lf",&sas6[i]);

fscanf(fdata,"%lf",&sas7[i]);

fscanf(fdata,"%lf",&sas8[i]);

}


fclose(fdata);


printf("Einlesen beendet\n");

printf("Berechnung der Differenzen\n");


for (j=0;i<22;i++) {

  dg0[j]=0;

  [COLOR="Red"]printf("STOP\n");[/COLOR]

  if (sas1[j]==100) {dg0[j]=100; printf("stimmt \n");}

  if (sas2[j]==100) {dg0[j]=100; printf("stimmt2 \n");}

  else {dg0[j]=sas2[j]-sas1[j]; printf("stimmt3 \n");}


  dg1[j]=0;

  if (sas2[j]==100) {dg1[j]=100;}

  if (sas3[j]==100) {dg1[j]=100;}

  else {dg1[j]=sas3[j]-sas2[j];}


  dg2[j]=0;

  if (sas3[j]==100) {dg2[j]=100;}

  if (sas4[j]==100) {dg2[j]=100;}

  else {dg2[j]=sas4[j]-sas3[j];}


  dg3[j]=0;

  if (sas4[j]==100) {dg3[j]=100;}

  if (sas5[j]==100) {dg3[j]=100;}

  else {dg3[j]=sas5[j]-sas4[j];}


  dg4[j]=0;

  if (sas5[j]==100) {dg4[j]=100;}

  if (sas6[j]==100) {dg4[j]=100;}

  else {dg4[j]=sas6[j]-sas5[j];}


  dg5[j]=0;

  if (sas6[j]==100) {dg5[j]=100;}

  if (sas7[j]==100) {dg5[j]=100;}

  else {dg5[j]=sas7[j]-sas6[j];}


  dg6[j]=0;

  if (sas7[j]==100) {dg6[j]=100;}

  if (sas8[j]==100) {dg6[j]=100;}

  else {dg6[j]=sas8[j]-sas7[j];}

}


printf("Berechnung beendet\n");

printf("Ausgabe:\n");


for (k=0;k<22;k++) {

printf("k: %d \n", k);

printf("0: %lf \n",dg0[k]);

/*printf("1: %lf \n",dg1[k]);

printf("2: %lf \n",dg2[k]);

printf("3: %lf \n",dg3[k]);

printf("4: %lf \n",dg4[k]);

printf("5: %lf \n",dg5[k]);

printf("6: %lf \n",dg6[k]);

*/

}

/*printf("Schreiben der Ausgabedatei\n");


FILE *fdat;

fdat=fopen("plot.dat","w");


for (k=0;k<22;k++) {

  if (dg0[k]==100) {continue;}

  if (dg1[k]==100) {continue;}

  else{ fprintf(fdat,"%lf %lf",dg0[k], dg1[k]);

	fprintf(fdat,"\n");}


  if (dg1[k]==100) {continue;}

  if (dg2[k]==100) {continue;}

  else{ fprintf(fdat,"%lf %lf",dg1[k], dg2[k]);

	fprintf(fdat,"\n");}


  if (dg2[k]==100) {continue;}

  if (dg3[k]==100) {continue;}

  else{ fprintf(fdat,"%lf %lf",dg2[k], dg3[k]);

	fprintf(fdat,"\n");}


  if (dg3[k]==100) {continue;}

  if (dg4[k]==100) {continue;}

  else{ fprintf(fdat,"%lf %lf",dg3[k], dg4[k]);

	fprintf(fdat,"\n");}


  if (dg4[k]==100) {continue;}

  if (dg5[k]==100) {continue;}

  else{ fprintf(fdat,"%lf %lf",dg4[k], dg5[k]);

	fprintf(fdat,"\n");}


  if (dg5[k]==100) {continue;}

  if (dg6[k]==100) {continue;}

  else{ fprintf(fdat,"%lf %lf",dg5[k], dg6[k]);

	fprintf(fdat,"\n");}


}


fclose(fdat);


printf("Schreiben beendet\n");

*/

printf("Auftrag ausgeführt\n");


return 0;

}

Schonmal vielen Dank im voraus. Ich hoffe auf eine schnelle Antwort.

checko

Moin.

FILE *fdata;

fdata=fopen("tore","r");

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

...

}

fclose(fdata);

printf("Einlesen beendet\n");

printf("Berechnung der Differenzen\n");

for (j=0;i<22;i++) {

Und jetzt bitte mal genau hinschauen! :D :e@sy

Hi,

Oh, das habe ich ja gar nicht gesehen. Vielen vielen Dank.

@erster Beitrag: Ich benutze doch Arrays.

Gruß

checko

Hi TDM,

Ich verstehe nicht ganz, was du meinst mit

Den Rückgabewert von malloc castet man nicht.

Vielleicht kannst du mir es erklären. Das wäre sehr nett von dir.

Gruß

checko

Vielleicht kannst du mir es erklären.
Ich kann's auch:

sas1=[COLOR="Red"](double*)[/COLOR] malloc(22*sizeof(double))

Der rot markierte Teil ist überflüssig, und kann in ganz bestimmten Situationen sogar dafür sorgen, dass statt eines (leicht zu behebenden) Compilezeitfehlers ein unangenehmer Laufzeitfehler auftritt.

Und wenn du jetzt sagst, dass dein Compiler den Code ohne diesen roten Teil nicht akzeptiert, muss ich dir antworten, dass du deinen C-Code durch einen C++-Compiler jagst ;)

Hi Klotzkopp,

Für was schreibt man denn dann diesen roten Teil?

Das verstehe ich jetzt nicht ganz.

checko

Hi TDM,

Wenn ich das also richtig verstehe, brauche ich das also deswegen nicht zu schreiben, weil der Datentap des Arrays gleichbleibt. Ist das so richtig?

@all: Ich möchte nochmal danke sagen, für all das, was ich hier schon gelernt habe.

checko

Wenn ich das also richtig verstehe, brauche ich das also deswegen nicht zu schreiben, weil der Datentap des Arrays gleichbleibt. Ist das so richtig?

Nein. Die Funktion malloc gibt einen void-Zeiger zurück. Du weist das einem double-Zeiger zu. Die Umwandlung von void* nach double* ist in C implizit, das heißt, sie passiert automatisch. Die explizite Typumwandlung mit dem Cast ist daher überflüssig, weil ohne genau dasselbe passiert.

Hi Klotzkopp,

Ich verstehe. Eine Frage habe ich aber noch:

Heißt das jetzt, dass ich das "double*" nie schreiben muss oder gibt es Situationen, in denen es doch notwendig ist?

checko

Heißt das jetzt, dass ich das "double*" nie schreiben muss oder gibt es Situationen, in denen es doch notwendig ist?
Es gibt jede Menge vorstellbarer Situationen, in denen ein Cast auf double* notwendig sein kann. Merk dir einfach, dass so ein Cast niemals notwendig ist, wenn malloc und verwandte Funktionen im Spiel sind ;)

Hi,

Ich habe hier noch ein anderes Problem. Der Input sind zwei 8*26 Tabellen aus zwei verschiedenen Dateien, die das Programm nacheinander einlesen soll.

Die Daten aus der ersten Datei liest er auch ohne Probleme ein. Doch die Daten aus der zweiten Datei werden komischer weise nicht mehr eingelesen, wie mir der Print-Befehl vor der Berechnung zeigt.

Angemerkt sei noch, dass die erste Zahl aus der zweiten Datei richtig eingelesen wird, alle anderen liest er aber nicht mehr ein.

Ihr habt mir schon einmal geholfen und ich würde mich freuen, wenn ihr es wieder tuen könntet.

Schon mal vielen Dank im Voraus.

checko

Hier der Code:


#include <stdio.h>

#include <stdlib.h>


int main () {

int i;

int j;

int k;

int a;

int b;


//Benötigte Größen

double sum1;

double sum2;

double sum3;

double sum4;

double sum5;

double sum6;

double sum7;

double sum8;


sum1=0;

sum2=0;

sum3=0;

sum4=0;

sum5=0;

sum6=0;

sum7=0;

sum8=0;


//Benötigte Arrays

printf("Start \n");

double *sas1;

double *sas2;

double *sas3;

double *sas4;

double *sas5;

double *sas6;

double *sas7;

double *sas8;

double *eta1;

double *eta2;

double *eta3;

double *eta4;

double *eta5;

double *eta6;

double *eta7;

double *eta8;

// double *sasi;


sas1=(double*) malloc(26*sizeof(double));

sas2=(double*) malloc(26*sizeof(double));

sas3=(double*) malloc(26*sizeof(double));

sas4=(double*) malloc(26*sizeof(double));

sas5=(double*) malloc(26*sizeof(double));

sas6=(double*) malloc(26*sizeof(double));

sas7=(double*) malloc(26*sizeof(double));

sas8=(double*) malloc(26*sizeof(double));

eta1=(double*) malloc(26*sizeof(double));

eta2=(double*) malloc(26*sizeof(double));

eta3=(double*) malloc(26*sizeof(double));

eta4=(double*) malloc(26*sizeof(double));

eta5=(double*) malloc(26*sizeof(double));

eta6=(double*) malloc(26*sizeof(double));

eta7=(double*) malloc(26*sizeof(double));

eta8=(double*) malloc(26*sizeof(double));

// sasi=(double*) malloc(15*sizeof(double));


printf("Einlesen der 1. Datei\n");


FILE *fdata;

fdata=fopen("tore","r");


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

fscanf(fdata,"%lf",&sas1[i]);

fscanf(fdata,"%lf",&sas2[i]);

fscanf(fdata,"%lf",&sas3[i]);

fscanf(fdata,"%lf",&sas4[i]);

fscanf(fdata,"%lf",&sas5[i]);

fscanf(fdata,"%lf",&sas6[i]);

fscanf(fdata,"%lf",&sas7[i]);

fscanf(fdata,"%lf",&sas8[i]);

}


fclose(fdata);

printf("Einlesen der 1. Datei beendet\n");


printf("Einlesen der 2. Datei\n");


FILE *fdato;

fdato=fopen("etat","r");


for (a=0;a<26;a++) {

fscanf(fdato,"%lf",&eta1[a]);

fscanf(fdato,"%lf",&eta2[a]);

fscanf(fdato,"%lf",&eta3[a]);

fscanf(fdato,"%lf",&eta4[a]);

fscanf(fdato,"%lf",&eta5[a]);

fscanf(fdato,"%lf",&eta6[a]);

fscanf(fdato,"%lf",&eta7[a]);

fscanf(fdato,"%lf",&eta8[a]);

}


fclose(fdato);


for (b=0;b<26;b++) {

printf("S1: %lf, S2: %lf, S3: %lf, S4: %lf \n",eta1[b],eta2[b],eta3[b],eta4[b]);

printf("S5: %lf, S6: %lf, S7: %lf, S8: %lf \n",eta5[b],eta6[b],eta7[b],eta8[b]);

}


//Abhier kommt die weiterführende Berechnung, die hier nicht mehr wichtig ist. 

//Falls sie doch notwendig ist, setzte ich sie noch nachträglich rein.

PS: Ich habe schon gelernt, dass bei dem malloc-Befehl das (double*) überflüssig ist. Ich schreibe es aber aus Gewohnheit. :)

Bearbeitet von checko

Hi,

Hat sich erledigt, das Problem. Der Fehler lag in der Input-Datei. Er konnte das "," nicht lesen.

checko

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.