2017-04-02 2 views
0

Ich muss einen Code in C erstellen, die in einer Liste von Paaren von Zahlen mit der ersten Nummer ist die Produktnummer (zwischen 1 und und die Gesamtzahl der Produkte wie ermittelt durch die Eingabe) und die zweite Zahl ist die Bewertung von 1-10 und speichert sie in einem 2D-Array. Die Zeilen des Arrays beginnend bei 1 sind Produktnummern und die Spalten 1-10 sind Bewertungen. Spalte 11 ist die Gesamtzahl der Sterne und Spalte 12 ist die Gesamtzahl der Bewertungen. Ich muss scanf verwenden, um die Werte einzulesen, und wenn scan()! = 2, sollten die Schleifen stoppen und der Bericht generiert werden.Wie zu stoppen Eingabe lesen und analysieren Daten in C

Der Bericht sollte für jedes Produkt die Anzahl der Bewertungen für jeden Wert (d. H. 2 Bewertungen von 5 Sternen, 17 Bewertungen von 10 Sternen usw.) sowie die durchschnittliche Bewertung aufführen.

Ich denke, ich habe die Logik korrekt und mein Code kompiliert, aber es bringt Fehler zurück, wenn ich versuche, einen Bericht zu generieren. Irgendwelche Hinweise, was das Problem sein könnte?

Dies ist der Code, den ich bisher habe:

int productStar[][13] = {0}; 
int i = 0; 
int product = 0; 
int stars = 0; 
int howMany = 0; 
int ratings = 0; 
float avg = 0; 
int sum = 0; 

printf("How many unique products?\n"); 
scanf("%d", &howMany); 

printf("Please enter product # and stars\n"); 

while(1) 
{ 
    scanf("%d %d", &product, &stars); /* Reads in two values; first is product number, second is rating */ 

    if (scanf("%d %d", &product, &stars) != 2) /* kills the loop */ 
    { 
     printf("Hi from the inside\n"); 
     break; 
    } 


    productStar[product][stars] += 1; /* adds tally to specific product rating */ 

    productStar[product][11] += stars; /* increments total number of stars; used later to find avg */ 

    productStar[product][12] += 1; /* increments how many ratings */ 

} 

for (int product = 0; product <= howMany; ++product) 
{ 
for (int stars = 0; stars <= 10; ++stars) 
    { 
     avg = productStar[product][11]/productStar[product][12]; 

     printf("Product %d Information:\n", product); 
     printf("1 star: %d\n", productStar[product][1]); 
     printf("2 star: %d\n", productStar[product][2]); 
     printf("3 star: %d\n", productStar[product][3]); 
     printf("4 star: %d\n", productStar[product][4]); 
     printf("5 star: %d\n", productStar[product][5]); 
     printf("6 star: %d\n", productStar[product][6]); 
     printf("7 star: %d\n", productStar[product][7]); 
     printf("8 star: %d\n", productStar[product][8]); 
     printf("9 star: %d\n", productStar[product][9]); 
     printf("10 star: %d\n", productStar[product][10]); 
     printf("Average rating: %2.2f out of 10 (%d ratings total).\n", avg, ratings); 
     printf("--------------------\n"); 
    } 
} 

for (product = 0; product <= howMany; ++product) 
{ 
    sum += productStar[product][12]; 
    /* Sum of all ratings */ 
} 

printf("%d product ratings evaluated in all.\n\n", sum); 


return 0; 

}

+0

Alle, 'printf ("1 Stern:% d \ n", productStar [Produkt] [1]);' Linien sein können ersetzt mit nur einem insi de für eine Schleife. –

+0

Welche Art von Fehlermeldung erhalten Sie? Ich denke, es könnte mit Ihrem 'int productStar [] [13] = {0};' Initialisierung ... – Zexus

Antwort

0

Du liest von der Standardeingabe zweimal in einer Reihe, die ich dir nicht denken, tun wollen. Vielleicht versuchen, den zweiten scanf innerhalb von Ihnen zu ändern Eingabe in eine Anweisung zu lesen Schleife, das überprüft, ob Produkt und Sterne sind keine leeren Saiten:

while(1) { 
     scanf("%d %d", &product, &stars); /* Reads in two values; first is product number, second is rating */ 

     if (product == '\0' || stars == '\0') /* kills the loop */ 
     { 
      // Break out of input loop and process data. 
      break; 
     } 
    } 

    // Processing code. 

Außerdem ist es eine gute Idee für die Division zu überprüfen, durch Null:

if (productStar[product][12] != 0) { 
    avg = productStar[product][11]/productStar[product][12]; 
} 

Nachdem ich diese Änderungen Code gemacht hat perfekt funktioniert :)

#include <stdio.h> 

int productStar[][13] = {0}; 
int i = 0; 
int product = 0; 
int stars = 0; 
int howMany = 0; 
int ratings = 0; 
float avg = 0; 
int sum = 0; 

int main(void) { 
    printf("How many unique products?\n"); 
    scanf("%d", &howMany); 

    printf("Please enter product # and stars\n"); 

    while(1) 
    { 


     scanf("%d %d", &product, &stars); /* Reads in two values; first is product number, second is rating */ 

     if (product == '\0' || stars == '\0') /* kills the loop */ 
     { 
      break; 
     } 


     productStar[product][stars] += 1; /* adds tally to specific product rating */ 

     productStar[product][11] += stars; /* increments total number of stars; used later to find avg */ 

     productStar[product][12] += 1; /* increments how many ratings */ 

    } 

    for (int product = 0; product <= howMany; ++product) 
    { 
    for (int stars = 0; stars <= 10; ++stars) 
     { 
     if (productStar[product][12] != 0) { 
      avg = productStar[product][11]/productStar[product][12]; 
     } 

      printf("Product %d Information:\n", product); 
      printf("1 star: %d\n", productStar[product][1]); 
      printf("2 star: %d\n", productStar[product][2]); 
      printf("3 star: %d\n", productStar[product][3]); 
      printf("4 star: %d\n", productStar[product][4]); 
      printf("5 star: %d\n", productStar[product][5]); 
      printf("6 star: %d\n", productStar[product][6]); 
      printf("7 star: %d\n", productStar[product][7]); 
      printf("8 star: %d\n", productStar[product][8]); 
      printf("9 star: %d\n", productStar[product][9]); 
      printf("10 star: %d\n", productStar[product][10]); 
      printf("Average rating: %2.2f out of 10 (%d ratings total).\n", avg, ratings); 
      printf("--------------------\n"); 
     } 
    } 



    for (product = 0; product <= howMany; ++product) 
    { 
     sum += productStar[product][12]; 
     /* Sum of all ratings */ 
    } 

    printf("%d product ratings evaluated in all.\n\n", sum); 


    return 0; 
} 
+0

Wie hast du es zum Funktionieren gebracht? Ich finde, dass jedes Mal, wenn ich kompiliere, bestimmte Werte im Array nicht auf Null initialisiert werden. Darüber hinaus berechnet der Code auch nicht die Durchschnittswerte. – Devon

Verwandte Themen