• 0
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /var/www/html/cnasolution/application/views/question.php
Line: 191
Function: _error_handler

File: /var/www/html/cnasolution/application/controllers/Questions.php
Line: 419
Function: view

File: /var/www/html/cnasolution/index.php
Line: 315
Function: require_once

so here is my code:

#include  #define NUM_SCORES 4   double CalculateFinalScore(int assignment[], int midterm[], int finalExam[], double scoreSum[], char gradeAchieved[])  {     int i = 0;      for (i = 0; i < NUM_SCORES; ++i)      {         scoreSum[i] = (assignment[i] * 0.2) + (midterm[i] * 0.3) + (finalExam[i] * 0.5);     } }  char grade(int assignment[], int midterm[], int finalExam[], double scoreSum[], char gradeAchieved[]){     char A = 'A';     char B = 'B';     char C = 'C';     char D = 'D';     char E = 'E';      int j = 0;      for (j = 0; j < NUM_SCORES; ++j){      if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 85 ) {         gradeAchieved[j] = A;     }     else if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 75) && (CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) < 85)  ){         gradeAchieved[j] = B;     }     else if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 60) && (CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) < 75)  ){         gradeAchieved[j] = C;     }     else if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 45) && (CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) < 60)  ){         gradeAchieved[j] = D;     }      else if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 0) && (CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) < 45)  ){         gradeAchieved[j] = E;     }     }  }  int main(void)  {     char finalGrade[NUM_SCORES];     double testScores[NUM_SCORES];      int i = 0;     int x[NUM_SCORES];     int y[NUM_SCORES];     int z[NUM_SCORES];      // Prompt user to enter test scores        for (i = 0; i < NUM_SCORES; ++i)      {         printf("Input student %d assignment score: ", (i+1));         scanf("%d", &(x[i]));         printf("Input student %d midterm score: ", (i+1));         scanf("%d", &(y[i]));         printf("Input student %d final exam score: ", (i+1));         scanf("%d", &(z[i]));         printf("
");      }      printf("
");     // Call function to calculate final score     CalculateFinalScore(x, y, z, testScores, finalGrade);      for (i = 0; i < NUM_SCORES; ++i)      {         printf("Final student %d test score: ", (i+1));         printf("%lf
", testScores[i]);          printf("Final student %d test score: ", (i+1));         printf("%lf
", finalGrade[i]);     }      return 0; } 

So bassicly, unlike in my code where NUM_SCORES is alerady determined with 4. instead I want to input the NUM_SCORES value so I can determined its value easily, but everytime I do that, there is always an error that my NUM_SCORES is undeclared. oh and I also got these error

main.c: In function ‘grade’: main.c:26:41: error: expected expression before ‘]’ token      if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 85 ) {                                          ^ main.c:26:105: error: expected ‘)’ before ‘{’ token      if ((CalculateFinalScore(assignment[], midterm[], finalExam[], scoreSum[], gradeAchieved[]) >= 85 ) {                                                                                                          ^ main.c:41:5: error: expected expression before ‘}’ token      }      ^ main.c: In function ‘main’: main.c:79:19: warning: format ‘%lf’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]          printf("%lf
", finalGrade[i]); 

Can somebody help me and fix it?

Download script demo [LINK]
Download script demo [LINK 2] Download script demo [LINK 2]
c

You cannot declare arrays using variable dimension (like in your's main). So, the easiest way is to assumpt constant MAX_SCORES, which will be always greather than your NUM_SCORES, and declare arrays using this.

To ensure that your NUM_SCORES is visible in all functions, use global static variable.

Additionally, you should pass arrays to functions by its name only, as pointers, without [] (like in c# ;) ).

#include   // define below to maximum value, what you expected #define MAX_SCORES 255  // declare global variable static int NUM_SCORES = 0; // this is good practice to initialize global variables  double CalculateFinalScore(int *assignment, int *midterm, int *finalExam, double *scoreSum, char *gradeAchieved)  {     // your function body }  char grade(int *assignment, int *midterm, int *finalExam, double *scoreSum, char *gradeAchieved) {     // your function body }  int main(void)  {     // int NUM_SCORES; <- remove this!     char finalGrade[MAX_SCORES];     double testScores[MAX_SCORES];      int i = 0;     int x[MAX_SCORES];     int y[MAX_SCORES];     int z[MAX_SCORES];      // Prompt user to enter test scores     printf("Input number of students: ");     scanf("%d", &NUM_SCORES);      // check NUM_SCORES is less than MAX_SCORES     if (NUM_SCORES >= MAX_SCORES) {         printf("
Number too big! Should be less than: %d
", MAX_SCORES);         return 0;     }      // the rest of main() body } 

Finally, this is not a good practice to name variables using upper case letters only - such naming is "reserved" for constants, defined using #define directive. If you want to signalize that it is global variable, simply add underscore ('_') at the beginning of the name, for example _numScores.

see demo
  • 208
Reply Report