Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What specifically does the compiler error message tell us to fix?

+3 votes
asked Jul 25, 2020 by Yungsil lee (230 points)
#include <stdlib.h>

#include <stdio.h>

#include <string.h>

/*

int main();

{

const int NUMBER_OF_ANSWERS = 10;

const int ANSWERS[NUMBER_OF_ANSWERS] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};

const int NUMBER_OF_GRADES = 9;

const int SCORES[NUMBER_OF_GRADES] = {95, 90, 85, 80, 75, 70, 65, 60, 0};

const char *GRADES[NUMBER_OF_GRADES] = {"A+", "A", "B+", "B", "C+", "C", "D+", "D", "F"};

int* charArrayToIntArray(char *target[], const int length);

int getNumberOfCollectAnswers(const int answers[], const int target[], int length);

int calculateScore(int numberOfCollectAnswers);

void printAnswers(char *who, const int target[], int length);

char* calculateGrade(int totalScore, const int scores[], const char *grades[], int length);

return 0;

}

int main(int argc, char *argv[]) {

if (argc != (NUMBER_OF_ANSWERS + 1)) {

printf("문제는 10 문제입니다. 현재 %d 개의 답안을 제출했습니다. 10개의 답안을 제출하시오.\n", argc - 1);

printf("ex) ./mission02 4 4 3 5 2 5 1 2 4 3\n");

return -1;

}

int *studentAnswers = charArrayToIntArray(argv, argc);

for (int i = 0; i < NUMBER_OF_ANSWERS; i++) {

if (studentAnswers[i] >= 0 && studentAnswers[i] <= 5) {

continue;

} else {

printf("답의 범위는 1 ~ 5 입니다. 범위 외의 답안이 있습니다.\n");

return -1;

}

}

printf("학점평가 시작\n");

int numberOfCollectAnswers = getNumberOfCollectAnswers(ANSWERS, studentAnswers, NUMBER_OF_ANSWERS);

int totalScore = calculateScore(numberOfCollectAnswers);

char *grade = calculateGrade(totalScore, SCORES, GRADES, NUMBER_OF_GRADES);

printAnswers("정답", ANSWERS, NUMBER_OF_ANSWERS);

printAnswers("학생", studentAnswers, NUMBER_OF_ANSWERS);

printf("정답 수: %d\n", numberOfCollectAnswers);

printf("점수: %d\n", totalScore);

printf("학점: %s\n", grade);

return 0;

}

int* charArrayToIntArray(char *target[], const int length) {

int *result = malloc(sizeof(int) * NUMBER_OF_ANSWERS);

for (int i = 0; i < NUMBER_OF_ANSWERS; i++) {

result[i] = atoi(target[i + 1]);

}

return result;

}

int getNumberOfCollectAnswers(const int answers[], const int target[], int length) {

int numberOfCollectAnswers = 0;

for (int i = 0; i < length; i++) {

if (answers[i] == target[i]) {

numberOfCollectAnswers++;

}

}

return numberOfCollectAnswers;

}

int calculateScore(int numberOfCollectAnswers) {

return numberOfCollectAnswers *10;

}

void printAnswers(char *who, const int target[], int length) {

printf("%s : ", who);

for (int,i;= 0; i < length; i++) {

printf("%d\t", target[i]);

}

}

char* calculateGrade(int totalScore, const int scores[], const char *grades[], int length) {

char *grade;

for (int,i;= 0; i < length; i++) {

if (totalScore >= scores[i]) {

grade = malloc(sizeof(char) * strlen(grades[i]));

strcpy(grade, grades[i]);

break;

}

}

return grade;

}

*/

//There's an error message like this.

//C:\Users\user\OneDrive\Documents\Dev C++\collect2.exe [Error] Ltd returned 1 exit status

4 Answers

0 votes
answered Jul 26, 2020 by xDELLx (6,200 points)

Not exactly sure what the error is , it could have been "ld returned status 1" ,here "ld" is the loader.

Loader is the compilers component ,which will load the executable (code which compiled successfully ) & start the prog execution.

The code shared has a few compiler errors which need to be fixed.

Sharing the compiled program below

-------------------------------------------------------------------------------------------------------------------------------


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//   int main();
//   {
//   const int NUMBER_OF_ANSWERS = 10;
#define NUMBER_OF_ANSWERS  10
#define NUMBER_OF_GRADES  9
const int ANSWERS[NUMBER_OF_ANSWERS] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
const int SCORES[NUMBER_OF_GRADES] = {95, 90, 85, 80, 75, 70, 65, 60, 0};
const char *GRADES[NUMBER_OF_GRADES] = {"A+", "A", "B+", "B", "C+", "C", "D+", "D", "F"};
int* charArrayToIntArray(char *target[], const int length);
int getNumberOfCollectAnswers(const int answers[], const int target[], int length);
int calculateScore(int numberOfCollectAnswers);
void printAnswers(char *who, const int target[], int length);
char* calculateGrade(int totalScore, const int scores[], const char *grades[], int length);
//   return 0;
//  }
int main(int argc, char *argv[]) {
    if (argc != (NUMBER_OF_ANSWERS + 1)) {
        printf("문제는 10 문제입니다. 현재 %d 개의 답안을 제출했습니다. 10개의 답안을 제출하시오.\n", argc - 1);
        printf("ex) ./mission02 4 4 3 5 2 5 1 2 4 3\n");
        return -1;
    }
    int *studentAnswers = charArrayToIntArray(argv, argc);
    for (int i = 0; i < NUMBER_OF_ANSWERS; i++) {
        if (studentAnswers[i] >= 0 && studentAnswers[i] <= 5) {
            continue;
        } else {
            printf("답의 범위는 1 ~ 5 입니다. 범위 외의 답안이 있습니다.\n");
            return -1;
        }
    }
    printf("학점평가 시작\n");
    int numberOfCollectAnswers = getNumberOfCollectAnswers(ANSWERS, studentAnswers, NUMBER_OF_ANSWERS);
    int totalScore = calculateScore(numberOfCollectAnswers);
    char *grade = calculateGrade(totalScore, SCORES, GRADES, NUMBER_OF_GRADES);
    printAnswers("정답", ANSWERS, NUMBER_OF_ANSWERS);
    printAnswers("학생", studentAnswers, NUMBER_OF_ANSWERS);
    printf("정답 수: %d\n", numberOfCollectAnswers);
    printf("점수: %d\n", totalScore);
    printf("학점: %s\n", grade);
    return 0;
}
int* charArrayToIntArray(char *target[], const int length) {
    int *result = malloc(sizeof(int) * NUMBER_OF_ANSWERS);
    for (int i = 0; i < NUMBER_OF_ANSWERS; i++) {
        result[i] = atoi(target[i + 1]);
    }
    return result;
}
int getNumberOfCollectAnswers(const int answers[], const int target[], int length) {
    int numberOfCollectAnswers = 0;
    for (int i = 0; i < length; i++) {
        if (answers[i] == target[i]) {
            numberOfCollectAnswers++;
        }
    }
    return numberOfCollectAnswers;
}
int calculateScore(int numberOfCollectAnswers) {
    return numberOfCollectAnswers *10;
}
void printAnswers(char *who, const int target[], int length) {
    printf("%s : ", who);
    for (int i = 0; i < length; i++) {
        printf("%d\t", target[i]);
    }
}
char* calculateGrade(int totalScore, const int scores[], const char *grades[], int length) {
    char *grade;
    for (int i = 0; i < length; i++) {
        if (totalScore >= scores[i]) {
            grade = malloc(sizeof(char) * strlen(grades[i]));
            strcpy(grade, grades[i]);
            break;
        }
    }
    return grade;
}


0 votes
answered Jul 28, 2020 by Peter Minarik (15,580 points)
My note here is that in C, you have do (pre-) declare your function, before you make the call to them.

So either move your main last in your program, or do pre-definition for the other functions.
+1 vote
answered Jul 29, 2020 by LiOS (6,160 points)

Not many issues in the code a few errors which seem to be caused by

  • const int NUMBER_OF_ANSWERS = 10;
  • const int NUMBER_OF_GRADES = 9;

Personally, I would recommend looking at some coding standards. It would make the code easier to read. I would also declare the whole function above the main rather than below.

Working code that's been cleaned up:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define NUMBER_OF_ANSWERS 10
const int ANSWERS[NUMBER_OF_ANSWERS] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};

#define NUMBER_OF_GRADES 9
const int SCORES[NUMBER_OF_GRADES] = {95, 90, 85, 80, 75, 70, 65, 60, 0};

const char *GRADES[NUMBER_OF_GRADES] = {"A+", "A", "B+", "B", "C+", "C", "D+", "D", "F"};

int* charArrayToIntArray(char *target[], const int length) {
    
    int *result = malloc(sizeof(int) * NUMBER_OF_ANSWERS);
    
    for (int i = 0; i < NUMBER_OF_ANSWERS; i++) {
        result[i] = atoi(target[i + 1]);
    }
    
    return result;
}

int getNumberOfCollectAnswers(const int answers[], const int target[], int length) {
    
    int numberOfCollectAnswers = 0;

    for (int i = 0; i < length; i++) {
        
        if (answers[i] == target[i]) {
            numberOfCollectAnswers++;
        }
    }

    return numberOfCollectAnswers;
}

int calculateScore(int numberOfCollectAnswers) {
    return numberOfCollectAnswers *10;
}

void printAnswers(char *who, const int target[], int length) {
    
    printf("%s : ", who);
    
    for (int i= 0; i < length; i++) {
        printf("%d\t", target[i]);
    }
}


char* calculateGrade(int totalScore, const int scores[], const char *grades[], int length) {
    
    char *grade;

    for (int i= 0; i < length; i++) {

        if (totalScore >= scores[i]) {
            grade = malloc(sizeof(char) * strlen(grades[i]));
            strcpy(grade, grades[i]);
            break;
        }
    }

    return grade;
}


int main(int argc, char *argv[]) {

    if (argc != (NUMBER_OF_ANSWERS + 1)) {
        printf("문제는 10 문제입니다. 현재 %d 개의 답안을 제출했습니다. 10개의 답안을 제출하시오.\n", argc - 1);
        printf("ex) ./mission02 4 4 3 5 2 5 1 2 4 3\n");
        
        return -1;
    }

    int *studentAnswers = charArrayToIntArray(argv, argc);

    for (int i = 0; i < NUMBER_OF_ANSWERS; i++) {
        if (studentAnswers[i] >= 0 && studentAnswers[i] <= 5) {
            continue;
    }
        else {
            printf("답의 범위는 1 ~ 5 입니다. 범위 외의 답안이 있습니다.\n");
            return -1;
        }
    }

    printf("학점평가 시작\n");

    int numberOfCollectAnswers = getNumberOfCollectAnswers(ANSWERS, studentAnswers, NUMBER_OF_ANSWERS);

    int totalScore = calculateScore(numberOfCollectAnswers);

    char *grade = calculateGrade(totalScore, SCORES, GRADES, NUMBER_OF_GRADES);

    printAnswers("정답", ANSWERS, NUMBER_OF_ANSWERS);

    printAnswers("학생", studentAnswers, NUMBER_OF_ANSWERS);

    printf("정답 수: %d\n", numberOfCollectAnswers);
    printf("점수: %d\n", totalScore);
    printf("학점: %s\n", grade);

    return 0;

}

0 votes
answered Aug 1, 2020 by vcs200831 (140 points)
  • Error does not have anything to do with code. Your operating system simply does not allow to modify a file while it is in use, so the compilation (actually, linking, ld is the linker) fails, because compiler can't remove the old executable and place a new one. To solve this, simply close all existing processes running that program.
  • If that won't work, check your permissions for directory the executable is in, or look for any programs that are currently using it (some systems allow programs to place a lock on a file, so no other program can modify it).
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...