PLEASE USE C++ PRINTF, SCANF. Do not use cout or cinn. If possible do not use \"
ID: 3603892 • Letter: P
Question
PLEASE USE C++ PRINTF, SCANF. Do not use cout or cinn.
If possible do not use " strtok command".
I also use Visual basic 2015.
Explanation / Answer
Solution:
code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SCORE_NEEDED 75
#define SIZE_OF_ARRAY 20
int main()
{
//Variable for student name
char studentName[30];
//Char array for correct answers
char correctAnswers[SIZE_OF_ARRAY]= {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
//Char array for users answers
char userAnswers[SIZE_OF_ARRAY],i=0;
//Variable to store correct answers
int totalCorrectAnswers=0;
//Ask user to enter name
printf("Enter the student's full name (enter nothing to end): ");
fgets(studentName,(int) sizeof(studentName),stdin);
while(studentName[0]!='' && studentName[0]!=' ')
{
//Print the student name, use strtok to remove new line character from student name which was entered because of using fgets
printf("Enter %s's 20 answers: ",strtok(studentName, " "));
//Ask user to enter 20 answers
for(i=0; i<SIZE_OF_ARRAY; i++)
{
printf("Enter answer #%d: ",i+1);
scanf(" %c",&userAnswers[i]);
//Store answer in userAnswers array after converting in uppercase
userAnswers[i]=toupper( userAnswers[i]);
//Check if answer is valid, if not ask to enter again
while(userAnswers[i]<'A' || userAnswers[i]>'D')
{
printf("The answer entered was not in the range A-D. ");
printf("Please re-enter answer #%d: ",i+1);
scanf(" %c",&userAnswers[i]);
userAnswers[i]=toupper( userAnswers[i]);
}
}
//Now, check for correct answers by compairing values in correctAnswers and userAnswers
for(i=0; i<SIZE_OF_ARRAY; i++)
{
//If values matches, increment totalCorrectAnswers
if(correctAnswers[i]==userAnswers[i])
{
totalCorrectAnswers+=1;
printf(" Question #%d - correct",i+1);
}
else
{
printf(" Question #%d - incorrect",i+1);
}
}
//Print the total number of correct answers
printf(" %s had %d correct answers and had %d incorrect answers. ",studentName,totalCorrectAnswers,20-totalCorrectAnswers);
//Find percent of correct answers
double percent=((double)totalCorrectAnswers/20)*100;
//If percent>=75, means pass else fail
if(percent>=SCORE_NEEDED)
{
printf(" The student had passed the questions with a score of %1.0f%%! ",percent);
}
else
{
printf(" The student did not pass the questions. Their score was %1.0f%%. ",percent);
printf("A score of 75%% or better is required to pass. ");
}
//Re-assign totalCorrectAnswers=0 to store correct answers for another student
totalCorrectAnswers=0;
scanf("%c",&studentName[0]);
printf("Enter the student's full name (enter nothing to end): ");
fgets(studentName,(int) sizeof(studentName),stdin);
}
return 0;
}
Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.