简体   繁体   中英

trouble passing strings from 2-D array which meet condition to main function from separate function

In this task, you are to get a list of the most critical reviewers. A critical reviewer is defined as:

A reviewer who has the same amount of negative recommendations ('n') as the reviewer with the most negative recommendations.

Using this definition of what a critical reviewer is, you are to look through the list of reviewer's recommendations and determine if they are a critical reviewer or not.

** The function's return value should be the number of critical reviewers. **

In addition, the list of critical reviewer's names created by the function must also be accessible outside of the function.

In this example, the highest number of 'n' recommendations for a single reviewer is 2. Once you have determined the highest amount of 'n' recommendations, you can check to see which reviewers are “critical reviewers”. In this example we can determine that reviewers "Larry", "Judi", "Manisha", "Dora", and "Nick" are critical reviewers as they are the reviewers represented by array indices 1, 3, 6, 8, and 9 respectively. The number 5 would be returned by the function's return value as that is the count of critical reviewers found in the list. This function has no print statements.

struggling to pass the names of only the critical reviewers to the array to be displayed in main.

//Function prototypes
void Recommendations(); //task 1
int criticalReviewers(); //task 2

//MAIN FUNCTION
int main(void) {

  //Variables
  char reviewerNames[NUMBER_REVIEWERS][30] = { "Ritu",
                                             "Larry",
                                             "Dan",
                                             "Judi",
                                             "Eric",
                                             "Isabelle",
                                             "Manisha",
                                             "Terra",
                                             "Dora",
                                             "Nick" };
  char movieNames[NUMBER_MOVIES][50] = { "Star Wars",
                                       "Incredibles",
                                       "Gone with the wind" };
  char userReviews[NUMBER_REVIEWERS][NUMBER_MOVIES];
  char reviewerAnswers[10][3];
  char negativeReviewers[10][30];


  //TASK TWO
  printf("\n**********************************************\n");
  printf("Task 2: Get names of critical reviewers\n\n");

  //call to task 2 function
  printf("Number of Critical Reviewers: %d\n", criticalReviewers(reviewerAnswers, reviewerNames, negativeReviewers));
  printf("Critical Reviewers: ");

  for (int k=0; k<criticalReviewers(reviewerAnswers, reviewerNames, negativeReviewers); k++) {

    printf("%s, ", negativeReviewers + k);

  }

  printf("%s", negativeReviewers + criticalReviewers(reviewerAnswers, reviewerNames, negativeReviewers));

  //CALL TO TASK 3 FUNCTION
  mostRecommended(reviewerAnswers, movieNames);

WINPAUSE; // REMOVE BEFORE SUBMITTING
return 0;
}

//TASK ONE FUNCTION

//TASK 2 FUNCTION
int criticalReviewers(char userAnswers[10][3], char Reviewers[][30], char critReviewers[][30]) {

  int i=0;
  int j=0;
  int numCriticalReviewers = 0;
  int criticalScore = 0;
  int criticalReviewers[10];
  int timesSkipped=0;

  //loop to determine number of critical REVIEWERS
  for (i=0; i<10; i++) {

    criticalReviewers[i] = 0;

    for (j=0; j<3; j++) {

      if (userAnswers[i][j] == 'n') {

        criticalReviewers[i] = criticalReviewers[i] + 1;

      }

    if (criticalReviewers[i] > criticalScore) {

      criticalScore = criticalReviewers[i];

  }
 }
}

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

        if (criticalReviewers[i] == criticalScore) {

          numCriticalReviewers = numCriticalReviewers + 1;

          for (int k=i; k<i+1; k++) {

            critReviewers[k-timesSkipped][30] = Reviewers[k][30];
            timesSkipped = 0;

          }

        }
        else {

          timesSkipped = timesSkipped + 1;

        }
  }

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

        if (criticalReviewers[i] == criticalScore) {

          critReviewers = Reviewers + i;

        }


    }

  return numCriticalReviewers;

 }

I have properly printed in main the number of critical reviewers, but below it should print the names of critical reviewers which i can not figure out. everytime i try to pass the values it prints a random string of letters and symbols.

The issue is where you assign the reviewers to the critReviewers array in function criticalReviewers . Notice how you have the second index as 30. By doing this, you are assigning only the 30th index in the array (which is beyond the bounds of the array, indices 0-29, but that is a different issue).

What you should be doing is either looping through the string to copy each index one by one, or copying the string using a function like strcpy in the string.h library. Otherwise, everything looks like it works fine.

Solution 1:

for(i = 0; i < 30; i++) {
    array1[someIndex][i] = array2[someIndex][i];
}

This will copy each entry in array2[someIndex] one by one.

Solution 2:

strcpy(array1[someIndex], array2[someIndex]);

This will copy the entire string in array2[someIndex] to array1[someIndex] .

I would also take a while to read up on 2d arrays, if you're still confused after reading this. I always like geeksforgeeks.com for stuff like this: https://www.geeksforgeeks.org/multidimensional-arrays-c-cpp/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM