60 688
Assignments Done
97,9%
Successfully Done
In April 2018

Answer to Question #42881 in C for Mpendulo

Question #42881
Q4) (30 pts.) Assume that you have two files given : answer.txt and key.txt files. answer.txt file includes student number (int) and the answers of the student. key.txt file includes the answer key for 100 questions.
Write a complete C program that reads the exam answers of the student from the file answer.txt, and then compare these answers with the correct answers in key.txt. Finally display the number of correct answers for the student.
Hint : You may use a string (char [ ] ) to store the answers.
answer.txt :
9999 abdaa...
key.txt:
abbacaca...

The program output may be displayed as follows for 5 questions:
=======================================
stno : 9999 correct answers : 55
=======================================
Expert's answer
#include <stdio.h>
int main()
{
  FILE *f_answer = fopen("answer.txt","r");
  FILE *f_key = fopen("key.txt", "r");
  char l_answer[256];
  char l_key[256];
  char s_id[256];
  int pos = 0;
  int corr_ans = 0;
  int i;
  fgets(l_answer, sizeof(l_answer), f_answer);
  fgets(l_key, sizeof(l_key), f_key);
  // parses student number
  while(l_answer[pos] != ' ')
  {
s_id[pos] = l_answer[i];
pos++;
  }
  // counts the number of correct answers
  for(i = 0; i < 100; i++)
  {
if (l_answer[pos + 1 + i] == l_key[i])
{
corr_ans++;
}
  }
  printf("=======================================\n");
  printf("stno : ");
  printf("s_id");
  printf(" correct answers : ");
  printf("%d\n", corr_ans);
  printf("=======================================");
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions